我创建了一个ERC20 SC,这是初始供应量100000000000。但是,当我运行单元测试(松露测试)以显示帐户的总余额[0],而实际金额为99999998877时,预期金额应为是1000亿。有人可以向我解释,为什么会这样? 谢谢。
it("should return the total supply", function() {
return CToken.deployed().then(function(instance){
return tokenInstance.totalSupply.call();
}).then(function(result){
assert.equal(result.toNumber(), 100000000000, 'total supply is wrong');
})
});
it("should return the balance of token owner", function() {
var token;
return CToken.deployed().then(function(instance){
token = instance
return tokenInstance.balanceOf.call(accounts[0]);
}).then(function(result){
assert.equal(result.toNumber(), 99999998877, 'balance is wrong');
})
});
Contract code:
pragma solidity >=0.4.21 <0.6.0;
import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
import "../node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol";
/**
* @title BearToken is a basic ERC20 Token
*/
contract CToken is StandardToken, Ownable{
uint256 public totalSupply;
string public name;
string public symbol;
uint32 public decimals;
/**
* @dev assign totalSupply to account creating this contract
*/
constructor() public {
symbol = "CT";
name = "CToken";
decimals = 18;
totalSupply = 100000000000;
owner = msg.sender;
balances[msg.sender] = totalSupply;
//emit Transfer(0x0, msg.sender, totalSupply);
}
}
答案 0 :(得分:0)
您的总供应量是10 ^ 29(10 ^ 11个令牌,每个令牌有10 ^ 18个小数点),这不是javascript数字可以安全处理的。 https://docs.ethers.io/ethers.js/html/notes.html#why-can-t-i-just-use-numbers
web3返回一个BN实例,不要将其转换为数字。
您可以这样声明:
const BN = web3.utils.BN
const ten = new BN("10")
const expected = ten.pow(new BN("27"))
assert(result.eq(expected), "Result doesn't match expected value")