Skip to content

Instantly share code, notes, and snippets.

@shingonu
Last active May 14, 2018 06:26
Show Gist options
  • Select an option

  • Save shingonu/1c2ddf4d08dde795424d88c3cad0bc77 to your computer and use it in GitHub Desktop.

Select an option

Save shingonu/1c2ddf4d08dde795424d88c3cad0bc77 to your computer and use it in GitHub Desktop.

Infura

Infura is a hosted Ethereum node cluster that lets your users run your application without requiring them to set up their own Ethereum node or wallet. Infura behaves just like a local install of Ethereum running RPC.

For security reasons, Infura does not manage your private keys, which means infura cannot sign transactions on your behalf.

However, Truffle can sign transactions through the use of its HDWalletProvider. This provider can handle the transaction signing as well as the connection to the Ethereum network.

Install HDWalletProvider:

npm install truffle-hdwallet-provider

Register with Infura

Before you can use Infura, you need to register for an Infura Access Token.

Configure your Truffle project

Notice that our INFURA provider URLs are all over SSL. Make sure you use https:// when you send requests to INFURA rather than http:// requests.

Currently, HDWalletProvider manages only one address at a time. It's the first address generated from the mnemonic.

require("babel-register");
require("babel-polyfill");

const HDWalletProvider = require("truffle-hdwallet-provider");
require("dotenv").config();

const mnemonic = "test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test11 test12";

const ropstenProviderUrl = "https://ropsten.infura.io";
const mainnetProviderUrl = "https://api.myetherapi.com/eth";

const providerRopsten = new HDWalletProvider(mnemonic, ropstenProviderUrl, 0, 50);
const providerMainnet = new HDWalletProvider(mnemonic, mainnetProviderUrl, 0, 50);

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*",
      gas: 4200000,
      gasPrice: 20e9,
    },
    ganache: {
      host: "localhost",
      port: 7545,
      network_id: "*",
      gas: 4200000,
    },
    ropsten: {
      network_id: 3,
      provider: providerRopsten,
      gas: 4200000,
      gasPrice: 30e9,
    },
    mainnet: {
      network_id: 1,
      provider: providerMainnet,
      gas: 4200000,
      gasPrice: 50e9,
    },
  },
  solc: {
    optimizer: {
      enabled: true,
      runs: 200,
    },
  },
};

A provider links to a running node. For example parity or geth. A node has the ability to view and interact with the blockchain. So https://api.myetherapi.com/eth is a provider. Web3 provider is a website running geth or parity node which talks to Ethereum network.

You might quickly recognize that Infura works just fine without adding the Access Token to the URL. That is only partially true. While INFURA will work without identifying your requests, these requests will be subjected to more restrictive throttling and filtering of available JSON-RPC methods.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment