当前位置:   article > 正文

如何写个众筹合约

如何写个众筹合约

编写众筹合约涉及使用 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` 函数关闭众筹。

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

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

  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
  4. contract Crowdfunding {
  5. address public owner;
  6. IERC20 public token; // 使用的代币合约地址
  7. uint256 public goal; // 众筹目标
  8. uint256 public deadline; // 截止日期
  9. uint256 public raisedAmount; // 已筹集金额
  10. mapping(address => uint256) public contributions; // 参与者的贡献记录
  11. bool public fundingGoalReached = false; // 众筹目标是否达成
  12. bool public crowdsaleClosed = false; // 众筹是否关闭
  13. event Contribution(address indexed contributor, uint256 amount);
  14. event FundingGoalReached(uint256 amountRaised);
  15. event CrowdsaleClosed();
  16. modifier onlyOwner() {
  17. require(msg.sender == owner, "Only the owner can call this function");
  18. _;
  19. }
  20. modifier notClosed() {
  21. require(!crowdsaleClosed, "Crowdsale is closed");
  22. _;
  23. }
  24. modifier afterDeadline() {
  25. require(block.timestamp >= deadline, "Crowdsale deadline has not passed");
  26. _;
  27. }
  28. constructor(
  29. address _token,
  30. uint256 _goal,
  31. uint256 _durationInMinutes
  32. ) {
  33. owner = msg.sender;
  34. token = IERC20(_token);
  35. goal = _goal;
  36. deadline = block.timestamp + _durationInMinutes * 1 minutes;
  37. }
  38. function contribute(uint256 _amount) external notClosed {
  39. require(block.timestamp < deadline, "Crowdsale has ended");
  40. require(raisedAmount + _amount <= goal, "Contribution exceeds the goal");
  41. token.transferFrom(msg.sender, address(this), _amount);
  42. contributions[msg.sender] += _amount;
  43. raisedAmount += _amount;
  44. emit Contribution(msg.sender, _amount);
  45. if (raisedAmount >= goal) {
  46. fundingGoalReached = true;
  47. emit FundingGoalReached(raisedAmount);
  48. }
  49. }
  50. function withdraw() external afterDeadline {
  51. require(fundingGoalReached, "Funding goal not reached");
  52. uint256 amount = contributions[msg.sender];
  53. contributions[msg.sender] = 0;
  54. token.transfer(msg.sender, amount);
  55. }
  56. function closeCrowdsale() external onlyOwner {
  57. require(!crowdsaleClosed, "Crowdsale already closed");
  58. crowdsaleClosed = true;
  59. emit CrowdsaleClosed();
  60. }
  61. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/177498
推荐阅读
相关标签
  

闽ICP备14008679号