Advertisement
Guest User

Untitled

a guest
May 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import Web3 from 'web3'
  4. import './../css/index.css'
  5.  
  6. class App extends React.Component {
  7. constructor(props){
  8. super(props)
  9. this.state = {
  10. lastWinner: 0,
  11. numberOfBets: 0,
  12. minimumBet: 0,
  13. totalBet: 0,
  14. maxAmountOfBets: 0,
  15. }
  16.  
  17. if(typeof web3 != 'undefined'){
  18. console.log("Using web3 detected from external source like Metamask")
  19. this.web3 = new Web3(web3.currentProvider)
  20. }else{
  21. console.log("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
  22. this.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
  23. }
  24.  
  25. const MyContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"generateNumberWinner","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[{"name":"myid","type":"bytes32"},{"name":"result","type":"string"}],"name":"__callback","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfBets","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_queryId","type":"bytes32"},{"name":"_result","type":"string"},{"name":"_proof","type":"bytes"}],"name":"__callback","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"player","type":"address"}],"name":"checkPlayerExists","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"resetData","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"bets","type":"uint256"}],"name":"updateMaxBets","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"number","type":"uint256"}],"name":"bet","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[{"name":"amountWei","type":"uint256"}],"name":"updateMinimumBet","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"distributePrizes","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberWinner","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"minimumBet","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"maxAmountOfBets","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"players","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalBet","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_maxAmountOfBets","type":"uint256"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"}])
  26. this.state.ContractInstance = MyContract.at("0x430d959fa54714aca8eecd61fae2661fca900e04")
  27.  
  28. window.a = this.state
  29. }
  30.  
  31. componentDidMount(){
  32. this.updateState()
  33. this.setupListeners()
  34.  
  35. setInterval(this.updateState.bind(this), 7e3)
  36. }
  37.  
  38. updateState(){
  39. this.state.ContractInstance.minimumBet((err, result) => {
  40. if(result != null){
  41. this.setState({
  42. minimumBet: parseFloat(web3.fromWei(result, 'ether'))
  43. })
  44. }
  45. })
  46. this.state.ContractInstance.totalBet((err, result) => {
  47. if(result != null){
  48. this.setState({
  49. totalBet: parseFloat(web3.fromWei(result, 'ether'))
  50. })
  51. }
  52. })
  53. this.state.ContractInstance.numberOfBets((err, result) => {
  54. if(result != null){
  55. this.setState({
  56. numberOfBets: parseInt(result)
  57. })
  58. }
  59. })
  60. this.state.ContractInstance.maxAmountOfBets((err, result) => {
  61. if(result != null){
  62. this.setState({
  63. maxAmountOfBets: parseInt(result)
  64. })
  65. }
  66. })
  67. }
  68.  
  69. // Listen for events and executes the voteNumber method
  70. setupListeners(){
  71. let liNodes = this.refs.numbers.querySelectorAll('li')
  72. liNodes.forEach(number => {
  73. number.addEventListener('click', event => {
  74. event.target.className = 'number-selected'
  75. this.voteNumber(parseInt(event.target.innerHTML), done => {
  76.  
  77. // Remove the other number selected
  78. for(let i = 0; i < liNodes.length; i++){
  79. liNodes[i].className = ''
  80. }
  81. })
  82. })
  83. })
  84. }
  85.  
  86. voteNumber(number, cb){
  87. let bet = this.refs['ether-bet'].value
  88.  
  89. if(!bet) bet = 0.1
  90.  
  91. if(parseFloat(bet) < this.state.minimumBet){
  92. alert('You must bet more than the minimum')
  93. cb()
  94. } else {
  95. this.state.ContractInstance.bet(number, {
  96. gas: 300000,
  97. from: web3.eth.accounts[0],
  98. value: web3.toWei(bet, 'ether')
  99. }, (err, result) => {
  100. cb()
  101. })
  102. }
  103. }
  104.  
  105. render(){
  106. return (
  107. <div className="main-container">
  108. <h1>Bet for your best number and win huge amounts of Ether</h1>
  109.  
  110. <div className="block">
  111. <b>Number of bets:</b> &nbsp;
  112. <span>{this.state.numberOfBets}</span>
  113. </div>
  114.  
  115. <div className="block">
  116. <b>Last number winner:</b> &nbsp;
  117. <span>{this.state.lastWinner}</span>
  118. </div>
  119.  
  120. <div className="block">
  121. <b>Total ether bet:</b> &nbsp;
  122. <span>{this.state.totalBet} ether</span>
  123. </div>
  124.  
  125. <div className="block">
  126. <b>Minimum bet:</b> &nbsp;
  127. <span>{this.state.minimumBet} ether</span>
  128. </div>
  129.  
  130. <div className="block">
  131. <b>Max amount of bets:</b> &nbsp;
  132. <span>{this.state.maxAmountOfBets}</span>
  133. </div>
  134.  
  135. <hr/>
  136.  
  137. <h2>Vote for the next number</h2>
  138.  
  139. <label>
  140. <b>How much Ether do you want to bet? <input className="bet-input" ref="ether-bet" type="number" placeholder={this.state.minimumBet}/></b> ether
  141. <br/>
  142. </label>
  143.  
  144. <ul ref="numbers">
  145. <li>1</li>
  146. <li>2</li>
  147. <li>3</li>
  148. <li>4</li>
  149. <li>5</li>
  150. <li>6</li>
  151. <li>7</li>
  152. <li>8</li>
  153. <li>9</li>
  154. <li>10</li>
  155. </ul>
  156.  
  157. <hr/>
  158.  
  159. <div><i>Only working with the Ropsten Test Network</i></div>
  160. <div><i>You can only vote once per account</i></div>
  161. <div><i>Your vote will be reflected when the next block is mined</i></div>
  162. </div>
  163. )
  164. }
  165. }
  166.  
  167. ReactDOM.render(
  168. <App />,
  169. document.querySelector('#root')
  170. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement