Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ethereum 限制发件人提取超过可用余额的金额_Ethereum_Solidity_Smartcontracts - Fatal编程技术网

Ethereum 限制发件人提取超过可用余额的金额

Ethereum 限制发件人提取超过可用余额的金额,ethereum,solidity,smartcontracts,Ethereum,Solidity,Smartcontracts,我正在编写一个简单的银行智能合约示例,但我在获取合约以限制发件人提取超过剩余余额的金额时遇到了困难。以下是我在合同中的职责: function withdraw(uint withdrawAmount) public returns (uint) { assert(balances[msg.sender] >= withdrawAmount); balances[owner] -= withdrawAmount; em

我正在编写一个简单的银行智能合约示例,但我在获取合约以限制发件人提取超过剩余余额的金额时遇到了困难。以下是我在合同中的职责:

function withdraw(uint withdrawAmount) public returns (uint) {
            assert(balances[msg.sender] >= withdrawAmount);
            balances[owner] -= withdrawAmount;
            emit LogWithdrawal(msg.sender, withdrawAmount, balances[msg.sender]);
            return balances[msg.sender];
    }
下面是.js测试:

it("should not be able to withdraw more than has been deposited", async() => {
    await instance.enroll({from: alice})
    await instance.deposit({from: alice, value: deposit})
    await catchRevert(instance.withdraw(deposit + 1, {from: alice}))
  })

我在想也许可以断言(…),但那不起作用,所以任何帮助都将不胜感激

首先,在这种情况下,您应该使用
require
而不是
assert
。也就是说,您的代码似乎没有问题,因此请确保您正确地跟踪用户余额。

您的代码不一致,您可以检查余额中的
msg.sender
,但从
所有者中退出

它应该是正确的版本:

function withdraw(uint withdrawAmount) public returns (uint) {
    require(balances[msg.sender] >= withdrawAmount);
    balances[msg.sender] -= withdrawAmount;
    emit LogWithdrawal(msg.sender, withdrawAmount, balances[msg.sender]);
    return balances[msg.sender];
}

您的代码错误,因为您将
取款金额
msg.sender
的余额进行比较,但减少了
所有者
的余额,因此
msg.sender
的余额不会改变。