编写众筹合约涉及使用 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(); } }
猜你喜欢
- 16天前(a级景区评定机构)全国A级旅游景区创建与提升培训班在敦煌市举办
- 16天前(鄂尔多斯航空公司客服电话)架起“北方之路” ,中国联合航空带您飞向鄂尔多斯重回1倍速
- 16天前(上海文旅产业发展高峰论坛)《上海打造文旅元宇宙新赛道行动方案》发布
- 16天前(杭州西湖区万怡酒店正式开业了吗)杭州西湖区万怡酒店正式开业
- 16天前(天气预报 华为)2025HDC华为天气上新系统级天气智能体,引领更智能的气象服务
- 16天前(“清透会呼吸”轻松拿捏春日出游氛围感)“清透会呼吸”轻松拿捏春日出游氛围感
- 16天前(纳米比亚旅游报价)纳米比亚旅游局2024年中国推介会圆满落幕
- 16天前(希尔顿集团2021年筹建的酒店)希尔顿集团两大重点项目亮相第四届上海旅游投资促进大会
- 16天前(内蒙古交通旅游图)内蒙古着力提升交通与旅游服务水平
- 16天前(内蒙古冬季旅游攻略)内蒙古冬日奇遇:携程租车带你策马踏雪
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章