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:

private static String NODE_ENDPOINT = "YOUR_NODE_URL"

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:

private static String PRIVATE_KEY = "YOUR_PRIVATE_KEY";

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:

private static final Aion aion = Aion.build(new HttpService(NODE_ENDPOINT));
private static final TransactionManager manager = new AionTransactionManager(
aion, new Ed25519KeyPair(PRIVATE_KEY), VirtualMachine.AVM
);

Deploy

To deploy your contract to an Aion blockchain, call your wrapper’s object along with the deploy function:

final Counter counterContract = Counter.deploy(aion, manager, AionGasProvider.INSTANCE, 1).send();

Make sure the include any deployment arguments that are necessary:

final Counter counterContract = Counter.deploy(aion, manager, AionGasProvider.INSTANCE, "This is a deployment argument").send();

To get the transaction hash and the receipt of your deployment use the following code:

 System.out.println("Tx Hash:"+ counterContract.getTransactionReceipt());
 System.out.println("Contract Address: " + counterContract.getContractAddress());

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.

Integer result = counterContract.call_getCount().send();

You can use the response from the blockchain as a standard variable:

System.out.println("Current count is: " + result);

Contract Transaction

To change a state in the blockchain, you will need an account with sufficient balance to send a transaction to the contract:

TransactionReceipt transactionReceipt = counterContract.send_incrementCounter(1).send();

You can find out if the transaction was successful by checking the transactionReceipt variable.

System.out.println("Success? " + tx.isStatusOK());

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.

final Counter counterContract = Counter.load("Your_Contract_Address", aion, manager, AionGasProvider.INSTANCE);

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:

// Create an ABI Object using the .abi file created from compiling the contract.
let abiObject = web3.avm.contract.Interface(
    `0.0
    org.aion.web3.Counter
    Clinit: ()
    public static void incrementCounter(int)
    public static int getCount()`
);

// Initiate the binding within the web3 object.
web3.avm.contract.initBinding(contractAddress, abiObject, privateKey);

// Calls to the network must be asynchronous.
async function contractTransaction() {

    // Call the incrementCounter while supplying `1` as an argument.
    let response = await web3.avm.contract.transaction.incrementCounter(1);

    // Print the reponse to the network.
    console.log(response);
};

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:

let response = await web3.avm.contract.readOnly.getCount();