1

I've created a test ERC20 token like at https://theethereum.wiki/w/index.php/ERC20_Token_Standard. I've sent some test ether to the contract and can verify my account has some of the token, eg with the following command (calling the "balanceOf" function):

contractInstance.balanceOf.call(web3.eth.accounts[0])

returns:

{ [String: '7000000000000000'] s: 1, e: 15, c: [ 70 ] }

After that I want to send some of the token to another account. My transfer function is as follows:

function transfer(address _to, uint256 _value) returns (bool success) {
           require(balances[msg.sender] >= _value && _value > 0);
           balances[msg.sender] = balances[msg.sender].sub(_value);
           balances[_to] = balances[_to].add(_value);
           Transfer(msg.sender, _to, _value);
           return true;
       }

(I'm using the SafeMath.sol contract at https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/math/SafeMath.sol)

Running the following command to execute the transfer function and send some of the token to my second account:

contractInstance.transfer.call(web3.eth.accounts[1], 0.005)

returns

false

So the transfer isn't being executed. What might the issue be here?

2
  • I've also unlocked the web3.eth.accounts[0] account before trying to execute the transfer function
    – ZhouW
    Commented Aug 2, 2017 at 8:40
  • Also note that your implementation violates the specification in that “Transfers of 0 values MUST be treated as normal transfers and fire the Transfer event.”
    – kirelagin
    Commented Feb 8, 2018 at 11:02

1 Answer 1

3

Three possible issues I spotted taking a quick glance.

Your second argument is "0.005", which is not an int, so it won't be possible to cast it to a uint256 as you requested. If you followed the tutorial, you probably also used 18 decimals to define your contract's token-decimals. You'll need to multiply "0.005" by 10^18. quick trick: as 1 ether is also 1*(10^18) wei, you can just replace your "0.005" with web3.toWei("0.005").

In your call function, also provide an object to specify the sender.

{ from: web3.eth.accounts[0] }

Also, when calling non-constant methods, you don't need the .call part, so this should work.

contractInstance.transfer(web3.eth.accounts[1], web3.toWei(0.005), { from: web3.eth.accounts[0] })
2
  • The final command works: contractInstance.transfer(web3.eth.accounts[1], web3.toWei(0.005), { from: web3.eth.accounts[0] })
    – ZhouW
    Commented Aug 2, 2017 at 9:30
  • Just a quick unsolicited comment: the fact that you can send 0.0005 to a function that expects an int is why we want strongly typed languages and why Javascript sucks. Commented Aug 2, 2017 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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