0

我创建了一个 ERC20 SC,这是初始供应量 100000000000。但是,当我运行单元测试(松露测试)以显示帐户的总余额 [0] 并且实际金额为 99999998877 时,预期金额应为 100000000000。有人可以向我解释一下,为什么会这样?谢谢你。

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

}
4

1 回答 1

0

您的总供应量为 10^29(10^11 个令牌,每个令牌有 10^18 个小数点),这不是 javascript 数字可以安全处理的。https://docs.ethers.io/ethers.js/html/notes.html#why-can-ti-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")
于 2019-07-24T10:10:20.480 回答