【问题标题】:What's the point of converting to BN twice?两次转换为BN有什么意义?
【发布时间】:2022-08-03 02:31:54
【问题描述】:

调试智能合约测试,我看到以下操作:

const alice_Before = toBN(web3.utils.toBN(await web3.eth.getBalance(alice)));

toBN 在哪里

 static toBN(num) {
    return web3.utils.toBN(num)
  }

如果我 console.log 这两个选项它们看起来像这样,地址中有一定的平衡:

BN {
  negative: 0,
  words: [ 39940619, 64700551, 7238971, 54128420, 49303 ],
  length: 5,
  red: null
}

谁能帮助理解为什么 BN 转换必须进行两次?

    标签: web3js bignumber.js


    【解决方案1】:

    它不必做两次。 web3.utils.toBN 将返回其输入参数,如果它已经是 BN,因此您可以删除其中一个转换,因为静态函数仅此而已。

    // Replaced toBN with the body, which is a call to web3.utils.toBN
    const alice_Before1 = web3.utils.toBN(web3.utils.toBN(await web3.eth.getBalance(alice)));
    const alice_Before2 = web3.utils.toBN(await web3.eth.getBalance(alice));
    
    // Results will be identical:
    console.log(alice_Before1);
    console.log(alice_Before2);
    

    【讨论】:

      猜你喜欢
      • 2021-04-17
      • 2020-07-24
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      相关资源
      最近更新 更多