您当前的位置: 首页 > 

【03】

暂无认证

  • 0浏览

    0关注

    196博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

solidity实战-众筹项目

【03】 发布时间:2022-03-09 09:14:21 ,浏览量:0

功能介绍 玩法

用户发起众筹->其他用户参与众筹

众筹可以提前关闭结束

众筹额度满了将不能继续参与

盈利

每个众筹成功的项目在提取时、合约将产生5%的扣点,扣点收益将全部交给合约管理员,管理员需要手动提取

实现代码
pragma solidity ^0.6.0;

contract Financing {
    // 出资人
    struct Donor {
        uint amount;//融资金额
        address addr;//地址
        uint time;//融资时间
    }
    // 融资发起人
    struct Sponsor {
        uint amount; // 当前已融资金额
        uint max_amount;//需要融资金额
        string name;//名称
        address addr;//地址
        uint start_time;//发起时间
        uint end_time;//结束时间
        uint financing_num;//融资地址数
        mapping(uint => Donor) map;//融资人
    }
    // 发起人自增id
    uint public sponsorId;
    // 发起人信息
    mapping(uint => Sponsor) public sponsorMap;
    // 已完成的融资
    mapping(uint => bool) public finishMap;

    //管理员
    address admin;
    // 扣点 5%
    uint deduction = 5;
    // 利润
    uint public profit = 0;

    constructor(address admin_) public {
        admin = admin_;
    }
    // 提取利润
    function extractProfit_() public {
        admin.call{value : profit}('');
    }

    // 发起融资项目
    function createFinancing(uint amount_, string memory name_, uint end) public {
        // 最少融资时常1天
        if (end - now  0, 'donate amount is 0');
        // 找出融资的发起人
        Sponsor storage sponsor_ = sponsorMap[sponsorId_];
        // 判断有没有发起时间,没有的话表示没有这个项目
        require(sponsor_.addr != address(0), 'notfund fundraising');
        sponsor_.financing_num ++;
        sponsor_.amount += msg.value;
        // 设置当前的融资信息,key为当前融资的个数
        sponsor_.map[sponsor_.financing_num] = Donor(msg.value, msg.sender, now);
    }

    // 定义事件
    event TakeOut(string name, address addr, uint start_time, uint end_time, uint financing_num, uint amount);
    // 结束并取出
    function endAndTakeOut(uint sponsorId_) public {
        require(!finishMap[sponsorId_], 'the financing is end');
        Sponsor storage sponsor_ = sponsorMap[sponsorId_];
        // 判断当前id是不是你的
        require(msg.sender == sponsor_.addr, 'Illegal withdrawal');
        // 结束当前项目
        finishMap[sponsorId_] = true;
        // 取出前扣点扣点
        uint amount_ = sponsor_.amount * (deduction / 100);
        // 已融资金额大于0,转给发起融资地址
        if (sponsor_.amount > 0) {
            // 收取扣点
            profit = profit + sponsor_.amount - amount_;
            // 发给募资发起人
            sponsor_.addr.call{value : amount_}('');
        }
        // 执行成功发事件
        emit TakeOut(sponsor_.name, sponsor_.addr, sponsor_.start_time, sponsor_.end_time, sponsor_.financing_num, amount_);
    }

    // 判断是否结束
    function isEnd(uint sponsorId_) public view returns(bool){
        Sponsor storage sponsor_ = sponsorMap[sponsorId_];
        return finishMap[sponsorId_] || sponsor_.end_time             
关注
打赏
1657344724
查看更多评论
0.0361s