So Im having some trouble with sending ETH from one account to another trough web3 with an erc20 smart contract and I`m using Infura RPC.
So, my process is to send 0.2 ETH from account 1 to account 2 trough a transfer function in the smart contract that I deployed on the kovan testnet. The account 1 and 2 are on the my metamask kovan testnet. After I sign transaction with metamask account private key, I broadcast the transaction and get the txhash (0xab20645e107d3963df31ad9184548f817efc3eaa6a69c8324cb04a9164fa40fc), but when I check the txHash on etherscan it is unable to locate that txHash…
My app.js code:
require('dotenv').config();
const Tx = require("ethereumjs-tx").Transaction;
const Web3 = require("web3");
const web3 = new Web3("https://kovan.infura.io/v3/2c4e643470574b06b81b...");
const contractAbiKovan = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"standard","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_initialSupply","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}];
const contractAddressKovan = "0x4CaE5eE0b695b99DB88d25b7CCA835078F7cdBE1";
const account1 = "0x8512fE60F40065E83850079423723291Cf289c18"; // metamask kovan account1
const account2 = "0x5745D7a8c848c871988f027F979105CfA0f09993"; // metamask kovan account2
const privateKey = Buffer.from(process.env.PRIVATE_KEY, "hex");
const contractKovan = new web3.eth.Contract(contractAbiKovan, contractAddressKovan);
const value = web3.utils.toHex(web3.utils.toWei('0.2', 'ether'));
data_ = contractKovan.methods.transfer(account2, value).encodeABI();
web3.eth.getTransactionCount(account1,(err, txCount) => {
const txObject = {
nonce : txCount,
to:contractAddressKovan,
gasLimit: web3.utils.toHex(50000),
gasCost: web3.utils.toHex(web3.utils.toWei("20","gwei")),
data: data_,
}
//sign transaction
const tx = new Tx(txObject);
tx.sign(privateKey);
const serializedTx = tx.serialize();
const raw = "0x" + serializedTx.toString("hex");
//broadcast the transaction
web3.eth.sendSignedTransaction(raw, (err, txhash) => {
console.log("err", err, "txHash:", txhash);
});
});
My transfer function in the smart contract:
function transfer(address _to, uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
Transfer(msg.sender, _to, _value);
return true;
}
Would really appreciate any help 🙂
Best Answer
Try to increase gwei, such as 100, then checking your transaction hash on etherscan. (Maybe I'm late but hoping it can help others)