首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >大众销售后如何将ETH从合同转到外部地址?

大众销售后如何将ETH从合同转到外部地址?
EN

Ethereum用户
提问于 2017-08-13 11:29:01
回答 2查看 2.2K关注 0票数 0

我正在努力学习创建智能契约。在实践中,我按照ethereum.org上的示例创建了令牌和众包契约,并进行了部署。令牌分配按预期进行,但当合同期限完成(供资目标也达到)时,合同账户的资金不会转入受益人账户。

以下是代码:

代码语言:javascript
复制
pragma solidity ^0.4.11;
contract token { function transfer(address, uint){  } }

contract Crowdsale {
    address public beneficiary;
    uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price;
    token public tokenReward;
    mapping(address => uint256) public balanceOf;
    bool fundingGoalReached = false;
    event GoalReached(address beneficiary, uint amountRaised);
    event FundTransfer(address backer, uint amount, bool isContribution);
    bool crowdsaleClosed = false;

    /* data structure to hold information about campaign contributors */

    /*  at initialization, setup the owner */
    function Crowdsale(
        address ifSuccessfulSendTo,
        uint fundingGoalInEthers,
        uint durationInMinutes,
        uint etherCostOfEachToken,
        token addressOfTokenUsedAsReward
    ) {
        beneficiary = ifSuccessfulSendTo;
        fundingGoal = fundingGoalInEthers * 1 ether;
        deadline = now + durationInMinutes * 1 minutes;
        price = etherCostOfEachToken * 1 ether;
        tokenReward = token(addressOfTokenUsedAsReward);
    }

    /* The function without name is the default function that is called whenever anyone sends funds to a contract */
    function () payable {
        if (crowdsaleClosed) revert();
        uint amount = msg.value;
        balanceOf[msg.sender] = amount;
        amountRaised += amount;
        tokenReward.transfer(msg.sender, amount / price);
        FundTransfer(msg.sender, amount, true);
    }

    modifier afterDeadline() { if (now >= deadline) _; }

    /* checks if the goal or time limit has been reached and ends the campaign */
    function checkGoalReached() afterDeadline {
        if (amountRaised >= fundingGoal){
            fundingGoalReached = true;
            GoalReached(beneficiary, amountRaised);
        }
        crowdsaleClosed = true;
    }


    function safeWithdrawal() afterDeadline {
        if (!fundingGoalReached) {
            uint amount = balanceOf[msg.sender];
            balanceOf[msg.sender] = 0;
            if (amount > 0) {
                if (msg.sender.send(amount)) {
                    FundTransfer(msg.sender, amount, false);
                } else {
                    balanceOf[msg.sender] = amount;
                }
            }
        }

        if (fundingGoalReached && beneficiary == msg.sender) {
            if (beneficiary.send(amountRaised)) {
                FundTransfer(beneficiary, amountRaised, false);
            } else {
                //If we fail to send the funds to beneficiary, unlock funders balance
                fundingGoalReached = false;
            }
        }
    }
}

我是不是遗漏了什么?

EN

回答 2

Ethereum用户

回答已采纳

发布于 2017-10-04 01:51:48

你可能提供的信息太少了,根本帮不上忙。

你能张贴合同的地址和用于创建它的参数吗?

代码语言:javascript
复制
address ifSuccessfulSendTo       =
uint fundingGoalInEthers         =
uint durationInMinutes           =
uint etherCostOfEachToken        =
token addressOfTokenUsedAsReward =

无论如何,有几件事你可以尝试:

  1. 确保您从创建契约时使用的相同地址调用safeWithdrawal()。如果该事务失败,那么fundingGoalReached将返回到false,这意味着您必须在再次调用safeWithdrawal()之前调用checkGoalReached()将其设置为true
  2. 当调用合同的方法时,尝试发送大量的气体(如200,000 )以确保安全(未使用的气体将始终被送回给您)。

balanceOfmsg.sender =量;

而且,这似乎是有问题的,因为它每次都设置存款人的余额值,而不增加它,例如。多笔存款的时候。

amountRaised +=量;

最后,这通常是不建议的,因为它会引发溢出问题。也许最好用齐柏林飞艇的SafeMath.sol之类的东西-坚固。

票数 2
EN

Ethereum用户

发布于 2017-08-13 22:43:59

在部署合同时,您是否从指定为受益人的地址执行了合同上的safeWithdrawal()函数?

合同帐户的资金不会自动转入受益人帐户。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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