I need to get the gasUsed and gasPrice so that I can calculate the correct amount of gas required for a transaction.

I can get the gasUsed using:

var accountOneReceipt = await contractInstance.functionName(parameterOne, {from: accountOne});

var accountOneGasUsed = accountOneReceipt.receipt.gasUsed;

But there is no gasPrice parameter in transaction receipt.

Info: getTransactionReceipt


Instead of using getTransactionReceipt, I used getTransaction

So, to solve the above, we can use:

var accountOneGasPrice = (await web3.eth.getTransaction(accountOneReceipt.tx)).gasPrice

Where accountOneReceipt is the return value we receive from the above function call. And accountOneReceipt.tx to get the transaction hash.

Note: await is necessary for this code, as else you might get a pending state of the return value, which won't include the gasPrice.

Info: getTransaction

| improve this answer | |

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.