Setting up truffle for ropsten and other testnet | DApp World

Published on 27 Jun 2021

Setting up truffle for ropsten and other testnet

Deploy solidity smart contracts on ropsten, rinkeby, kovan , etc testnet

Install truffle

You should have nodeJS installed then install truffle with this command:

npm install -g truffle

 

Get Sample truffle project to edit further

If you want to set-up from scratch you can directly use truffle init command otherwise get sample project and edit it further.

to get simple project use this command :

truffle unbox metacoin

Now you will get some project folders called contracts, migrations, test and truffle-config file.  change your contract files and content , also do the respective changes in migrations file also.

 

Install HDWalletprovider

We will need a wallet provider to deploy contract. Install with following command:

npm install --save truffle-hdwallet-provider

 

Get RPC provider

Along with wallet provider we will also need RPC provider. The most popular RPC provider is Infura.

 

Get Wallet and accounts

The most popular ethereum wallet is Metamask.

 

Setting up Truffle Config

Sample Truffle-config:

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*" // Match any network id
    }
  },
  compilers: {
    solc: {
      version: "^0.8.0"
    }
  }
};

 

For Ropsten Testnet:

const HDWalletProvider = require("@truffle/hdwallet-provider") 

const mnemonic = 'your_mnemonic'
const rpc_url = 'https://ropsten.infura.io/v3/your_api_key'

module.exports = {
    networks: {
        ganache: {
          host: "localhost",
          port: 7545,
          network_id: "*",
        },
        ropsten: {
          provider: function() {
            return new HDWalletProvider(mnemonic, rpc_url);
          },
          network_id: '3',
        }
    } , 
    compilers : {
      solc :{
        version : '^0.4.23'
      }
    }
};

 

thats it. then use the command for compile and deploy your contract. you can also set the compiler version for solidity.

 

truffle compile
truffle deploy --network ropsten // replace ropsten with your network name as defined in truffle-confiig file

 

Here is netword id’s for different testnets :

Testnet

ChainID/NetworkID

Mainnet

1

Ropsten

3

Rinkeby

4

Kovan

42

Goerli

5

Also don’t forget to use RPC url’s for respective networks you gt from infura..