# Calling a smart contract function (JS, NodeJS)

**by Delbypoc**

\ <br>

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]\(<https://www.matrix.io/gitbook/Guides/Guides_17.fld/image001.png>)

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 call a smart contract function on MATRIX AI NETWORK.

This is a continuation of the last guide:

[**https://medium.com/@delbypoc/deploying-a-smart-contract-on-matrix-ai-network-using-truffle-demo-part-1-cff8714add2**](https://medium.com/@delbypoc/deploying-a-smart-contract-on-matrix-ai-network-using-truffle-demo-part-1-cff8714add2)

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

You can install them by using :

npm install aiman matrixjs-tx bignumber.js bs58 fs

and declare them:

!\[Text

Description automatically generated]\(<https://www.matrix.io/gitbook/Guides/Guides_17.fld/image002.png>)

For testing, I will use the smart contract from the last guide:

!\[Text

Description automatically generated]\(<https://www.matrix.io/gitbook/Guides/Guides_17.fld/image003.png>)

It contains one function that changes the result value and a view function.

After running Truffle migrate (from the last guide), some files will also get generated under the build/contracts folder, SimpleContract.json

I will read it using fs (it will be required to get the contract ABI)

!\[Text

Description automatically generated]\(<https://www.matrix.io/gitbook/Guides/Guides_17.fld/image004.png>)

Now I will initialize the man3 instance with a provider:

![](https://www.matrix.io/gitbook/Guides/Guides_17.fld/image005.png)

And other variables that will be used in building the tx:

!\[Text

Description automatically generated]\(<https://www.matrix.io/gitbook/Guides/Guides_17.fld/image006.png>)

Next, I will declare and implement a function that will prepare the tx and broadcast it:

!\[Text

Description automatically generated]\(<https://www.matrix.io/gitbook/Guides/Guides_17.fld/image007.png>)

&#x20;

!\[Text

Description automatically generated]\(<https://www.matrix.io/gitbook/Guides/Guides_17.fld/image008.png>)

The program should output the tx that got broadcasted (blurred out a few details):

!\[Graphical user interface, text

Description automatically generated]\(<https://www.matrix.io/gitbook/Guides/Guides_17.fld/image009.png>)

After solving the promise (and tx get mined), we should have the “result” variable of the smart contract changed to 220.

We can also check the input parameters by decoding the transaction “input” (I will use [**https://lab.miguelmota.com/ethereum-input-data-decoder/example/**](https://lab.miguelmota.com/ethereum-input-data-decoder/example/) for this)

!\[Graphical user interface, application

Description automatically generated]\(<https://www.matrix.io/gitbook/Guides/Guides_17.fld/image010.png>)

The whole program source code can be found at the github page: (Matrix-AI-Network-Code-Snippets/callingSmartContractFunction.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/bignumber.js**](https://www.npmjs.com/package/bignumber.js)

[**https://www.npmjs.com/package/buffer**](https://www.npmjs.com/package/buffer)

[**https://www.npmjs.com/package/bs58**](https://www.npmjs.com/package/bs58)

[**https://www.npmjs.com/package/fs**](https://www.npmjs.com/package/fs)

Also, check out the official Matrix AI Network Developer Portal: [**dev.matrix.io**](https://www.matrix.io/blog/dev.matrix.io)

&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.matrix.io/guides/calling-a-smart-contract-function-js-nodejs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
