Matrix AI Network Utils — Part 2.1: Sending a MAN transaction(JS, NodeJS), Intermediate level

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 send a MAN transaction (Mainnet).

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

You can install them by using :

npm install aiman matrixjs-tx bignumber.js

then declare them:

Let’s start programming!

First off, we are going to start by declaring some of the tx variables we are going to use, we will need the following, the names of the variables should be self-explanatory: privateKey, addressWeSendFrom, addressWeSendTo, gasLimit, gasPrice, amountWeSend, chainId

The gasPrice should be around that value for the network at the moment, in case it ever changes, you can always get a rough estimate by calling the getGasPrice().

The amount we send can be denominated in the following ways:

If we would like to send 1 MAN, for example, we can change amountWeSend to man3.toWei(‘1’,’man’)

Now we are going to create a Promise that will get the address nonce:

And another promise that creates the raw transaction we are going to use:

Now we can prepare our raw tx, it has the following format, we are going to change the nonce of it once we resolve the getNonce() promise:

After changing the nonce of the raw tx we call the sendRawTransaction Promise then display the tx hash if the transaction got sent successfully or the error message if for some reason, the transaction could not be sent/ there was some error when creating the transaction.

Here we have the getTxParams function, if currency parameter is not provided, it is going to use MAN as default, it converts the transaction parameters to hex (and makes it the right format) and adds the v, r, s values, which are the ECDSA signatures:


Another utils we used:

Sample output:

Whole program (https://github.com/CognitiveProcessing/Matrix-AI-Network-Code-Snippets/blob/main/js/sendManTransaction.js)







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

https://www.npmjs.com/package/bignumber.js

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

Also, check out the official Matrix AI Network Developer Portal:

dev.matrix.io