Executing and Accessing Smart Contract Without Truffle

Niharika Singh
Coinmonks

--

I know that Truffle has become a standard to work around with smart contracts but practicing everything fundamentally and transparently is what makes everything more lucid.

In this tutorial, we will write a simple smart contract in Solidity and make calls to it directly via Web3. Of course, this is going to be longer than the Truffle method but, it is going to be worthwhile.

I hope you like it.

So let’s get started.

Setting up the environment

  1. Any text editor (Vim maybe haha)
  2. NodeJS (latest version) — https://nodejs.org/en/
  3. Ganache to simulate blockchain in one click! — https://truffleframework.com/ganache
  4. Will to learn

Step 1: Writing a smart contract

Since this tutorial is not about solidity per se, we will just write a basic smart contract.

Open your text editor and create a new file named Add.sol.

pragma solidity ^0.4.18;contract Add {uint num1;uint num2;uint num3;function Sum (uint _num1, uint _num2) public {num3 = _num1 + _num2;}function getSum() public view returns (uint){return num3;}}

This program takes two numbers, adds them, and returns the sum.

Step 2: Launch Ganache

Make sure the RPC Server is http://127.0.0.1:7545.

Step 3: Add Web3 and Solc package using NPM

Open your terminal and install the Web3 and Solc packages using the command —

$ npm install web3 solc --save

⚠️ Make sure you add these modules in the same folder as your smart contract.

Step 4: Using NodeJS console to interact with the contract

To enter the NodeJS console, write node in the terminal.

You should see something like this —

Step 4.1: Load Web3 library in the console.

> Web3 = require ('web3')

Step 4.2: Create an HTTP Provider (should be same as Ganache)

> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))

This command will ensure that we have connected to Ganache.

This will generate a long output.

Step 4.3: Load all 10 Ganache addresses using Web3

Since we have connected to our Ganache local blockchain on port 7545, let’s verify that the addresses are correctly loaded.

> web3.eth.accounts
These ten addresses should be the same as in Ganache
Ganache

Step 4.4: Load in Solidity compiler (Solc)

> solc = require ('solc')

Step 4.5: Load Add.sol source code using built-in Node module (FS)

> sourceCode = fs.readFileSync('Add.sol').toString()

Step 4.6: Compile the code using Solc

> compiledCode = solc.compile(sourceCode)

This will generate a lot of stuff, but only the following three are essential:

  • Bytecode
  • Metadata
  • Opcode
  • Interface

Each of these is linked to a fundamental Ethereum Virtual Machine (EVM) concept. https://ethereum.github.io/yellowpaper/paper.pdf

Step 4.7: Identifying smart contract ABI

> contractABI = JSON.parse(compiledCode.contracts[':Add'].interface)

Application Binary Interface (ABI) is required to access the bytecode of the contract. It contains a list of all functions and arguments in JSON format. Which is what we will leverage to access our contract.

> addContract = web3.eth.contract(contractABI)

This will generate a long output.

Step 4.8: Identify bytecode

> byteCode = compiledCode.contracts[':Add'].bytecode

Step 4.9: Deploy contract on Ganache

> addDeployed = addContract.new({data: byteCode, from: web3.eth.accounts[0], gas: 4700000})

Using the first Ganache account with 4700000 wei gas, we will deploy the bytecode.

⚠️ Keep in mind that writing anything to the blockchain will take some fee. In this step, we are writing the smart contract to the blockchain.

This will give us a transaction hash, and you can view this transaction on Ganache on ‘Transactions’ tab.

Step 4.10: Create an instance to use the contract

> app = addContract.at(addDeployed.address)

addDeployed.address is the address on the blockchain where the contract is deployed.

Step 4.11: Let’s add 4 and 8 using our contract.

> app.Sum(4, 8, {from: web3.eth.accounts[0]})

Since this Sum function writes to the blockchain, we have to pay some gas which is paid by the first account in Ganache since we used accounts[0].

Contract call is visible on Ganache.

To get the sum, let’s invoke the getSum function.

Since JavaScript is unable to read uint256, we convert the result toString() .

Since getSum function does not write anything to the blockchain, it will not result into any transaction.

So this is how we use the JavaScript console to interact with the smart contract.

I hope you found this informational. If so, please appreciate by applauding the article. :)

Also, Read

Get Best Software Deals Directly In Your Inbox

--

--