polkadot-typed-api
v1.4.5
Published
The universal typed wrapper for polkadot ApiPromise.
Downloads
111
Maintainers
Readme
Polkadot Typed Api
The universal typed wrapper for polkadot ApiPromise.
General idea
Use this
import { ApiPromise, WsProvider } from "@polkadot/api";
import { api } from "polkadot-typed-api";
const connecton = await ApiPromise.create({
provider: new WsProvider(/*provider url*/),
});
const validators = await api.query.staking.validators(connecton);
Instead of this
import { ApiPromise, WsProvider } from "@polkadot/api";
const connecton = await ApiPromise.create({
provider: new WsProvider(/*provider url*/),
});
const validators = await connecton.query.staking.validators();
Because
await api.query.staking.validators(connecton); // returns typed data
type Staking_Validators_Json = {
commission: number;
blocked: boolean;
};
await connecton.query.staking.validators(); // Returns Codec and you should guess what to do with that!
Installation
npm i polkadot-typed-api
Usage
Import
import { api } from "polkadot-typed-api";
Typescript
import { ApiPromise, WsProvider } from "@polkadot/api";
import { api } from "polkadot-typed-api";
const connecton = await ApiPromise.create({
provider: new WsProvider(/*provider url*/),
});
const data = api["query" || "rpc" || "tx"][palletName][methodName](connection, ...params);
Typed Pallets:
| Pallet | Fully Typed | Tests Coverage | |---------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------:|:--------------:| | api.query.nominationPools | - | 5% | | api.query.proxy | - | 5% | | api.query.system | + | 100% ✅ | | api.query.timestamp | + | 100% ✅ | | api.query.session | - | 10% | | api.query.staking | - | 5% | | api.rpc.system | - | 1% | | api.tx.balances | + | 0% | | api.tx.nominationPools | + | 0% | | api.call.authorityDiscoveryApi | + | 100% ✅ | | api.call.accountNonceApi | + | 100% ✅ |
Types usage
import type { Staking_Ledger_Json } from "polkadot-typed-api/types/api/query/staking/ledger";
import { api } from "polkadot-typed-api";
// Define variable type
let unblocking: Staking_Ledger_Json['unlocking'] = [];
const data = await api.query.staking.ledger(connection, account); // Staking_Ledger_Json
if (data) {
// Set-Up variable value
unblocking = data.unblocking;
}
Useful Utils
awaitTransaction
- await transaction cancellationpolkadotExplorerUrl
- get subscan or another explorer link url by params
import { utils } from "polkadot-typed-api";