How to send custom ERC20 tokens from one wallet to another, if i want to send EBTC/ECASH custom ERC20 tokens.
I wrote a method that can send the ether from one account to another, how could i do the same for non ether transactions?
public static void sendCustomToken(String privateKey,String toAccount, double amount) throws Exception{
Web3j web3 = Web3j.build(new HttpService("http://localhost:8180/"));
BigInteger key = new BigInteger(privateKey,16);
ECKeyPair ecKeyPair = ECKeyPair.create(key.toByteArray());
Credentials credentials = Credentials.create(ecKeyPair);
TransactionReceipt transactionReceipt = Transfer.sendFundsAsync( web3, credentials, toAccount, BigDecimal.valueOf(amount), Convert.Unit.ETHER).get();
System.out.println("Transaction Hash:"+transactionReceipt.getTransactionHash());
}
Custom Token: eBTC
Contract Address: 0x2fd41f516fac94ed08e156f489f56ca3a80b04d0
Token Decimals: 8
Any help or pointers?
Best Answer
I assume you are using Web3j and a deployed smart contract implementing ERC20 interface, where transfer method looks like this:
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:You can find more info on how to work with smart contracts wrappers here. Hope that helps.
edit.
abi
andbin
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: