智能合约测试例子

javascript 复制代码
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.11 <0.9.0;
contract CrowdFunding {
    // 定义的新类型包含两个属性。
    struct Funder {
        address addr;
        uint amount;
    }

    struct Campaign {
        address payable  beneficiary;
        uint fundingGoal;
        uint numFunders;
        uint amount;
        mapping (uint => Funder) funders;
    }

    uint numCampaigns;
    mapping (uint => Campaign)   campaigns;

    function newCampaign(address beneficiary, uint goal) public  returns (uint ) {
       uint campaignID = numCampaigns++; 

         Campaign storage c = campaigns[campaignID];
       c.beneficiary=payable(beneficiary);
       c.fundingGoal=goal;
       c.amount=0;
       c.numFunders=0;
       return campaignID;
    }

    function contribute(uint campaignID) public payable   {

        
        Campaign storage c = campaigns[campaignID];
       
        c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: 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;
    }
    function info(uint campaignID) public view returns(uint){
        Campaign storage c = campaigns[campaignID];
         uint amount = c.amount;
         return amount;
    }
}

Remix - Ethereum IDE

相关推荐
baiduopenmap12 分钟前
百度世界2024精选公开课:基于地图智能体的导航出行AI应用创新实践
前端·人工智能·百度地图
loooseFish20 分钟前
小程序webview我爱死你了 小程序webview和H5通讯
前端
请叫我欧皇i32 分钟前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript
533_34 分钟前
[vue] 深拷贝 lodash cloneDeep
前端·javascript·vue.js
guokanglun40 分钟前
空间数据存储格式GeoJSON
前端
zhang-zan1 小时前
nodejs操作selenium-webdriver
前端·javascript·selenium
猫爪笔记1 小时前
前端:HTML (学习笔记)【2】
前端·笔记·学习·html
brief of gali1 小时前
记录一个奇怪的前端布局现象
前端
Json_181790144802 小时前
电商拍立淘按图搜索API接口系列,文档说明参考
前端·数据库
风尚云网3 小时前
风尚云网前端学习:一个简易前端新手友好的HTML5页面布局与样式设计
前端·css·学习·html·html5·风尚云网