Deploy your smart contracts on any Ethereum network
Tags: ethereum, web3, solidity, truffle
Published: 2022-01-03 23:59:02

---
title: Deploy your smart contracts on any Ethereum network
published: true
description: Learn how to deploy a smart contract on any Ethereum network using Truffle.
tags: ethereum, web3, solidity, truffle
//cover_image: https://direct_url_to_image.jpg
---
So you finally wrote your smart contract, it works fine, you’ve tested it locally, maybe wrote a cool wep app that goes along with it and want to deploy it live? I got you covered.

In this article, we will se how to use Truffle to deploy any contract on a test network or the main Ethereum network.

## Choosing a network

First let’s enumerate our network options. Ethereum has a few public test network that we can work on, you can find their name and their specificity on the official [Ethereum webiste](https://ethereum.org/en/developers/docs/networks/).
We could also work on the Polygon network which is a Layer 2 network.

## Setup a wallet

Once you’ve decided where to deploy, we need to have a wallet on that network with some funds on it. Deploying a contract, like any other operation on the Ethereum blockchain isn’t free. If we are working on a public test network, you can create a wallet and claim some free Ether on it with [faucets](https://classroom.github.com/a/Iaz_xDSH). I would suggest to use a new wallet that you’ll use only for contract deployment.

## Infura: your way to the blockchain

For the next steps, we will work with Infura. Infura provides a suite of tools for developers to work on the Ethereum blockchain.

It allows you to develop anything on the Ethereum network without having to host or setup any of the complexity that working with a large blockchain come with, hosting Ethereum nodes. Essentially Infura is a Blockchain nodes provider.

To create a infura project, go on the [Infura website](https://infura.io/), create an account. Once done, login and go to your dashboard. There you have the option to create a new project. Select Ethereum in the product option and give it a name (you can change the name latter if you want). Infura will generate an API key and a project secret Key. The API key will be required for the next steps.

## Configure Truffle for your network

First of all, we need a wallet provider. As told before, you need a wallet with a few Ether (or MATIC if you work with the Polygon network) to deploy a contract.

We will use `hdwallet-provider` that does the job well enough.

In a Terminal on the root of your project type:

```
npm install --save truffle-hdwallet-provider
```

For the next step, I’ll assume you’ve setup your project with the command `truffle init`, which implies that you have, on your project’s root, a file named `truffle-config.js`. That is the file we will work on.

Before the module.export statement, import the freshly installed wallet provider :

```jsx
const HDWalletProvider = require('truffle-hdwallet-provider');
```

For now, every time you’ve deployed your contracts, truffle automatically went for the development environment, and has used your local blockchain (probably run with Ganache). We need to add a new network. For now, your file should look like this :

```jsx
module.exports = {
  networks: {
    development: {
        host: "127.0.0.1",
        port: 7545,
        network_id: "*",
    },
	},
	mocha: {
    timeout: 100000
  },
  compilers: {
    solc: {
      version: "0.8.11"
    }
  },
  db: {
    enabled: false
  }
};
```

As you would guess, we will add the network we want to deploy to in the `networks` object, after `development`. For the rest of the post, I will work with the Ropsten network.

So I will add the following under `networks`:

```jsx
ropsten: {
    provider: function() {
      return new HDWalletProvider("YOUR_WALLET_MNEMONIC", "https://ropsten.infura.io/YOUR_API_KEY")
    },
    network_id: 3,
    gas: 4000000      //make sure this gas allocation isn't over 4M, which is the max
}
```

If you are deploying on the main network, name the network “main” instead of “ropsten”, the network id should be `1`. For the gas price, you should be able to get an idea of reasonable price checking out the [Ether Gas Station](https://ethgasstation.info/).

Obviously, replace the mnemonic and the infura URL with the one that goes with the network you are deploying to followed by your project’s API key. You can get the URL from your Infura project setting, there should be a dropdown menu with all the supported networks, just select one and copy past the URL.

**⚠️ Do not commit your mnemonic ⚠️**

Make sure it’s stored in a .env file, or something that is in the .gitignore file. 

You could put your mnemonic on a .secret file, that is listed in the .gitignore and access like :

```jsx
const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();
```

Then you would be good to go! Just run the following :

```jsx
truffle deploy --network <network name>
```

Here I would replace `<network name>` by `ropsten`.

Happy deployments 🙂