Deploying A Smart Contract To The Aion Network

Follow

Deploying a smart contract to the Aion network

Aion Java API

Assuming that the smart contract is stored in a .sol on the filesystem, the user must take the following steps for deploying a smart contract on the Aion network:

  1. Connect to the Aion Java API.
  2. Unlock the account being used for sending the contract creation transaction.
  3. Load the solidity contract from the filesystem into a variable.
  4. Compile the contract.
  5. Send the contract creation transaction to the Aion network.
  6. Wait for the contract creation transaction to be “mined” or included in at least 1 block.

The following is a Java program that uses the Aion Java API to execute these steps:

IAionAPI api = IAionAPI.inst();
ApiMsg apiMsg = new ApiMsg();
apiMsg.set (api.connect("tcp://127.0.0.1:8547"));
if (apiMsg.error()) {
    System.out.println("Aion api connection could not be established.");
    closeApi();
    // safely close API connection
}

// set up the arguments for unlock account operation
Address account = Address.wrap("cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe");
String password = "password";
int unlockTimeout = 300; 
	
// unlock an account
apiMsg.set(api.getWallet().unlockAccount(account, password, unlockTimeout));
if (apiMsg.error() || !(boolean)apiMsg.getObject()) {
	System.out.println("Unlock account failed! Please check your password  " + apiMsg.getErrString());
	// cleanup() on return
} 
	 
// get the Smart Contract code from file
String sc = null; 
try {
	sc = new String(Files.readAllBytes(Paths.get("/path/to/Ticker.sol")));
} catch (IOException e) {
	e.printStackTrace();
	closeApi();
	// safely close API connection
}
// contract creation:
// this function blocks until the contract creation transaction
// has been included in at least one block on the Blockchain
long energyLimit = 1_000_000L;
long energyPrice = 1L;
apiMsg.set(api.getContractController().createFromSource(sc, account, energyLimit, energyPrice));
if (apiMsg.error()) {
	System.out.println("Deploy contract failed with error: " + apiMsg.getErrString());
	closeApi();
	// safely close API connection
} 

IContract contract = api.getContractController().getContract();

// make sure to save the ABI definition for the contract since an API
// limitation is that in order to create a Contract object, one needs the
// contract address and the ABI definition
String contractAddress = contract.getContractAddress();
String contractAbi = contract.getAbiDefToString();
	
closeApi(): {
		if (api != null)
			api.destroyApi();
}

 

 

More resources...

Interacting with the Smart Contract

There are three types of interactions one can have with Smart Contracts using the Aion Java API:

  1. Contract Creation Transactions This is the transaction which creates the smart contract on the Aion network; this translates to the fact that for any fully-validating node on the network which has a block on its consensus chain that includes this contract creation transaction, then that block has the ability to respond to any contract calls or transactions. The use of this type of transaction with the Aion Java API has been covered in the previous section.

  2. Contract Calls These are reads from the Contract storage of the API-connected node; since these are simple database reads and do not involve any state changes for contract storage and therefore does not involve the consensus process; this information can be retrieved directly from the contract storage database for the smart contract.

  3. Contract Transactions These are state changes to the contract storage and require a consensus process to guarantee that the contract state change is agreed to and is propagated across a majority of nodes on the network.

All interaction with smart contracts happens using the Contract object using the IContract interface. The IContract interface has no public constructors.

Please see the Aion java API documentation for all available overloads.

The Contract object represents a smart contract deployed on-chain. There are two methods of obtaining and using a Contract instance:

  1. At the contract-deploy time, the contract instance returned can be used for sending transactions to that contract. After the IAionAPI connection has been closed or this reference is lost, a new Contract instance must be created pointing to the same Smart Contract

  2. In order to create a new Contract instance, the address of the contract and the ABI definition is required (this is both a limitation and feature, since this allows the Aion Java API to do compile-time checks, allowing programmers to write correct programs without requiring them to test transaction calls against a test Blockchain, just to validate if the transaction, as described in software does not have any errors in its declaration, as is the case in Web3)

The Contract object uses the currying pattern to prepare the Contract for a transaction, by changing its state and returning the object. Calling the .execute() function will send the contract transaction or contract call to the Kernel.

The following example shows how to interact with the Ticker contract:

  1. Assuming that an active connection to the Aion Java API exists, and the known account whose address is stored in the variable String account has been unlocked.
  2. A contract object is created from the known contract address and ABI for the Tickercontract, which we acquired in the last code snippet.
  3. A contract transaction is sent, calling the tick() function.
  4. The contract state change is verified by using the contract call showMeCount()
// open a connection to the Aion API called api
// ...

// unlock account to be used for transactions
// ...

ApiMsg apiMsg = new ApiMsg();
String abiDef = "known ABI definition";
Address contractAddress = Address.wrap("known contract address");

// create a contract object from a known smart contract address
apiMsg.set(api.getContractController().getContractAt(account, contractAddress, abiDef));
    
if (apiMsg.error()) {
	System.out.println("Contract instantiation failed!" + apiMsg.getErrString());
	closeApi();
	// safely close API connection
}

IContract contract = api.getContractController().getContract();
// perform a contract transaction by calling the tick() function:
// by default, the execute() function blocks until this
// transaction has been included in at least one block on the Blockchain
apiMsg.set(contract.newFunction("tick")
      .build()
      .execute());

if (apiMsg.error()) {
	System.out.println("Function execution error! " + apiMsg.getErrString());
	closeApi();
	// safely close API connection
}
	
apiMsg.set(contract.newFunction("showMeCount")
.build()
.execute());
	
if (apiMsg.error()) {
	System.out.println("Function showMeCount error! " + apiMsg.getErrString());
	closeApi();
	// safely close API connection
}

int tickCount = (int) apiMsg.getObject();

// now you can verify that the value of the count variable
// in the Ticker contract has incremented by printing out this value to std.out
System.out.println("Ticker contract count value: " + tickCount);

// more operations
// ...
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.