How to deploy a smart contract on a public test network (Rinkeby) using INFURA + TRUFFLE + truffle-hdwallet-provider

Alvaro Andres Pinzon Cortes
4 min readOct 11, 2018

Concepts

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. With Infura you can:

  • Interact with Ethereum networks like Rinkeby or the Mainnet.
  • Deploy contracts to Ethereum networks.
  • Interact with contracts on Ethereum networks.

Truffle

Truffle is the most popular development framework for Ethereum with a mission to make your life a whole lot easier. You can develop contracts, test them and deploy them to test and production Ethereum networks with the help of Infura and truffle-hdwallet-provider.

truffle-hdwaller-provider

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.

Explanation

  1. Register to infura.

2. Create a new project, change the endpoint to Rinkeby and copy the URL of the endpoint for Rinkeby.

3. Install HDWalletProvider

npm install truffle-hdwallet-provider

4. Edit truffle.js to set up the truffle-hdwallet-provider and the connection to the Rinkeby network

var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = "orange apple banana ... ";
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
},
rinkeby: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://rinkeby.infura.io/v3/<INFURA_Access_Token>");
},
network_id: 4,
gas: 4500000,
gasPrice: 10000000000,
}
}
};

Provide a reference to your mnemonic that generates your accounts on metamask.

5. Use an ether faucet to fund the transaction to deploy the contract

Make sure you have enough ether in your account to do the deployment. You can acquire ether on the Rinkeby network through a service known as a faucet like the ones mentioned in this link and using the account that you own.

6. Deploy the contract

Create a file on the migrations folder to add instructions to deploy your contract, in this case called MyNotary. Note that the filename needs to have a prefixed with a number and is suffixed by a description, for example 4_example_migration.js. The numbered prefix is required in order to record whether the migration ran successfully. The suffix is purely for human readability and comprehension.

var MyNotary = artifacts.require(“MyNotary”);
module.exports = function(deployer, network, accounts) {
deployer.deploy(MyNotary,{from: accounts[0]});
};

Compile your project, if not already done:

truffle compile

Deploy to the Rinkeby network

truffle migrate --network rinkeby

Troubleshooting

If you are having problems deploying the contract because it takes a long time to deploy or because it consumes a lot of funds please add this to the truffle-config.js:

The number of runs (--optimize-runs) specifies roughly how often each opcode of the deployed code will be executed across the life-time of the contract. This means it is a trade-off parameter between code size (deploy cost) and code execution cost (cost after deployment). A “runs” parameter of “1” will produce short but expensive code. In contrast, a larger “runs” parameter will produce longer but more gas efficient code. The maximum value of the parameter is 2**32-1.

link

Checklist for deploying a contract with truffle

  • You tested all of your functions in your contract locally with truffle and all of the tests pass successfully.
  • You have the Migrations.sol contract.
  • You have a migrations file for Migrations.sol
  • You have a migrations file for your smart contract.
  • You registered in infura.io and created a new project.
  • You have a truffle.js or truffle-config.js file to configure truffle and deployment.
  • You updated the mnemonic in truffle.js.
  • You have enough funds. If you need funds please try the faucets mentioned in this link.

--

--

Responses (5)