【问题标题】:Ethereum, crowdsale returns 0.00 tokens to the wallet以太坊,众筹向钱包返还 0.00 代币
【发布时间】:2017-12-06 21:59:03
【问题描述】:

我正在尝试在以太坊测试网设置一个基本的众筹,而我使用的solidity 代码是在

找到的基本示例

https://ethereum.org/crowdsale#the-code

按照该指南中的说明执行步骤。

首先的问题是,由于第一行,以太坊钱包不接受要编译的代码: contract token { function transfer(address receiver, uint amount){ } }

特别是它的函数返回未使用局部变量的警告并且不会编译。除了在函数中定义空变量之外,还有其他方法吗?

第二个问题是在按照上述修改部署后,它可以工作。但是当它向发送以太币的钱包发送代币时,金额始终锁定在 0.00 个代币。

完整代码:

pragma solidity ^0.4.2;
contract token { function transfer(address receiver, uint amount){ receiver; amount; } }

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 a name is the default function that is called whenever anyone sends funds to a contract */
    function () payable {
        if (crowdsaleClosed) throw;
        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;
            }
        }
    }
}

编辑:我忘了提及导致这一点的步骤,也就是令牌创建/股东协会使用指南中提供的代码。

【问题讨论】:

  • 所有代码 sn-p 都应该缩进,以便堆栈溢出 UI 正确显示

标签: ethereum solidity


【解决方案1】:

我遇到了同样的问题。我通过实际定义 transfer 函数来解决它,因为 ethereum.org 的示例提供了一个空函数。

我替换了这个:

contract token { function transfer(address receiver, uint amount){  } }

通过这个:

contract token {    
    event Transfer(address indexed from, address indexed to, uint256 value);
    function transfer(address _to, uint256 _value) {
        if (_to == 0x0) throw;
        Transfer(msg.sender, _to, _value);
    }

}

对于您的第二个问题,您是否确实确认创建的令牌已实际发送?你向众售合约发送了多少以太币?您的代币似乎使用了两位小数,如果您像示例中那样将“每个代币的以太币成本”定义为 5,那么您不应向众售合约发送少于 0.05 的以太币。

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    相关资源
    最近更新 更多