Examples
Web3J allows you to deploy, call, and send transactions to a contract straight from the framework itself. This article walks you through each of those workflows.
These examples assume you have already created a wrapper for your application. See the end-to-end walkthrough for more details.
Connect to a Node
To link your Java application to an Aion Node, use the following code:
Define an Account
To set an account that you wish to use in your Java application, supply the private key for the account you want to use:
Initialize the Aion and TransactionManager Objects
Before you can interact or deploy anything to the Aion network, you need to create an aion
object and a manager
object using the Aion
and TransactionManager
types respectivly:
Deploy
To deploy your contract to an Aion blockchain, call your wrapper’s object along with the deploy
function:
Make sure the include any deployment arguments that are necessary:
To get the transaction hash and the receipt of your deployment use the following code:
Contract Call
Simple calls to the network do not change the state of the blockchain, they simply ask for a particular variable or value. Because of this, they are substantially simpler than Transaction Calls which do change the blockchain.
You can use the response from the blockchain as a standard variable:
Contract Transaction
To change a state in the blockchain, you will need an account with sufficient balance to send a transaction to the contract:
You can find out if the transaction was successful by checking the transactionReceipt
variable.
Load
To load a contract into your Java application that has already been deployed, call your wrapper object followed by the load
function. You must include the contract address as the first argument in the load
function.
Binding
ABI binding is a feature available for the Web3.js framework. It allows you to call your deployed contract without you having to write any complicated functions or calls. An ABI definition would look something like this:
If you wanted to just return a value from the network and not change any data, then you would need to add the readOnly
only method to the contract call. For example, the getCount
function just returns the current count
value, and doesn’t require any funds to run: