Contract to Contract
Java smart contracts are able to interact with other Java contracts on the Aion network. This page details how that is achieved.
Contract-to-contract calls still follow the same rules as regular contract calls. See Java Contract Fundamentals section for more information.
Example Contracts
You can use these contracts as templates. The Caller
contract makes a call to the Returner
contract.
Returner
This is the contract that will be called by the Caller
contract. If you want to test these contracts out, deploy this one first to get the contract address.
Caller
Make sure to supply the address of the contract you want to call as a deployment argument. Supplying the contract address as a hardcoded variable within a contract is more expensive and less efficient​.
Blockchain Call
To call a method in another Java contract, you can use Blockchain.call() method, and pass in the target contract address
, value
to transfer, data
to pass and the max energy
the invoked contract can use.
To get the right data
you want to pass, you will need an ABI StreamingEncoder.
Use it to encode the method
name as a String
first and then the arguments
corresponding to their types in order.
Now you get everything you need to make a contract call.
If you are expecting some return data, you must create a Result
object.
AVM Result
The AVM Result
object represents a cross-call invocation result. To create a Result
instance for the contract method call:
Then, you must create an ABIDecoder object and use Result.getReturnData()
to get the data returned by the invoked call. See AVM ABIDecoder section for more details.
Once this has been created, you can decode the return data
according to the return data type.