3

Testing this function:

function withdrawAll () 
public
stopInEmergency () {
    if (!msg.sender.send(12 finney)) {
        throw;
    }
}

My truffle test:

it("Should let correct payouts and withdrawals", function(done) {
var my_contract = My15.deployed();
var unit = "finney";
const user_6_initialBalance = web3.eth.getBalance(user_6);
var  user_6_gas_cost = 0;

return my_contract.withdrawAll ({from: user_6});
    .then(function(tx_id){

        user_6_gas_cost = web3.eth.getTransactionReceipt(tx_id).gasUsed * web3.eth.gasPrice;

        const user_6_finalBalance = web3.eth.getBalance(user_6); 
        const withdrawn = web3.toWei(12, unit);
        const recieved = user_6_finalBalance - user_6_initialBalance;
        const diff = withdrawn - recieved;

        console.log('gasPrice: ', web3.eth.gasPrice.toString(10)); 
        console.log('withdrawn: ', web3.fromWei(withdrawn, unit));
        console.log('recieved: ', web3.fromWei(recieved, unit));
        console.log("gas cost: ", web3.fromWei(user_6_gas_cost, unit));
        console.log('diff: ', web3.fromWei(diff, unit));

    }).then(function(){
        done();
    }).catch(done);

});

When running testrpc --g 1

gasPrice:  1
withdrawn:  12
recieved:  9.127600000008192
gas comission:  0.000000000028724
diff:  2.872399999991808

When running testrpc --g 100000000000

gasPrice:  100000000000
withdrawn:  12
recieved:  9.127600000008192
gas comission:  2.8724
diff:  2.872399999991808

Why doesn't gas price influence the amount user_6 receives?

2
  • 1
    I suppose var unit = "finney" is missing? Commented Sep 6, 2016 at 11:47
  • @XavierLeprêtreB9lab yes, thank you! Updated the question.
    – takeshi
    Commented Sep 6, 2016 at 12:14

1 Answer 1

1

In your example you are using the network gas price and you should use the transaction gas price.

user_6_gas_cost = web3.eth.getTransactionReceipt(tx_id).gasUsed *
    web3.eth.getTransaction(tx_id).gasPrice;

If you do not set the gasPrice in a transaction it will use a default value (with testrpc appears to be 100 Gwei). It will not use the network gas price (returned by web3.eth.gasPrice). You have to explicitely set the gas price you want.

my_contract.withdrawAll ({ from: user_6, gasPrice: web3.eth.gasPrice })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.