上海古都建筑设计集团,上海办公室装修设计公司,上海装修公司高质量的内容分享社区,上海装修公司我们不是内容生产者,我们只是上海办公室装修设计公司内容的搬运工平台

如何写个众筹合约

guduadmin441月前

编写众筹合约涉及使用 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 < deadline, "Crowdsale has ended");

        require(raisedAmount + _amount <= goal, "Contribution exceeds the goal");

        token.transferFrom(msg.sender, address(this), _amount);

        contributions[msg.sender] += _amount;

        raisedAmount += _amount;

        emit Contribution(msg.sender, _amount);

        if (raisedAmount >= 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 < deadline, "Crowdsale has ended");
        require(raisedAmount + _amount <= goal, "Contribution exceeds the goal");
        token.transferFrom(msg.sender, address(this), _amount);
        contributions[msg.sender] += _amount;
        raisedAmount += _amount;
        emit Contribution(msg.sender, _amount);
        if (raisedAmount >= 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();
    }
}

网友评论

搜索
最新文章
热门文章
热门标签
 
 梦见毒蛇咬人  已婚女子梦见被蛇咬  梦见家中亲人