2

For some reason my truffle test suite "should send coin correctly" passes when it shouldn't. I'm just running a test suite on the standard ERC20 code found on ethereum.org. When you take 1e+21 -1010, it sees it as equal to 1e+21 -1000, and still passes it even though it is not actually equal, there is a difference of 10 between the values. If I change it to -1000000 for example, the test will fail as expected. Do I have some kind of error in the data type? Or am I doing something wrong here?

var TokenERC20 = artifacts.require("./TokenERC20.sol")

contract('TokenERC20', function(accounts) {
    it("should return the correct symbol name", function() {
    return TokenERC20.deployed(TokenERC20, "1000", "BAT_CS_180D022818", "BATC1").then(function(instance) {
        return instance.symbol.call();
        }).then(function(symbol) {
            assert.equal(symbol.valueOf(), "BATC1", "BATC1 wasn't the symbol");
        });
    });
    it("should return the correct balance", function() {
    return TokenERC20.deployed(TokenERC20, "1000", "BAT_CS_180D022818", "BATC1").then(function(instance) {
        return instance.balanceOf.call(accounts[0]);
        }).then(function(balanceOf) {
            assert.equal(balanceOf, "1e+21", "1e+21 wasn't the supply");
        });
    }); 
    it("should send coin correctly", function() {
        var meta;

        //get initial balances of first two accounts
        var account_one = accounts[0];
        var account_two = accounts[1];

    var account_one_starting_balance;
    var account_two_starting_balance;
    var account_one_ending_balance;
    var account_two_ending_balance;

    var amount = 1000;

    return TokenERC20.deployed(TokenERC20, "1000", "BAT_CS_180D022818", "BATC1").then(function(instance) {
      meta = instance;
      return meta.balanceOf.call(account_one);
    }).then(function(balance) {
        account_one_starting_balance = balance.toNumber();
        return meta.balanceOf.call(account_two);
    }).then(function(balance) {
        account_two_starting_balance = balance.toNumber();
        return meta.transfer(account_two, amount, {from: account_one});
    }).then(function() {
        return meta.balanceOf.call(account_one);
    }).then(function(balance) {
        account_one_ending_balance = balance.toNumber();
        return meta.balanceOf.call(account_two);
    }).then(function(balance) {
        account_two_ending_balance = balance.toNumber();
        assert.equal(account_one_ending_balance, account_one_starting_balance - 1010, "Amount wasn't correctly taken from the sender");
      assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver");
    })
  });
});
  • I am having the same issue – ferit Jul 5 '18 at 9:59
  • A bit late, but it was probably converting the BigNumber into a Javascript 64-bit floating point, thereby losing precision. – Jesse Busman Feb 25 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.