Matrix AI Network Utils — Part 2.1: Sending a MAN transaction(Java + Maven), Intermediate level
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 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):
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):
We are going to get the nonce (which is the number of transactions a specific address have):
Then we are going to get the gas price:
And the balance (so we can print it in console, MAN Denomination):
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:
It should look like this in JSON format:
Now we are going to build the RawTransaction by using the parameters we initialized earlier (also sign it):
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):
convertObjToString function for pretty print:
byteToHex function used for RawTransaction:
After running the program, the transaction will get broadcasted via the provider node we used.
Also, check out the official Matrix AI Network Developer Portal: