我正在使用以太坊的 Truffle 测试框架(v4.0.1)。我无法弄清楚为什么gasPrice*gasUsed
以下简单合约的交易费用没有增加:
contract MinTest {
function run() public returns(bool) {
return true;
}
}
我正在使用的摩卡测试是:
contract('Minimum Test', function (accounts) {
it("min test", function () {
var initial = web3.eth.getBalance(accounts[1]);
var final;
return MinTest.deployed().then(function(instance) {
return instance.run({from: accounts[1]});
}).then(function(result) {
final = web3.eth.getBalance(accounts[1]);
var gasPrice = new BigNumber(web3.eth.gasPrice);
var gasUsed = new BigNumber(result.receipt.gasUsed);
var gasCost = gasPrice.times(gasUsed);
console.log("gasPrice : " + gasPrice);
console.log("gasUsed : " + gasUsed);
console.log("gasCost : " + gasCost);
console.log("initial : " + initial);
console.log("initial-gasCost: " + initial.minus(gasCost));
console.log("final : " + final);
console.log("unaccounted : " + initial.minus(gasCost).minus(final));
});
});
});
上面的测试产生以下输出:
gasPrice : 20000000000
gasUsed : 21478
gasCost : 429560000000000
initial : 100000000000000000000
initial-gasCost: 99999570440000000000
final : 99997852200000000000
unaccounted : 1718240000000000
我预计MinTest.run
对合约函数的调用会导致accounts[1]
借记的金额正好等于gasPrice*gasUsed
,但在本例中并非如此。有一个额外的1718240000000000
wei 借记,我无法解释。为什么1718240000000000
这里要多加一个wei?