You can get iwan-java-sdk by two ways:
- By cloning the Java SDK source code and packaging the jar by
maven install
cmd:
git clone https://github.com/wandevs/iwan-java-sdk.git
cd wanchain-java-sdk
maven install
Then there will be a new folder target
in which you can get the sdk named iwanj-*.*.*.jar
.
- By downloading the jar we have packaged already: iwan-java-sdk
Once you get the sdk, you can integrate it into you application.
After installation, the iWan SDK can be used to connect to the iWan RPC server to call a method such as getBalance
. The default config can be used or custom config parameters can be passed using the option
object.
import com.wanchain.iwanj.lib.apis.Wanj;
If you won't config the URL, that the 3rd parameter is set to null
, the SDK will connect to api.wanchain.org:8443
by default:
Wanj javaApi = new Wanj(YourApiKey, YourSecretKey, null);
A different URL can be specified in the option
object which is subject to iWan.
//Subject to https://iwan.wanchain.org
Map<String, String> option = new HashMap<>();
option.put("socketUrl", "wss://apitest.wanchain.org:8443");
option.put("apiFlag", "ws");
option.put("apiVersion", "v3");
Wanj javaApi = new Wanj(YourApiKey, YourSecretKey, option);
Instead of using the iWan SDK for connecting to the iWan RPC server, a raw WebSocket API can also be used, for more information, please see the documentation iWan RPC API. However, we strongly recommend using the iWan SDK.
The 3rd parameter option
is Map<String, String>
type. See below for examples of usage.
option
Map<String, String>- key
socketUrl
is mapping the value of the RPC server URL, default is 'api.wanchain.org:8443'. - key
apiFlag
is mapping the value of the flag to connect the iWan RPC server, default is 'ws'. - key
apiVersion
is mapping the value of the RPC method version, default is 'v3'.
- key
In order to get an ApiKey
, sign up at iWan. Then create a new project to get a new ApiKey
and SecretKey
key pair.
Both send()
and sendAsync()
are supported for each method.
send()
: Api method is called for synchronous mode.sendAsync()
: Api method is called for asynchronous mode.
The method getBalance
is used as an example below to show the use of send()
and sendAsync()
in the iwan-java-SDK :
try {
BigInteger rslt = javaApi.getBalance("WAN", addresses[0]).send();
System.out.println("\n[sync]getBalance : " + rslt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CompletableFuture<BigInteger> rsltFuture = javaApi.getBalance("WAN", addresses[0]).sendAsync();
try {
BigInteger rslt = rsltFuture.get();
System.out.println("\n[async]getBalance : " + rslt);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iWan Java SDK API : API details about iWan Java SDK