1

Im using the official ERC20 code example from here: https://www.ethereum.org/token

I've got everything running fine in truffle, but when i call this function:

function transfer(address _to, uint256 _value) public {
        _transfer(msg.sender, _to, _value);
    } 

I get Error: Invalid number of arguments to Solidity function

I'm running these commands from the truffle console:

 var token;
 Token.deployed().then((res) => {token = res});
 var alice = '0xbb452da6021229d93b28a6fea487fd3098eb8b16';
 token.transfer(alice, 44);

I've copy pasted the address from the accounts listed by testrpc Any idea whats going on? Thanks!

1

You are missing the from:account parameter.

var account_one = "0x1234..."; // the address calling this function. Get the list of accounts and use account[0]; 

token.transfer(alice, 44, {from: account_one}).then(function(tx_id) {
  // If this callback is called, the transaction was successfully processed.
  // Note that Ether Pudding takes care of watching the network and triggering
  // this callback.
  alert("Transaction successful!")
}).catch(function(e) {
  // There was an error! Handle it.
})
  • i tried truffle(development)> token.transfer(alice, 55, {from: account_one}); and i got the same error message. I'm really confused since transfer clearly has only two parameters and I'm passing exactly two... (Note that I'm calling transfer not _transfer) – mark Nov 3 '17 at 18:52
  • your code does work though. Can you explain what {from: account_one} is and why it is necessary? Also what is tx_id? Thanks! – mark Nov 3 '17 at 20:36
  • From the Truffle docs: We passed an object as the third parameter to sendCoin. Note that the sendCoin function in our Solidity contract doesn't have a third parameter. What you see above is a special object that can always be passed as the last parameter to a function that lets you edit specific details about the transaction. Here, we set the from address ensuring this transaction came from account_one. You can use that object to pass, for example, how much gas you want to supply. tx_id is the id of the transaction returned by this execution. – pabloruiz55 Nov 3 '17 at 21:47
1

Just for those that are trying to do without truffle (and with metamask web3 js).

I called this below function on button click for test purpose, and it sent the tokens properly.

Note : clicking the button will trigger metamask confirmation, submitting which will transfer the tokens upon successful mining into a block.

function transferTokens()
{
    if (typeof web3 == 'undefined') {console.log("Metamask Not found"); return; }
    var contrctAddress = "PLACE_YOUR_CONTRAT_ADDRESS"; //address of token contract
    var abi = []; //REPLACE JSON OF your token contract
    var MyContract = web3.eth.contract(abi);
    var recipient = "RICIPIENT_ADDRESS"; //to whom you want to send the tokens to
    var myContractInstance = MyContract.at(contrctAddress);
    var tokensToSend = 2; //replace with number of tokens to send
    var weiAmount = tokensToSend*1e18;
    web3.eth.getAccounts(function(error, result) {
         myContractInstance.transfer(recipient ,weiAmount,function (err, result) {
         if (err) {
              console.log(err); //handle the error
              return;
         }
         else
         {
            console.log('success'); //your code for success
          }
       }); });
}

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.