Categories
Getting started javascript

Compiling and deploying smart contracts in JavaScript and command line

This tutorial covers how to compile and deploy your smart contracts using JavaScript and Solidity comand line compiler solc. All our classes are made for beginners and advanced programmers to understand and build on the Ethereum blockchain. We really advise people to understand simple tools like solc compiler instead of jumping directly with complicated frameworks like Truffle or Embark to start with.

To get started you’ll need to install the Solidity compiler and have a basic JS Ethereum development environement ready.

sudo npm install -g solc

Then create a file to contain the smart contract, for example helloworld.sol: (If you’re getting started with Solidity and smart contracts you can read our getting started tutorial)

pragma solidity ^0.6.6;

contract HelloWorld {
  
   function test() public pure returns (uint) {
    return 1337;
  }

}

to compile the smart contract, just use recently installed solc:

solcjs --bin --abi -o ./build helloworld.sol

This command will generate the binary code for the contract and the ABI in the build folder:

Support EthereumDev.io

We hope you enjoy our free tutorials about ethereum programming and would love to get some help to keep new articles coming and cover server costs.

Support free content

The two files that are generated are:

  • helloworld.abi: Contains a Javascript array that will be used by web3js to define how to interact with the deployed smart contract.
  • helloworld.bin: The bytecode of the contract you can use to deploy the smart contract to the blockchain.

Now let’s deploy the contract using JavaScript, for this we need to load the content of the ABI and the binary code of the contract. You’ll also need to setup a test environement with for example ganache. We’ll then instanciate the smart contract with the selected aby and bytecode and deploy it.

const fs = require('fs');
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');

const bytecode = fs.readFileSync('./build/helloworld_sol_HelloWorld.bin');
const abi = JSON.parse(fs.readFileSync('./build/helloworld_sol_HelloWorld.abi'));

(async function () {
  const ganacheAccounts = await web3.eth.getAccounts();

  const helloWorld = new web3.eth.Contract(abi);

  helloWorld.deploy({
    data: bytecode
  }).send({
    from: ganacheAccounts[0],
  }).then((deployment) => {
    console.log('Contract was deployed at the following address:');
    console.log(deployment.options.address);
  }).catch((err) => {
    console.error(err);
  });
})();

The result of executing this code should be something like this with the address of the new contract:

On our local chain the contract address is: 0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab but yours might be different. Copy the address of the contract as we’ll need it in the next step where we try to call the test function:

const fs = require('fs');
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');

const bytecode = fs.readFileSync('./build/helloworld_sol_HelloWorld.bin');
const abi = JSON.parse(fs.readFileSync('./build/helloworld_sol_HelloWorld.abi'));

(async function () {
    const ganacheAccounts = await web3.eth.getAccounts();

    const helloWorld = new web3.eth.Contract(abi, "0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab");
    helloWorld.methods.test().call().then((result) => {
        console.log("The result is : ", result)
    }).catch((err) => {
        console.error(err);
    });

})();

If everything went smooth the 1337 result should print in your console when you execute the file:


As you can see, compiling and publihing your smart contracts to the blockchain is a really smooth experience that any Solidity developpers should try at least once before deep diving into more complex frameworks that often complicates things that make it hard to learn for beginners.

Leave a Reply