Paritosh
Paritosh

Reputation: 61

Execute raw transaction for ERC20 token 'transfer function' for smart contracts deployed on RinkeBy Testnet

I'm trying to transfer balance from contract deployer address to another address using ERC20 transfer function.

For local Ganache network transfer from deployer address is as simple as

await contract.transfer(receiverAddress, amount);

Since Rinkeby Testnet doesnot allow to use method 'eth_sendTransaction' and gives error, I, therefore, tried to create raw transaction as given below. No error received on executing below written code but there is no change in receiver's balance either.

const result = {};
const contract = new web3.eth.Contract(token_artifact.abi, config.contractAddress);
const count = await web3.eth.getTransactionCount(config.deployerAddress);

const nonce = web3.utils.toHex(count);
const gasLimit = web3.utils.toHex(90000);
const gasPrice = web3.utils.toHex(web3.eth.gasPrice || web3.utils.toHex(2 * 1e9));
const value = web3.utils.toHex(web3.utils.toWei('0', 'wei'));
const data = contract.methods.transfer(receiver, amount).encodeABI();

const txData = {
      nonce,
      gasLimit,
      gasPrice,
      value,
      data,
      from: config.deployerAddress,
      to: receiver
     };

web3.eth.accounts.wallet.add(config.deployerPrivateKey);
const sentTx = await web3.eth.sendTransaction(txData);
result['receipt'] = sentTx;

Response from the Rinkeby testnet:

{
    "receipt": {
        "blockHash": "0xcb259fa6c63153f08b51153cf9f575342d7dd0b9c091c34da0af5d204d1aff14",
        "blockNumber": 8985823,
        "contractAddress": null,
        "cumulativeGasUsed": 21572,
        "effectiveGasPrice": "0x77359400",
        "from": "0x6d10f3f1d65fadcd1b6cb15e28f98bcfb0f4e9e5",
        "gasUsed": 21572,
        "logs": [],
        "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
        "status": true,
        "to": "0xbc0422d1b7e2f7ad4090acb6896779c56b3af331",
        "transactionHash": "0xdd86d849769e87fef0fd99f2034a5d32821c0dc565c900a5ed1274edbd956b41",
        "transactionIndex": 0,
        "type": "0x0"
    }
}

[Note: I'm able to check total supply and balance at the given address.]

let data = {};
const contract = require('@truffle/contract');
const Token = contract(token_artifact);
Token.setProvider(web3.currentProvider);

const instance = await Token.at(config.contractAddress);

const result = await Promise.all([instance.totalSupply(), instance.balanceOf(address)]);
data['totalSupply'] = result[0].toNumber();
data['balanceOf'] = result[1].toNumber();

resolve(data);

Upvotes: 0

Views: 1376

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43521

When transferring tokens, you need to interact with the contract (i.e. send a transaction to the contract). Not with the token receiver directly.

So you need to change the to field of the transaction to the contract address.

// this field says that you want to transfer tokens to the `receiver`
const data = contract.methods.transfer(receiver, amount).encodeABI();
const txData = {
      nonce,
      gasLimit,
      gasPrice,
      value,
      data, // containing info that you want to `transfer()` to the `receiver`
      from: config.deployerAddress, // send transaction from your address
      to: config.contractAddress // CHANGED; send transaction to the contract address
};

Upvotes: 3

Related Questions