构建众筹应用程序。一旦达到目标,对项目的任何以太贡献都将通过 msg.sender.transfer(excess) 或 msg.sender.send(excess) 退还。
在 Remix 和 Metamask 上进行了广泛的测试(通过 Web3 部署)。关键问题是多余的资金会从发件人的账户中全额扣除,并且不会退还给发件人。
pragma solidity ^0.5.0;
contract Funds {
uint public maximumValue = 100;
uint public currentValue = 0;
// addFunds function that takes in amount in ether
function addFunds (uint _amount) public payable {
// Converts wei (msg.value) to ether
require(msg.value / 1000000000000000000 == _amount);
// if the sum of currentValue and amount is greater than maximum value, refund the excess
if (currentValue + _amount > maximumValue){
uint excess = currentValue + _amount - maximumValue;
currentValue = maximumValue;
msg.sender.transfer(excess);
} else {
currentValue += _amount;
}
}
}