区块链应用--Web3众筹项目--以太坊
01/23
本文最后更新于
2023年01月23日,已超过
801天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
今天的这个Web3项目是一个部署在以太坊测试网络Goerli上的一个众筹项目,假如kickstar变成web3版本,也许就是这个样子
视频(Youtube,Bilibili)
实现功能
- 发起众筹
- 及时收款
- 查询当前账户下的众筹项目
- 手机观看优化模式
- 手机和PC端均可流畅完成项目
- 前端云部署
智能合约代码
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract CrowdFunding {
struct Campaign {
address owner;
string title;
string description;
uint256 target;
uint256 deadline;
uint256 amountCollected;
string image;
address[] donators;
uint256[] donations;
}
mapping(uint256 => Campaign) public campaigns;
uint256 public numberOfCampaigns = 0;
function createCampaign(address _owner, string memory _title, string memory _description, uint256 _target, uint256 _deadline, string memory _image) public returns (uint256) {
Campaign storage campaign = campaigns[numberOfCampaigns];
require(campaign.deadline < block.timestamp, "The deadline should be a date in the future.");
campaign.owner = _owner;
campaign.title = _title;
campaign.description = _description;
campaign.target = _target;
campaign.deadline = _deadline;
campaign.amountCollected = 0;
campaign.image = _image;
numberOfCampaigns++;
return numberOfCampaigns - 1;
}
function donateToCampaign(uint256 _id) public payable {
uint256 amount = msg.value;
Campaign storage campaign = campaigns[_id];
campaign.donators.push(msg.sender);
campaign.donations.push(amount);
(bool sent,) = payable(campaign.owner).call{value: amount}("");
if(sent) {
campaign.amountCollected = campaign.amountCollected + amount;
}
}
function getDonators(uint256 _id) view public returns (address[] memory, uint256[] memory) {
return (campaigns[_id].donators, campaigns[_id].donations);
}
function getCampaigns() public view returns (Campaign[] memory) {
Campaign[] memory allCampaigns = new Campaign[](numberOfCampaigns);
for(uint i = 0; i < numberOfCampaigns; i++) {
Campaign storage item = campaigns[i];
allCampaigns[i] = item;
}
return allCampaigns;
}
}
感谢Javascript Master的分享,开源万岁,希望大家能帮助到大家