Advertisement
Guest User

app

a guest
Mar 21st, 2019
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. App = {
  2.     web3Provider: null,
  3.     contracts: {},
  4.     minBetAmount: 0,
  5.     loading: false,
  6.  
  7.     init: function () {
  8.         console.log("initializing app ...");
  9.  
  10.         $(".none").hide();
  11.         $(".active").hide();
  12.         $(".end").hide();
  13.         $(".claim").hide();
  14.  
  15.         App.setLoading();
  16.  
  17.         return App.refreshView();
  18.     },
  19.  
  20.     initWeb3: async function () {
  21.         console.log("initializing web3 ...");
  22.  
  23.         // Modern dapp browsers...
  24.         if (window.ethereum) {
  25.             App.web3Provider = window.ethereum;
  26.             try {
  27.                 // Request account access
  28.                 await window.ethereum.enable();
  29.             } catch (error) {
  30.                 // User denied account access...
  31.                 console.error("User denied account access")
  32.             }
  33.         }
  34.         // Legacy dapp browsers...
  35.         else if (window.web3) {
  36.             App.web3Provider = window.web3.currentProvider;
  37.         }
  38.         // If no injected web3 instance is detected, fall back to Ganache
  39.         else {
  40.             App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
  41.         }
  42.         web3 = new Web3(App.web3Provider);
  43.  
  44.         return App.bindEvents();
  45.     },
  46.  
  47.  
  48.     bindEvents: function () {
  49.         $("button.refresh").on('click', App.refreshView);
  50.         $("button.create").on('click', App.createLottery);
  51.         $("button.buy").on('click', App.buyTicket);
  52.         // $("button.end").on('click', App.endLottery);
  53.         // $("button.claim").on('click', App.claimPrize);
  54.     },
  55.  
  56.     setLoading: function () {
  57.         App.loading = !App.loading;
  58.         $(".loading").prop("hidden", App.loading);
  59.     },
  60.  
  61.     refreshView: function () {
  62.  
  63.         console.log("refreshing ...");
  64.  
  65.         if (App.web3Provider === null
  66.             || (Object.entries(App.contracts).length === 0
  67.                 && App.contracts.constructor === Object)) {
  68.             $(".none").show();
  69.             $(".active").hide();
  70.             $(".end").hide();
  71.             $(".claim").hide();
  72.  
  73.             return App.initWeb3();
  74.         } else {
  75.             App.contracts.Lottery.getStatus.call(
  76.                 function (err, message) {
  77.                     if (!err) {
  78.                         $("#message").text(message);
  79.                     }
  80.                 }
  81.             );
  82.  
  83.             App.contracts.Lottery.getRemainingTimeInSeconds.call(
  84.                 function (err, timeMessage) {
  85.                     if (!err) {
  86.  
  87.                         if (timeMessage.includes("seconds")) {
  88.                             $(".none").hide();
  89.                             $(".active").show();
  90.                             $(".end").hide();
  91.                             $(".claim").hide();
  92.  
  93.                             $("#time").text(timeMessage);
  94.  
  95.                             App.contracts.Lottery.fee.call(
  96.                                 function (err, message) {
  97.                                     if (!err) {
  98.                                         $("#fee").text(message.toNumber());
  99.                                     }
  100.                                 });
  101.  
  102.                             App.contracts.Lottery.ticketPrice.call(
  103.                                 function (err, message) {
  104.                                     if (!err) {
  105.                                         $("#ticket").text(message.toNumber());
  106.                                     }
  107.                                 });
  108.  
  109.                             App.contracts.Lottery.lotteryRange.call(
  110.                                 function (err, message) {
  111.                                     if (!err) {
  112.                                         $("#range").text(message.toNumber());
  113.                                     }
  114.                                 });
  115.  
  116.                             App.contracts.Lottery.prizeSize.call(
  117.                                 function (err, message) {
  118.                                     if (!err) {
  119.                                         $("#prize").text(message.toNumber());
  120.                                     }
  121.                                 });
  122.  
  123.                             App.contracts.Lottery.lotteryAddress.call(
  124.                                 function (err, message) {
  125.                                     if (!err) {
  126.                                         $("#address").text(message);
  127.                                     }
  128.                                 });
  129.  
  130.                         } else {
  131.  
  132.                             App.minBetAmount = 0;
  133.  
  134.                             App.contracts.Lottery.winningNumber.call(
  135.                                 function (err, message) {
  136.                                     if (!err) {
  137.                                         if (message.toNumber() === 0) {
  138.                                             $(".none").hide();
  139.                                             $(".active").hide();
  140.                                             $(".end").show();
  141.                                             $(".claim").hide();
  142.  
  143.                                         } else {
  144.                                             $("#winner").text(message.toNumber());
  145.  
  146.                                             $(".none").hide();
  147.                                             $(".active").hide();
  148.                                             $(".end").hide();
  149.                                             $(".claim").show();
  150.                                         }
  151.                                     }
  152.                                 });
  153.                         }
  154.                     }
  155.                 });
  156.         }
  157.     },
  158.  
  159.     createLottery: function () {
  160.         console.log("creating ...");
  161.  
  162.         App.setLoading();
  163.  
  164.         let compiledContract;
  165.         $.getJSON('./build/contracts/Lottery.json', function (data) {
  166.             compiledContract = data;
  167.         }).then(() => {
  168.  
  169.             let abi = compiledContract.abi;
  170.             let bytecode = compiledContract.bytecode;
  171.             let gasEstimate = web3.eth.estimateGas({data: bytecode}, () => {
  172.                 let MyContract = web3.eth.contract(abi);
  173.  
  174.                 App.minBetAmount = +$("#inputPrice").val() + +$("#inputFee").val();
  175.                 $("#inputBet").val(App.minBetAmount);
  176.  
  177.                 let myContractReturned = MyContract.new($("#inputDuration").val(),
  178.                     $("#inputPrice").val(),
  179.                     $("#inputFee").val(),
  180.                     $("#inputRange").val(),
  181.                     {
  182.                         from: web3.eth.accounts[0],
  183.                         data: bytecode,
  184.                         gas: gasEstimate,
  185.                         value: $("#inputValue").val()
  186.                     },
  187.                     function (err, contract) {
  188.                         if (!err) {
  189.                             if (!contract.address) {
  190.                                 console.log(contract.transactionHash)
  191.                             } else {
  192.                                 console.log(contract.address); // the contract address
  193.  
  194.                                 App.contracts.Lottery = contract;
  195.  
  196.                                 $("#inputDuration").val("");
  197.                                 $("#inputPrice").val("");
  198.                                 $("#inputFee").val("");
  199.                                 $("#inputRange").val("");
  200.                                 $("#inputValue").val("0");
  201.  
  202.                                 App.setLoading();
  203.  
  204.                                 return App.refreshView();
  205.                             }
  206.                         }
  207.                     });
  208.             });
  209.         });
  210.     },
  211.  
  212.     buyTicket: function () {
  213.         console.log("buying ...");
  214.  
  215.         App.setLoading();
  216.  
  217.         //TODO not working
  218.         let gasEstimate;
  219.         App.contracts.Lottery
  220.             .buyTicket
  221.             .estimateGas($("#inputName").val(),
  222.                 $("#inputNumber").val(),
  223.                 {
  224.                     gas: 5000000,
  225.                     value: $("#inputBet").val()
  226.                 },
  227.                 function (err, message) {
  228.                     if (!err) {
  229.                         gasEstimate = message;
  230.                     }
  231.                 });
  232.  
  233.         console.log(gasEstimate);
  234.  
  235.         App.contracts.Lottery
  236.             .buyTicket
  237.             .sendTransaction($("#inputName").val(), $("#inputNumber").val(),
  238.                 {
  239.                     from: web3.eth.accounts[0],
  240.                     value: $("#inputBet").val()
  241.                 },
  242.                 function (err, message) {
  243.                     if (!err) {
  244.                         console.log(message);
  245.  
  246.                         $("#inputName").val("");
  247.                         $("#inputNumber").val("");
  248.                         $("#inputBet").val(App.minBetAmount);
  249.  
  250.                         $("#message").text(message);
  251.  
  252.                         App.setLoading();
  253.  
  254.                         return App.refreshView();
  255.                     }
  256.                 }
  257.             );
  258.     },
  259. };
  260.  
  261. $(function () {
  262.     $(window).load(function () {
  263.         App.init();
  264.     });
  265. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement