Matrix AI Network Contracts — Part 2 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).

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:

and the one in which we deploy the contract

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

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:

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

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:

Then solve the promise:

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).

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