智能合约众筹项目(solidity)

pragma solidity ^0.4.0;

//创建crowdfunding智能合约
contract crowdfunding{

    //捐赠者结构
    struct funder {
        address funderAddress; //捐赠者地址
   }
    //需求者结构
    struct needer {
        address neederAddress; //需求者地址
        uint goal;//众筹金额
        uint amount;//已筹集金额
        uint funderAcccount; //捐赠者数量
        mapping(uint => funder) funderMap;//捐赠者id字典
    }
    
    uint neederAmount;//需求者数量
    mapping(uint => needer) neederMap; //需求者id字典
    
    //new需求者
    function newNeeder(address addr,uint neederGoal){
        neederAmount++;
        neederMap[neederAmount] = needer(addr,neederGoal,0,0);
    }
    
    //捐赠者捐赠
    function donation(address funderAddr, uint neederId) payable{ //payable修饰符
        needer storage _needer = neederMap[neederId];
        _needer.funderAcccount++;
        _needer.amount += msg.value;//捐赠者金额++
        _needer.funderMap[_needer.funderAcccount] = funder(funderAddr,msg.value);
    }
    //完成目标可进行转账
    function Transfer( uint neederId){
        needer storage _needer = neederMap[neederId];
        if(_needer.amount >= _needer.goal){
            _needer.neederAddress.transfer(_needer.amount);//给需求者转账,使用transferAPI
        }
    }
    //打印当前需求者信息
    function print(uint neederId) view returns(uint,address){
        return (neederMap[neederId].amount,neederMap[neederId].neederAddress);
    }
}

编译后结果参照下图
智能合约众筹项目(solidity)_第1张图片

你可能感兴趣的:(智能合约,solidity)