Deploying Smart Contract on Test/Main Network Using Truffle

A Smart Contract is a computer program that directly and automatically controls the transfer of digital assets between the parties under certain conditions. A smart contract works in the same way as a traditional contract while also automatically enforcing the contract. Smart contracts are programs that execute exactly as they are set up(coded, programmed) by their creators. Just like a traditional contract is enforceable by law, smart contracts are enforceable by code.

Up to this point, we have our smart contracts written and compiled without any errors. Let’s see how we can deploy our smart contract on rinkeby test network. Deploying a contract on the main blockchain costs money (in the form of cryptocurrency), so we have test networks to develop and test our smart contracts. In the test network, fake ethers are added to our wallet to facilitate transactions. One such network provided by Ethereum is Rinkeby.

Installing MetaMask

MetaMask provides browser support to communicate with blockchain networks. We can add an account, get ethers to our account or send transactions to another account. It is like a wallet from where we can spend our cryptocurrency. To install metamask, 

  1. Go to https://metamask.io/ 
  2. Download it to your browser.

On starting for the first time, to set up your wallet MetaMask gives you a seed phrase of 12 words. This phrase is unique and can be used to restore your account, in case you forgot your password.



Getting Test Ethers

Deploying contracts on the test network requires some test ethers, which, obviously does not have any real value. In the blockchain, every interaction with a contract costs some fees(gas) and the interaction is known as a transaction. Since deploying our contract on rinkeby is also a transaction, we need some test ethers to facilitate the transaction. To get test ethers we will be using a Faucet.  Go to https://faucet.metamask.io/ and do the simple task asked to get some free test ethers. 

Creating Infura Endpoint 

We need an API through which we can access the rinkeby network. Infura makes it quite easier to access the test/main network and deploy our contract on them. Create an infura endpoint on the rinkeby network. 

  • Go to infura.io and sign up for an account if you don’t have one.
  • Click on Ethereum on the left panel.
  • Write your project name and click to create the project.
  • This should create an endpoint for your project and it should look somewhat similar to below.

Now we need an npm package HDWalletProvider, this signs our transaction and makes the deployment process a less pain. 

npm install @truffle/hdwallet-provider

Configuring truffle-config.js And Deploying To Rinkeby

Now we have, our truffle hdWallet-provider in our node modules, an endpoint from infura account, and the seed-phrase which we have used during MetaMask installation.

Let’s configure our truffle-config.js.  Open truffle-config.js and import truffle hdWallet provider at the top.

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

To configure the network, we will edit the network section of truffle-config.js. This should look somewhat like this.

const HDWalletProvider = require('truffle-hdwallet-provider');    // Useful for deploying to a public network.
    // NB: It's important to wrap the provider as a function.
    // ropsten: {
      // provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`),
      // network_id: 3,       // Ropsten's id
      // gas: 5500000,        // Ropsten has a lower block limit than mainnet
      // confirmations: 2,    // # of confs to wait between deployments. (default: 0)
      // timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      // skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    // },
  • Replace ropsten with rinkeby.
  • Mnemonic with seed-phrase (obtained from MetaMask).
  • https://ropsten.infura.io/v3/YOUR-PROJECT-ID with the infura endpoint you created for your project.
  • network_id:4,

After doing these changes your section should look something like this.

 rinkeby: {
       provider: () => new HDWalletProvider(`YOUR_SEED_PHRASE`, `https://rinkeby.infura.io/v3/b63bffeec2e545f2a3e9b3e9423d6180`),
       network_id: 4,       // Ropsten's id
       gas: 5500000,        // Ropsten has a lower block limit than mainnet
       confirmations: 2,    // # of confs to wait between deployments. (default: 0)
       timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
       skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
     },

Save the file and then open the truffle console and then migrate on the rinkeby network.

truffle migrate --network rinkeby

That’s all done. Once your contract is deployed on rinkeby, you can check the transaction detail on etherscan.  Go to the link and copy and paste your deployed contract address to get the details about your transaction.

Note:

  1. To deploy on the main network, get infura endpoint for the main network, and configure the config file.
  2. Replace rinkeby with main, and network id with 1.
  3. Then truffle migrate –network main.
My Personal Notes arrow_drop_up

Im a student who loves exploring into new technologies and adding them into my skill set My core interest is in cyber security and I am currently practicing bug bounty hunting As my side skills, I am learning machine learning algorithms and implementing data structures along with some daily life

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.