我有问题,使用甜糖测试,我可以退出我的合同。这是一个非常简单的测试,但是在下面调用withdrawBalance
函数之后。当我稍后使用web3.eth.getBalance
时,余额保持不变。
我还可以看到,在Ganache中,owner
没有接收到ETH。
但是,如果我从withdrawBalance
方法返回余额。实际上是0。
contract Room {
address public owner = msg.sender;
function withdrawBalance() public {
require(msg.sender == owner);
owner.transfer(this.balance);
}
}
测试文件:
it('should allow withdrawls to original owner', function () {
var meta;
return Room.deployed()
.then(instance => {
meta = instance;
return web3.eth.getBalance(meta.address);
})
.then((balance) => {
// Assertion passes as previous test added 6 ETH
assert.equal(balance.toNumber(), ONE_ETH * 6, 'Contract balance is incorrect.');
return meta.withdrawBalance.call();
})
.then(() => {
return web3.eth.getBalance(meta.address);
})
.then((balance) => {
// Assertion fails. There is still 6 ETH.
assert.equal(balance.toNumber(), 0, 'Contract balance is incorrect.');
});
});
我的问题是:
发布于 2018-02-12 18:54:48
您使用的是return meta.withdrawBalance.call();
而不是return meta.withdrawBalance.sendTransaction();
。
.call()
在您的EVM中本地运行,并且是免费的。在自己的机器上运行所有计算,执行后的任何更改都会恢复到初始状态。
要实际更改区块链的状态,需要使用.sendTransaction()
。这要花费天然气,因为矿工在执行你的交易过程中所做的计算会得到回报。
总结:
ETH没有被撤回。
https://stackoverflow.com/questions/48753350
复制相似问题