Can we send ERC20 tokens to a contract address??
[Ethereum] Is it possible to send ERC20 tokens to a contract address like sending ether
erc-20go-ethereumsolidity
Related Solutions
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.
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.
I assume you are using Web3j and a deployed smart contract implementing ERC20 interface, where transfer method looks like this:
function transfer(address _to, uint256 _value) public returns (bool success) {
require(balances[msg.sender] >= _value && _value > 0);
balances[msg.sender] -= _value;
balances[_to] += _value;
return true;
}
You can use web3j wrapper for your contract to call contract methods on a blockahain. Suppose your Contract filname is MyContract.sol
, then you'll need solc compiler and web3j tool and do the following in your console:
$ solc {contract}.sol --bin --abi --optimize -o {output-dir}/
this will generate .abi
and .bin
files from which you generate a java wrapper class:
$ web3j solidity generate /path/to/<smart-contract>.bin /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name
This will produce a java wrapper class for your contract like MyContract.java
on which you can call all the methods available on the smart contract:
import static org.web3j.tx.Contract.GAS_LIMIT;
import static org.web3j.tx.ManagedTransaction.GAS_PRICE;
//web3j initialization code goes here
//Load the deployed contract:
MyContract contract = MyContract.load(contractAddress, web3j, credentials, GAS_PRICE, GAS_LIMIT);
//Call methods on the contract:
Future<> result = contract.transfer(_to, _value);
You can find more info on how to work with smart contracts wrappers here. Hope that helps.
edit.
abi
and bin
for this particular contract can be obtained from etherscan.io
You can transfer tokens even if you are not the owner of the contract. But you are only abe to transfer tokens available to your own account as seen in the contract:
function transfer(address _to, uint256 _amount) returns (bool success) {
if (balances[msg.sender] >= _amount
&& _amount > 0
&& balances[_to] + _amount > balances[_to]) {
balances[msg.sender] -= _amount;
balances[_to] += _amount;
Transfer(msg.sender, _to, _amount);
return true;
} else {
return false;
}
}
Best Answer
Yes, any address (contract or not) can be the recipient of ERC20 tokens.
The token contract keeps track of each address's balance. The implementation is usually very simple; just a mapping of addresses to their balances. The
transfer
method implementation probably won't do anything aside from check the sender's balance before making the transfer.For more details see here.