Тестирование этой функции:
function withdrawAll ()
public
stopInEmergency () {
if (!msg.sender.send(12 finney)) {
throw;
}
}
Мой трюфельный тест:
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);
});
При запуске testrpc --g 1
gasPrice: 1
withdrawn: 12
recieved: 9.127600000008192
gas comission: 0.000000000028724
diff: 2.872399999991808
При запуске testrpc --g 100000000000
gasPrice: 100000000000
withdrawn: 12
recieved: 9.127600000008192
gas comission: 2.8724
diff: 2.872399999991808
Почему цена газа не влияет на сумму, которую получает user_6?
В вашем примере вы используете цену газа в сети, и вы должны использовать цену газа транзакции.
user_6_gas_cost = web3.eth.getTransactionReceipt(tx_id).gasUsed *
web3.eth.getTransaction(tx_id).gasPrice;
Если вы не установите gasPrice в транзакции, будет использоваться значение по умолчанию (с testrpc, кажется, 100 Gwei). Он не будет использовать цену газа в сети (возвращаемую web3.eth.gasPrice). Вы должны явно установить цену на газ, которую хотите.
my_contract.withdrawAll ({ from: user_6, gasPrice: web3.eth.gasPrice })
Ксавье Лепретр B9lab
var unit = "finney"
, отсутствует?Такеши