区块链技术

eth智能合约众筹Crowdsale精简版注释解读

2017-09-04  本文已影响72人  剑有偏锋

'''
pragma solidity ^0.4.2;
contract token { function transfer(address receiver, uint 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;//每个地址的众筹数目,map类型
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) throw;//众筹关闭时,抛出异常
    uint amount = msg.value;
    balanceOf[msg.sender] = amount;                 //计入统计,发送者发送的数量
    amountRaised += amount;                         //累计收到的金额
    tokenReward.transfer(msg.sender, amount / price);
    FundTransfer(msg.sender, amount, true);         //发送资金变动事件通知
}

// modifier可以方便的验证输入信息
modifier afterDeadline() { if (now >= deadline) _; }//modifier 修饰符,函数启动前执行。_表示函数体占位符,此处表示afterDeadline修饰的函数本体。

//检查是否众筹成功,达到目标金额
/* 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;                     //众筹标识关闭
}


//安全撤出,       有afterDeadline的话,只有到众筹结束时间才可以执行
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;//记录放入balanceOfmap里面
            }
        }
    }

    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; ////众筹金额未达成
        }
    }
}

}

'''

上一篇下一篇

猜你喜欢

热点阅读