networks.js

OpenZeppelin SDK, the backbone of the OpenZeppelin Platform and Starter Kits uses a Truffle compatible format for defining the connection settings for connecting your project to an Ethereum node.

Unlike truffle-config.js, networks.js includes an additional option: protocol which allows you to specify the protocol you wish to use when connecting to your node: http, https, ws, wss, or ipc.

OpenZeppelin-SDK (included in the Starter Kit) will generate a networks.js for you when calling the command openzeppelin init. This file is entirely customizable to your needs.

Options

The default networks.js file created when running the openzeppelin init command looks like the following:

module.exports = {
 networks: {
   development: {
     protocol: 'http',
     host: 'localhost',
     port: 8545,
     gas: 5000000,
     gasPrice: 5e9,
     networkId: '*',
   },
 },
};

This file can be expanded to include additional networks, such as rinkeby, kovan, and mainnet using truffle-hdwallet-provider.

HDWallet Provider

You will need to install truffle-hdwallet-provider via npm install truffle-hdwallet-provider. To use, assign it to a variable and then pass it your memonic and host as your provider.

const HDWalletProvider = require("truffle-hdwallet-provider");
let provider = new HDWalletProvider(mnemonic, host);

Your provider can now be used to create a connection to a node.

Sample

The following is a complete example for connecting to various networks using Infura as the node.

const mnemonic = "Lorem ipsum dolor sit amet consetetur sadipscing elitr sed diam nonumy eirmod";
const INFURA_API_KEY="xxxxxxxxxxxxxxxxxxxx";
const HDWalletProvider = require("truffle-hdwallet-provider");

module.exports = {
 networks: {
   development: {
     protocol: 'ws', //For connecting to a local Ganache-cli instance via websockets
     host: "127.0.0.1",
     port: 8545,
     network_id: "*",
   },
   ropsten: {
     protocol: 'http',
     provider: function() {
       return new HDWalletProvider(mnemonic, 'https://ropsten.infura.io/v3/' + process.env.INFURA_API_KEY)
     },
     network_id: '3',
     gas: 4465030,
     gasPrice: 10000000000,
   },
   kovan: {
     protocol: 'http',
     provider: function() {
       return new HDWalletProvider(mnemonic, 'https://kovan.infura.io/v3/' + process.env.INFURA_API_KEY)
     },
     network_id: '42',
     gas: 4465030,
     gasPrice: 10000000000,
   },
   rinkeby: {
     protocol: 'http',
     provider: () => new HDWalletProvider(process.env.MNENOMIC, "https://rinkeby.infura.io/v3/" + process.env.INFURA_API_KEY),
     network_id: 4,
     gas: 3000000,
     gasPrice: 10000000000
   },
   // main ethereum network(mainnet)
   main: {
     protocol: 'http',
     provider: () => new HDWalletProvider(process.env.MNENOMIC, "https://mainnet.infura.io/v3/" + process.env.INFURA_API_KEY),
     network_id: 1,
     gas: 3000000,
     gasPrice: 10000000000
   }
 }
};