编写众筹合约涉及使用 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` 函数关闭众筹。
请确保在实际使用前仔细测试和审查合约,以确保其符合项目需求并且安全可靠。文章来源:https://www.toymoban.com/news/detail-808303.html
具体可以参照下面的合约适当修改,仅供学习参考。文章来源地址https://www.toymoban.com/news/detail-808303.html
// 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();
}
}
到了这里,关于如何写个众筹合约的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!