Deploying Smart Contract on Test/Main Network Using Truffle - GeeksforGeeks

14 Oct, 2020
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.
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, 
Go to https://metamask.io/ Download it to your browser.
Go to https://metamask.io/ 
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.
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. 
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


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:
To deploy on the main network, get infura endpoint for the main network, and configure the config file.Replace rinkeby with main, and network id with 1.Then truffle migrate –network main.
To deploy on the main network, get infura endpoint for the main network, and configure the config file.
Replace rinkeby with main, and network id with 1.
Then truffle migrate –network main.
Solidity
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
What is Decentralized Voting Application (DApps)?
Solidity - Break and Continue Statements
Creating Ownable Contracts in Solidity
Solidity - Libraries
Solidity - Programming ERC-20 Token
Solidity - Assembly
Solidity - Basics of Contracts
Build a To-do List Web Application Powered by Blockchain
Solidity - Decision Making Statements
What is Escrow Smart Contract?