Photo by Lorenzo Herrera on Unsplash

Writing, Compiling, Deploying Solidity 2021.

aster27
Coinmonks
Published in
3 min readApr 18, 2021

--

In this bleeding edge technology of blockchain, it is very easy to feel lost even as a learner. The developer toolkit in the Ethereum space also evolves at a fast pace. So this tutorial with show you how to write basic solidity program, compile it locally, and deploy to a test network like Rinkeby with the latest versions.

lets get started 🌠

  • you will need to install some dependencies via npm. so let’s get them first
  • make a new directory and cd into it. then run
npm init -y //to start a new project
npm i solc web3 @truffle/hdwallet-provider //to install dependencies

The Contract

brief explanation

  • define which version of solidity to use using pragma
  • the contract defines a variable x
  • a constructor which is called when the contract is instantiated, just like a constructor in other languages
  • functions to set a new value of x and get the value of x

Compiling the script

lets run this piece of code first to see what it does

node compile.js //the compile file can be named anything

brief explanation

  • we bring in the solc compiler
  • then path and fs module. why you ask? the path modules to get the absolute path to the contract file (irrespective of OS) and the fs to read the file so we can store it in a variable, source in this case.
  • then we have to pass it to do compiler but wait, it expects a certain format so we create the input variable for that and pass it the source as you see
  • its all done at this point, we feed it to the compiler, by stringifying it.
  • take your time to view the console log, even expand it further if you like.
  • why export it like that? nice question. remember we still have to deploy it, and the answers lies there.

Deploying the script

great, we’ll deploy to the test network rinkeby, for a simplicity of this tutorial, I’ll assume you have metamask installed ( if not, its pretty straightforward ) and that, you have some test ether in your metamask on the rinkeby network, or the test network you wish to deploy on ( you can get some from https://faucet.rinkeby.io/ or corresponding faucets)

NOTE: there’s a lot to unpack in this script, i’ll try to be short and simple about it

NOTE: the 12 word mnemonic phrase is confidential, revealing or losing it means your account could get compromised

brief explanation

HDWalletProvider — deploying costs ether, so this package uses

  • your 12 word mnemonic to get the accounts and sign transactions so you don’t do it manually.
  • the second argument is the infura api, whats infura? the blockchain network consists of nodes [computers] storing all the data and running non stop, so to deploy onto it, you will need to have the entire copy of blockchain yourself, which means setting up your own node which is not very practical is it? so basically infura provides us their api which connects us to the blockchain directly. so a lot of hassle in avoided using infura’s api.

Web3js — this library allows us talk with the nodes in an easy way, in javascript. it requires a provider which we pass it.

  • remember the library provides us a constructor so its capital “W” Web3, then we instantiate it with “w” web3

abi, bytecode — these two things are necessary for us to deploy, this is what the compiler spit out, the abi contains the functionalities of your contract in a json format, the bytecode is your contract in low level machine code

the deploy() function — there’s not much to explain here but some things to notice

  • we pass in the abi and bytecode
  • arguments array is for our constructor, remember it required an argument _x , in case our constructor didn’t require any arguments we wouldn’t pass the arguments array then
  • console log on “contract” after its deployed contains information like our contract methods and importantly the address its deployed to.

You will get the address of the contract and you can search it in the https://rinkeby.etherscan.io/

Congratulations you’ve learnt quite a bit in this reading, atleast I hope so. tada!!

--

--