public class ApiPromise extends ApiBase<Promise>
ApiPromise is a standard JavaScript wrapper around the RPC and interfaces on the Polkadot network. As a full Promise-based, all interface calls return Promises, including the static .create(...)
. Subscription calls utilise (value) => {}
callbacks to pass through the latest values.
The API is well suited to real-time applications where either the single-shot state is needed or use is to be made of the subscription-based features of Polkadot (and Substrate) clients.
Making rpc calls -
import org.polkadot.api.promise.ApiPromise;
// initialise via static create
ApiPromise api = ApiPromise.create();
// make a subscription to the network head
api.rpc.chain.subscribeNewHead((header) => {
System.out.print("Chain is at ");
System.out.println(header.blockNumber);
});
Subscribing to chain state -
import org.polkadot.api.promise.ApiPromise;
// initialise a provider with a specific endpoint
WsProvider provider = new WsProvider("wss://example.com:9944")
// initialise via isReady & new with specific provider
ApiPromise api = new ApiPromise(provider).isReady;
// retrieve the block target time
int blockPeriod = api.query.timestamp.blockPeriod().toNumber();
int last = 0;
// subscribe to the current block timestamp, updates automatically (callback provided)
api.query.timestamp.now((timestamp) => {
int elapsed = 0;
if(last > 0) elapsed = timestamp - last;
last = timestamp.toNumber();
System.out.printf("timestamp %d %d since last %d target", timestamp, elapsed, blockPeriod);
});
Submitting a transaction -
import org.polkadot.api.promise.ApiPromise;
ApiPromise.create().then((api) => {
int nonce = api.query.system.accountNonce(keyring.alice.address());
api.tx.balances
// create transfer
.transfer(keyring.bob.address(), 12345)
// sign the transcation
.sign(keyring.alice, { nonce })
// send the transaction (optional status callback)
.send((status) => {
System.out.print("current status ");
System.out.println(status.type);
})
// retrieve the submitted extrinsic hash
.then((hash) => {
System.out.print("submitted with hash ");
System.out.println(hash);
});
});
ApiBase.ApiType
decoratedRpc, genesisHash, KEEPALIVE_INTERVAL, options, rpcBase, runtimeVersion, signer
Modifier and Type | Method and Description |
---|---|
static <any> |
create() |
static <any> |
create(IProvider iProvider)
Creates an ApiPromise instance using the supplied provider.
|
ApiBase.ApiType |
getType()
The type of this API instance, either ‘rxjs’ or ‘promise’
|
protected Promise |
onCall(Types.OnCallFunction method,
java.util.List<java.lang.Object> params,
boolean needCallback,
IRpcFunction.SubscribeCallback callback) |
decorateRpc, derive, disconnect, emit, getGenesisHash, getRuntimeVersion, getSigner, hasSubscriptions, on, once, query, rpc, runtimeMetadata, setSigner, tx
public static <any> create(IProvider iProvider)
Creates an ApiPromise instance using the supplied provider. Returns an Promise containing the actual Api instance.
provider
- provider that is passed to the class contructor. Example
import org.polkadot.api.promise.ApiPromise;
Api.create().then((api) => {
int timestamp = await api.query.timestamp.now();
System.out.print("lastest block timestamp ");
System.out.println(timestamp);
});
public static <any> create()
public ApiBase.ApiType getType()
ApiBase
The type of this API instance, either ‘rxjs’ or ‘promise’
getType
in interface Types.ApiBaseInterface<Promise>
getType
in class ApiBase<Promise>
protected Promise onCall(Types.OnCallFunction method, java.util.List<java.lang.Object> params, boolean needCallback, IRpcFunction.SubscribeCallback callback)