2

I need to send erc20 tokens from one address to another, provided that the token balance is not empty. I check the balance as follows:

const balance = contractInstance.methods.balanceOf (walletAddress) .call (function (err, result) {
       //console.log(err)
if (result> 0) {
console.log ("result")
}

If there are 3 tokens on the account, then the result variable is 3000000000000000000 After that, I want to send all tokens from this account to my other account:

web3.eth.getTransactionCount (fromAddress)
  .then ((count) => {
    let rawTransaction = {
      'from': walletAddress,
      'gasPrice': web3.utils.toHex (20 * 1e9),
      'gasLimit': web3.utils.toHex (41000),
      'to': tokenAddress,
      'value': '0x' + result.toString('hex'),
      'data': contract.methods.transfer (fromAddress, amount) .encodeABI (),
      'nonce': web3.utils.toHex (count)
    }

But in the end I get the error insufficient funds for gas * price + value, how do I correctly convert the result variable to send tokens?

2 Answers 2

1

You are using:

  • gas = 41000
  • price = 20 * 1e9
  • value = result

As you might understand at this point, the error-message "insufficient funds for gas * price + value" indicates that the ether balance of walletAddress is smaller than 41000 * 20 * 1e9 + result.

One way to solve it is simply by sending some ether to walletAddress.

Another way you can try is reducing your gas-price from 20 * 1e9 to something smaller.

0

If you are using the yarn package Ganache like I am the first wallet wont work even if it's showing you have 1000eth present it wont work just put the secret key of the second wallet and check if it has balance

const balance = await wallet.getBalance();
console.log("Account balance:", ethers.utils.formatEther(balance));

This solution worked for me

Your Answer

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

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