Package org.polkadot.api.promise
Class ApiPromise
- java.lang.Object
-
- org.polkadot.api.ApiBase<com.onehilltech.promises.Promise>
-
- org.polkadot.api.promise.ApiPromise
-
- All Implemented Interfaces:
Types.ApiBaseInterface<com.onehilltech.promises.Promise>,IApi<com.onehilltech.promises.Promise>
public class ApiPromise extends ApiBase<com.onehilltech.promises.Promise>
# @polkadot/api/promise ## Overview 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. ## Usage Making rpc calls - ```java 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 - ```java 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 - ```java 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); }); }); ```
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.polkadot.api.ApiBase
ApiBase.ApiType, ApiBase.StorageOnCallFunction
-
-
Field Summary
-
Fields inherited from class org.polkadot.api.ApiBase
decoratedRpc, genesisHash, KEEPALIVE_INTERVAL, options, rpcBase, rpcRx, runtimeVersion, signer
-
-
Method Summary
Modifier and Type Method Description static com.onehilltech.promises.Promise<ApiPromise>create()static com.onehilltech.promises.Promise<ApiPromise>create(IProvider iProvider)Creates an ApiPromise instance using the supplied provider.ApiBase.ApiTypegetType()The type of this API instance, either 'rxjs' or 'promise'protected com.onehilltech.promises.PromiseonCall(Types.OnCallFunction method, java.util.List<java.lang.Object> params, boolean needCallback, IRpcFunction.SubscribeCallback callback)-
Methods inherited from class org.polkadot.api.ApiBase
decorateRpc, derive, disconnect, emit, getGenesisHash, getRuntimeVersion, getSigner, hasSubscriptions, on, once, query, rpc, runtimeMetadata, setSigner, tx
-
-
-
-
Method Detail
-
create
public static com.onehilltech.promises.Promise<ApiPromise> create(IProvider iProvider)
Creates an ApiPromise instance using the supplied provider. Returns an Promise containing the actual Api instance.- Parameters:
provider- provider that is passed to the class contructor. **Example** ```java 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); }); ```
-
create
public static com.onehilltech.promises.Promise<ApiPromise> create()
-
getType
public ApiBase.ApiType getType()
Description copied from class:ApiBaseThe type of this API instance, either 'rxjs' or 'promise'- Specified by:
getTypein interfaceTypes.ApiBaseInterface<com.onehilltech.promises.Promise>- Overrides:
getTypein classApiBase<com.onehilltech.promises.Promise>
-
onCall
protected com.onehilltech.promises.Promise onCall(Types.OnCallFunction method, java.util.List<java.lang.Object> params, boolean needCallback, IRpcFunction.SubscribeCallback callback)
-
-