Comment on page
Java SDK
The source code is found on GitHub at https://github.com/icon-project/icon-sdk-java
This document describes how to interact with
ICON Network
using Java SDK. This document contains SDK installation, API usage guide, and code examples.Get different types of examples as follows.
Code Example | Description |
An example of creating and loading a keywallet. | |
An example of transferring ICX and confirming the result. | |
An example of deploying an IRC token, transferring the token and confirming the result. | |
An example of checking block confirmation and printing the ICX and token transfer information. |
This document is focused on how to use the SDK properly. For the detailed API specification, see the API reference documentation.
This Java SDK works on the following platforms:
- Android 3.0+ (API 11+)
<dependency>
<groupId>foundation.icon</groupId>
<artifactId>icon-sdk</artifactId>
<version>[x.y.z]</version>
</dependency>
or Gradle:
dependencies {
implementation 'foundation.icon:icon-sdk:[x.y.z]'
}
APIs are called through
IconService
. IconService
can be initialized as follows.// Creates an instance of IconService using the HTTP provider.
IconService iconService = new IconService(new HttpProvider("http://localhost:9000", 3));
The code below shows initializing
IconService
with a custom HTTP client.OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(200, TimeUnit.MILLISECONDS)
.writeTimeout(600, TimeUnit.MILLISECONDS)
.build();
IconService iconService = new IconService(new HttpProvider(okHttpClient, "http://localhost:9000", 3));
All queries are requested by a
Request
object. Query requests can be executed as Synchronized or Asynchronized. Once the request has been executed, the same request object cannot be executed again.Request<Block> request = iconService.getBlock(height);
// Synchronized request execution
try {
Block block = request.execute();
...
} catch (Exception e) {
...
}
// Asynchronized request execution
request.execute(new Callback<Block>(){
@Override
public void onSuccess(Block block) {
...
}
@Override
public void onFailure(Exception exception) {
...
}
});
The querying APIs are as follows.
// Gets the block
Request<Block> request = iconService.getBlock(new BigInteger("1000")); // by height
Request<Block> request = iconService.getBlock(new Bytes("0x000...000"); // by hash
Request<Block> request = iconService.getLastBlock(); // the last block
// Gets the balance of an given account
Request<BigInteger> request = iconService.getBalance(new Address("hx000...1");
// Gets a list of the SCORE API
Request<List<ScoreApi>> request = iconService.getScoreApi(new Address("cx000...1"));
// Gets the total supply of icx
Request<BigInteger> request = iconService.getTotalSupply();
// Gets a transaction matching the given transaction hash
Request<Transaction> request = iconService.getTransaction(new Bytes("0x000...000"));
// Gets the result of the transaction matching the given transaction hash
Request<TransactionResult> request = iconService.getTransactionResult(new Bytes("0x000...000"));
// Calls a SCORE read-only API
Call<BigInteger> call = new Call.Builder()
.from(wallet.getAddress())
.to(scoreAddress)
.method("balanceOf")
.params(params)
.buildWith(BigInteger.class);
Request<BigInteger> request = iconService.call(call);
// Calls without response type
Call<RpcItem> call = new Call.Builder()
.from(wallet.getAddress())
.to(scoreAddress)
.method("balanceOf")
.params(params)
.build();
Request<RpcItem> request = iconService.call(call);
try {
RpcItem rpcItem = request.execute();
BigInteger balance = rpcItem.asInteger();
...
} catch (Exception e) {
...
}
Calling SCORE APIs to change states is requested as sending a transaction.
Before sending a transaction, the transaction should be signed. It can be done using a
Wallet
object.Loading wallets and storing the Keystore
// Generates a wallet.
Wallet wallet = KeyWallet.create();
// Loads a wallet from the private key.
Wallet wallet = KeyWallet.load(new Bytes("0x0000"));