1

I have this solidity contract:

contract Token {

  mapping(address => uint256) public balanceOf;

  event Transfer(
    address indexed _from,
    address indexed _to,
    uint256 _value
    );

  constructor(uint256 _initialSupply) public {
    owner = msg.sender;
    balanceOf[msg.sender] = _initialSupply;
    totalSupply = _initialSupply;
  }

  function getTokens() public returns (bool success){
    balanceOf[owner] -= 48;
    balanceOf[msg.sender] += 48;
    emit Transfer(owner, msg.sender, 48);
    return true;
  }


}

When I run this using ganache and truffle manually, the method getTokens() works fine. I tried to write a test for it, but it does not transfer the tokens and tells me the balance of account 1 is 0.

let ControlToken = artifacts.require("./ControlToken.sol");

contract('ControlToken', function (accounts) {
    it('transfers token ownership', function() {
        return ControlToken.deployed().then(function(instance) {
            let tokenInstance = instance;
            return tokenInstance.getTokens.call({ from: accounts[1] });
        }).then(function(success) {
            assert.equal(success, true, 'it returns true');
            return tokenInstance.balanceOf(accounts[1]);
        }).then(function (balance) {
            assert.equal(balance, 48, "Balance should be 48")
        });

    });
});

This tests fails when i run it using truffle test. According to Solidity Transfer Token Test Case is not working fine. the problem may be that I'm using call() on getToken instead of send(), yet when i try send() the test does not recognize the command. Any help would be much appreciated!

| improve this question | | | | |
1

Since you are using truffle the correct syntax is

tokenInstance.getTokens({ from: accounts[1] });

Adding .call() will make the call but any change will not be recorded in the blockchain.

| improve this answer | | | | |
  • Well, I feel silly now! Thank you for your help! – Nicolas Schapeler Mar 15 at 17:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.