Reading a smart contract function (JS, NodeJS)

by Delbypoc

This tutorial is for educational purposes only, I’m not responsible for any losses, damages, or other liabilities which may occur by using any info/code provided here(or anywhere else it is posted).

Text

Description automatically generated

I’m under no circumstances a Javascript professional (or a JS Ninja/Expert)!

The code is not production-ready! (only for learning purposes)

In this guide, I’m going to show you how to read a smart contract function on MATRIX AI NETWORK.

This is also a continuation of the last guide:

Graphical user interface, text, application, email

Description automatically generated

and the one in which we deploy the contract

Graphical user interface, text, application, email

Description automatically generated

I will use the following solidity smart contract for this example:

Text

Description automatically generated

We will make use of the following packages : ‘aiman’, ‘matrixjs-tx’, ‘bignumber.js’, ‘bs58’, ‘fs’.

You can install them by using :

npm install aiman fs

and declare them:

Text

Description automatically generated

Now I will initialize ‘aiman’ with a provider, read the contract ABI and put it in a variable, also declare the smart contract address:

Text

Description automatically generated

In the next function I will initialize a contract instance with the ABI, make it use the contract address and call the “get” method of the smart contract:

Text

Description automatically generated

Then solve the promise:

Text

Description automatically generated

The program will output the value contained within the “result” variable of the smart contract (the function got called with 152,58 as input parameters last time).

Logo

Description automatically generated

The whole program source code can be found on the Github page: (Matrix-AI-Network-Code-Snippets/readingSmartContractFunction.js at main · CognitiveProcessing/Matrix-AI-Network-Code-Snippets (github.com))

For more documentation on the packages used, you can check:

https://www.npmjs.com/package/fs

https://www.npmjs.com/package/aiman

Also, check out the official Matrix AI Network Developer Portal: dev.matrix.io

Last updated