0

I want to use whatever ethereum is available in my account as transaction fee to send a token transaction. I know the gas I need, so in theory I thought the following formula would work:

gasPrice = balanceInWei / gas

so I tried with 0.001 ETH:

gasPrice = 1000000000000000 Wei / 130091

which results in 0.000000007686926843 ETH per gas unit

But I got

Error: Returned error: insufficient funds for gas * price + value

I am sending no value, so in theory the gas * price do match the funds.

What am I missing? Do I have to use a minimum of 1 gwei as gas?

I am using Web3js version 5.6.0. Here's the code:

function main() {
    console.log('Transferring from address 0x8..');
    web3.eth.getTransactionCount(SOURCE_WALLET_ADDRESS).then(function (nonce) {
        console.log('The nonce is ' + nonce);
        web3.eth.getBalance(SOURCE_WALLET_ADDRESS).then(function (balance) { 

            console.log('Balance ' + balance); 
            sendTransaction(nonce, balance);
        });
    });
}

function sendTransaction (nonce, balance) {
    var tokensToSend = '1000';
    var contract = new web3.eth.Contract(abiArray, CONTRACT_ADDRESS, { from: SOURCE_WALLET_ADDRESS });
    var balanceInWei = balance * 1000000000000000000;
    var gas = 130091;

    // Find out gas price to use full eth balance
    gasPrice = balanceInWei / gas;

    console.log('\r\nStarting transaction. with nonce ' + nonce);
    console.log('Gas price : ' + gasPrice + '\r\n');

    let details = {
        "to": CONTRACT_ADDRESS,
        "from": SOURCE_WALLET_ADDRESS,
        "data": contract.methods.transfer(DESTINATION_WALLET_ADDRESS, tokensToSend).encodeABI(),
        "value": web3.utils.toHex(tokensToSend),
        "nonce": nonce,
        "gas": gas, // tx fee (gas * gasPrice)
        "gasPrice": gasPrice, // has to be in Wei
        "chainId": 1 // EIP 155 chainId - mainnet: 1, rinkeby: 4
    }

    const transaction = new EthereumTx(details);
    transaction.sign(Buffer.from(WALLET_PRIVATE_KEY, 'hex'));
    const serializedTransaction = transaction.serialize();

    web3.eth.sendSignedTransaction('0x' + serializedTransaction.toString('hex')).then(function (transId) {
        console.log('My job here is done. ' + transId.transactionHash);
    });
}

main();

Example output:

*** PROGRAM START ***

Transferring from address 0x8Df3..
The nonce is 71
Balance: 0.003703316

Starting transaction. with nonce 71
Gas price : 28467119170.426853
  • Which wallet do you use? I mean the one where the error happens. – Roman Frolov Feb 9 '18 at 19:16
  • I am using web3js, so no wallet involved, not sure what you mean – Victor Feb 9 '18 at 19:57
  • 1
    Can you share the exact code you ran to make the transaction? – user19510 Feb 9 '18 at 19:59
  • of course @smarx -- I've updated the question with the source code – Victor Feb 10 '18 at 11:22
1

Your code multiplies the balance passed in by 10**18, presumably to convert from ether to wei. But the balance is coming from web3.eth.getBalance, which returns wei already. So you're setting the gas price way too high (by a factor of 10**18). Just use:

var balanceInWei = balance;

(Or use balance directly.)

EDIT

You should also avoid trying to use a gas price with fractions of wei, so floor the result of the division:

gasPrice = Math.floor(balanceInWei / gas);

EDIT 2

This line tries to send the same amount of ether as you're sending tokens. You said you're not trying to transfer any ether, so delete this line:

"value": web3.utils.toHex(tokensToSend),
  • When I see the output of the balance after its retrieved, the amount is shown in eth - I have updated the question with an output example – Victor Feb 10 '18 at 11:30
  • Thanks for confirming that the formula is correct. The error must be in how things are handled in the code - perhaps var types or something – Victor Feb 10 '18 at 11:36
  • I've seen that people like to use hex values in all fields of the transaction, not sure if could be for things like this – Victor Feb 10 '18 at 11:43
  • web3.eth.getBalance returns the balance in wei. Can you share the code that produces the output you added to the question? (The current code produces no such output.) – user19510 Feb 10 '18 at 12:42
  • 1
    (FYI, 5.6.0 is the version of npm you're using.) – user19510 Feb 10 '18 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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