基于以太坊的众筹系统设计与实现(Solidity智能合约 + Web3.js + Truffle 实战)
传统众筹平台(如京东众筹、Kickstarter)依赖于中心化机构运营,普遍存在以下痛点:
- 信任问题:资金流向不透明,项目方可能挪用资金;
- 高额手续费:平台通常抽取 5%~10% 的服务费;
- 审核门槛高:中小项目难以通过平台审核;
- 数据可篡改:筹款进度、支持者记录均存于中心化数据库。
区块链技术的去中心化、不可篡改、可编程特性,为解决上述问题提供了新思路,本文将带你从零实现一个基于以太坊的去中心化众筹系统,技术栈包括 Solidity、Truffle、Ganache、Web3.js 和 MetaMask。
技术选型
| 模块 | 技术 | 说明 |
|---|---|---|
| 智能合约 | Solidity ^0.8.0 | 以太坊官方合约语言 |
| 开发框架 | Truffle | 合约编译、部署、测试一体化 |
| 本地测试链 | Ganache | 提供本地以太坊模拟环境 |
| 前端交互 | Web3.js / Ethers.js | DApp 与合约交互 |
| 钱包 | MetaMask | 用户身份与交易签名 |
| 前端框架 | Vue3 + Element Plus | 界面展示 |
系统架构设计
系统整体架构分为三层:
┌─────────────────────────────────────┐
│ 前端展示层(Vue3 + Web3.js) │
│ 项目列表 / 发起众筹 / 参与投资 / 提款 │
├─────────────────────────────────────┤
│ 区块链层(以太坊智能合约) │
│ 众筹合约 CampaignFactory / Campaign │
├─────────────────────────────────────┤
│ 基础设施层 │
│ Ganache(测试) / Sepolia(测试网) │
└─────────────────────────────────────┘
核心业务流程:
- 项目发起人调用工厂合约创建众筹项目,设定目标金额与截止时间;
- 投资人通过
contribute()向合约地址转账,资金锁定在合约内; - 到期后若达到目标,发起人可提取资金;若未达标,投资人可退款。
智能合约设计与实现
1 合约代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Campaign {
address public owner; // 发起人
string public title; // 项目标题
string public description; // 项目描述
uint256 public goal; // 筹款目标(单位:wei)
uint256 public deadline; // 截止时间戳
uint256 public raisedAmount; // 已筹金额
bool public withdrawn; // 是否已提款
mapping(address => uint256) public contributions; // 投资记录
address[] public contributors;
event Contribute(address indexed contributor, uint256 amount);
event Refund(address indexed contributor, uint256 amount);
event Withdraw(address indexed owner, uint256 amount);
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this");
_;
}
constructor(
address _owner,
string memory _title,
string memory _description,
uint256 _goal,
uint256 _durationInDays
) {
owner = _owner;
title = _title;
description = _description;
goal = _goal;
deadline = block.timestamp + _durationInDays * 1 days;
}
// 参与投资
function contribute() external payable {
require(block.timestamp < deadline, "Campaign expired");
require(msg.value > 0, "Amount must be greater than 0");
if (contributions[msg.sender] == 0) {
contributors.push(msg.sender);
}
contributions[msg.sender] += msg.value;
raisedAmount += msg.value;
emit Contribute(msg.sender, msg.value);
}
// 是否达到目标
function goalReached() public view returns (bool) {
return raisedAmount >= goal;
}
// 发起人提款
function withdraw() external onlyOwner {
require(block.timestamp >= deadline, "Campaign still active");
require(goalReached(), "Goal not reached");
require(!withdrawn, "Already withdrawn");
withdrawn = true;
uint256 amount = address(this).balance;
payable(owner).transfer(amount);
emit Withdraw(owner, amount);
}
// 未达标退款
function refund() external {
require(block.timestamp >= deadline, "Campaign still active");
require(!goalReached(), "Goal reached, no refund");
uint256 amount = contributions[msg.sender];
require(amount > 0, "No contribution to refund");
contributions[msg.sender] = 0;
payable(msg.sender).transfer(amount);
emit Refund(msg.sender, amount);
}
function getContributorsCount() external view returns (uint256) {
return contributors.length;
}
}
2 工厂合约
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./Campaign.sol";
contract CampaignFactory {
address[] public campaigns;
event CampaignCreated(address indexed campaignAddress, address indexed owner);
function createCampaign(
string memory _title,
string memory _description,
uint256 _goal,
uint256 _durationInDays
) external {
Campaign newCampaign = new Campaign(
msg.sender,
_title,
_description,
_goal,
_durationInDays
);
campaigns.push(address(newCampaign));
emit CampaignCreated(address(newCampaign), msg.sender);
}
function getCampaigns() external view returns (address[] memory) {
return campaigns;
}
}
3 设计要点说明
- 资金托管:投资人资金直接转入合约地址,任何人(包括发起人)在众筹结束前都无法动用;
- 自动退款机制:未达标时投资人自行调用
refund()取回资金,无需信任第三方; - 事件机制:所有关键操作均触发 Event,便于前端监听并实时刷新页面;
- 权限控制:
onlyOwner修饰器保证只有发起人能提款。
合约部署
1 编写迁移脚本(migrations/2_deploy.js)
const CampaignFactory = artifacts.require
The End
发布于:2026-10-31,除非注明,否则均为原创文章,转载请注明出处。

