* Add jest watch option to package.json * convert promise-alex-spec to ts & add types * convert ./api tests from JS to TS * convert ./api tests from JS to TS pt. 2 * More convert tests from JS to TS * rpc-provider convert tests from js to ts * rpc-provider tests js => ts * rpc-provider tests js => ts * linting issues * tslint fixes * 101 => 68 typescript errors * WIP * connect() e2e tests * more fixes * Fix linting errors of new ERA tests after merge * test for listeners in connect * formatting * getting there... * re-add skips * test refactoring & codeclimate fixes * more fixed tests * mock.body workaround * Fixed wrong test description * ApiPromiseInterface => Api * ws polyfill / send * bring back isZero() test * .then().catch() => .catch() * Removed leftover body * Type ProviderInterface => Http * Fixed polyfill tests * trailing whitespce * (mock.body as any)['test_body'] and Balance * Fix autoconnect in WsProvider * Fixed part of linting errors in ERA tests * call unused const unsubscribe * added ts-ignores for testing private properties/ methods of Provider * Archive node test * Fixed linting for ERA tests * Further changes * Update packages/rpc-provider/src/ws/polyfill.spec.ts Co-Authored-By: Amaury Martiny <amaury.martiny@protonmail.com> * relative paths for imports inside module & reordering of imports pt. 1 * relative paths for imports inside module & reordering of imports pt. 2 * added skip back to e2e transactions * Fix some more tests in TS (#952) * Fix some more tests in ts * Add more constructor * More typing in ./api * .skip * <SubmittableResult>'s * .catch() -> return() * Extrinsics tests * Left TODO's for extrinsic e2e tests * Workaround for failing Type Extrinsic tests
@plugnet/api
The Plugnet-JS API provides easy-to-use wrappers around JSONRPC calls that flow from an application to a node. It handles all the encoding and decoding or parameters, provides access to RPC functions and allows for the query of chain state and the submission of transactions.
The API wrappers provide a standard interface for use -
- A static
.create(<optional ApiOptions>)that returns an API instance when connected, decorated and ready-to use. ApiOptions can include an optional WsProvider and optional custom type definitions{ provider: <Optional WsProvider>, types: <Optional RegistryTypes> }. - The above is just a wrapper for
new Api(<optional ApiOptions>), exposing theisReadygetter api.rpc.<section>.<method>provides access to actual RPC calls, be it for queries, submission or retrieving chain informationapi.query.<section>.<method>provides access to chain state queries. These are dynamically populated based on what the runtime providesapi.tx.<section>.<method>provides the ability to create a transaction, like chain state, this list is populated from a runtime query
API Selection
There are two flavours of the API provided, one allowing a standard interface via JavaScript Promises and the second provides an Observable wrapper using RxJS. Depending on your use-case and familiarity, you can choose either (or even both) for your application.
- ApiPromise All interface calls returns Promises, including the static
.create(...). Additionally any subscription method uses(value) => {}callbacks, returning the value as the subscription is updated. - ApiRx All interface calls return RxJS Observables, including the static
.create(...). In the same fashion subscription-based methods return long-running Observables that update with the latest values.
Dynamic by default
Substrate (upon which Plugnet is built) uses on-chain WASM runtimes, allowing for upgradability. Each runtime defining the actual chain extrinsics (submitted transactions and block intrinsics) as well as available entries in the chain state. Due to this, the API endpoints for queries and transactions are dynamically populated from the running chain.
Due to this dynamic nature, this API departs from traditional APIs which only has fixed endpoints, driving use by what is available by the runtime. As a start, this generic nature has a learning curve, although the provided documentation, examples and linked documentation tries to make that experience as seamless as possible.
Installation & import
Installation -
npm install --save @plugnet/api
Subscribing to blocks via Promise-based API -
import { ApiPromise } from '@plugnet/api';
// initialise via static create
const api = await ApiPromise.create();
// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHead((header) => {
console.log(`Chain is at #${header.blockNumber}`);
});
Subscribing to blocks via RxJS-based API -
import { ApiRx } from '@plugnet/api';
// initialise via static create
const api = await ApiRx.create().toPromise();
// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHead().subscribe((header) => {
console.log(`Chain is at #${header.blockNumber}`);
});
Registering custom types
Additional types used by runtime modules can be added when a new instance of the API is created. This is necessary if the runtime modules use types which are not available in the base Substrate runtime.
import { ApiPromise } from '@plugnet/api';
// initialise via static create and register custom types
const api = await ApiPromise.create({
types: {
CustomTypesExample: {
"id": "u32",
"data": "Vec<u8>",
"deposit": "Balance",
"owner": "AccountId",
"application_expiry": "Moment",
"whitelisted": "bool",
"challenge_id": "u32"
}
}
});