我正在为彩票系统创建智能合约,我想进行一个基于时间的交易,就像将一定数量的玩家添加到阵列中一样,然后应该运行一个时间戳,在我将声明的特定时间,应该将金额发送给中奖者
这是我陷入困境的部分,我正试图从这个功能进入彩票,当条件满足时,我想通过相同的功能转移金额,因为我想自动化中奖功能:
function enter() public payable{
require(msg.value > 1 wei);
players.push(msg.sender);
if(players.length==10){
start = block.timestamp;
}
if(block.timestamp>= start+totalTime){
uint index = random()% players.length;
players[index].transfer(this.balance);
dead[index].transfer((this.balance*2)/100);
winner = players[index];
players = new address[](0);
}
这是我的完整代码:
pragma solidity ^0.4.26;
contract Lottery{
address public manager;
address[] public players;
address [0x000000000000000000000000000000000000dead] private dead;
address public winner;
uint start;
uint end;
uint totalTime=50;
constructor()public {
manager = msg.sender;
}
function enter() public payable{
require(msg.value > 1 wei);
players.push(msg.sender);
if(players.length==10){
start = block.timestamp;
}
if(block.timestamp>= start+totalTime){
uint index = random()% players.length;
players[index].transfer(this.balance);
dead[index].transfer((this.balance*2)/100);
winner = players[index];
players = new address[](0);
}
}
function random() private view returns (uint){
return uint(keccak256(block.difficulty,now,players));
}
function getBalance() public view returns(uint){
return address(this).balance;
}
function getPlayers() public view returns (address[]){
return players;
}
function getWinner() public view returns (address){
return winner;
}
function getTime() public view returns (uint){
return end-block.timestamp;
}
}
你不能,必须有人调用函数并支付汽油费,你可以做的是有一个脚本来监听事件,每次有人"输入";在抽奖中,检查是否已经达到了所需的金额,然后调用函数来获得中奖者,而且你不能在函数之外有任何逻辑,如果你计划部署到生产中,使用块难度和时间戳作为随机性的来源。最好使用链式