WEB3JS (以太坊) 查询以太币余额

112 阅读1分钟

WEB3JS (以太坊) 查询以太币余额

node js

web3

npm install web3 -S

infura infura.io/ 提供了托管的以太坊节点


const Web3 = require('web3')
const baseurl = 'https://mainnet.infura.io/ < INFURA访问令牌 >' 
// set the provider you want from Web3.providers
const web3 = new Web3(new Web3.providers.HttpProvider(baseurl))
// 账号地址
const currentAccount = ""

// 查询以太币余额
web3.eth.getBalance(currentAccount, (err, result) => {
  if (err == null) {
    console.log('~:'+ web3.utils.fromWei(result,'ether'))
  } else  {
    console.log('~error:'+err)
  }
})