How To: Compile a Solidity Smart Contract Using Node.js

Morgan Fogarty
May 11, 2018 · 4 min read

In the previous blog post, we wrote a smart contract using the Solidity programming language.

Next, we need to compile the hello.sol file so that we can test and deploy our smart contract.

The properties we’ll need to use on the compiled file are the Application Binary Interface (ABI) or interface and the bytecode. The bytecode is what will actually go onto the blockchain to make the smart contract work and the interface will be the Javascript layer that acts like a human-friendly map of the bytecode.

Let’s get started!

In the terminal at the root of your project, type:

npm install --save solc

solc is a Solidity compiler.

Next, create a file called compile.js at the root of your project. It should be siblings with your contracts folder. Write the compile.js file, pictured below.

compile.js

We require two Node modules(path and fs) and our Solidity compiler (solc). The reason we can’t simply require our hello.sol file is because Node will try to execute files as Javascript and would throw an error with a .sol file.

The helloPath variable finds the current working directory (__dirname) and navigates to the hello.sol file within it. The source variable is populated by the raw source code of hello.sol which is located in the helloPath. The UTF-8 string represents the encoding of the file.

On line 8, we call compile() on the source code and specify how many contracts we’ll be compiling. In our case, it’s one.

It may help us to see what’s happening under the hood, so let’s change line 8 a bit and log our contracts object to our console.

altered line 8

Type node compile.js into your command line at the root of your project and you should get something like this:

weeeeeeeeeeee!!!!!!!

The result of compile() will always be an object. contracts is the top level object, containing all the contracts we’ve compiled. Again, in our case it’s only one contract. Let’s look closer at the two properties that we really care about: interface and bytecode.

First, the bytecode.

bytecode

This is the code that will be deployed onto the blockchain and executed in the Ethereum Virtual Machine (EVM). As you can see, it’s not human readable. Which brings us to our second important property, the interface. This is the ABI.

Application Binary Interface (ABI)

Looks a little more familiar, right? These are the Javascript methods that we’ll be able to use when testing and deploying our contract.

Once you’ve thoroughly investigated the contracts object, go ahead and press undo a few times to bring line 8 back to it’s ready state.

Ready!

Compile file written!

Tune in next time when we will test the contract code using the Mocha testing framework.

Copy / paste-able compile.js file:

const path = require('path');
const fs = require('fs');
const solc = require('solc');
const helloPath = path.resolve(__dirname, 'contracts', 'hello.sol');
const source = fs.readFileSync(helloPath, 'UTF-8');
module.exports = solc.compile(source, 1).contracts[':Hello'];

Get Best Software Deals Directly In Your Inbox

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Morgan Fogarty

Written by

I’m just a girl, standing in front of some code, asking it to love her.

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store