[Docs] Add info on how to add custom types to API (#672)

* Add info on how to add custom types to API

* Fixed typo instanmce -> instance

* Use <ApiOptions> instead of <WsProvider>

* fixed wording `wsProvider` -> `provider`
This commit is contained in:
Stefanie Doll
2019-02-13 15:52:36 +01:00
committed by GitHub
parent 79df0047c7
commit 9bfaf726fe
+25 -2
View File
@@ -4,8 +4,8 @@ The Polkadot-JS API provides easy-to-use wrappers around JSONRPC calls that flow
The API wrappers provide a standard interface for use -
- A static `.create(<optional WsProvider>)` that returns an API instance when connected, decorated and ready-to use
- The above is just a wrapper for `new Api(<optional WsProvider>) `, exposing the `isReady` getter
- 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 the `isReady` getter
- `api.rpc.<section>.<method>` provides access to actual RPC calls, be it for queries, submission or retrieving chain information
- [RPC (node interface)](../METHODS_RPC.md)
- `api.query.<section>.<method>` provides access to chain state queries. These are dynamically populated based on what the runtime provides
@@ -62,6 +62,29 @@ api.rpc.chain.subscribeNewHead().subscribe((header) => {
});
```
## 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.
```javascript
import { ApiPromise } from '@polkadot/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"
}
}
});
```
## Users
Some of the users of the API (let us know if you are missing from the list), include -