0

I created an ERC20 SC, this is the initial supply amount 100000000000. But, when I run unit test (truffle test) to show the total balance amount of the accounts[0] and the actual amount is 99999998877, expected amount should be 100000000000. Can someone explain to me, why that's happened? Thank you.

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

}
David
  • 607
  • 1
  • 6
  • 19
  • Show the contract and tests. – Zulhilmi Zainudin Jul 23 '19 at 03:23
  • @ZulhilmiZainudin - I have shared my unit test code – David Jul 23 '19 at 07:21
  • Where is the contract code? – Zulhilmi Zainudin Jul 23 '19 at 07:58
  • @ZulhilmiZainudin - Sorry for the missed, please find the contract code shown above. – David Jul 23 '19 at 08:07
  • I saw you are using `openzeppelin-solidity` via NPM. What's the package version that you're using? Apart from the above contract, do you have any other contract? Did you change anything inside the openzeppelin contracts? – Zulhilmi Zainudin Jul 23 '19 at 11:57
  • @ZulhilmiZainudin - As for as I know, I didn't change anything in the openzepplin-solidity library. What do you suspect here? Please advise. I will investigate further. – David Jul 24 '19 at 01:10
  • Strange. I suspect something is wrong with the openzeppelin library that you are using. Please make sure that you are using the latest version. You can also use Remix (https://remix.ethereum.org) to play around with your contracts and tokens. If Remix shows no error, that means something is wrong with your setup or Truffle. FYI, you can import libs from GitHub in Remix. You can't use `node_modules` inside Remix. – Zulhilmi Zainudin Jul 24 '19 at 03:29
  • Deleted old answer, wrote a new one. – Ferit Jul 24 '19 at 10:12

1 Answers1

0

Your total supply is 10^29 (10^11 tokens, each token has 10^18 decimal points), which is not something javascript number can safely handle. https://docs.ethers.io/ethers.js/html/notes.html#why-can-t-i-just-use-numbers

web3 returns a BN instance, don't convert it to number.

You can assert like this:

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")
Ferit
  • 8,692
  • 8
  • 34
  • 59