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

相关推荐
前端 贾公子7 分钟前
vue移动端适配方案 === postcss-px-to-viewport
前端·javascript·html
生骨大头菜1 小时前
使用python实现相似图片搜索功能,并接入springcloud
开发语言·python·spring cloud·微服务
绝不收费—免费看不了了联系我1 小时前
Fastapi的单进程响应问题 和 解决方法
开发语言·后端·python·fastapi
GISer_Jing1 小时前
AI营销增长:4大核心能力+前端落地指南
前端·javascript·人工智能
消失的旧时光-19431 小时前
深入理解 Java 线程池(二):ThreadPoolExecutor 执行流程 + 运行状态 + ctl 原理全解析
java·开发语言
咖啡续命又一天1 小时前
Trae CN IDE 中 Python 开发的具体流程和配置总结
开发语言·ide·python·ai编程
4311媒体网1 小时前
帝国cms调用文章内容 二开基本操作
java·开发语言·php
GSDjisidi2 小时前
东京IT软件会社-(株)GSD|多种技术栈募集,高度人才+20分
开发语言·面试·职场和发展
程序员zgh2 小时前
Linux系统常用命令集合
linux·运维·服务器·c语言·开发语言·c++