Update docs for system.account (#1944)
This commit is contained in:
+1
-1
@@ -36,7 +36,7 @@ The length for this transaction validity is set to 50 blocks, which translates t
|
||||
|
||||
## My chain does not support system.account queries
|
||||
|
||||
The API always tracks the latest Substrate master in terms of examples. This means that nonce & balance queries are done via the `api.query.system.account(<account>)` which returns a Tuple `(Index, AccountData)` where the first is the nonce, the second a struct containing the free and reserved balances. As with all `api.query.*` endpoints, this is decorated based on what the chain you connect to support, via the metadata exchange.
|
||||
The API always tracks the latest Substrate master in terms of examples. This means that nonce & balance queries are done via the `api.query.system.account(<account>)` which returns a struct `{ nonce: Index, data: AccountData }` where the `data` is struct containing the free and reserved balances. As with all `api.query.*` endpoints, this is decorated based on what the chain you connect to support, via the metadata exchange.
|
||||
|
||||
It is possible that you are connecting to an older chain that has not been upgraded yet. For these chains, this storage entry won't be available (yet). To query the nonce on older chains, you can do a query to `api.query.system.accountNonce(<account>)` and balances can be retrieved via `api.query.balances.freeBalance(<account>)`.
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ const ADDR = '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE';
|
||||
const now = await api.query.timestamp.now();
|
||||
|
||||
// Retrieve the account balance & nonce via the system module
|
||||
const [nonce, balance] = await api.query.system.account(ADDR);
|
||||
const { nonce, data: balance } = await api.query.system.account(ADDR);
|
||||
|
||||
console.log(`${now}: balance of ${balance.free} and a nonce of ${nonce}`);
|
||||
```
|
||||
@@ -27,7 +27,7 @@ There have been some additions in the code above comparing with retrieving runti
|
||||
```js
|
||||
...
|
||||
// Retrieve last block timestamp, account nonce & balances
|
||||
const [now, [nonce, balances]] = await Promise.all([
|
||||
const [now, { nonce, data: balances }] = await Promise.all([
|
||||
api.query.timestamp.now(),
|
||||
api.query.system.account(ADDR)
|
||||
]);
|
||||
|
||||
@@ -11,7 +11,7 @@ Where possible, the use of multi queries are encouraged since it tracks a number
|
||||
|
||||
// Subscribe to balance changes for 2 accounts, ADDR1 & ADDR2 (already defined)
|
||||
const unsub = await api.query.system.account.multi([ADDR1, ADDR2], (balances) => {
|
||||
const [[, balance1], [, balance2]] = balances;
|
||||
const [{ data: balance1 }, { data: balance2 }] = balances;
|
||||
|
||||
console.log(`The balances are ${balance1.free} and ${balance2.free}`);
|
||||
});
|
||||
@@ -44,7 +44,7 @@ The previous `.multi` examples assumes that we do queries for the same types, i.
|
||||
const unsub = await api.queryMulti([
|
||||
api.query.timestamp.now,
|
||||
[api.query.system.account, ADDR]
|
||||
], ([now, [nonce, balance]]) => {
|
||||
], ([now, { nonce, data: balance }]) => {
|
||||
console.log(`${now}: balance of ${balance.free} and a nonce of ${nonce}`);
|
||||
});
|
||||
```
|
||||
|
||||
@@ -13,7 +13,7 @@ Quite often is is useful (taking pruning into account, more on this later) to re
|
||||
const lastHdr = await api.rpc.chain.getHeader();
|
||||
|
||||
// Retrieve the balance at both the current and the parent hashes
|
||||
const [[, balanceNow], [, balancePrev]] = await Promise.all([
|
||||
const [{ data: balanceNow }, { data: balancePrev }] = await Promise.all([
|
||||
api.query.system.account.at(lastHdr.hash, ADDR),
|
||||
api.query.system.account.at(lastHdr.parentHash, ADDR)
|
||||
]);
|
||||
|
||||
@@ -25,7 +25,7 @@ If we had a query with parameters, i.e. where we wish to perform a query for a s
|
||||
...
|
||||
|
||||
// Subscribe to balance changes for our account
|
||||
const unsub = await api.query.system.account(ADDR, ([nonce, balance]) => {
|
||||
const unsub = await api.query.system.account(ADDR, ({ nonce, data: balance }) => {
|
||||
console.log(`free balance is ${balance.freee} with ${balance.reserved} reserved and a nonce of ${nonce}`);
|
||||
});
|
||||
```
|
||||
|
||||
@@ -33,7 +33,7 @@ However, to make this sane from a developer perspective the injected methods are
|
||||
import { AccountData, Index } from '@polkadot/types/interfaces';
|
||||
|
||||
...
|
||||
const [nonce, balance] = await api.query.system.account<[Index, AccountData]>(ADDR);
|
||||
const { nonce, data: balances } = await api.query.system.account<[Index, AccountData]>(ADDR);
|
||||
```
|
||||
|
||||
In both these case we can instruct the TypeScript compiler that the type we are expecting in `Index` and `Balance` respectively, not just pure `Codec`. This means that functions like `.toNumber()` is available on both these types - as opposed to just the [general type defaults](types.basics.md#everything-is-a-type) with `.toHex()` and friends.
|
||||
|
||||
Reference in New Issue
Block a user