dapp众筹合约模式系统开发
智能合约开发【I8I-系统259I-开发3365】入门步:正确认识并理解合约代码的含义
智能合约开发步,在于要认识与理解合约代码的含义。接下来让我们先看一下基本的例子。现在就算你都不理解也不要紧,后面我们会有更深入的讲解。
1、存储合约示例
把一个数据保存到链上
//SPDX-License-Identifier:GPL-3.0
pragma solidity>=0.4.16<0.9.0;
contractSimpleStorage{
uintstoredData;
functionset(uintx)public{
storedData=x;
}
functionget()public view returns(uint){
returnstoredData;
}
}
行是说明源代码是根据GPL 3.0版本授权的。默认情况下,在发布源代码时加入机器可读许可证说明是很重要的,
下一行是告诉编译器源代码所适用的Solidity版本为>=0.4.16及pragma是告知编译器如何处理源代码的通用指令(例如,pragma once)。
Solidity中智能合约的含义就是一组代码(它的功能)和数据(它的状态)的集合,并且它们是位于以太坊区块链的一个特定地址上的。uintstoredData;这一行代码声明了一个名为``storedData``的状态变量,其类型为uint(256位无符号整数)。你也可以认为它是数据库里的一个插槽,并且可以通过调用管理数据库代码的函数进行查询和更改。在这个例子中,上述的合约定义了``set``和``get``函数,可以用来修改或检索变量的值。//SPDX-License-Identifier:SimPL-2.0
pragma solidity>=0.7.0<0.8.9;
contract zhongchou{
//投资者投资记录:投资目标,投资金额
struct toMoney{
address payable addressReceiptor;
uint money;
}
//投资者基本信息:地址,是否被,总投资金额,投资次数,映射记录投资记录
struct funder{
address payable addressfunder;
bool isActive;
uint totalMoney;
uint numberGive;
mapping(uint=>toMoney)expMap;
}
//众筹合约:合约创建者,是否被,金额总需求,已投资金额,投资人数量,映射记录投资人
struct needMoneyContract{
address payable addressNeeder;
//payable address addressContract;
bool isActive;
uint totalMoney;
uint giveMoney;
uint amountFunder;
mapping(uint=>funder)mapFunder;
}
//众筹发起者:地址,状态,需求总金额,已经被投资的金额,发起的众筹的数量,映射记录投资合约
struct needer{
address addressNeeder;
bool isActive;
uint amountMoneyNeed;
uint amountHasFunded;
uint numberContract;
mapping(uint=>needMoneyContract)expMap;
}
//记录众筹合约总数,合约地址(资金池地址)
uint amountContract;
address payable public addressFinance;
//三方数组
mapping(address=>funder)funderMap;
mapping(uint=>needMoneyContract)contractMap;
mapping(address=>needer)neederMap;
constructor(){
addressFinance=payable(msg.sender);
}
//创建一个众筹发起者
function createNeeder()public returns(bool){
//需要判定是否已经被
if(neederMap[msg.sender].isActive){
return false;
}
else{
address _addressNeeder=msg.sender;
//0.8.0后不允许直接创建一个包含映射的结构体。需要通过引用的方式,先创建一个storage类型的结构体(与目标是引用关系),再对新变量进行操作即可。
needer storage tmp1=neederMap[_addressNeeder];
tmp1.addressNeeder=_addressNeeder;
tmp1.isActive=true;
tmp1.amountMoneyNeed=0;
tmp1.amountHasFunded=0;
tmp1.numberContract=0;
return true;
}
}
function createContract(
uint _amountMoneyNeed
)public returns(bool){
address _addressNeeder=msg.sender;
uint tmpNum=amountContract++;
needMoneyContract storage tmp2=contractMap[tmpNum];
tmp2.addressNeeder=payable(_addressNeeder);
tmp2.isActive=true;
tmp2.totalMoney=_amountMoneyNeed;
tmp2.giveMoney=0;
tmp2.amountFunder=0;
uint tmpContract=neederMap[_addressNeeder].numberContract++;
neederMap[_addressNeeder].amountMoneyNeed+=_amountMoneyNeed;
needMoneyContract storage tmp3=neederMap[_addressNeeder].expMap[tmpContract];
needMoneyContract storage tmp4=contractMap[tmpNum];
tmp3=tmp4;
return true;
}
function createFunder()public returns(bool){
if(funderMap[msg.sender].isActive){
return false;
}
else{
address _address=msg.sender;
funder storage tmpfund=funderMap[_address];
tmpfund.addressfunder=payable(_address);
tmpfund.isActive=true;
tmpfund.totalMoney=0;
tmpfund.numberGive=0;
return true;
}
}
function donateMoney(
uint money,
uint idContract,
address addressNeeder
)public payable returns(bool){
require(contractMap[idContract].isActive==true);
require(money==msg.value);
require(contractMap[idContract].addressNeeder==addressNeeder);
//payable address adressDonate=msg.sender;
address tmpfunder=msg.sender;
funderMap[tmpfunder].totalMoney+=money;
toMoney storage tmpMoney=funderMap[tmpfunder].expMap[funderMap[tmpfunder].numberGive];
tmpMoney.addressReceiptor=payable(addressNeeder);
tmpMoney.money=money;
funderMap[tmpfunder].numberGive++;
contractMap[idContract].giveMoney+=money;
funder storage tmpfund1=contractMap[idContract].mapFunder[contractMap[idContract].amountFunder++];
funder storage tmpfund2=funderMap[tmpfunder];
tmpfund1=tmpfund2;
return true;
}
function isComplete(uint idContract)public payable returns(bool){
require(contractMap[idContract].isActive==true);
require(contractMap[idContract].addressNeeder==msg.sender);
require(contractMap[idContract].totalMoney<=contractMap[idContract].giveMoney);
needMoneyContract storage tmptrans=contractMap[idContract];
tmptrans.isActive=false;
address tmpaddr=msg.sender;
uint getMoney=tmptrans.giveMoney;
neederMap[tmpaddr].amountHasFunded+=getMoney;
tmptrans.addressNeeder.transfer(getMoney);
return true;
}
}
智能合约开发步,在于要认识与理解合约代码的含义。接下来让我们先看一下基本的例子。现在就算你都不理解也不要紧,后面我们会有更深入的讲解。
1、存储合约示例
把一个数据保存到链上
//SPDX-License-Identifier:GPL-3.0
pragma solidity>=0.4.16<0.9.0;
contractSimpleStorage{
uintstoredData;
functionset(uintx)public{
storedData=x;
}
functionget()public view returns(uint){
returnstoredData;
}
}
行是说明源代码是根据GPL 3.0版本授权的。默认情况下,在发布源代码时加入机器可读许可证说明是很重要的,
下一行是告诉编译器源代码所适用的Solidity版本为>=0.4.16及pragma是告知编译器如何处理源代码的通用指令(例如,pragma once)。
Solidity中智能合约的含义就是一组代码(它的功能)和数据(它的状态)的集合,并且它们是位于以太坊区块链的一个特定地址上的。uintstoredData;这一行代码声明了一个名为``storedData``的状态变量,其类型为uint(256位无符号整数)。你也可以认为它是数据库里的一个插槽,并且可以通过调用管理数据库代码的函数进行查询和更改。在这个例子中,上述的合约定义了``set``和``get``函数,可以用来修改或检索变量的值。//SPDX-License-Identifier:SimPL-2.0
pragma solidity>=0.7.0<0.8.9;
contract zhongchou{
//投资者投资记录:投资目标,投资金额
struct toMoney{
address payable addressReceiptor;
uint money;
}
//投资者基本信息:地址,是否被,总投资金额,投资次数,映射记录投资记录
struct funder{
address payable addressfunder;
bool isActive;
uint totalMoney;
uint numberGive;
mapping(uint=>toMoney)expMap;
}
//众筹合约:合约创建者,是否被,金额总需求,已投资金额,投资人数量,映射记录投资人
struct needMoneyContract{
address payable addressNeeder;
//payable address addressContract;
bool isActive;
uint totalMoney;
uint giveMoney;
uint amountFunder;
mapping(uint=>funder)mapFunder;
}
//众筹发起者:地址,状态,需求总金额,已经被投资的金额,发起的众筹的数量,映射记录投资合约
struct needer{
address addressNeeder;
bool isActive;
uint amountMoneyNeed;
uint amountHasFunded;
uint numberContract;
mapping(uint=>needMoneyContract)expMap;
}
//记录众筹合约总数,合约地址(资金池地址)
uint amountContract;
address payable public addressFinance;
//三方数组
mapping(address=>funder)funderMap;
mapping(uint=>needMoneyContract)contractMap;
mapping(address=>needer)neederMap;
constructor(){
addressFinance=payable(msg.sender);
}
//创建一个众筹发起者
function createNeeder()public returns(bool){
//需要判定是否已经被
if(neederMap[msg.sender].isActive){
return false;
}
else{
address _addressNeeder=msg.sender;
//0.8.0后不允许直接创建一个包含映射的结构体。需要通过引用的方式,先创建一个storage类型的结构体(与目标是引用关系),再对新变量进行操作即可。
needer storage tmp1=neederMap[_addressNeeder];
tmp1.addressNeeder=_addressNeeder;
tmp1.isActive=true;
tmp1.amountMoneyNeed=0;
tmp1.amountHasFunded=0;
tmp1.numberContract=0;
return true;
}
}
function createContract(
uint _amountMoneyNeed
)public returns(bool){
address _addressNeeder=msg.sender;
uint tmpNum=amountContract++;
needMoneyContract storage tmp2=contractMap[tmpNum];
tmp2.addressNeeder=payable(_addressNeeder);
tmp2.isActive=true;
tmp2.totalMoney=_amountMoneyNeed;
tmp2.giveMoney=0;
tmp2.amountFunder=0;
uint tmpContract=neederMap[_addressNeeder].numberContract++;
neederMap[_addressNeeder].amountMoneyNeed+=_amountMoneyNeed;
needMoneyContract storage tmp3=neederMap[_addressNeeder].expMap[tmpContract];
needMoneyContract storage tmp4=contractMap[tmpNum];
tmp3=tmp4;
return true;
}
function createFunder()public returns(bool){
if(funderMap[msg.sender].isActive){
return false;
}
else{
address _address=msg.sender;
funder storage tmpfund=funderMap[_address];
tmpfund.addressfunder=payable(_address);
tmpfund.isActive=true;
tmpfund.totalMoney=0;
tmpfund.numberGive=0;
return true;
}
}
function donateMoney(
uint money,
uint idContract,
address addressNeeder
)public payable returns(bool){
require(contractMap[idContract].isActive==true);
require(money==msg.value);
require(contractMap[idContract].addressNeeder==addressNeeder);
//payable address adressDonate=msg.sender;
address tmpfunder=msg.sender;
funderMap[tmpfunder].totalMoney+=money;
toMoney storage tmpMoney=funderMap[tmpfunder].expMap[funderMap[tmpfunder].numberGive];
tmpMoney.addressReceiptor=payable(addressNeeder);
tmpMoney.money=money;
funderMap[tmpfunder].numberGive++;
contractMap[idContract].giveMoney+=money;
funder storage tmpfund1=contractMap[idContract].mapFunder[contractMap[idContract].amountFunder++];
funder storage tmpfund2=funderMap[tmpfunder];
tmpfund1=tmpfund2;
return true;
}
function isComplete(uint idContract)public payable returns(bool){
require(contractMap[idContract].isActive==true);
require(contractMap[idContract].addressNeeder==msg.sender);
require(contractMap[idContract].totalMoney<=contractMap[idContract].giveMoney);
needMoneyContract storage tmptrans=contractMap[idContract];
tmptrans.isActive=false;
address tmpaddr=msg.sender;
uint getMoney=tmptrans.giveMoney;
neederMap[tmpaddr].amountHasFunded+=getMoney;
tmptrans.addressNeeder.transfer(getMoney);
return true;
}
}