* Make a start * Intro * Installation * Flatten * Move METHODS_*.md to substrate/*.md * Basics * Apply suggestions from code review Co-Authored-By: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com> * Substrate links * Create * api.consts * Typo * Typo, ms -> md * types.basics (SIP) * Type basics * api.consts jumps to api.query * Start to api.query * Basic queries * Point starting to actual overview * api.rpc * Derive detour * Query subs & query multi * Apply suggestions from code review Co-Authored-By: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com> * typo * Query extras * Basic transactions * Misplaced , * keyring WIP * Keyring wrap-up * Subs SIP * tx subs more WIP * tx subs * Small types cleanups * Apply suggested changes from @JoshOrndorff * Clarify naked await * tx wrapping WIP * Wrap api.wrap * TS placeholder * [WIP] markers for not-started * Clarifying comments * Additional clarifications * types.extend... well, extended... * Remove *nix * Warp up TypeScript... it is it (for now) * Some small text clarifications * Some cleanups * Cleanups, typos * keyring cleanups * Cleanups * Wrap * api.wrap wrapped... hopefully * typos * Registration typos * typescript typos, hope I caught most...
2.0 KiB
Runtime Constants
Constant queries will introduce you to the concepts behind the types and the interaction of the API with those types. The same concepts are implemented in the remainder of the API - the runtime constants is just the simplest starting point.
For some background: constants are values that are defined in the runtime and used as part of chain operations. These constants can be changed as part of an upgrade.
// initialize the API as per previous sections
...
// the length of an epoch (session) in Babe
console.log(api.consts.babe.epochDuration.toNumber());
// the amount required to create a new account
console.log(api.consts.balances.creationFee.toNumber());
// the amount required per byte on an extrinsic
console.log(api.consts.balances.transactionByteFee.toNumber());
Since these are constants and defined by the metadata, it is not a call, but rather the values immediately available - as you'll see in subsequent sections, there is no need for await on these, it immediately returns the type and value for you to work with.
The API and types
There is some magic applied by the API. For instance as the createFee result is returned, the API knows the expected type and makes a Balance object available, hence the toNumber. This result mapping is consistent in retrieving constants, making queries or even sending transactions:
- when values are passed to the API, the API will convert whatever is provided into the correct type as required by the call
- when a value is retrieved, the API will provide an object of the correct type that wraps this value
From the last point, this means that a Balance will be returned as a number object extending bn.js. In a later section we will go through a breakdown of all the commonly-used types and all the basics available on types.
Making queries
In the next section we will take an initial dive into chain state and state queries, allowing the use of the API to retrieve information contained in the chain state.