智能合约测试例子

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

相关推荐
轻口味41 分钟前
命名空间与模块化概述
开发语言·前端·javascript
前端小小王1 小时前
React Hooks
前端·javascript·react.js
迷途小码农零零发1 小时前
react中使用ResizeObserver来观察元素的size变化
前端·javascript·react.js
娃哈哈哈哈呀2 小时前
vue中的css深度选择器v-deep 配合!important
前端·css·vue.js
旭东怪2 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
ekskef_sef4 小时前
32岁前端干了8年,是继续做前端开发,还是转其它工作
前端
sunshine6414 小时前
【CSS】实现tag选中对钩样式
前端·css·css3
真滴book理喻5 小时前
Vue(四)
前端·javascript·vue.js
蜜獾云5 小时前
npm淘宝镜像
前端·npm·node.js