首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能多次退出Crowdsale

不能多次退出Crowdsale
EN

Ethereum用户
提问于 2018-08-26 12:23:54
回答 1查看 103关注 0票数 1

我使用的是众包合同(如下所示),在Ropsten网络上,我刚刚设定了创建目标:1 ETH,将其发送到众包地址,然后我可以撤回到受益人地址,但当我第二次发送ETH时,我不能再撤回它了。

我发现自己需要一个Crowdsale代码,可以随时撤回创建,也需要一种自动将ETH从众包发送到ETH地址的方法,就像有人向众包发送1条ETH一样:

  • 那个人收到代币
  • Crowdsale将该ETH发送到特定的ETH地址
  • 该地址的所有者可以随时/多次提取该地址。

这是我正在尝试使用的代码,但只有第一次提取有效(第二次退出)

代码语言:javascript
复制
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;
            }
        }
    }
EN

回答 1

Ethereum用户

回答已采纳

发布于 2018-08-26 20:05:00

要自动将资金发送到受益人地址,您可以使用transfer

代码语言:javascript
复制
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);
}
票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/57476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档