我试图写一份合同,其中有一个函数,将金额分成等份,然后在一笔交易中存入多个帐户。问题是,如果在存放来自给定地址的任何地址时产生了一些错误,我希望函数反转或失败事务。有可能让它更有活力吗?谢谢
contract amountSplit {
mapping(address => uint) public toAccountBalance;
address[2] public beneficiaryList;
event LogReceived(address sender, uint amount);
function addAddresses(address addressOne, address addressTwo) {
beneficiaryList[0]=addressOne;
beneficiaryList[1]=addressTwo;
}
function pay(uint amount) public payable returns(bool success)
{
if(amount==0) throw;
uint forth = amount / 2;
toAccountBalance[beneficiaryList[0]] += forth;
toAccountBalance[beneficiaryList[1]] += forth;
emit LogReceived(msg.sender, msg.value);
return true;
}
}发布于 2019-03-11 10:19:29
在您的代码中,只有以下可能的错误:
Amount是零。增加任何beneficiaries的余额会导致溢出。
第一个是由你照顾,但我建议这样做:
require(ammount>0)第二个问题是,在执行数学操作时,需要检查溢出。为此,您可以使用safeMath库或只检查溢出。最后一种情况是这样的:
function pay(uint amount) public payable returns(bool success)
{
require(amount>0);
uint forth = amount / 2;
require(toAccountBalance[beneficiaryList[0]] + forth > forth);
require(toAccountBalance[beneficiaryList[1]] + forth > forth);
toAccountBalance[beneficiaryList[0]] += forth;
toAccountBalance[beneficiaryList[1]] += forth;
emit LogReceived(msg.sender, msg.value);
return true;
}如果金额为零或和产生溢出,则此函数将抛出。
希望这能有所帮助。
https://ethereum.stackexchange.com/questions/68192
复制相似问题