9

I've fired up my testrpc and deployed a simple HelloWorld contract using truffle. I send a simple transaction to my contract and I log my account balance before and after the transaction. Unfortunately, this does not add up to the gasUsed parameter I see in the transaction.

contract HelloWorld {

    uint public x;   

    function HelloWorld() {
        x = 5;
    }

    function set_x(uint _x) returns(uint x) {
        x = _x;
        return x;
    }

}

And my javascript

var HelloWorld = artifacts.require("./HelloWorld.sol");

module.exports = function(callback) {}

from_wei = web3._extend.utils.fromWei

var account1 = web3.eth.accounts[0]; 
var start_balance = web3.eth.getBalance(account1).toNumber();

HelloWorld.deployed().then(function(instance) {

    return instance.set_x(500)

}).then(function(tx) {

    console.log(tx)
    var new_balance = web3.eth.getBalance(account1).toNumber();

    console.log(start_balance + " initial balance");
    console.log(new_balance + " balance after transaction");
    console.log((start_balance - new_balance) + " difference");
    console.log(from_wei(start_balance - new_balance) + " difference (from_wei) ?");
    console.log(web3.eth.gasPrice.toNumber() + " gas price")

})

Outputs something like this:

{ tx: '0x1a2876b330618c75c7e264becbee75c491b47b5a17a0f9f9fe7fb124f0e93113',
  receipt:
   { transactionHash: '0x1a2876b330618c75c7e264becbee75c491b47b5a17a0f9f9fe7fb124f0e93113',
     transactionIndex: 0,
     blockHash: '0xf30134cac3be167d6053a21953b87010ce7145ccb36775e76d38458dddfa81b9',
     blockNumber: 16,
     gasUsed: 21736,
     cumulativeGasUsed: 21736,
     contractAddress: null,
     logs: [] },
  logs: [] }
99939363900000000000 initial balance
99937190300000000000 balance after transaction
2173600000000000 difference
0.0021736 difference (from_wei) ?
20000000000 gas price

It looks like the difference in my account balance IS the same as the gas used, just a different magnitude. So I'm confused? Are these numbers expressed in wei? I thought I needed to multiply by the gas price to see how much the transaction would affect my account balance...? How can I get GasUsed to equal the difference in my account balance?

  • Check that TestRPC isn't using a gas price of 1 – o0ragman0o Apr 2 '17 at 3:52
  • No, its at the default gas price of 20000000000 – natsuki_2002 Apr 2 '17 at 15:42
  • 1
    The most likely explanation is that truffle is using a different gas price than the one that testrpc uses as default. The transaction object (not receipt: get it with eth.getTransaction) will show the actual gas price used – Tjaden Hess Apr 2 '17 at 17:13
  • 1
    You're very likely running into floating point arithmetic errors. You should avoid .toNumber() at all cost. – frangio Apr 18 '17 at 20:46
4

testrpc hardcodes the gas price of transactions to 0x01, at least as of 4727b40

  • 2
    Interesting. Thank you. Do you know why web3.eth.gasPrice returns 20000000000? And what magnitude is it expressed in? I can't get the difference in my account balance to be the same magnitude as the gas used... – natsuki_2002 Apr 3 '17 at 20:15
  • @natsuki_1996 It's a mismatch between what the internal state manager returns vs what's actually passed into transactions when they're created. I noted a similar discrepancy with coinbase / miner in a PR – DeviateFish Apr 3 '17 at 20:26
  • 1
    Missed the second half of that... Gas price might be denominated in eth/Mgas, though that doesn't seem to align with the results you obtained. I'll have to dig through how transactions are processed in testrpc/its dependencies to give you a better answer – DeviateFish Apr 3 '17 at 20:49
  • @natsuki_1996: I'm wondering if the issue might be around using .toNumber() on the BigNumbers you're pulling from web3. I've used something like this to accurately account for gas paid in tests on the testrpc (which should be agnostic of the actual gas price). It might need some translation to the non-promisified version, but maybe give that a shot and see what you get? – DeviateFish Apr 4 '17 at 23:52
4

You can configure network as you wish in truffle.js file.

One of the parameters is gasPrice (Default is 100000000000)

Documentation

  • I believe this gasPrice is only used for contract deploys – emilebaizel Jul 18 '18 at 14:05
0

As stated above:

truffle uses a different gasPrice than testRPC

If you really want to use web3.eth.gasPrice as a reference on your tests, while using testRPC, you can force its own gasPrice by running it with:

testrpc --g 100000000000

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.