Matrix AI Network Contracts — Part 2 Reading a smart contract function (JS, NodeJS)

By: CognitiveProcessing

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:

https://matrixainetwork.medium.com/matrix-ai-network-contracts-part-1-calling-a-smart-contract-function-js-nodejs-919caead471d

and the one in which we deploy the contract

https://matrixainetwork.medium.com/deploying-a-smart-contract-on-matrix-ai-network-using-truffle-demo-part-1-68999ad2022a

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

Matrix AI Network leverages the latest AI technology to deliver on the promise of blockchain.

FOLLOW MATRIX:

Website | GitHub | Twitter | YouTube

Telegram (Official) | Telegram (Masternodes)

Owen Tao (CEO) | Steve Deng (Chief AI Scientist) | Eric Choy (CMTO)

Matrix AI Network leverages the latest AI technology to deliver on the promise of blockchain. Website: www.matrix.io Telegram: t.me/matrixainetwork