From 6ea19850dcbaab35541c080839f2785f1124d66d Mon Sep 17 00:00:00 2001 From: Ayanami Date: Thu, 27 Jan 2022 21:35:55 +0900 Subject: [PATCH] Fix send & printBalance using BigNumber --- cli.js | 41 +++++++++++++++++++++-------------------- package.json | 1 + 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/cli.js b/cli.js index af54652..836d10a 100755 --- a/cli.js +++ b/cli.js @@ -16,6 +16,7 @@ const Web3HttpProvider = require('web3-providers-http'); const buildGroth16 = require('websnark/src/groth16') const websnarkUtils = require('websnark/src/utils') const { toWei, fromWei, toBN, BN } = require('web3-utils') +const BigNumber = require('bignumber.js'); const config = require('./config') const program = require('commander') const { GasPriceOracle } = require('gas-price-oracle') @@ -42,19 +43,19 @@ function toHex(number, length = 32) { /** Display ETH account balance */ async function printETHBalance({ address, name }) { - const checkBalance = await web3.eth.getBalance(address) - console.log(`${name} balance is`, web3.utils.fromWei(checkBalance),`${netSymbol}`) + const checkBalance = new BigNumber(await web3.eth.getBalance(address)); + console.log(`${name} balance is`, checkBalance.div(BigNumber(10).pow(18)).toString(),`${netSymbol}`) } /** Display ERC20 account balance */ async function printERC20Balance({ address, name, tokenAddress }) { const erc20ContractJson = require('./build/contracts/ERC20Mock.json') erc20 = tokenAddress ? new web3.eth.Contract(erc20ContractJson.abi, tokenAddress) : erc20 - const tokenBalance = await erc20.methods.balanceOf(address).call() + const tokenBalance = new BigNumber(await erc20.methods.balanceOf(address).call()) const tokenDecimals = await erc20.methods.decimals().call() const tokenName = await erc20.methods.name().call() const tokenSymbol = await erc20.methods.symbol().call() - console.log(`${name}`,tokenName,`Token Balance is`,toDecimals(tokenBalance, tokenDecimals, (tokenBalance.length + tokenDecimals)).toString().replace(/\B(?