Making a Node.js App to Interact With a Smart Contract the Git Way | Hacker Noon

@BassaganasJordi Bassagañas

Hi there! How are you today? I blog about technology, the Internet, SEO, programming tips, and more.

Hey everybody, this is the second post of the series on writing apps the Git way, where you just go take a cup of coffee and browse a Git repo to learn something new!

Be sure to take it step by step, if you’re a new kid on the block.

Today we’re going to write a Node.js app for the purpose to interact with our shiny new first Solidity smart contract deployed on the Ropsten testnet already — which remember, basically outputs the

Hello world!

string.

The code is available at programarivm/solidity-interacting-with-nodejs.

Here is the git log for you to get the application up and running.

$ git log --oneline
45e59bd (HEAD -> master) app.js is implemented
80195ad npm install web3 dotenv
97a0d34 .gitignore is added
fdd0e78 npm init

As you can see in commit

45e59bd

, our

app.js

is really really simple.

require('dotenv').config() ;

const Web3 = require('web3');

const infuraKey = process.env.INFURA_KEY;

const web3 = new Web3(new Web3.providers.HttpProvider( `https://ropsten.infura.io/v3/${infuraKey}`));

const helloWorld = new web3.eth.Contract([
  {
    "constant": true,
    "inputs": [],
    "name": "output",
    "outputs": [
      {
        "internalType": "string",
        "name": "",
        "type": "string"
      }
    ],
    "payable": false,
    "stateMutability": "pure",
    "type": "function"
  }
], '0x35c113E1AB11B3001e9085CBafD224FfC3470C67');

helloWorld.methods.output().call({from: '0x8863ae48646c493efF8cd54f9Ffb8Be89669E62A'}, function(error, result) {
    console.log(result);
});

This is how to output the message:

$ node app.js
Hello world!

There are just two things to be noted. In the first place we’re using

web3

, the Ethereum JavaScript API, for

app.js

to interact with the smart contract.

const Web3 = require('web3');

Secondly, the JSON representation of the contract’s application binary interface (ABI) is passed as a parameter when creating a new contract object.

const helloWorld = new web3.eth.Contract([
  {
    "constant": true,
    "inputs": [],
    "name": "output",
    "outputs": [
      {
        "internalType": "string",
        "name": "",
        "type": "string"
      }
    ],
    "payable": false,
    "stateMutability": "pure",
    "type": "function"
  }
], '0x35c113E1AB11B3001e9085CBafD224FfC3470C67');

Given the fact that the smart contract’s

output()

method is quite basic, meaning its state is not being altered, the simplest possible way to run the method for learning purposes is probably through web3’s

call()

function.

helloWorld.methods.output().call({from: '0x8863ae48646c493efF8cd54f9Ffb8Be89669E62A'}, function(error, result) {
  console.log(result);
});

That’s it! In future posts, I’ll show you a little bit of error handling.

You may also be interested in…

Read my stories

Hi there! How are you today? I blog about technology, the Internet, SEO, programming tips, and more.

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!

read original article here