在Remix.Ethereum模块上使用LearnEth IDE。这个练习是关于错误处理的。我已经成功地编译并部署了errorHandling.sol脚本到JSVM。
但是,当我与IDE中的mint函数交互时(输入帐号和不同的整数)--我能够成功地从1-53中“mint”,但不能超过它--它失败并给出了错误消息:
对于我的生活,我不明白为什么其他数字会工作,而不是这个,考虑到限制?如有任何指导,我将不胜感激。
对不起,如果有明显的错误或误解-非常新的这里。
contract Coin {
address public minter;
mapping (address => uint) public balances;
event Sent(address from, address to, uint amount);
constructor() public {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
require(minter == receiver, 'Cannot Mint! Minter is not contract Creator!');
require(amount < (1 * (10^60)), 'Amount requested too High for minting');
balances[receiver] = amount;
}
function send(address receiver, uint amount) public {
require(balances[minter] >= amount, 'Coin balance to low for transaction!');
balances[minter] = -amount;
balances[receiver] = amount;
emit Sent(minter, receiver, amount);
}
}```
> [vm] from: 0x5B3...eddC4to: Coin.mint(address,uint256)
> 0xEf9...10eBfvalue: 0 weidata: 0x40c...00036logs: 0hash: 0xc0a...40b8b
> transact to Coin.mint errored: VM error: revert.
>
> revert The transaction has been reverted to the initial state. Reason
> provided by the contract: "Amount requested too High for minting".
> Debug the transaction to get more information.发布于 2021-10-16 14:15:24
就是因为这种情况。
require(amount < (1 * (10^60)), 'Amount requested too High for minting');如果amount的值大于或等于54,则抛出异常,有效地恢复事务。
您可以将(1 * (10^60))表达式简化为10^60。请注意,在稳健语法中,这是而不是 "10 to of 60“--它是"10 异或 60”(结果是54)。
如果要计算"10到60的幂“,语法是10 ** 60。
发布于 2021-10-20 19:06:31
问题已经回答了。我想在这里记录逻辑操作的完整性:
当比较两个字节(10和60)在一个位级的XOR操作(即这两个位必须是不同的才能得到一个1)时,我得到了54:
255: 1 1 1 1 1 1 1 1 = (128 + 64 + 32 + 16 +8+4+2+ 1) = 255
60: 0 0 1 1 1 1 0 0 = (0 +0+ 32 + 16 +8+4+0+ 0) = 60
10: 0 0 0 0 1 0 1 0 = (0 +0+0+0+8+0+2+ 0) = 10
60 XOR 10: 0 0 1 1 0 1 1 0 = (0 +0+ 32 + 16 +0+4+2+ 0) = 54
https://stackoverflow.com/questions/69510681
复制相似问题