Ethereum — Smart Contracts Compile and Deploy — Part 1

Munish Kohli
4 min readDec 8, 2018

--

Using Node.Js to Compile Smart Contracts

In my previous article, I went over how the truffle framework can be a one-stop-shop for compiling and deploying a smart contract (SC).

In this article, I will show that you don’t necessarily have to use truffle because you can also write your own framework in Node.Js which will accomplish the same task. This way, writing your own script will give insight into what is exactly happening under the hood. Let’s get started — below is the development life cycle of a SC.

Compiler produces two main objects, ByteCode and ABI, as in the above diagram.

Why compile a SC? Why not deploy it directly on Ethereum network? Because the Ethereum network runs SC within EVM (Ethereum Virtual Machine). EVM only understand Bytecode, which the compiler produces.

How to interact with SC? Interaction with SC can be through a browser application or any other java script program — that’s where ABI comes in. To interact with variables, functions or other parts of SC, ABI is the route to take. Let me show you.

Create a project. Remember we created project in Truffle using Truffle init. You can create your own folders.

1. Create project root folder <CompileSCUsingJavasScript> or any other name

2. Create a folder “contracts” under <CompileSCUsingJavasScript>

3. We will compile through solidity compiler. Install solidity compiler through npm install — save solc

Solc in above step 3 stands for solidity compiler.

4. Copy MyFirstContract.sol from first article into contracts folder

5. Create an empty compile.js file (or any other file name of your choice) under project folder <CompileSCUsingJavasScript>

6. **I will not go into the Node.js tutorial here. I am assuming that you are familiar with JavaScript, which I am using in the below script. The script is pretty much self-explanatory.

Below path,fs constants are standard modules in Node.js. They are being set up to provide access to Solidity SC file

const path = require(‘path’);

const fs = require(‘fs’);

//You already installed solc compiler in Step 3

const solc = require(‘solc’);

const myContractPath = path.resolve(__dirname, ‘contracts’,’MyFirstContract.sol’);

const sourceCode = fs.readFileSync(myContractPath,’utf8');

console.log(solc.compile(sourceCode,1));

//contracts[‘:MyFirstContract’] in below will provide us only //Bytecode and Interface (ABI). Both will be used when we will //deploy and test our contract

module.exports = solc.compile(sourceCode,1).contracts[‘:MyFirstContract’];

In the above script, Module.exports will ensure all code of compile.js will be available in other files. When command node compile.js is ran on root folder of project, console.log spits out multiple objects.

First object at the very top is contracts suffixed by : Within contracts you will notice another object :MyFirstContract. Go to 2nd line below :MyFirstContract you will see a bytecode: (partially circled in red) and next to it is a long hexadecimal number. This bytecode gets stored on Ethereum blockchain and it’s a runtime version of smart contract.

Go further below you will see interface: object. This interface is ABI (partially circled in blue) which is a communication layer between solidity and JavaScript world.

Byte Code and ABI

In the above (Top of this article) flow diagram beneath Byte Code, I have drawn 3 boxes — Rinkeby, Ropsten and Live Ethereum.

If you remember, in the previous article truffle deployed SC in the local blockchain environment. This local blockchain was just local to your machine. Rinkeby and Ropsten are full fledge Ethereum networks except they are test one. These networks allow deployment and run transactions without you spending real Ether. Local blockchain had inbuilt accounts with fake Ethers. Accounts in test networks needs to be funded with fake Ethers.

In next few articles, I will show how to deploy bytecode to Rinkeby and fund accounts. Once SC is tested in test networks, the next step is to deploy in Live Ethereum which is for hosting real word applications. Now, there is no need for fake ethers in live networks.

Clap and leave comments in case you find this article to be useful!

In every article I publish, I try to provide link to articles from other authors which I found them interesting and useful. Here is the one 30 Things you can do with a BlockChain. Enjoy

--

--

Munish Kohli

Stock Market Enthusiast | Technology & Data Explorer | Exploring life's journey while navigating unexpected twists and turns | Tackle one challenge at a time!