1

I have a webpage that talks to a smart contract on testrpc. I connect using: web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

I have deployed my contract to Ropsten Test Network with Metamask. How do I change my webpage to point to the Ropsten test network? Is it as simple as changing the web address from "localhost" to a Ropsten web address?

Thanks!

0

If the Geth node on the localhost is on Ropsten network i.e. you started geth with a networkid of 3 , then your webpage will point to Test network (Ropsten) by default

0

Edit : As i did a little playing around on Metamask plugin i could see how to change enter image description here

Localhost is nothing to do with your question unless you try to connect remote Ethereum clients which are running on Robsten. It looks like you are using Metamask's plugin as an Ethereum client (you can use Geth or Parity too). If this is your case you will continue use localhost to connect your Ethereum client and change check blockchain network as it is documented in Github repo of MetaMask :

web3.version.getNetwork((err, netId) => {
  switch (netId) {
    case "1":
      console.log('This is mainnet')
      break
    case "2":
      console.log('This is the deprecated Morden test network.')
      break
    case "3":
      console.log('This is the ropsten test network.')
      break
    case "4":
      console.log('This is the Rinkeby test network.')
      break
    case "42":
      console.log('This is the Kovan test network.')
      break
    default:
      console.log('This is an unknown network.')
  }
})

In case you are using Geth client you need to run your Geth client with : --testnet flag, or if you are using parity you need to use --chain ropsten flag. Check documentation for further reading.

0
var HDWalletProvider = require("truffle-hdwallet-provider");

var infura_apikey = "Your infura key";<br>
var mnemonic = "Your 12 word metamask key";<br>

module.exports = {
    networks: {
    development: {
    host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    },
    ropsten: {
      provider: new HDWalletProvider(mnemonic, "https://ropsten.infura.io/"+infura_apikey),
      network_id: 3,
      gas: 500000      
    }
  }
};

Add the above code in your truffle.js
and then run the command at your terminal
truffle deploy --network='ropsten' --reset

0

You'll have to set up a Ropsten node either remotely or locally. You can use the infura.io node by subscribing to their service. They will email you a link of the format https://ropsten.infura.io/API_KEY which you can add in your code like this:

web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/API_KEY"));

-1

Even easier. The MetaMask plugin automatically injects itself. See their guide.

  • 1
    Does this mean you expect users to enter their private keys on your website? – user19510 Jan 8 '18 at 4:52

Your Answer

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