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

相关推荐
灵哎惹,凌沃敏9 分钟前
华为C语言编程规范总结
c语言·开发语言
计算机毕设指导623 分钟前
基于Springboot的景区民宿预约系统【附源码】
java·开发语言·spring boot·后端·mysql·spring·intellij idea
pumpkin8451430 分钟前
什么是 LuaJIT?
开发语言
染指悲剧41 分钟前
vue实现虚拟列表滚动
前端·javascript·vue.js
云端 架构师1 小时前
Lua语言的语法
开发语言·后端·golang
AI向前看1 小时前
Objective-C语言的网络编程
开发语言·后端·golang
梦想的初衷~1 小时前
AI赋能R-Meta分析核心技术:从热点挖掘到高级模型、助力高效科研与论文发表
开发语言·人工智能·r语言
霜雪殇璃1 小时前
c++对结构体的扩充以及类的介绍
开发语言·c++·笔记·学习
编程小筑1 小时前
Clojure语言的并发编程
开发语言·后端·golang
心向阳光的天域1 小时前
黑马跟学.苍穹外卖.Day03
java·开发语言·spring boot