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?
Best Answer
You are using:
41000
20 * 1e9
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 than41000 * 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.