268-ethereum抽奖小游戏二









ethereum抽奖小游戏二






我们来写一个把奖金池的奖金全都退回去的功能
1.只有管理员可以操作
2.彩民人数大于0时才执行
3.遍历彩民池,对每一个彩民进行转账,每个地址1ether
4.彩民池清零
5.期数加一


function giveback() public onlyManager{
	require(players.length !=0)
	
	//遍历彩民池
	for(uint i=0; i<players.length; i++){
	players[i].transfer(1 ether);
	}

	delete players;
	
	round++;
}



然后我们再加一个辅助函数
function getPlayersCount public view returns(uint){
	return players.length;
}






创建一个react工程

然后我们写下代码

这是compile.js文件

//导入
let fs = require('fs')
let solc = require('solc')

//读取合约
let lotteryInfo = fs.readFileSync('lottery.sol')
console.log(lotteryInfo)

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

//导出
module.exports = compileInfo['contracts'][':Lottery']






然后是deploy.js

//导入
let {bytecode, interface} = require('./compile')
console.log(bytecode);
console.log(interface);

//导入web3
let Web3 = require('web3')
let web3 = new Web3('http://127.0.0.1:7545');
console.log(web3.version);

//账户
let account1 = '0xabcd....'

//部署
let deploy = async() =>{
	//实例
	let contractInstance = new web3.eth.Contract(JSON.parse(interface))
	
	//部署
	let res = await contractInstance.deploy({
		data: bytecode,
		arguments: [],
	}).send(
		from: account1,
		gas: '100000',
		value: 0,
	)

	console.log(res.options.address)
}

//调用,部署
deploy()







然后获取合约实例
loadInstance.js文件

//abi和address
let abi = '[abcd....]'
let address = '0xabcd....'

//web3
let Web3 = require('web3')
let web3 = new Web3('http://127.0.0.1:7545')
console.log(web3.version)

//获取合约实例
let lotteryInstance = new web3.eth.Contract(JSON.parse(abi),address)

//导出
module.exports = lotteryInstance








 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值