261-ethereum合约应用模块







ethereum合约应用模块









现在跟合约进行交互

我们创建三个文件
1.display.js  	  负责展示
2.interaction.js  负责交互
3.utils.js	  工具

然后我们创建3个文件夹
1.eth
2.utils
3.display
分别放不同的东西



我们来看下结构
用户与App进行交互
App控制一个控制模块Controller App.js
然后App.js来控制3个模块
1.Web3模块 initWeb3.js
2.合约模块 lottery.js
3.显示模块 display.js


再来看下目录
1.eth
  interaction.js
  loadInstance.js
2.display
  display.js
3.utils
  getWeb3.js







生命周期函数
async componentWillMount(){

let manager = await lotteryInstance.methods.manager().call()
let winner = await lotteryInstance.methods.winner().call()
let round = await lotteryInstance.methods.round().call()
...

console.log(manager)

//给状态变量赋值
this.setState({
	manager: manager,
	winner,
	round,
	players,
	balance,
	playersCount,
})

}


然后render
render(){
	return (

	<div>
	<p>manager: {this.state.manager}</p>
	<p>winner: {this.state.winner}</p>
	...
	</div>

	);
}






先来分析一下业务
1.全民参与
2.每一注只能投一个eth
3.每个人可以投注多次
4.仅管理员定时开奖
5.仅管理员可以退款

1.管理员,manager地址类型
2.彩民池,地址数组
3.期数,uint
4.赢家,address

来写一下合约
pragma solidity ^0.4.24;

contract Lottery{

//1.管理员
address public manager;
//2.玩家
address[] public players;
//3.期数
uint public round;
//4.赢家
address public winner;


//构造函数
constructor() public{
	manager = msg.sender;
}


//函数
function play() public payable{
	
	//每次只能投注一个eth
	require(msg.value == 1 ether);

	//记录参与的彩民
	players.push(msg.sender);
}

//返回所有的彩民地址
function getPlayers() public view returns(address[]){
	return players;
}


}





看一下开奖逻辑
1.只有管理员可以开奖
2.选择一个随机的彩民,称为中奖者
  哈希函数: keccak函数=>bytes32=>uint256
  随机数%彩民数组长度
  100%11 => 1
3.转账
4.彩民池清零
5.期数加1




function getResult() public onlyManager{
	bytes memory infos = abi.encodePacked(block.timestamp, players.length, block.difficulty);
	bytes32 hashValue = keccak256(infos);

	//中奖者的索引
	uint winnerIndex = uint(hashValue) % players.length;
	
	winner = player[winnerIndex];

	//向中奖者转账
	winner.transfer(address(this).balance);

	//彩民池清零
	delete players;

	//期数加1
	round++;
}

//返回所有彩民的地址
function getPlayers() public view returns(address[]){
	return players;
}









编译合约

let fs = require('fs')
let solc = require('solc')

//1.读入合约
let lotteryInfo = fs.readdirSync('.lottery.sol')
console.log(lotterInfo)

//2.编译合约
let compileInfo = solc.compile(lotteryInfo.toString(),1)
console.log(compileInfo)

//3.导出abi,bytecode
module.exports = compileIno['contracts'][':Lottery']









//部署函数,异步操作
let deploy = async() =>{

//1.填写abi
let contractInstance = new web3.eth.Contract(JSON.parse(interface))

//2.填写bytecode和构造函数参数
contractInstance.deploy({
	data: bytecode,
	arguments: []		//空参构造函数可以不写
})

//3.send交易
.send({

from: account0,		//部署人
gas: '1000000'		//gas limit
value: 0,

})

//获取部署后合约的地址
console.log('address', )

}







项目相关
1.项目名称
2.目标金额
3.单笔金额
4.结束时间

其他:
1.项目方
2.投资人集合


先来写合约
contract CrowFunding{

string public projectName;
uint256 public targetBalance;
uint256 public supportBalance;
uint256 public endTime;

address public manager;
address[] supportors;

//构造函数
constructor(string _projectName, 
	uint256 _targetBalance, 
	uint256 _supportBalance,
	uint256 _lastSeconds,	
) public{
	manager = msg.sender;

	projectName= _projectName;
	targetBalance= _targetBalance;
	supportBalance= _supportBalance;
	
	//终止时间= 当前时间+持续时间
	endTime = block.timestamp + _lastSeconds;

}

}


现在状态变量和构造函数都写好了



然后写一下support方法

function support() public payable{

require(msg.value==supportBalance);

require(supportExistMap[msg.sender]==false);

supportors.push(msg.sender);

supportExistMap[msg.sender]=true;

}





//众筹失败退款
function tuikuan() public{

for(uint256 i=0; i<supportors.length; i++){
	supportors[i].transfer(supportBalance);
}

//delete supportors;
//selfDestroy();

}





写个修饰符
modifier onlyManager(){
	require(msg.sender == manager);
	_;
}






写个结构体
struct Request{

string purpose;
uint256 cost;
address seller;
uint256 approveCount;

}








 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值