org.polkadot.rpc.core.README.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of polkadot-java Show documentation
Show all versions of polkadot-java Show documentation
Java Polkadot API, this is a clone of https://github.com/polkadot-java/api
The newest version!
# @polkadot/rpc-core
This library provides a clean wrapper around all the methods exposed by a Polkadot network client.
## Usage
Installation -
```
yarn add @polkadot/rpc-core
```
Initialisation -
```java
import org.polkadot.rpc.core.RpcCore;
import org.polkadot.rpc.provider.ws.WsProvider;
const provider = new WsProvider('http://127.0.0.1:9944');
const api = new RpcCore(provider);
```
Retrieving the block header object for a given block header hash (a 0x-prefixed hex string with length of 64) -
```java
api.chain
.getHeader("0x1234567890")
.then((header) => System.out.println(header))
.catch((error) => System.out.println(error));
```
Retrieving the best block number, parent hash, state root hash, extrinsics root hash, and digest (once-off) -
```java
api.chain
.getHead()
.then((headerHash) => {
return api.chain.getHeader(headerHash);
})
.then((header) => {
System.out.print("best ");
System.out.println(header.blockNumber.toString());
System.out.print("parentHash: ");
System.out.println(header.parentHash.toString());
System.out.print("stateRoot: ");
System.out.println(header.stateRoot.toString());
System.out.print("extrinsicsRoot: ");
System.out.println(header.extrinsicsRoot.toString());
System.out.print("digest: ");
System.out.println(header.digest.toString());
})
.catch((error) => {
System.out.println(error);
});
```
Retrieving best header via subscription -
```java
api.chain
.subscribeNewHead((header) => {
System.out.print("best ");
System.out.println(header.blockNumber);
})
.then((subscriptionId) => {
System.out.print("subscriptionId: ");
System.out.println(subscriptionId);
// id for the subscription, can unsubscribe via
// api.chain.subscribeNewHead.unsubscribe(subscriptionId);
})
.catch((error) => {
System.out.println(error);
});
```