2

I have implemented a function for transferring ETH from my account to another account:

async function transfer(to, value) {
    let options = {
        from : PUBLIC_ADDRESS,
        to   : to,
        value: value
    };
    options.gas = await web3.eth.estimateGas(options);
    let signedTransaction = await web3.eth.accounts.signTransaction(options, PRIVATE_KEY);
    return await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
}

This is purely for ETH transfer; I am not trying to invoke any smart contract function, so I can omit the data field from the options object.

I've noticed that web3.eth.estimateGas(options) always returns 21000.

Can I count on that, and simplify my code to:

    let options = {
        from : PUBLIC_ADDR,
        to   : to,
        value: value,
        gas  : 21000
    };

?

Thanks

3

If the to address is not a smart contract, then yes. This is the standard gas cost of a simple ether transfer.

If the to address is a smart contract, even if you're not sending anything in the data field, a different gas amount could be consumed.

  • Indeed, my to address is not a smart contract, hence the data field is empty (and I now understand that an empty data field doesn't necessarily imply a non-contract to address). Thank you very much!!! – goodvibration Oct 21 '18 at 19:59

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.