1 Star 0 Fork 10

沧海辽望/crowFunding-eth

forked from duke.du/crowFunding-eth 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
basicFunding.sol 5.14 KB
一键复制 编辑 原始数据 按行查看 历史
pragma solidity ^0.4.24;
import './InvestorToFunding.sol';
contract CrowFunding {
address public creator; // 发起人
string public projectName; // 项目名称
uint public supportBalance; // 参与众筹金额
uint public targetBalance; // 众筹目标金额
uint public endTime; // 众筹截止日期
address[] public investors; //参与众筹的人,即投资人
mapping(address => bool) public investorExistMap; //标记一个人是否参与了当前众筹
InvestorToFunding i2f;
constructor(string _projectName, uint _supportBalance, uint _targetBalance, uint _durationInSeconds, address _creator, InvestorToFunding _i2f) public {
creator = _creator;
i2f = _i2f;
projectName = _projectName;
supportBalance = _supportBalance;
targetBalance = _targetBalance;
//传递进来剩余的秒数,比如若众筹30天,则传入:30天 * 24小时 * 60分 * 60秒 = 2592000
endTime = now + _durationInSeconds; //2592000
}
//参与众筹,投资
function invest() public payable {
require(msg.value == supportBalance); // 支持固定金额
investors.push(msg.sender); // 添加到众筹人数组中
investorExistMap[msg.sender] = true; // 标记当前账户为参与人
i2f.joinFunding(msg.sender, this);
}
//众筹失败,退款
function drawBack() public onlyCreator {
for (uint i = 0 ; i < investors.length; i++) {
investors[i].transfer(supportBalance);
}
//这个合约基本就会被废弃掉,所以就不去处理investors、investorExistMap了
}
//查看合约当前余额
function getCurrentBalance() public view returns(uint) {
return address(this).balance;
}
//返回所有的投资人
function getInvestors() public view returns(address[]) {
return investors;
}
enum RequestStatus {Voting,Approved,Completed}
struct Request {
string purpose; // 买什么?
uint cost; // 需要多少钱?
address shopAddress; // 向谁购买?
uint voteCount; // 多少人赞成了,超半数则批准支出
mapping(address => bool) investorVotedMap; //赞成人的标记集合,防止一人重复投票多次
RequestStatus status; //这个申请的当前状态:投票中?已批准? 已完成?
}
Request[] public requests; //请求可能有多个,所以定义一个数组。
function createRequest(string _purpose, uint _cost, address _shopAddress) public onlyCreator {
Request memory request = Request({
purpose : _purpose,
cost : _cost,
shopAddress : _shopAddress,
voteCount : 0,
status : RequestStatus.Voting
});
requests.push(request); //将新的请求添加至数组中
}
function approveRequest(uint index) public {
//首先要确保是参与众筹的人,否则无权投票
//require(investorExistMap[msg.sender] == true); //这种写法比价low
require(investorExistMap[msg.sender]);
//根据索引找到特定的请求
Request storage request = requests[index];
//确保没有投过票,人手一票
//require(request.investorVotedMap[msg.sender] == false);
require(!request.investorVotedMap[msg.sender]);
//如果已经完成,或者已经获得批准了,就不用投票了,当前投票不会影响决策。
require(request.status == RequestStatus.Voting);
//支持票数加1
request.voteCount++;
//标记为已投票
request.investorVotedMap[msg.sender] = true;
}
function finalizeRequest(uint index) public onlyCreator {
Request storage request = requests[index];
//合约金额充足才可以执行
require(address(this).balance >= request.cost);
//赞成人数过半
require(request.voteCount * 2 > investors.length);
//转账
request.shopAddress.transfer(request.cost);
//更新请求状态为已完成
request.status = RequestStatus.Completed;
}
modifier onlyCreator() {
require(msg.sender == creator);
_;
}
//返回投资人数量
function getInvestorsCount() public view returns (uint) {
return investors.length;
}
//返回众筹剩余时间
function getRemainTime() public view returns (uint) {
return (endTime - now) / 60 / 60 / 24;
}
//返回花费申请数量
function getRequestsCount() public view returns (uint) {
return requests.length;
}
//返回某一个花费申请的具体信息
function getRequestDetailByIndex(uint index) public view returns (string, uint, address, bool, uint, uint){
//确保访问不越界
require(index < requests.length);
Request storage req = requests[index];
bool isVoted = req.investorVotedMap[msg.sender];
return (req.purpose, req.cost, req.shopAddress, isVoted, req.voteCount, uint(req.status));
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/lz1289/crowFunding-eth.git
git@gitee.com:lz1289/crowFunding-eth.git
lz1289
crowFunding-eth
crowFunding-eth
master

搜索帮助