Created
March 2, 2018 09:37
-
-
Save goastoman/994c9d663bd5d2e822c27c4a2f42cc29 to your computer and use it in GitHub Desktop.
Lottery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import './App.css'; | |
import web3 from './web3.js'; | |
import lottery from './lottery.js'; | |
class App extends Component { | |
state = { | |
manager : '', | |
players : [], | |
balance : '', | |
value : '', | |
message : '' | |
}; | |
async componentDidMount() { | |
const manager = await lottery.methods.manager().call(); | |
const players = await lottery.methods.getPlayers().call(); | |
const balance = await web3.eth.getBalance(lottery.options.address); | |
this.setState({ manager, players, balance }); | |
} | |
onSubmit = async event => { | |
event.preventDefault(); | |
const accounts = await web3.eth.getAccounts(); | |
this.setState({ message: 'Waiting on transaction success...' }); | |
await lottery.methods.enter().send({ | |
from: accounts[0], | |
value: web3.utils.toWei(this.state.value, 'ether') | |
}); | |
this.setState({ message: 'You have been placed to the hat!' }); | |
}; | |
onClick = async () => { | |
const accounts = await web3.eth.getAccounts(); | |
this.setState({ message: 'Waiting on transaction success...'}); | |
await lottery.methods.pickwinner().send({ | |
from: accounts[0] | |
}); | |
this.setState({ message: 'The one has been picked!'}) | |
} | |
render() { | |
return ( | |
<div> | |
<h2>Lottery contract</h2> | |
<p>This contract is managed by {this.state.manager}.</p> | |
<p>There are currently {this.state.players.length} people entered, | |
competing to win {web3.utils.fromWei(this.state.balance, 'ether')} ether!</p> | |
<hr/> | |
<form onSubmit={this.onSubmit}> | |
<h4>Want to check our luck here?</h4> | |
<div> | |
<label>Amount of Ether to put in: </label> | |
<input | |
value={this.state.value} | |
onChange={event => this.setState({ value: event.target.value })} | |
/> | |
</div> | |
<button>Enter</button> | |
</form> | |
<hr/> | |
<h4>Ready to pick a winner?</h4> | |
<button onClick={this.onClick}>Pick winner!</button> | |
<hr/> | |
<h1>{this.state.message}</h1> | |
</div> | |
); | |
} | |
} | |
export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.18; | |
contract Lottery { | |
address public manager; | |
address[] public players; //creates dynamic array of addresess | |
modifier onlyManager() { | |
require(msg.sender == manager); | |
_; | |
} | |
function Lottery() public { | |
manager = msg.sender; //msg - global variable used to describe who just sender | |
} | |
// function het's the rigth to participate in the Lottery | |
function participate() public payable { //payable for sending ether | |
require(msg.value >= .01 ether); | |
players.push(msg.sender); //push participants to the array | |
} | |
function random() private view returns (uint) { | |
return uint(keccak256(block.difficulty, now, players)); //the same as sha3 it's class of algo and sha3 is particular instance of it | |
//block - global var, difficulty number that indictes numbers of block, now - it's time of now, players just players | |
} | |
function randomWinner() public onlyManager { | |
uint index = random() % players.length; | |
players[index].transfer(this.balance); // 0xsome hush | |
players = new address[](0); | |
} | |
function getPlayers() public view returns (address[]) { | |
return players; | |
} | |
function balanceOfLotteryIs() public view returns (uint256) { | |
return this.balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment