如何写个众筹合约

2024-03-15 1464阅读

温馨提示:这篇文章已超过372天没有更新,请注意相关的内容是否还可用!

编写众筹合约涉及使用 Solidity 语言来定义智能合约。以下是一个简单的众筹合约示例,基于以太坊的 ERC-20 代币标准。请注意,这只是一个基础示例,实际应用中可能需要更多的安全性和功能。

如何写个众筹合约
(图片来源网络,侵删)

```solidity

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Crowdfunding {

    address public owner;

    IERC20 public token; // 使用的代币合约地址

    uint256 public goal; // 众筹目标

    uint256 public deadline; // 截止日期

    uint256 public raisedAmount; // 已筹集金额

    mapping(address => uint256) public contributions; // 参与者的贡献记录

    bool public fundingGoalReached = false; // 众筹目标是否达成

    bool public crowdsaleClosed = false; // 众筹是否关闭

    event Contribution(address indexed contributor, uint256 amount);

    event FundingGoalReached(uint256 amountRaised);

    event CrowdsaleClosed();

    modifier onlyOwner() {

        require(msg.sender == owner, "Only the owner can call this function");

        _;

    }

    modifier notClosed() {

        require(!crowdsaleClosed, "Crowdsale is closed");

        _;

    }

    modifier afterDeadline() {

        require(block.timestamp >= deadline, "Crowdsale deadline has not passed");

        _;

    }

    constructor(

        address _token,

        uint256 _goal,

        uint256 _durationInMinutes

    ) {

        owner = msg.sender;

        token = IERC20(_token);

        goal = _goal;

        deadline = block.timestamp + _durationInMinutes * 1 minutes;

    }

    function contribute(uint256 _amount) external notClosed {

        require(block.timestamp

        require(raisedAmount + _amount = goal) {

            fundingGoalReached = true;

            emit FundingGoalReached(raisedAmount);

        }

    }

    function withdraw() external afterDeadline {

        require(fundingGoalReached, "Funding goal not reached");

        uint256 amount = contributions[msg.sender];

        contributions[msg.sender] = 0;

        token.transfer(msg.sender, amount);

    }

    function closeCrowdsale() external onlyOwner {

        require(!crowdsaleClosed, "Crowdsale already closed");

        crowdsaleClosed = true;

        emit CrowdsaleClosed();

    }

}

```

上述合约使用了 OpenZeppelin 的 ERC20 合约库,确保了在代币交互方面的安全性。在实际使用中,你需要引入相关的库文件,可以通过 [OpenZeppelin GitHub](https://github.com/OpenZeppelin/openzeppelin-contracts) 获取。

此众筹合约支持以下功能:

1. 设置众筹目标、截止日期和代币。

2. 参与者可以通过 `contribute` 函数贡献代币。

3. 在达到众筹目标后,参与者可以通过 `withdraw` 函数提取代币。

4. 合约拥有者可以通过 `closeCrowdsale` 函数关闭众筹。

请确保在实际使用前仔细测试和审查合约,以确保其符合项目需求并且安全可靠。

具体可以参照下面的合约适当修改,仅供学习参考。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Crowdfunding {
    address public owner;
    IERC20 public token; // 使用的代币合约地址
    uint256 public goal; // 众筹目标
    uint256 public deadline; // 截止日期
    uint256 public raisedAmount; // 已筹集金额
    mapping(address => uint256) public contributions; // 参与者的贡献记录
    bool public fundingGoalReached = false; // 众筹目标是否达成
    bool public crowdsaleClosed = false; // 众筹是否关闭
    event Contribution(address indexed contributor, uint256 amount);
    event FundingGoalReached(uint256 amountRaised);
    event CrowdsaleClosed();
    modifier onlyOwner() {
        require(msg.sender == owner, "Only the owner can call this function");
        _;
    }
    modifier notClosed() {
        require(!crowdsaleClosed, "Crowdsale is closed");
        _;
    }
    modifier afterDeadline() {
        require(block.timestamp >= deadline, "Crowdsale deadline has not passed");
        _;
    }
    constructor(
        address _token,
        uint256 _goal,
        uint256 _durationInMinutes
    ) {
        owner = msg.sender;
        token = IERC20(_token);
        goal = _goal;
        deadline = block.timestamp + _durationInMinutes * 1 minutes;
    }
    function contribute(uint256 _amount) external notClosed {
        require(block.timestamp  
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]