How To: Compile a Solidity Smart Contract Using Node.js
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.
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.
Type node compile.js
into your command line at the root of your project and you should get something like this:
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
.
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.
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.
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'];
Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News
Also, Read
- Copy Trading | Crypto Tax Software
- Grid Trading | Crypto Hardware Wallet
- Crypto Telegram Signals | Crypto Trading Bot
- Best Online Casinos | Futures Trading Bots
- Decentralized Exchanges | Bitbns FIP
- 10 Best Places to Buy Crypto with Credit Card
- Best Crypto Trading bots in Canada | Bybit vs Binance
- 5 Best Crypto Exchanges in UAE | SimpleSwap Review
- Best Crypto Exchange | Best Crypto Exchange in India
- Best Crypto APIs for Developers
- Best Crypto Lending Platform
- An ultimate guide to Leveraged Token