[Ethereum] Send erc20 tokens using the json-rpc api

erc-20go-ethereumgolemjson-rpc

I need to send ERC20 tokens (golem to be precise) in an automated manner, so I set up a geth json-rpc server with

./geth --fast --rpc

So I can call the api just fine.

The documentation for eth_sendtransaction makes it easy to send ethereum to another address, but I couldn't find how to send ERC20 tokens. I saw the data field which allowed to trigger a smart contracts. So I suppose I need to trigger a function within the smart contract of the ERC20 token but since I would need to learn solidity and read through the whole golem smart contract I thought I'd ask here first.

Thanks !

Best Answer

To send ERC20 compliant token you need to call method transfer of contract. To do this with geth you correctly have chosen eth_sendTransaction RPC Call.

In data parameter you should specify ABI-encoded function transfer with parameters encoded as described here (ABI-encoded).

To encode parameters you may want to use online tool https://abi.hashex.org. You enter name of function (transfer) and add parameters with types and values. Or you can just paste in token abi and pick function transfer in dropdown list "Function type". Here is example of using this tool.

enter image description here

There is also a javascript library to encode parameters if you want to do this automatically.

After you got your ABI-encoded data, you set it to data field in Geth RPC call, set other values and you are ready to call Geth and transfer tokens.

Pay attention to that account your are sending tokens from (from parameter in RPC call) should be unlocked in Geth to sign transaction.

Related Topic