Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- App = {
- web3Provider: null,
- contracts: {},
- minBetAmount: 0,
- loading: false,
- init: function () {
- console.log("initializing app ...");
- $(".none").hide();
- $(".active").hide();
- $(".end").hide();
- $(".claim").hide();
- App.setLoading();
- return App.refreshView();
- },
- initWeb3: async function () {
- console.log("initializing web3 ...");
- // Modern dapp browsers...
- if (window.ethereum) {
- App.web3Provider = window.ethereum;
- try {
- // Request account access
- await window.ethereum.enable();
- } catch (error) {
- // User denied account access...
- console.error("User denied account access")
- }
- }
- // Legacy dapp browsers...
- else if (window.web3) {
- App.web3Provider = window.web3.currentProvider;
- }
- // If no injected web3 instance is detected, fall back to Ganache
- else {
- App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
- }
- web3 = new Web3(App.web3Provider);
- return App.bindEvents();
- },
- bindEvents: function () {
- $("button.refresh").on('click', App.refreshView);
- $("button.create").on('click', App.createLottery);
- $("button.buy").on('click', App.buyTicket);
- // $("button.end").on('click', App.endLottery);
- // $("button.claim").on('click', App.claimPrize);
- },
- setLoading: function () {
- App.loading = !App.loading;
- $(".loading").prop("hidden", App.loading);
- },
- refreshView: function () {
- console.log("refreshing ...");
- if (App.web3Provider === null
- || (Object.entries(App.contracts).length === 0
- && App.contracts.constructor === Object)) {
- $(".none").show();
- $(".active").hide();
- $(".end").hide();
- $(".claim").hide();
- return App.initWeb3();
- } else {
- App.contracts.Lottery.getStatus.call(
- function (err, message) {
- if (!err) {
- $("#message").text(message);
- }
- }
- );
- App.contracts.Lottery.getRemainingTimeInSeconds.call(
- function (err, timeMessage) {
- if (!err) {
- if (timeMessage.includes("seconds")) {
- $(".none").hide();
- $(".active").show();
- $(".end").hide();
- $(".claim").hide();
- $("#time").text(timeMessage);
- App.contracts.Lottery.fee.call(
- function (err, message) {
- if (!err) {
- $("#fee").text(message.toNumber());
- }
- });
- App.contracts.Lottery.ticketPrice.call(
- function (err, message) {
- if (!err) {
- $("#ticket").text(message.toNumber());
- }
- });
- App.contracts.Lottery.lotteryRange.call(
- function (err, message) {
- if (!err) {
- $("#range").text(message.toNumber());
- }
- });
- App.contracts.Lottery.prizeSize.call(
- function (err, message) {
- if (!err) {
- $("#prize").text(message.toNumber());
- }
- });
- App.contracts.Lottery.lotteryAddress.call(
- function (err, message) {
- if (!err) {
- $("#address").text(message);
- }
- });
- } else {
- App.minBetAmount = 0;
- App.contracts.Lottery.winningNumber.call(
- function (err, message) {
- if (!err) {
- if (message.toNumber() === 0) {
- $(".none").hide();
- $(".active").hide();
- $(".end").show();
- $(".claim").hide();
- } else {
- $("#winner").text(message.toNumber());
- $(".none").hide();
- $(".active").hide();
- $(".end").hide();
- $(".claim").show();
- }
- }
- });
- }
- }
- });
- }
- },
- createLottery: function () {
- console.log("creating ...");
- App.setLoading();
- let compiledContract;
- $.getJSON('./build/contracts/Lottery.json', function (data) {
- compiledContract = data;
- }).then(() => {
- let abi = compiledContract.abi;
- let bytecode = compiledContract.bytecode;
- let gasEstimate = web3.eth.estimateGas({data: bytecode}, () => {
- let MyContract = web3.eth.contract(abi);
- App.minBetAmount = +$("#inputPrice").val() + +$("#inputFee").val();
- $("#inputBet").val(App.minBetAmount);
- let myContractReturned = MyContract.new($("#inputDuration").val(),
- $("#inputPrice").val(),
- $("#inputFee").val(),
- $("#inputRange").val(),
- {
- from: web3.eth.accounts[0],
- data: bytecode,
- gas: gasEstimate,
- value: $("#inputValue").val()
- },
- function (err, contract) {
- if (!err) {
- if (!contract.address) {
- console.log(contract.transactionHash)
- } else {
- console.log(contract.address); // the contract address
- App.contracts.Lottery = contract;
- $("#inputDuration").val("");
- $("#inputPrice").val("");
- $("#inputFee").val("");
- $("#inputRange").val("");
- $("#inputValue").val("0");
- App.setLoading();
- return App.refreshView();
- }
- }
- });
- });
- });
- },
- buyTicket: function () {
- console.log("buying ...");
- App.setLoading();
- //TODO not working
- let gasEstimate;
- App.contracts.Lottery
- .buyTicket
- .estimateGas($("#inputName").val(),
- $("#inputNumber").val(),
- {
- gas: 5000000,
- value: $("#inputBet").val()
- },
- function (err, message) {
- if (!err) {
- gasEstimate = message;
- }
- });
- console.log(gasEstimate);
- App.contracts.Lottery
- .buyTicket
- .sendTransaction($("#inputName").val(), $("#inputNumber").val(),
- {
- from: web3.eth.accounts[0],
- value: $("#inputBet").val()
- },
- function (err, message) {
- if (!err) {
- console.log(message);
- $("#inputName").val("");
- $("#inputNumber").val("");
- $("#inputBet").val(App.minBetAmount);
- $("#message").text(message);
- App.setLoading();
- return App.refreshView();
- }
- }
- );
- },
- };
- $(function () {
- $(window).load(function () {
- App.init();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement