public class ApiRx extends ApiBase<Observable>
ApiRx is a powerfull RxJS Observable wrapper around the RPC and interfaces on the Polkadot network. As a full Observable API, all interface calls return RxJS Observables, including the static .create(...)
. In the same fashion and subscription-based methods return long-running Observables that update with the latest values.
The API is well suited to real-time applications where the latest state is needed, unlocking the subscription-based features of Polkadot (and Substrate) clients. Some familiarity with RxJS is a requirement to use the API, however just understanding .subscribe
and .pipe
on Observables will unlock full-scale use thereof.
Making rpc calls -
import org.polkadot.api.rx.ApiRx;
// initialise via Promise & static create
ApiRx api = ApiRx.create().toPromise();
// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHead().subscribe((header) => {
System.out.print("Chain is at ");
System.out.println(header.blockNumber);
});
Subscribing to chain state -
import org.polkadot.api.rx.ApiRx;
// initialise a provider with a specific endpoint
WsProvider provider = new WsProvider('wss://example.com:9944')
// initialise via isReady & new with specific provider
new ApiRx(provider)
.isReady
.pipe(
switchMap((api) =>
combineLatest([
api.query.timestamp.blockPeriod(),
api.query.timestamp.now().pipe(pairwise())
])
)
)
.subscribe(([blockPeriod, timestamp]) => {
int elapsed = timestamp[1] - timestamp[0];
System.out.printf("timestamp %d \nelapsed %d \n(%d target)", timestamp[1], elapsed, blockPeriod);
});
Submitting a transaction -
import org.polkadot.api.rx.ApiRx;
const keyring = testingPairs();
// get api via Promise ApiRx api = ApiRx.create().toPromise();
// retrieve nonce for the account api.query.system .accountNonce(keyring.alice.address()) .pipe( first(), // pipe nonce into transfer switchMap((nonce) => api.tx.balances // create transfer .transfer(keyring.bob.address(), 12345) // sign the transcation .sign(keyring.alice, { nonce }) // send the transaction .send() ) ) // subscribe to overall result .subscribe(({ status }) => { if (status.isFinalized) { System.out.print(“Completed at block hash ”); System.out.println(status.asFinalized.toHex()); } }); ```
ApiBase.ApiType
decoratedRpc, genesisHash, KEEPALIVE_INTERVAL, options, rpcBase, runtimeVersion, signer
Modifier and Type | Method and Description |
---|---|
static <any> |
create(IProvider provider)
Creates an ApiRx instance using the supplied provider.
|
protected Observable |
onCall(Types.OnCallFunction method,
java.util.List<java.lang.Object> params,
boolean needCallback,
IRpcFunction.SubscribeCallback callback) |
decorateRpc, derive, disconnect, emit, getGenesisHash, getRuntimeVersion, getSigner, getType, hasSubscriptions, on, once, query, rpc, runtimeMetadata, setSigner, tx
public static <any> create(IProvider provider)
Creates an ApiRx instance using the supplied provider. Returns an Observable containing the actual Api instance.
provider
- provider that is passed to the class contructor
Example
import org.polkadot.api.rx.ApiRx;
ApiRx.create()
.pipe(
switchMap((api) =>
api.rpc.chain.subscribeNewHead()
))
.subscribe((header) => {
System.out.print("new block ");
System.out.println(header.blockNumber);
});
protected Observable onCall(Types.OnCallFunction method, java.util.List<java.lang.Object> params, boolean needCallback, IRpcFunction.SubscribeCallback callback)