我试图在以太坊测试网上设置一个基本的众筹,我使用的可靠性代码是
中的基本示例。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;
}
}
}
}
编辑:我忘了提及导致这一点的步骤,即令牌创建/股东协会使用指南中提供的代码。
答案 0 :(得分: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);
}
}
对于您的第二个问题,您是否确实已确认创建的令牌是否已实际发送?你发给人群销售合同有多少以太?您的令牌似乎使用两位小数,如果您定义了每个令牌的以太网成本"在示例中,您不应该向群众销售合同发送少于0.05的以太币。
希望它有所帮助!