0

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 :)

2
  • 1
    I see not obvious problem with the code. The gas is a bit low (I'd use 250k), but it should fail with not enough gas and not disappear. Perhaps it was caused by a network glitch. Did you log the nonce? Did you send another transaction at the same time?
    – Ismael
    Commented Jul 1, 2020 at 23:08
  • Yes my nonce is 200, so the 201 transaction doesnt go trough, its like it isnt picked up by the miners ? Both accounts are on metamask btw , can that be the issue, do I need to init metamask ? Yes i tried to bump the gas to 300k and upped the gasCost too, but still the same, in the output I get the txHash no errors , but when I check the txHash in kovan etherscan, it can`t find it and my funds stay the same in both accounts ... :/ Hmm yes I waited for like a few minutes and checked for the txhash and then tried a new transaction...
    – heyooo12
    Commented Jul 2, 2020 at 17:06

1 Answer 1

0

Try to increase gwei, such as 100, then checking your transaction hash on etherscan. (Maybe I'm late but hoping it can help others)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.