准备
参考: http://www.tryblockchain.org/Solidity-Struct-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.html
pragma solidity ^0.4.24;
/*
** WARNING: THIS PRODUCT IS HIGHLY ADDICTIVE. IF YOU HAVE AN ADDICTIVE NATURE. DO NOT PLAY.
*
*/
contract CrowdFunding{
//投资者结构体
struct Funder{
address _addr; //投资人钱包地址
uint _amount; // 投资金额
}
struct Product{
address _addr; //众筹金额地址
string _name; //众筹产品名
uint _goal; //希望众筹金额
uint _amount; //实际众筹金额
uint _funderNum; //投资者统计
//映射投资者和产品
mapping(uint => Funder) funders;
}
//总产品数量
uint procount;
//记录平台产品信息
mapping (uint => Product) public Products;
/*
** WARNING: THIS PRODUCT IS HIGHLY ADDICTIVE. IF YOU HAVE AN ADDICTIVE NATURE. DO NOT PLAY.
* function 添加一个众筹产品信息
* address 众筹金额地址
* name 众筹产品信息
* gogl 希望众筹产品金额
*/
function createProduct(address addr, string name, uint goal){
Products[procount++] = Product({_addr:addr, _name:name, _goal:goal, _amount:0, _funderNum:0});
}
/*
** WARNING: THIS PRODUCT IS HIGHLY ADDICTIVE. IF YOU HAVE AN ADDICTIVE NATURE. DO NOT PLAY.
* function 投资人投资产品
* index 投资第几个产品
* amount 投资金额
*/
function funderToProduct(uint index, uint amount) payable{
Product product = Products[index];
product.funders[product._funderNum] = Funder({_addr : msg.sender, _amount : amount});
product._amount += product.funders[product._funderNum]._amount;
product._funderNum++;
}
}
网友评论