当前位置:首页 » 《关注互联网》 » 正文

solidity:简单众筹合约及其调用流程_骗招狗子的博客

24 人参与  2022年01月22日 12:00  分类 : 《关注互联网》  评论

点击全文阅读


函数解释:
newCampaign:创建公司受益人以及筹款目标
contribute:进行捐赠,并将捐赠名单计入结构体
CheckGoalReached:若达到众筹目标,清空amout,并将goal转给受益者

调用次序说明:
在这里插入图片描述

合约代码:

pragma solidity ^0.4.11;

contract CrowdFunding{
    
    struct Funder{
        address addr;
        uint amout;
    }
    
    struct Campaign{
        address beneficiary;
        uint fundingGoal;
        uint numFunders;
        uint amount;
        //when initialize the Campaign
        //ignore the mapping
        mapping(uint => Funder) funders;
    }
    
    uint numCampaigns;
    mapping(uint => Campaign) campaigns;
    
    function newCampaign(address beneficiary, uint goal) 
        public returns (uint campaignID){
            campaignID = numCampaigns++;
            campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
    }
    
    function contribute(uint campaignID) public payable{
        Campaign storage c = campaigns[campaignID];
        c.funders[c.numFunders++] = Funder(msg.sender, msg.value);
        c.amount += msg.value;
    }
    
    function checkGoalReached(uint campaignID) public returns (bool reached){
        Campaign storage c = campaigns[campaignID];
        if(c.amount < c.fundingGoal)
            return false;
        uint amount = c.amount;
        c.amount = 0;
        c.beneficiary.transfer(amount);
        return true;
    }
}

总结:
模拟众筹


点击全文阅读


本文链接:http://zhangshiyu.com/post/33726.html

并将  捐赠  目标  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1