溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

以太坊眾籌智能合約怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2021-12-24 17:08:11 來源:億速云 閱讀:280 作者:iii 欄目:互聯(lián)網(wǎng)科技

本篇內(nèi)容主要講解“以太坊眾籌智能合約怎么實(shí)現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“以太坊眾籌智能合約怎么實(shí)現(xiàn)”吧!

實(shí)現(xiàn)一個(gè)好的idea常常需要付出巨大的努力,并且需要大量的資金。我們可以尋求用戶捐贈(zèng),或者尋求投資機(jī)構(gòu)投資,但這往往很難。對(duì)于捐贈(zèng),國內(nèi)的風(fēng)氣不太好,資金去向往往不了了之,捐贈(zèng)者對(duì)于當(dāng)前的捐贈(zèng)形式早已失去了信心。而風(fēng)險(xiǎn)投資,對(duì)于沒有人脈的創(chuàng)業(yè)者來說,非常困難。 區(qū)塊鏈提供了一種眾籌的新形式——眾籌智能合約。募資人通過眾籌合約設(shè)定好眾籌目標(biāo),以及完成時(shí)間,設(shè)定不同眾籌結(jié)果所對(duì)應(yīng)的操作(例如目標(biāo)失敗退回全款、目標(biāo)成功時(shí)受益人獲得加密代幣或ETH)。由于區(qū)塊鏈不可篡改的特性,眾籌合約會(huì)是一個(gè)非常吻合的應(yīng)用場景。

代幣和分布自治組織

這個(gè)例子中我們將通過解決兩個(gè)重要的問題進(jìn)行更好的眾籌:

  • 如何管理資金,保證流動(dòng)性;

  • 籌集資金后如何花錢。

區(qū)塊鏈出現(xiàn)之前的眾籌項(xiàng)目一般缺少流動(dòng)性,投資人一旦錯(cuò)過眾籌截止時(shí)間將無法參與眾籌;一旦參與眾籌,投資人也不能中途退出。智能合約通過發(fā)行代幣的形式來記錄投資額,并提供了類似股票市場的流動(dòng)性。投資人可以選擇交易或者繼續(xù)持有。項(xiàng)目成功后投資者可以使用代幣交換實(shí)物或者產(chǎn)品服務(wù)。項(xiàng)目失敗的話投資者可以按照原先的約定退出,并且繼續(xù)持有代幣以表紀(jì)念。

同樣,當(dāng)前眾籌項(xiàng)目也存在資金去向不明的問題。在這個(gè)項(xiàng)目中,我們使用DAO(分布式自治組織)記錄每一筆資金去向。

合約代碼

先放上代碼,然后再一步步解讀。

pragma solidity ^0.4.16;
interface 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;
    bool fundingGoalReached = false; 
    bool crowdsaleClosed = false; 
    event GoalReached(address recipient, uint totalAmountRaised); 
    event FundTransfer(address backer, uint amount, bool isContribution); 
    /**
     * Constrctor function
     *
     * Setup the owner
     */
    function Crowdsale(
        address ifSuccessfulSendTo, 
        uint fundingGoalInEthers,        
        uint durationInMinutes,        
        uint etherCostOfEachToken,
        address addressOfTokenUsedAsReward    
    ) {
        beneficiary = ifSuccessfulSendTo;
        fundingGoal = fundingGoalInEthers * 1 ether;
        deadline = now + durationInMinutes * 1 minutes;
        price = etherCostOfEachToken * 1 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 {
        require(!crowdsaleClosed); 
        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) _; 
    }
    /**
     * Check if goal was reached
     *
     * 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;
    }
    /**
     * 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() 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;
            }
        }
    }
}

構(gòu)造函數(shù)中

fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;

ether和minutes是以太坊預(yù)留的關(guān)鍵字,1 ether == 1000 finney , 2 days == 48 hours。日期類型的關(guān)鍵字有seconds,minutes,hours, days,weeks,years,以太幣單位預(yù)留的關(guān)鍵字有wei,finney,szabo,ether。1 finney == 1000 szabo,1 szabo == 10^12 wei。now也是以太坊預(yù)留的關(guān)鍵字,代表當(dāng)前時(shí)間。

接下來我們實(shí)例化了一個(gè)合約:

tokenReward = token(addressOfTokenUsedAsReward);
token的定義在代碼開頭:

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

這里我們并未實(shí)現(xiàn)token合約,只是告訴編譯器我們的token是一個(gè)合約,具有一個(gè)transfer()函數(shù),并且在給定的地址上有這個(gè)合約。

接下來我們看看合約如何接收資金,相關(guān)代碼如下:

function () {    
    require(!crowdsaleClosed);
    uint amount = msg.value;
    // ...

這個(gè)函數(shù)很特別,它沒有名字,在solidity中我們稱之為回退函數(shù)(Fallback function),回退函數(shù)沒有參數(shù),也沒有返回值。如果合約接收ether,則必須明確定義回退函數(shù),否則會(huì)觸發(fā)異常,并返回ether。接收ether的函數(shù)必須帶有關(guān)鍵字payable,否則會(huì)報(bào)錯(cuò)。

require語句先判斷眾籌是否結(jié)束,如果眾籌已經(jīng)結(jié)束,錢將退回給主叫方,避免主叫方出現(xiàn)不必要的損失。

部署通過之后可以用自己的測試賬戶向合約地址轉(zhuǎn)賬,這樣就可以參與眾籌了。

眾籌成功后,如果繼續(xù)往合約地址轉(zhuǎn)賬,錢將會(huì)退回你的賬戶。

到此,相信大家對(duì)“以太坊眾籌智能合約怎么實(shí)現(xiàn)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI