0

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?

  • I've also unlocked the web3.eth.accounts[0] account before trying to execute the transfer function – ZhouW Aug 2 '17 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 Feb 8 '18 at 11:02
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] })
  • The final command works: contractInstance.transfer(web3.eth.accounts[1], web3.toWei(0.005), { from: web3.eth.accounts[0] }) – ZhouW Aug 2 '17 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. – Thomas Jay Rush Aug 2 '17 at 11:07

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.