TransWikia.com

Need help in deploying a dapp on Ropsten without truffle

Ethereum Asked by Deb on December 9, 2020

I am trying to deploy a Decentralized application on Ropsten without using truffle (trying to challenge myself), but for now I am stuck at few places, the deploy.js file which is supposed to deploy the contract on to the network, the createWeb3.js file where connection to web3 is defined, then finally the app.js file, as I am little amateur in terms of the front-end development, please find the files below and help me in deploying the dapp successfully.

Here is a link to the overall github repo: https://github.com/dmajumder4292/weatherBetDapp

createWeb3.js file:

const Web3 = require('web3');
var HDWalletProvider = require("truffle-hdwallet-provider");
const mnemonic = "access carpet olympic service heart tissue lucky pill dress ball half share";

let web3 = null;
module.exports = {
    getWeb3Instance : function(){
        if (web3 === null) {
            const provider = new HDWalletProvider(mnemonic, "https://ropsten.infura.io/b13e328e12fa4a1bb93c4097b7241e6e");
            web3 = new Web3(provider);
        } 
        return web3;
    }
};

Deploy.js File:

const Web3 = require('../ethereum/createWeb3');
const web3 = Web3.getWeb3Instance();


const weatherBet = require('../ethereum/build/WeatherBet.json')

const deployContracts = async () => {
    const accounts = await web3.eth.getAccounts();

    const weatherBetResult = await new web3.eth.Contract(JSON.parse(weatherBet.interface))
     .deploy({data : weatherBet.bytecode})
     .send({gas : '4000000', from : accounts[0]});


      console.log("mobile manufacturer contract deployed to ", weather.options.address);

}

deployContracts()

app.js file:

const Web3 = require('../ethereum/createWeb3');
const web3 = Web3.getWeb3Instance();
var request = require('request');

const weatherBet = require('../ethereum/build/WeatherBet.json');


var WeatherBetInstance = web3.eth.Contract(JSON.parse(weatherBet.interface))
var accounts, account;

function getBalance(address) {
    return web3.fromWei(web3.eth.getBalance(address).toNumber(), 'ether');
}

window.App = { 
    start: function() {
        var self = this;

        web3.eth.getAccounts(function(err, accs) {
            if (err != null) {
                alert("There was an error fetching your accounts.");
                return;
            }

            if (accs.length == 0) {
                alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
                return;
            }
    accounts = accs;

    $("#playerAddress").text(web3.eth.accounts[0]); //prints account address
        console.log(accounts);
        console.log("uSER iS cREATED");  
        self.initializeConference();
    });
    },

    initializeConference: function() {
        var self = this;

        WeatherBetInstance.deployed().then(function(instance) {
            $("#confAddress").html(instance.address);


        }).catch(function(e) {
            console.log(e);
        });

    },

    createBet: function() {
        WeatherBetInstance.deployed().then(function(instance) {
            instance.createBet($(#ch).val(), {from: accounts[0], gas: 3000000, value: $(#cba).val()}).then(function(result){
                console.log(result);
            })
        })
    },

    acceptBet: function() {
        WeatherBetInstance.deployed().then(function(instance) {
            instance.acceptBet($(#betid).val(), {from: accounts[0], gas: 3000000, value: $(#aba).val()}).then(function(result){
                console.log(result);
            })
        })
    },

    result: function() {
        WeatherBetInstance.deployed().then(function(instance) {
            var _resTemp;
            var url = "http://api.openweathermap.org/data/2.5/forecast?q=london,us&mode=json&appid=aad54055817b7630b3545053cfe8fed5";
            request(url, function(error, response, body){
                if(!error && response.statusCode == 200){
                    var results = JSON.parse(body);
                    var temp = results.list[0].main.temp;
                    _resTemp = temp;
                }
            });
            instance.result($(#resbetid).val(), _resTemp, {from: accounts[0], gas: 3000000}).then(function(result){
                console.log(result);
            })
        })
    },
};

window.addEventListener('load', function() {
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
    if (typeof web3 !== 'undefined') {
        console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
    // Use Mist/MetaMask's provider
        window.web3 = new Web3(web3.currentProvider);
    } else {
        console.warn("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");
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
        window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    }

    WeatherBetInstance.setProvider(web3.currentProvider);
    App.start();


    $("#BetCreate").click(function() {
       App.createBet();
    });

    $("#BetAccept").click(function() {
       App.acceptBet();
    });

    $("#BetResult").click(function() {
       App.result();
    });
});

One Answer

You can use HttpProvider from Web3 providers list. Here is your modified createWeb3.js file.

const Web3 = require('web3');
let web3 = null;
module.exports = {
    getWeb3Instance : function(){
        if (web3 === null) {
            const provider = new Web3.providers.HttpProvider("https://ropsten.infura.io/b13e328e12fa4a1bb93c4097b7241e6e");
            web3 = new Web3(provider);
        } 
        return web3;
    }
};

Answered by Nithin D J on December 9, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP