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,040 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 27,620 points

Related Questions In Blockchain

0 votes
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 in Blockchain by vishal tyagi
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
+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 20, 2018 in Blockchain by Perry
• 16,450 points
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
0 votes
1 answer
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
• 67,000 points
+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
• 27,620 points
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
• 27,620 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.