npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

bailidoctor-wallet

v1.2.1

Published

简易的web3方法封装

Readme

使用方式

import { web3,isbsc,Approve,networkOnchange,accountOnchange,send,call} from 'bailidoctor-wallet'
web3:初始化web3对象
//web3为一个函数,初始化web3环境,存在web3环境返回值为web3对象,否则为null,可以此判断是否安装了metamask或者在dapp浏览器中
const dapp=web3()
if(!dapp){
  // 没有安装metamask或者没有dapp浏览器中
}
web3如何初始化合约
const dapp=web3()
if(!dapp){
  // 没有安装metamask或者没有dapp浏览器中
}
const abi=[...]
const contractAddress = '合约地址'
const Contract = new dapp.eth.Contract(abi, contractAddress)
isbsc:判断当前dapp链环境
//isbsc为函数,入参为链id,返回值为布尔值true/false
//例如bsc主网链id为56
const bsc=isbsc(56)
if(!bsc){
  //当前网络不是bsc主网
}
Approve:获取当前登录的账户
//Approve返回一个数组,第一个账户为当前选择的账户
try{
  const account = await Approve()
  const current=account[0]
}catch(e){
  //拒绝授权或者发生错误
}
账户签名登录
//返回一个promise对象
dapp.eth.personal.sign(dapp.utils.utf8ToHex(签名数据), current)
send:发送交易等,涉及到上链的方法
/** 
 * Contract:合约对象
 * method:调用合约的方法
 * params:调用合约的方法参数,类型为数组[],
 * account:调用者账户,即当前链接的账户
 * value:默认为0,可选
 * callback:交易成功回调,可选
 * errorback:交易失败回调,可选
 */
send(Contract,method,params,account,value = 0,callback,errorback)
//调用示例
const account = await Approve()
const current=account[0]
try {
  const account = await Approve()
    const result = await send(
      Contract,
      'purchase',
      [2],
      account[0],
      dapp.utils.toWei('0.001')
    )
    console.log(result)
  } catch (error) {
    console.log(error)
}
call:链上查询
/** 
 * Contract:合约对象
 * method:调用合约的方法
 * params:调用合约的方法参数,类型为数组[],
 * account:调用者账户,即当前链接的账户
 * callback:交易成功回调,可选
 * errorback:交易失败回调,可选
 */
call(Contract,method,params,account,callback,errorback)
//调用示例
const account = await Approve()
const current=account[0]
try {
  const result = await call(contract, 'depositBalances', [current, 1],current)
    console.log(result)
  } catch (error) {
    console.log(error)
}
networkOnchange 监听链网络变化
/**
* callback:回调函数
*/
networkOnchange((chainId)=>{
  //这里一般进行页面重载
})
accountOnchange 监听账户切换变化
/**
* callback:回调函数
*/
accountOnchange((accounts)=>{
  //重新获取当前选择账户
})

完整示例

import { web3,isbsc,Approve,networkOnchange,accountOnchange,send,call} from 'bailidoctor-wallet'
const dapp=web3()
if(!dapp){
  // 没有安装metamask或者没有dapp浏览器中
  return false
}
//判断是否是bsc主网
const bsc=isbsc(56)
if(!bsc){
  //当前网络不是bsc主网
  return false
}
//获取当前链接账户,没有则链接
const account = await Approve()
const current=account[0]
//生成调用合约
const abi=[...]
const contractAddress = '合约地址'
const Contract = new dapp.eth.Contract(abi, contractAddress)
//充值方法
async purch(amount)=>{
  try {
      const result = await send(
        contract,
        'purchase',
        [1],
        current,
        web3js.utils.toWei(amount)
      )
      console.log(result)
    } catch (error) {
      console.log(error)
    }
}
//针对erc20和erc721的授权和查询授权
//erc20查询授权
async callapprove()=>{
  try {
      const result = await call(
        ecr20contract,
        'allowance',
        [用户地址,需要获得权限的合约地址],
        current
      )
      console.log(result) //返回授权的数量,如果大于0为已授权,否则需要授权
      if(result<1){
        //此处调用erc20授权方法
      }
    } catch (error) {
      console.log(error)
    }
}
//erc20授权方法
async approve()=>{
  try {
      const result = await send(
        ecr20contract,
        'approve',
        [需要权限的合约地址,数量],
        current
      )
      console.log(result)
    } catch (error) {
      console.log(error)
    }
}
//erc721查询授权
async nftcallapprove()=>{
  try {
      const result = await call(
        ecr721contract,
        'isApprovedForAll',
        [用户地址,需要获得权限的合约地址],
        current
      )
      console.log(result) //返回布尔类型,是否授权,false为未授权
      if(!result){
        //此处调用erc721授权方法
      }
    } catch (error) {
      console.log(error)
    }
}
//erc721授权方法
async nftapprove()=>{
  try {
      const result = await send(
        ecr721contract,
        'setApprovalForAll',
        [需要权限的合约地址,是否授权:true/false],
        current
      )
      console.log(result)
    } catch (error) {
      console.log(error)
    }
}
//检查订单充值金额
async deposit(orderId)=>{
  try {
    const result = await call(contract, 'depositBalances',  [current, 1],current)
    console.log(result)
  } catch (error) {
    console.log(error)
  }
}
//通过hash获取交易结果
const hashResult = async (hash) => {
  const result = await web3.eth.getTransactionReceipt(hash)
  if (result && result.status) {
    return Promise.resolve(result.status)
  } else {
    return Promise.reject(false)
  }
}