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

相关推荐
tanyongxi666 分钟前
C++ 特殊类设计与单例模式解析
java·开发语言·数据结构·c++·算法·单例模式
遗憾皆是温柔8 分钟前
24. 什么是不可变对象,好处是什么
java·开发语言·面试·学习方法
wearegogog12329 分钟前
C语言中的输入输出函数:构建程序交互的基石
c语言·开发语言·交互
Fine姐32 分钟前
The Network Link Layer: 无线传感器中Delay Tolerant Networks – DTNs 延迟容忍网络
开发语言·网络·php·硬件架构
晓得迷路了1 小时前
栗子前端技术周刊第 94 期 - React Native 0.81、jQuery 4.0.0 RC1、Bun v1.2.20...
前端·javascript·react.js
江城开朗的豌豆1 小时前
React Native 实战心得
javascript
HAPPY酷1 小时前
给纯小白的Python操作 PDF 笔记
开发语言·python·pdf
liulilittle1 小时前
BFS寻路算法解析与实现
开发语言·c++·算法·宽度优先·寻路算法·寻路
江城开朗的豌豆1 小时前
React状态更新踩坑记:我是这样优雅修改参数的
前端·javascript·react.js