Rename st -> query (#269)
This commit is contained in:
committed by
Amaury Martiny
parent
b913ace9be
commit
fb528d051c
@@ -10,13 +10,13 @@ async function main () {
|
|||||||
|
|
||||||
// Retrieve the initial balance. Since the call has no callback, it is simply a promise
|
// Retrieve the initial balance. Since the call has no callback, it is simply a promise
|
||||||
// the resolves to the current on-chain value
|
// the resolves to the current on-chain value
|
||||||
let previous = await api.st.balances.freeBalance(Alice);
|
let previous = await api.query.balances.freeBalance(Alice);
|
||||||
|
|
||||||
console.log(`${Alice} has an ${previous} balance`);
|
console.log(`${Alice} has an ${previous} balance`);
|
||||||
console.log(`You may leave this example running and start example 06 or transfer any value to ${Alice}`);
|
console.log(`You may leave this example running and start example 06 or transfer any value to ${Alice}`);
|
||||||
|
|
||||||
// Here we subscribe to any balance changes and updates the on-screen value
|
// Here we subscribe to any balance changes and updates the on-screen value
|
||||||
api.st.balances.freeBalance(Alice, (current) => {
|
api.query.balances.freeBalance(Alice, (current) => {
|
||||||
// Calculate the delta
|
// Calculate the delta
|
||||||
const change = current.sub(previous);
|
const change = current.sub(previous);
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ async function main () {
|
|||||||
|
|
||||||
// Make our basic chain state/storage queries, all in one go
|
// Make our basic chain state/storage queries, all in one go
|
||||||
const [accountNonce, blockPeriod, validators] = await Promise.all([
|
const [accountNonce, blockPeriod, validators] = await Promise.all([
|
||||||
api.st.system.accountNonce(Alice),
|
api.query.system.accountNonce(Alice),
|
||||||
api.st.timestamp.blockPeriod(),
|
api.query.timestamp.blockPeriod(),
|
||||||
api.st.session.validators()
|
api.query.session.validators()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
console.log(`accountNonce(${Alice}) ${accountNonce}`);
|
console.log(`accountNonce(${Alice}) ${accountNonce}`);
|
||||||
@@ -21,7 +21,7 @@ async function main () {
|
|||||||
// get the balances for all validators
|
// get the balances for all validators
|
||||||
const validatorBalances = await Promise.all(
|
const validatorBalances = await Promise.all(
|
||||||
validators.map((authorityId) =>
|
validators.map((authorityId) =>
|
||||||
api.st.balances.freeBalance(authorityId)
|
api.query.balances.freeBalance(authorityId)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ async function main () {
|
|||||||
const api = await ApiPromise.create();
|
const api = await ApiPromise.create();
|
||||||
|
|
||||||
// retrieve the nonce for Alice, used to sign the transaction
|
// retrieve the nonce for Alice, used to sign the transaction
|
||||||
const aliceNonce = await api.st.system.accountNonce(alice.address());
|
const aliceNonce = await api.query.system.accountNonce(alice.address());
|
||||||
|
|
||||||
// Create a extrinsic, transferring 12345 units to Bob. We can also create,
|
// Create a extrinsic, transferring 12345 units to Bob. We can also create,
|
||||||
// sign and send in one operation (as per the samples in the Api documentation),
|
// sign and send in one operation (as per the samples in the Api documentation),
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ The API wrappers provide a standard interface for use -
|
|||||||
- A static `.create(<optional WsProvider>)` that returns an API istance when connected, decorated and ready-to use
|
- A static `.create(<optional WsProvider>)` that returns an API istance when connected, decorated and ready-to use
|
||||||
- The above is just a wrapper for `new Api(<optional WsProvider>) `, exposing the `isReady` getter
|
- The above is just a wrapper for `new Api(<optional WsProvider>) `, exposing the `isReady` getter
|
||||||
- `api.rpc.<section>.<method>` provides access to actual RPC calls, be it for queries, submission or retrieving chain information
|
- `api.rpc.<section>.<method>` provides access to actual RPC calls, be it for queries, submission or retrieving chain information
|
||||||
- `api.st.<section>.<method>` provides access to chain state queries. These are dynamically populated based on what the runtime provides
|
- `api.query.<section>.<method>` provides access to chain state queries. These are dynamically populated based on what the runtime provides
|
||||||
- `api.tx.<section>.<method>` provides the ability to create transaction, like chain state, this list is populated from a runtime query
|
- `api.tx.<section>.<method>` provides the ability to create transaction, like chain state, this list is populated from a runtime query
|
||||||
|
|
||||||
## API Selection
|
## API Selection
|
||||||
|
|||||||
@@ -115,13 +115,13 @@ export default abstract class ApiBase<R, S, E> implements ApiBaseInterface<R, S,
|
|||||||
/**
|
/**
|
||||||
* @description Contains all the chain state modules and their subsequent methods in the API. These are attached dynamically from the runtime metadata.
|
* @description Contains all the chain state modules and their subsequent methods in the API. These are attached dynamically from the runtime metadata.
|
||||||
*
|
*
|
||||||
* All calls inside the namespace, is denoted by `section`.`method` and may take an optional query parameter. As an example, `api.st.timestamp.now()` (current block timestamp) does not take parameters, while `api.st.system.accountNonce(<accountId>)` (retrieving the associated nonce for an account), takes the `AccountId` as a parameter.
|
* All calls inside the namespace, is denoted by `section`.`method` and may take an optional query parameter. As an example, `api.query.timestamp.now()` (current block timestamp) does not take parameters, while `api.query.system.accountNonce(<accountId>)` (retrieving the associated nonce for an account), takes the `AccountId` as a parameter.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* <BR>
|
* <BR>
|
||||||
*
|
*
|
||||||
* ```javascript
|
* ```javascript
|
||||||
* api.st.balances
|
* api.query.balances
|
||||||
* .freeBalance(<accountId>)
|
* .freeBalance(<accountId>)
|
||||||
* .subscribe((balance) => {
|
* .subscribe((balance) => {
|
||||||
* console.log('new balance', balance);
|
* console.log('new balance', balance);
|
||||||
|
|||||||
@@ -61,11 +61,11 @@ import { StorageFunction } from '@polkadot/types/StorageKey';
|
|||||||
* const api = await new ApiPromise(provider).isReady;
|
* const api = await new ApiPromise(provider).isReady;
|
||||||
*
|
*
|
||||||
* // retrieve the block target time
|
* // retrieve the block target time
|
||||||
* const blockPeriod = await api.st.timestamp.blockPeriod().toNumber();
|
* const blockPeriod = await api.query.timestamp.blockPeriod().toNumber();
|
||||||
* let last = 0;
|
* let last = 0;
|
||||||
*
|
*
|
||||||
* // subscribe to the current block timestamp, updates automatically (callback provided)
|
* // subscribe to the current block timestamp, updates automatically (callback provided)
|
||||||
* api.st.timestamp.now((timestamp) => {
|
* api.query.timestamp.now((timestamp) => {
|
||||||
* const elapsed = last
|
* const elapsed = last
|
||||||
* ? `, ${timestamp.toNumber() - last}s since last`
|
* ? `, ${timestamp.toNumber() - last}s since last`
|
||||||
* : '';
|
* : '';
|
||||||
@@ -83,7 +83,7 @@ import { StorageFunction } from '@polkadot/types/StorageKey';
|
|||||||
* import ApiPromise from '@polkadot/api/promise';
|
* import ApiPromise from '@polkadot/api/promise';
|
||||||
*
|
*
|
||||||
* ApiPromise.create().then((api) => {
|
* ApiPromise.create().then((api) => {
|
||||||
* const nonce = await api.st.system.accountNonce(keyring.alice.address());
|
* const nonce = await api.query.system.accountNonce(keyring.alice.address());
|
||||||
*
|
*
|
||||||
* api.tx.balances
|
* api.tx.balances
|
||||||
* // create transfer
|
* // create transfer
|
||||||
@@ -116,7 +116,7 @@ export default class ApiPromise extends ApiBase<Rpc, QueryableStorage, Submittab
|
|||||||
* import Api from '@polkadot/api/promise';
|
* import Api from '@polkadot/api/promise';
|
||||||
*
|
*
|
||||||
* Api.create().then(async (api) => {
|
* Api.create().then(async (api) => {
|
||||||
* const timestamp = await api.st.timestamp.now();
|
* const timestamp = await api.query.timestamp.now();
|
||||||
*
|
*
|
||||||
* console.log(`lastest block timestamp ${timestamp}`);
|
* console.log(`lastest block timestamp ${timestamp}`);
|
||||||
* });
|
* });
|
||||||
|
|||||||
@@ -72,8 +72,8 @@ const l = logger('api-rx');
|
|||||||
* .pipe(
|
* .pipe(
|
||||||
* switchMap((api) =>
|
* switchMap((api) =>
|
||||||
* combineLatest([
|
* combineLatest([
|
||||||
* api.st.timestamp.blockPeriod(),
|
* api.query.timestamp.blockPeriod(),
|
||||||
* api.st.timestamp.now()
|
* api.query.timestamp.now()
|
||||||
* ])
|
* ])
|
||||||
* )
|
* )
|
||||||
* .subscribe(([blockPeriod, timestamp]) => {
|
* .subscribe(([blockPeriod, timestamp]) => {
|
||||||
@@ -97,7 +97,7 @@ const l = logger('api-rx');
|
|||||||
* const api = await ApiRx.create().toPromise();
|
* const api = await ApiRx.create().toPromise();
|
||||||
*
|
*
|
||||||
* // retrieve nonce for the account
|
* // retrieve nonce for the account
|
||||||
* api.st.system
|
* api.query.system
|
||||||
* .accountNonce(keyring.alice.address())
|
* .accountNonce(keyring.alice.address())
|
||||||
* .pipe(
|
* .pipe(
|
||||||
* // pipe nonce into transfer
|
* // pipe nonce into transfer
|
||||||
@@ -132,7 +132,7 @@ export default class ApiRx extends ApiBase<RpcRx, QueryableStorage, SubmittableE
|
|||||||
* import Api from '@polkadot/api/rx';
|
* import Api from '@polkadot/api/rx';
|
||||||
*
|
*
|
||||||
* Api.create().subscribe((api) => {
|
* Api.create().subscribe((api) => {
|
||||||
* api.st.timestamp.now.subscribe((timestamp) => {
|
* api.query.timestamp.now.subscribe((timestamp) => {
|
||||||
* console.log(`lastest block timestamp ${timestamp}`);
|
* console.log(`lastest block timestamp ${timestamp}`);
|
||||||
* });
|
* });
|
||||||
* });
|
* });
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ describe.skip('e2e queries', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('queries state for a balance', async () => {
|
it('queries state for a balance', async () => {
|
||||||
const balance = await api.st.balances.freeBalance(keyring.alice.address());
|
const balance = await api.query.balances.freeBalance(keyring.alice.address());
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
balance.isZero()
|
balance.isZero()
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ describe.skip('e2e transfer', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('makes a transfer', async () => {
|
it('makes a transfer', async () => {
|
||||||
const nonce = await api.st.system.accountNonce(keyring.alice.address());
|
const nonce = await api.query.system.accountNonce(keyring.alice.address());
|
||||||
const hash = await api.tx.balances
|
const hash = await api.tx.balances
|
||||||
.transfer(keyring.bob.address(), 12345)
|
.transfer(keyring.bob.address(), 12345)
|
||||||
.sign(keyring.alice, nonce)
|
.sign(keyring.alice, nonce)
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ describe.skip('e2e queries', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('queries state for a balance', (done) => {
|
it('queries state for a balance', (done) => {
|
||||||
api.st.balances.freeBalance(keyring.alice.address()).subscribe((balance) => {
|
api.query.balances.freeBalance(keyring.alice.address()).subscribe((balance) => {
|
||||||
expect(
|
expect(
|
||||||
balance.isZero()
|
balance.isZero()
|
||||||
).toBe(false);
|
).toBe(false);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ describe.skip('e2e transfer', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('makes a transfer', (done) => {
|
it('makes a transfer', (done) => {
|
||||||
api.st.system
|
api.query.system
|
||||||
.accountNonce(keyring.alice.address())
|
.accountNonce(keyring.alice.address())
|
||||||
.pipe(
|
.pipe(
|
||||||
switchMap((nonce) =>
|
switchMap((nonce) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user