MATRIX
Search
K

Sending a MAN transaction(Java + Maven), 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).
Text

Description automatically generated
I’m under no circumstances a Java/Maven professional (or an Java/Maven 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) using Java + Maven.
We will make use of the following dependencies (+ GSON):
Text

Description automatically generated
They have to be added in the pom.xml file(you can find more info about it on https://maven.apache.org/guides/introduction/introduction-to-the-pom.html, you can find those dependencies on maven repository https://mvnrepository.com/search?q=io.github.matrixainetwork)
Let’s start coding:
I will initialize a provider first (I will pass in a public node of Matrix for use):
After that, we are going to start by declaring some of the variables we are going to use for the transaction, we will need the following, the variable names should be self-explanatory: privateKey, addressWeSendFrom, addressWeSendTo, gasLimit, value (which is the amount we are going to send), chainId, unixTime (not mandatory):
Text

Description automatically generated
We are going to get the nonce (which is the number of transactions a specific address have):
Text

Description automatically generated
Then we are going to get the gas price:
Text

Description automatically generated
And the balance (so we can print it in console, MAN Denomination):
Text

Description automatically generated
Then we are going to create a Credentials object and initialize it with our private key:
Now we are going to make a list that has the extra_to field format:
Text

Description automatically generated
It should look like this in JSON format:
Diagram

Description automatically generated
Now we are going to build the RawTransaction by using the parameters we initialized earlier (also sign it):
Text

Description automatically generated
After the Raw transaction is built, we can send it and print the hash/pretty-print the transaction object in the console (I’m going to pretty print by using the GSON library, https://mvnrepository.com/artifact/com.google.code.gson/gson):
Text

Description automatically generated
convertObjToString function for pretty print:
Text

Description automatically generated
byteToHex function used for RawTransaction:
Text

Description automatically generated
Sample output:
Text

Description automatically generated
After running the program, the transaction will get broadcasted via the provider node we used.
Github code:
Also, check out the official Matrix AI Network Developer Portal: