public abstract class ApiBase<ApplyResult> extends java.lang.Object implements Types.ApiBaseInterface<ApplyResult>
Modifier and Type | Class and Description |
---|---|
static class |
ApiBase.ApiType |
Modifier and Type | Field and Description |
---|---|
protected Types.DecoratedRpc<ApplyResult> |
decoratedRpc |
Hash |
genesisHash
Contains the genesis Hash of the attached chain.
|
static int |
KEEPALIVE_INTERVAL |
protected Types.ApiOptions |
options |
RpcCore |
rpcBase |
RuntimeVersion |
runtimeVersion
Contains the version information for the current runtime.
|
Types.Signer |
signer
An external signer which will be used to sign extrinsic when account passed in is not KeyringPair
|
Constructor and Description |
---|
ApiBase(IProvider provider,
ApiBase.ApiType apiType)
Create an instance of the class
|
ApiBase(Types.ApiOptions options,
ApiBase.ApiType apiType) |
Modifier and Type | Method and Description |
---|---|
protected <ApplyResult> |
decorateRpc(RpcCore rpcCore,
Types.OnCallDefinition<ApplyResult> onCall) |
Types.Derive<ApplyResult> |
derive()
Derived results that are injected into the API, allowing for combinations of various query results.
|
void |
disconnect()
Disconnect from the underlying provider, halting all comms
|
protected void |
emit(IProvider.ProviderInterfaceEmitted type,
java.lang.Object... args) |
Hash |
getGenesisHash()
Contains the genesis Hash of the attached chain.
|
RuntimeVersion |
getRuntimeVersion()
Contains the version information for the current runtime.
|
Types.Signer |
getSigner() |
ApiBase.ApiType |
getType()
The type of this API instance, either ‘rxjs’ or ‘promise’
|
boolean |
hasSubscriptions()
true when subscriptions are supported |
EventEmitter |
on(IProvider.ProviderInterfaceEmitted type,
EventEmitter.EventListener handler)
Attach an eventemitter handler to listen to a specific event
|
protected abstract ApplyResult |
onCall(Types.OnCallFunction method,
java.util.List<java.lang.Object> params,
boolean needCallback,
IRpcFunction.SubscribeCallback callback) |
EventEmitter |
once(IProvider.ProviderInterfaceEmitted type,
EventEmitter.EventListener handler)
Attach an one-time eventemitter handler to listen to a specific event
|
Types.QueryableStorage<ApplyResult> |
query()
Contains all the chain state modules and their subsequent methods in the API.
|
Types.DecoratedRpc<ApplyResult> |
rpc()
Contains all the raw rpc sections and their subsequent methods in the API as defined by the jsonrpc interface definitions.
|
Metadata |
runtimeMetadata()
Yields the current attached runtime metadata.
|
void |
setSigner(Types.Signer signer)
Set an external signer which will be used to sign extrinsic when account passed in is not KeyringPair
|
Types.SubmittableExtrinsics |
tx()
Contains all the extrinsic modules and their subsequent methods in the API.
|
public static final int KEEPALIVE_INTERVAL
public Types.Signer signer
An external signer which will be used to sign extrinsic when account passed in is not KeyringPair
public RpcCore rpcBase
protected Types.DecoratedRpc<ApplyResult> decoratedRpc
protected Types.ApiOptions options
public Hash genesisHash
Contains the genesis Hash of the attached chain. Apart from being useful to determine the actual chain, it can also be used to sign immortal transactions.
public RuntimeVersion runtimeVersion
Contains the version information for the current runtime.
public ApiBase(IProvider provider, ApiBase.ApiType apiType)
Create an instance of the class
provider
- the Provider instanceapiType
- the type of the API
Example
import org.polkadot.api.ApiBase;
ApiBase api = new ApiBase();
api.rpc().subscribeNewHead((header) => {
System.out.println("new block ");
System.out.println(header.blockNumber);
});
public ApiBase(Types.ApiOptions options, ApiBase.ApiType apiType)
protected <ApplyResult> Types.DecoratedRpc<ApplyResult> decorateRpc(RpcCore rpcCore, Types.OnCallDefinition<ApplyResult> onCall)
protected void emit(IProvider.ProviderInterfaceEmitted type, java.lang.Object... args)
protected abstract ApplyResult onCall(Types.OnCallFunction method, java.util.List<java.lang.Object> params, boolean needCallback, IRpcFunction.SubscribeCallback callback)
public Types.Derive<ApplyResult> derive()
Derived results that are injected into the API, allowing for combinations of various query results.
Example
api.derive.chain.bestNumber((number) => {
System.out.print("best number ");
System.out.println(number);
});
derive
in interface IApi<ApplyResult>
public Types.QueryableStorage<ApplyResult> query()
Contains all the chain state modules and their subsequent methods in the API. These are attached dynamically from the runtime metadata.
All calls inside the namespace, is denoted by section
.method
and may take an optional query parameter. As an example, api.query.timestamp.now()
(current block timestamp) does not take parameters, while api.query.system.accountNonce(<accountId>)
(retrieving the associated nonce for an account), takes the AccountId
as a parameter.
Example
api.query.balances.freeBalance(<accountId>, (balance) => {
System.out.print("new balance ");
System.out.println(balance);
});
query
in interface IApi<ApplyResult>
public Types.DecoratedRpc<ApplyResult> rpc()
Contains all the raw rpc sections and their subsequent methods in the API as defined by the jsonrpc interface definitions. Unlike the dynamic api.query
and api.tx
sections, these methods are fixed (although extensible with node upgrades) and not determined by the runtime.
RPC endpoints available here allow for the query of chain, node and system information, in addition to providing interfaces for the raw queries of state (usine known keys) and the submission of transactions.
Example
api.rpc.chain.subscribeNewHead((header) => {
System.out.print("new header ");
System.out.println(header);
});
rpc
in interface IApi<ApplyResult>
public Types.SubmittableExtrinsics tx()
Contains all the extrinsic modules and their subsequent methods in the API. It allows for the construction of transactions and the submission thereof. These are attached dynamically from the runtime metadata.
Example
api.tx.balances
.transfer(<recipientId>, <balance>)
.signAndSend(<keyPair>, ({status}) => {
System.out.print("tx status ");
System.out.println(status.asFinalized.toHex());
});
tx
in interface IApi<ApplyResult>
public ApiBase.ApiType getType()
The type of this API instance, either ‘rxjs’ or ‘promise’
getType
in interface Types.ApiBaseInterface<ApplyResult>
public EventEmitter on(IProvider.ProviderInterfaceEmitted type, EventEmitter.EventListener handler)
Attach an eventemitter handler to listen to a specific event
on
in interface Types.ApiBaseInterface<ApplyResult>
type
- The type of event to listen to. Available events are connected
, disconnected
, ready
and error
handler
- The callback to be called when the event fires. Depending on the event type, it could fire with additional arguments. Example
api.on('connected', () => {
System.out.println("API has been connected to the endpoint");
});
api.on('disconnected', () => {
System.out.println("API has been disconnected from the endpoint");
});
public EventEmitter once(IProvider.ProviderInterfaceEmitted type, EventEmitter.EventListener handler)
Attach an one-time eventemitter handler to listen to a specific event
once
in interface Types.ApiBaseInterface<ApplyResult>
type
- The type of event to listen to. Available events are connected
, disconnected
, ready
and error
handler
- The callback to be called when the event fires. Depending on the event type, it could fire with additional arguments. Example
api.once('connected', () => {
System.out.println("API has been connected to the endpoint");
});
api.once('disconnected', () => {
System.out.println("API has been disconnected from the endpoint");
});
public Hash getGenesisHash()
Contains the genesis Hash of the attached chain. Apart from being useful to determine the actual chain, it can also be used to sign immortal transactions.
getGenesisHash
in interface IApi<ApplyResult>
public RuntimeVersion getRuntimeVersion()
Contains the version information for the current runtime.
getRuntimeVersion
in interface IApi<ApplyResult>
public Types.Signer getSigner()
getSigner
in interface IApi<ApplyResult>
public boolean hasSubscriptions()
true
when subscriptions are supported
public Metadata runtimeMetadata()
Yields the current attached runtime metadata. Generally this is only used to construct extrinsics & storage, but is useful for current runtime inspection.
public void setSigner(Types.Signer signer)
Set an external signer which will be used to sign extrinsic when account passed in is not KeyringPair
public void disconnect()
Disconnect from the underlying provider, halting all comms