Deploy your smart contracts to a public testnet

A quick guide for deploying your smart contracts to a public testnet

wissal haji
Nerd For Tech
5 min readMar 15, 2021

--

Photo by Andrey Larin on Unsplash

Welcome to another article in ethereum programming basics. In the previous articles we have seen the process and the basic tools to develop a decentralized application, and in today’s article we will see how to deploy a dapp in a public testnet, I will also give you some resources you can use to strenghten your skills as an ethereum developer.

What is Infura

The main goal of Infura is to provide instant access to the Ethereum and IPFS networks without all the hassle of setting up your own Ethereum or IPFS node.

Infura is powered by a cutting-edge microservice-driven architecture that dynamically scales to support our APIs. Developers can connect to Ethereum and IPFS via HTTPS and WebSocket, where request response times are up to 20 times faster than other services and self-hosted solutions. Our API suite always has the latest network updates and maintains service availability during all network changes. — From Infura product description.

Deploy to kovan testnet

In this section I will show you the steps needed in order to deploy your smart contracts to the kovan testnet.
You can find a detailed comparison between different Ethereum testnets here in Ethereum stackexchange.

  1. Create a project in Infura: In order to use the infura API you need to create an account and a new project since you will need the project Id in order to be able to send requests to the API (More details in this link).
  2. Get the mnemonic of your accounts in MetaMask: If you havn’t saved your accounts mnemonic you can go to MetaMask settings, select security and privacy then click on “reveal seed phrase” and enter your password to reveal the mnemonic.
  3. Create a file to keep the project secrets like the mnemonic and the infura project id private: In the root of your truffle project create a json file named .secrets.json and add the following content:
{
"mnemonic": <your mnemonic>,
"projectId": <your project Id>
}

4. Install HDWalletProvider : In order to deploy the smart contract we will have to send a transaction with the contract bytecode to the network, and in order to be able to sign that transaction we need the hdwallet-provider package from the truffle framework: npm install @truffle/hdwallet-provider

5. Update the truffle-config.js file: Start by importing the dependencies and parsing the .secrets.json file.(More details on configuration can be found in truffle docs).

const HDWalletProvider = require("@truffle/hdwallet-provider");const fs = require("fs");
const secrets = JSON.parse(fs.readFileSync(".secrets.json").toString().trim());

In the networks section add a new network as follows:

kovan: {
networkCheckTimeout: 10000,
provider: () => {
return new HDWalletProvider(
secrets.mnemonic,
`wss://kovan.infura.io/ws/v3/${secrets.projectId}`
);
},
network_id: "42",
},

Now you can go to your terminal and deploy your project to the Kovan network using :

truffle migrate --network kovan

What’s next

Now that you have the basics to write smart contracts, and develop your own dapps, you’re ready to go to the next level.

In this section I will give you some resources that you can use to enhance your skills as an ethereum developer:

Books:

Courses:

Other resources:

Some tips to remember

  • Enable the optimizer when compiling smart contracts for a production environment to optimize the generated bytecode.
  • If a function needs to transfer ether to a recipient, the transfer operation should be the last thing to be executed by this function, in case the recipient is a smart contract with a malicious payable fallback (reentrancy attack). If there are some other operations that needs to be executed after the transfer, think about using ReentrancyGuard from openzeppelin.(see this link)
  • Do not use ether transfer inside a control structure like loops or an if/else statement.( same as the previous point, in case of a malicious fallback, if the atacker can’t steal ether he can perform some DOS attack).
  • Prefer using the withdraw pattern for transfering ether to avoid reentrancy attack.
  • Do not use unbounded loops inside a transaction, this can leed to a transaction that can never make it to the blockchain due to a gas cost that exceeds the block gas limit.(see this link)
  • If a contract is not supposed to receive ether, do not make any logic based on the contract balance using address(this).balance since ether can be sent forcibly to a contract using selfdestrct(recipient) .
  • Use SafeMath library from openzeppelin to avoid integer overflow and underflow.
  • If you need to restrict access to funtions based on users roles, you can use AccessControl contract from the openzeppelin library.
  • Do not use tx.origin for authorization.
  • Always check if the function’s parameters are valid with a require statement before using them.

Conclusion

This was the last article in this series that introduces the basics of ethereum programming. If you feel like you need more advanced topics, here are some to start with: Oracles, Gasless transactions.
You might want also take a look at some tools from github:
scaffold-eth
solidity-template

Previous articles:
Learn Solidity: Introduction
Learn Solidity: Variables part 1
Learn Solidity: Variables part 2
Learn Solidity: Variables part 3
Learn Solidity: Functions
Learn Solidity: Smart contract creation and inheritance
Learn Solidity: The factory pattern
Build your first dapp with web3.js
Learn Solidity: Events
Dapp Front-end: Drizzle Store

--

--