【发布时间】:2018-03-12 03:58:19
【问题描述】:
我正在尝试在 truffle 框架中编写 javascript 测试,以验证转移资金的交易后 eth 余额的变化。
我想排除 gas 费用,以便我可以断言余额变化的确切金额。
以下是我尝试确定在 gas 上花费的 eth 数量的方法:
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);
我的理由是,因为:
- 交易不会改变资金
- 我明确地将交易 gasPrice 设置为 1
receipt.gasUsed 应该等于余额变化。
但是针对 testrpc 运行,测试抱怨余额比gasUsed * gasPrice 减少了大约 2000 年。
是否还有其他因素可能导致 eth 余额变化?
谢谢!
【问题讨论】: