web3js查询钱包余额

javascript 复制代码
const { Web3 } = require('web3')
let apiKeyOnServer = process.env.apiKeyOnServer
const web3 = new Web3(
  `https://bsc-mainnet.infura.io/v3/${process.env.metamaskApiKey}`
)

const getWeb3Instance = () => {
  let endpoints = [
    {
      url: 'https://polygon-mainnet.infura.io/v3/',
      name: 'polygon',
    },
    // {
    //   url: 'https://mainnet.infura.io/v3/',
    //   name: 'mainnet',
    // },
    {
      url: 'https://bsc-mainnet.infura.io/v3/',
      name: 'bscMainnet',
    },
  ]

  let web3InstanceObj = {}
  endpoints.forEach((item) => {
    let instance = new Web3(item.url + process.env.metamaskApiKey)

    web3InstanceObj[item.name] = {
      ...item,
      instance,
    }
  })
  return web3InstanceObj
}

let web3InstanceObj = getWeb3Instance()

//查询
const web3OnAzure = async (req, res, type) => {
  const { pageNum = 1, pageSize = 10, apiKey = 'sk-xxx' } = req.body

  if (apiKey === apiKeyOnServer) {
    // let getBlockNumberRes = await web3.eth.getBlockNumber()
    // console.log(getBlockNumberRes)
    // const chainId = await web3.eth.getChainId()

    console.log('binance钱包')
    for (let key in web3InstanceObj) {
      let bBalance = await web3InstanceObj[key].instance.eth.getBalance(
        process.env.binanceWallet1
      )
      // console.log(web3InstanceObj[key].name, bBalance)
      bBalance = web3.utils.fromWei(bBalance, 'ether')
      console.log(web3InstanceObj[key].name + ' ether', bBalance)
    }

    console.log('币安交易账号地址')
    for (let key in web3InstanceObj) {
      let bBalance = await web3InstanceObj[key].instance.eth.getBalance(
        process.env.binanceMarketWallet1
      )
      // console.log(web3InstanceObj[key].name, bBalance)
      bBalance = web3.utils.fromWei(bBalance, 'ether')
      console.log(web3InstanceObj[key].name + ' ether', bBalance)
    }

    console.log('onekey钱包')
    for (let key in web3InstanceObj) {
      let bBalance = await web3InstanceObj[key].instance.eth.getBalance(
        process.env.onkeyWallet1
      )
      // console.log(web3InstanceObj[key].name, bBalance)
      bBalance = web3.utils.fromWei(bBalance, 'ether')
      console.log(web3InstanceObj[key].name + ' ether', bBalance)
    }

    console.log('公开的钱包')
    for (let key in web3InstanceObj) {
      let bBalance = await web3InstanceObj[key].instance.eth.getBalance(
        '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
      )
      // console.log(web3InstanceObj[key].name, bBalance)
      bBalance = web3.utils.fromWei(bBalance, 'ether')
      console.log(web3InstanceObj[key].name + ' ether', bBalance)
    }

    // const gasPrice = await web3.eth.getGasPrice()
    // console.log('gasPrice', gasPrice)

    // get the balance of an address
    // let wallet1 = web3.eth.accounts.wallet.create(1)
    // console.log('wallet1', wallet1)

    // const account = web3.eth.accounts.create()

    // console.log(account)
    // // use the account to sign a message
    // const signature = account.sign('Hello, Web3.js!')
    // console.log(signature)

    res.send({
      code: 200,
      data: {
        // getBlockNumberRes: getBlockNumberRes + '',
        // chainId: chainId + '',
      },
      message: '成功',
    })
  } else {
    res.send({
      code: 400,
      message: '失败:参数apiKey',
    })
  }
}

module.exports = {
  web3OnAzure,
}

人工智能学习网站

https://chat.xutongbao.top

相关推荐
郝学胜-神的一滴2 分钟前
Python高级编程技术深度解析与实战指南
开发语言·python·程序人生·个人开发
起名时在学Aiifox8 分钟前
深入解析 Electron 打包中的 EPERM: operation not permitted 错误
前端·javascript·electron
charlie1145141919 分钟前
使用 Poetry + VS Code 创建你的第一个 Flask 工程
开发语言·笔记·后端·python·学习·flask·教程
Codeking__11 分钟前
查缺补漏c语言——c标准字符串函数
c语言·开发语言
铅笔小新z18 分钟前
【C++】从理论到实践:类和对象完全指南(中)
开发语言·c++
千疑千寻~21 分钟前
【C++】std::move与std::forward函数的区别
开发语言·c++
hachi031329 分钟前
Vue中input disabled时点击事件不触发怎么办?
javascript·vue.js·ecmascript
Murphy_lx29 分钟前
C++ 条件变量
linux·开发语言·c++
羚羊角uou42 分钟前
【C++】智能指针
开发语言·c++
漫天黄叶远飞1 小时前
别再把对象当“字典”!JS 零基础也能看懂的“属性账本”拆解笔记
javascript