我使用的是众包合同(如下所示),在Ropsten网络上,我刚刚设定了创建目标:1 ETH,将其发送到众包地址,然后我可以撤回到受益人地址,但当我第二次发送ETH时,我不能再撤回它了。
我发现自己需要一个Crowdsale代码,可以随时撤回创建,也需要一种自动将ETH从众包发送到ETH地址的方法,就像有人向众包发送1条ETH一样:
这是我正在尝试使用的代码,但只有第一次提取有效(第二次退出)
pragma solidity ^0.4.18;
interface token {
function transfer(address receiver, uint amount) external;
}
contract Crowdsale {
address public beneficiary;
uint public fundingGoal;
uint public amountRaised;
uint public price;
token public tokenReward;
mapping(address => uint256) public balanceOf;
bool fundingGoalReached = false;
event GoalReached(address recipient, uint totalAmountRaised);
event FundTransfer(address backer, uint amount, bool isContribution);
/**
* Constructor function
*
* Setup the owner
*/
constructor(
address ifSuccessfulSendTo,
uint fundingGoalInEthers,
uint milietherCostOfEachToken,
address addressOfTokenUsedAsReward
) public {
beneficiary = ifSuccessfulSendTo;
fundingGoal = fundingGoalInEthers * 1 ether;
price = milietherCostOfEachToken * 0.0001 ether;
tokenReward = token(addressOfTokenUsedAsReward);
}
/**
* Fallback function
*
* The function without name is the default function that is called whenever anyone sends funds to a contract
*/
function () payable public {
uint amount = msg.value;
balanceOf[msg.sender] += amount;
amountRaised += amount;
tokenReward.transfer(msg.sender, amount * 10**18 / price);
emit FundTransfer(msg.sender, amount, true);
}
/**
* Check if goal was reached
*
* Checks if the goal or time limit has been reached and ends the campaign
*/
function checkGoalReached() public {
if (amountRaised >= fundingGoal){
fundingGoalReached = true;
emit GoalReached(beneficiary, amountRaised);
}
}
/**
* Withdraw the funds
*
* Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
* sends the entire amount to the beneficiary. If goal was not reached, each contributor can withdraw
* the amount they contributed.
*/
function safeWithdrawal() public {
if (!fundingGoalReached) {
uint amount = balanceOf[msg.sender];
balanceOf[msg.sender] = 0;
if (amount > 0) {
if (msg.sender.send(amount)) {
emit FundTransfer(msg.sender, amount, false);
} else {
balanceOf[msg.sender] = amount;
}
}
}
if (fundingGoalReached && beneficiary == msg.sender) {
if (beneficiary.send(amountRaised)) {
emit FundTransfer(beneficiary, amountRaised, false);
} else {
//If we fail to send the funds to beneficiary, unlock funders balance
fundingGoalReached = false;
}
}
}发布于 2018-08-26 20:05:00
要自动将资金发送到受益人地址,您可以使用transfer
function () payable public {
uint amount = msg.value;
balanceOf[msg.sender] += amount;
amountRaised += amount;
tokenReward.transfer(msg.sender, amount * 10**18 / price);
emit FundTransfer(msg.sender, amount, true);
// Forward funds immediately to beneficiary
beneficiary.transfer(amount);
}https://ethereum.stackexchange.com/questions/57476
复制相似问题