Javascript Custom RPC
Frequency Custom RPC and Types for Polkadot JS API
An easy way to get all the custom rpc and types config to be able to easily use Frequency with the Polkadot JS API library with TypeScript.
Getting Started
npm install @frequency-chain/api-augment
(API Augmentation Library)npm install @polkadot/api
(Polkadot API Library)
Usage
For details on use, see the Polkadot API library documentation.
import { options } from "@frequency-chain/api-augment";
import { ApiPromise } from '@polkadot/api';
const api = await ApiPromise.create({
...options,
});
Contributing
See CONTRIBUTING.md for more information.
Updating Definitions
- Update
./definitions
- Build and start the chain (does not need to be on-boarded)
- Run
npm run fetch:local
- Run
npm run build
- Commit changes
Helpful Notes
Fails to Resolve Custom RPCs
The api augmentation declares the modules used by @polkadot/api
.
Thus the import for @frequency-chain/api-augment
must come before any @polkadot/api
so that the Frequency declarations resolve first.
import { options } from "@frequency-chain/api-augment";
import "@frequency-chain/api-augment";
import { ApiPromise } from '@polkadot/api';
Caches can also wreck this even if you reorder, so watch out.
- Yarn cache can sometimes cause issues (if you are using yarn):
yarn cache clear
- Sometimes I have found blowing away the
node_modules
helps as well: rm -Rf node_modules
Option
Optional responses are not mapped to null
and instead return an object with a few properties.
For more details see the code for the Option class.
const optionalExample = await api.rpc.msa.getMsaId(account);
if (!optionalExample.isEmpty) {
return optionalExample.value;
}
return null;
Vec
Vector responses are not mapped directly to a JavaScript Array.
Instead they are mapped to the Vec class which does extend Array.
Thus, you can still use map
, forEach
, etc... with responses or access the values directing via .values()
.
License
Distributed under the Apache 2.0 License. See LICENSE
for more information.