Web3J
In this walkthrough, we're going to create a simple Java contract and compile it. We're then going to run the compiled contract through the Web3J wrapping tool which will give us a Java class we can import into a standard Java application. Finally, we'll build a simple Java application and import the wrapped Java contract and have it interact with the blockchain.
We’ll be using IntelliJ and the Aion4J plugin.
Prerequisities
This project assumes that you’re already familiar with IntelliJ and how to create Aion contracts using the Aion4j plugin. To find out more on how to do this, check out the IntelliJ section. You will also need to following software installed.
- Java 10 or above.
- IntelliJ
Create the Contract
The contract we’re going to create is a simple getter-setter application. The user can either view the value of a String variable or change that variable.
- Open IntelliJ and create a new Maven project using the latest AVM archetype.
- Set the
GroupIdfield toaionexampleand theArtifactIdfield togettersetter. - Click Next through the rest project creation window and click Finish.
- Click Run Initialize in the pop-up at the bottom right, or right-click within your project and select Aion Virtual Machine > Run Intialize.
- To keep things simple we won’t be using tests in this project. So within the
srcfolder of your new project, delete thetestfolder. - Within the
src/main/java/aionexamplefolder, right-click onHelloAvm, select Refactor → Rename, and renameHelloAvmtoGetterSetter. Depending on your IntelliJ setup, IntelliJ might rename all the instances of
HelloAvmwithin the class toGetterSetter. If it doesn’t however, do this manually. Make sure to set thecontract.main.classfield within yourpom.xmlfile to<contract.main.class>aionexample.GetterSetter</contract.main.class>You should now have the basis of your Java contract project. If you want, you can delete the
greet()andsayHello()functions within yourGetterSetterclass since we won’t be using them. But there’s no harm in leaving them in there. You can also copy and paste the contract below:Compile your contract by right-clicking on the
gettersetterfolder in the navigation panel and selecting Aion Virtual Machine → Embedded → Deploy.You should now have a
gettersetter-1.0-SNAPSHOT.abiandgettersetter-1.0-SNAPSHOT.jarfiles within your projectstargetfolder. Copy them to somewhere handy like your desktop. These are the files we’re going to wrap within the Web3J packages.You can close this project now: File → Close Project.
Wrap the Contract
For a standard Java application to interact with your Java contract, you need to wrap the contract within the Web3J wrapper. While the process is the same for any Java contract, the output is different. A wrapper for one Java contract will not work for any other Java contract.
Download the Aion Web3J package from GitHub:
Move into the new folder:
Generate the binary distribution using Gradlew:
You might see a warning about
Deprecated Gradle features. You can safely ignore this for now. It will be fixed in a future release.Move into the distributions folder:
Extract the
web3j-aion-0.1.0-SNAPSHOT.zipfile:Move into
binfolder:Web3J now needs the
jarandabifiles we made in IntelliJ. Copy your.jarand.abifiles into this folder:Generate your Aion contract wrapper:
The
-o ~/Desktopdirectory in this command is the location where you wrapper will be saved. To keep thing simple we’ve told the script to save it to the desktop. You can now find yourGetterSetter.javawrapper in the~/Desktop/gettersetter/folder.For future reference, the following arguments are available for the
web3j-aionscript:Flag Required Description -a,--abiFiletrueABI file in AVM format with a contract definition. -b,--binFilefalseBIN or JAR file with the contract compiled code in order to generate deploy methods. -o,--outputDirtrueDestination base directory. -p,--packagetrueBase package name. -t,--targetVmfalseTarget Aion virtual machine (AVM by default). We also need to create a
shadowJarfile. Back in the root of the Web3J repository run:Copy this new
.jarinto thegettersetterfolder on your desktop:
We now have the two files we need to include within our Java project. Next up is creating the Java project to house everything.
Set Up your Application and Import the Wrapper
In this step, we’re going to create an incredibly simple Java application that prints out the myStr variable within our contract. This application will also deploy our contract for us.
- Create a new Java project in IntelliJ called
GetTheString. - Within your projects root folder create a new directory called
lib. - Within this new
libfolder, paste theweb3j-aion-avm-0.1.0-SNAPSHOT-all.jarfile we created and saved into~/Desktop/gettersetterin the previous step. - Copy the
GetterSetter.javafile from within~/Desktop/gettersetterinto thesrcfolder. Within the
srcfolder, create a new Java class calledGetTheString. Your project folder should look something like this now:
Lastly, we need to tell IntelliJ that we want to use the lib folder as this project library location.
- Go to File > Project Structure.
- Select Libraries from the left panel.
- Click the
+icon and select Java. - In the window that opens, go into the
libfolder within yourGetTheStringproject folder. - Select the
web3j-aion-avm-0.1.0-SNAPSHOT-all.jarfile and click Open. - Click OK on the confirmation window. Then click Apply and OK within the Project Structure window.
Now we have the framework to start interacting with the blockchain. Next up, we’re going to deploy our contract and interact with it using the Aion test network.
Write, Deploy, and Interact
First up we need to tell our Java application to deploy our contract to the Aion test network. To do this you’ll need an Aion node on the Amity testnet, and an account with sufficient balance to deploy and call a contract. You can use Nodesmith to connect to a node, and the Aion Testnet Faucet to get some free test tokens.
Write
Now that you’ve got those two details, we’re ready to start writing our Java application.
- Open the
GetTheStringfile. At the very top of the file add the following lines to import the packages we need:
Within the
public class GetTheStringclass definiton add these two lines, filling in your information:Create an
Aionobject by adding this line:This sets up the endpoint to talk to an Aion network.
Create
TranasactionManagerobject calledmanager:This sets up the account for signing and sending the transactions later.
Create a
main()class that will house all our further code:
Deploy
We can now get to deploying your contract. Since we’ve already set up the scaffolding in the rest of this class, all we need to do is call one function.
Call the
.deployfunction within yourGetterSetterobject:You can also request the transaction receipt and contract address once your Java contract has been deployed:
Note: Remember to check the status of the of the transaction as well. A contract address will be returned even the deployment fails.
You should now be able to run your application. Click Run > Run… from the title bar.
You may get an error about JDK7 types. You can safely ignore this. It can take up to 30 seconds to deploy your contract. Once it’s deployed you should be able to see the transaction hash and contract address:
Interact
So now that we’re able to deploy our contract, we should be able to interact with it. There are two functions within our contract getString and setString. Let’s start by calling the getString method and checking the response.
Create a variable called
resultof typeString, and have it set to the response of thegetStringmethod:Note: We are using
call_getStringinstead ofsend_getStringhere is becausegetString()is a constant function, we are only getting information from the blockchain and not change any state.Print out the
resultvariable:Run your project again to see the results.
Next, let’s try setting the string. Since we’re going to change the state of the blockchain this action will use funds from your account, so make sure there is enough AION in there to facilitate the request.
Create a variable called
transactionReceiptof typeTransactionReceipt, and have it set to the response of thesetStringmethod. Add the string you want to set themyStrvariable to as an argument:Print out whether or not the transaction was successful and the string was set:
Call the
getStringmethod again using the same code from earlier:Run your project again to see the results.
And there you have it! You’ve successfully written, deployed, and interacted with a Java contract on the Aion test network using Web3J! Check out the official Web3J documentation for more information on what you can do with the framework.