两次转换成BN有什么意义?

What's the point of converting to BN twice?

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

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 转换必须进行两次?

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

// 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);