0

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();
    });
});
  • It would be better to show, what you did, and the errors you encountered. – Kaki Master Of Time Oct 1 '18 at 9:28
  • I was able to compile it properly, but it won't deploy. And I am sure the app.js file is full of errors. – Deb Oct 1 '18 at 13:44
  • I possible, try running it on your local system. – Deb Oct 1 '18 at 13:47
0

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;
    }
};
| improve this answer | |
  • Since I am using metamask, I think I will have to provide the mnemonics somewhere, I tried the method suggested by you, but it didn't work either. – Deb Oct 1 '18 at 13:46
  • I possible, try running it on your local system. – Deb Oct 1 '18 at 13:46
  • if you have ropsten account, You do not need any wallet or mnemonic related to that. Just replace account info to your ropsten account address which has some ethers to make deployment. – Nithin D J Oct 1 '18 at 14:06
  • Where will I provide the Ropsten account info? Would you mind showing how the code will look like with your suggested modifications? – Deb Oct 1 '18 at 16:12
  • Please replace the web3.eth.accounts[0] or anywhere you are using accounts[0] with your ropsten account address. – Nithin D J Oct 3 '18 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.