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

相关推荐
《源码好优多》17 分钟前
基于Java Springboot华为数码商城交易平台
java·开发语言·spring boot
GIS好难学36 分钟前
《Vue零基础入门教程》第五课:挂载
前端·javascript·vue.js
康熙38bdc38 分钟前
Linux 线程互斥
linux·运维·开发语言
学习前端的小z38 分钟前
【前端】JavaScript中的柯里化函数与普通函数嵌套的区别详解
javascript
Mr__vantasy1 小时前
数据结构(初阶7)---七大排序法(堆排序,快速排序,归并排序,希尔排序,冒泡排序,选择排序,插入排序)(详解)
c语言·开发语言·数据结构·算法·排序算法
可缺不可滥1 小时前
前端 用js封装部分数据结构
前端·javascript·数据结构
机器视觉知识推荐、就业指导1 小时前
基于Qt实现的自定义树结构容器:设计与应用
开发语言·qt
JMS_兔子1 小时前
elementUI非常规数据格式渲染复杂表格(副表头、合并单元格)
前端·javascript·elementui·vue
九圣残炎1 小时前
【Vue】Ego商城项目跟做
前端·javascript·vue.js·node.js
qing_0406031 小时前
C++——多态(下)
开发语言·c++