Why is startingBalance - endBalance gasUsed gasPrice for an Ethereum transaction

0 votes

I'm trying to write javascript tests in truffle framework to verify eth balance change after a transaction that moves funds.

I want to rule out the gas expense so that I can assert the balance change by an exact amount.

The following is how I'm trying to determine the amount of eth spent on gas:

let startingBalance = await web3.eth.getBalance(me);
let tx = await contract.method.sendTransaction({from: me, gasPrice: 1});
let endBalance = await web3.eth.getBalance(me);
let receipt = await web3.eth.getTransactionReceipt(tx);
await (startingBalance - endBalance).should.be.equal(receipt.gasUsed);

My reasoning is that, since:

  • The transaction doesn't change funds
  • I explicitly set the transaction gasPrice to 1

the receipt.gasUsed should be equal to the balance change.

But running against testrpc, the test complained that the balance decremented slightly more than gasUsed * gasPrice, by about 2000.

Is there other factors that could contribute to eth balance change?

Oct 1, 2018 in Blockchain by slayer
• 29,370 points
858 views

1 answer to this question.

0 votes
startingBalance and endBalance are BigNumber objects. Change your test to

await (startingBalance.minus(endBalance)).should.be.equal(receipt.gasUsed);
answered Oct 1, 2018 by digger
• 26,740 points

Related Questions In Blockchain

+1 vote
1 answer
0 votes
0 answers

Is there any API for ether sent transaction in ethereum without using web3.js

I want to do a transaction (sent ...READ MORE

Jun 17, 2019 in Blockchain by vishal tyagi
1,221 views
0 votes
1 answer

Is it possible to setup private ethereum network for creating applications??

yes, ethereum is an open source platform ...READ MORE

answered Apr 18, 2018 in Blockchain by Christine
• 15,790 points
797 views
+1 vote
1 answer

What is the best way to search for an item in blockchain?

All transactions and records in blockchain are ...READ MORE

answered Apr 21, 2018 in Blockchain by Perry
• 17,100 points
1,898 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
1,464 views
+1 vote
1 answer

Ethereum vs Hyperledger: Which is better for business networks?

Hyperledger is preferred for B2B business and ...READ MORE

answered Sep 20, 2018 in Blockchain by digger
• 26,740 points
629 views
0 votes
1 answer

Why is Nubits faster than Bitcoin?

Different implementation of blockchain can have different ...READ MORE

answered Jul 11, 2018 in Blockchain by digger
• 26,740 points
644 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP