parent
d63fa6b2f6
commit
c4104bc42d
@ -7,7 +7,7 @@ import { Snippet } from './../types';
|
||||
import { constsStakingParameters } from './consts-examples';
|
||||
import { extrinsicMakeTransfer } from './extrinsics-examples';
|
||||
import { rpcNetworkAuthoring, rpcNewHead, rpcQueryState, rpcSysthemInfo } from './rpc-examples';
|
||||
import { storageGetInfo, storageSystemEvents, storageListenToBalanceChange, storageListenToMultipleBalancesChange, storageRetrieveInfoOnQueryKeys } from './storage-examples';
|
||||
import { storageGetInfo, storageSystemEvents, storageKeys, storageListenToBalanceChange, storageListenToMultipleBalancesChange, storageRetrieveInfoOnQueryKeys } from './storage-examples';
|
||||
|
||||
const snippets: Snippet[] = [
|
||||
rpcNetworkAuthoring,
|
||||
@ -19,6 +19,7 @@ const snippets: Snippet[] = [
|
||||
storageListenToBalanceChange,
|
||||
storageListenToMultipleBalancesChange,
|
||||
storageRetrieveInfoOnQueryKeys,
|
||||
storageKeys,
|
||||
constsStakingParameters,
|
||||
extrinsicMakeTransfer
|
||||
];
|
||||
|
||||
@ -4,13 +4,13 @@
|
||||
|
||||
import { Snippet } from '@polkadot/app-js/types';
|
||||
|
||||
// We must fix this :(
|
||||
/* eslint-disable sort-keys */
|
||||
const label = {
|
||||
children: 'Storage',
|
||||
color: 'blue',
|
||||
size: 'tiny'
|
||||
};
|
||||
|
||||
export const storageGetInfo: Snippet = {
|
||||
value: 'storageGetInfo',
|
||||
text: 'Get chain state information',
|
||||
label: { color: 'blue', children: 'Storage', size: 'tiny' },
|
||||
code: `// Get chain state information
|
||||
// Make our basic chain state / storage queries, all in one go
|
||||
|
||||
@ -28,21 +28,21 @@ if (validators && validators.length > 0) {
|
||||
console.log('Validators');
|
||||
|
||||
const validatorBalances = await Promise.all(
|
||||
validators.map(authorityId => api.query.balances.freeBalance(authorityId))
|
||||
validators.map((authorityId) => api.query.system.account(authorityId))
|
||||
);
|
||||
|
||||
validators.forEach((authorityId, index) => {
|
||||
console.log('Validator: ' + authorityId.toString() )
|
||||
console.log('Balance: ' + validatorBalances[index].toString() );
|
||||
console.log('AccountData: ' + validatorBalances[index].toHuman() );
|
||||
});
|
||||
}
|
||||
`
|
||||
`,
|
||||
label,
|
||||
text: 'Get chain state information',
|
||||
value: 'storageGetInfo'
|
||||
};
|
||||
|
||||
export const storageSystemEvents: Snippet = {
|
||||
value: 'storageSystemEvents',
|
||||
text: 'Listen to system events',
|
||||
label: { color: 'blue', children: 'Storage', size: 'tiny' },
|
||||
code: `// Subscribe to system events via storage
|
||||
api.query.system.events((events) => {
|
||||
console.log('----- Received ' + events.length + ' event(s): -----');
|
||||
@ -59,39 +59,39 @@ api.query.system.events((events) => {
|
||||
console.log(types[index].type + ';' + data.toString());
|
||||
});
|
||||
});
|
||||
});`
|
||||
});`,
|
||||
label,
|
||||
text: 'Listen to system events',
|
||||
value: 'storageSystemEvents'
|
||||
};
|
||||
|
||||
export const storageListenToBalanceChange: Snippet = {
|
||||
value: 'storageListenToBalanceChange',
|
||||
text: 'Listen to balance changes',
|
||||
label: { color: 'blue', children: 'Storage', size: 'tiny' },
|
||||
code: `// You may leave this example running and make a transfer
|
||||
// of any value from or to Alice address in the 'Transfer' App
|
||||
const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
|
||||
|
||||
// Retrieve the initial balance.
|
||||
let previous = await api.query.balances.freeBalance(ALICE);
|
||||
// Retrieve the initial data
|
||||
let [, { free: previous }] = await api.query.system.account(ALICE);
|
||||
|
||||
console.log('ALICE has a balance of ' + previous);
|
||||
|
||||
// Subscribe and listen to balance changes
|
||||
api.query.balances.freeBalance(ALICE, (balance) => {
|
||||
api.query.system.account(ALICE, ([, { free }]) => {
|
||||
// Calculate the delta
|
||||
const change = balance.sub(previous);
|
||||
const change = free.sub(previous);
|
||||
// Only display positive value changes (Since we are pulling 'previous' above already,
|
||||
// the initial balance change will also be zero)
|
||||
if (!change.isZero()) {
|
||||
previous = balance;
|
||||
previous = free;
|
||||
console.log('New transaction of: '+ change);
|
||||
}
|
||||
});`
|
||||
});`,
|
||||
label,
|
||||
text: 'Listen to balance changes',
|
||||
value: 'storageListenToBalanceChange'
|
||||
};
|
||||
|
||||
export const storageListenToMultipleBalancesChange: Snippet = {
|
||||
value: 'storageListenToMultipleBalancesChange',
|
||||
text: 'Listen to multiple balances changes',
|
||||
label: { color: 'blue', children: 'Storage', size: 'tiny' },
|
||||
code: `// You may leave this example running and make a transfer
|
||||
// of any value from or to Alice/Bob address in the 'Transfer' App
|
||||
const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
|
||||
@ -100,38 +100,66 @@ const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
|
||||
console.log('Tracking balances for:', [ALICE, BOB])
|
||||
|
||||
// Subscribe and listen to several balance changes
|
||||
api.query.balances.freeBalance.multi([ALICE, BOB], (balances) => {
|
||||
console.log('Change detected, new balances: ', balances)
|
||||
});`
|
||||
api.query.system.account.multi([ALICE, BOB], (info) => {
|
||||
console.log('Change detected, new balances: ', info)
|
||||
});`,
|
||||
label,
|
||||
text: 'Listen to multiple balances changes',
|
||||
value: 'storageListenToMultipleBalancesChange'
|
||||
};
|
||||
|
||||
export const storageRetrieveInfoOnQueryKeys: Snippet = {
|
||||
value: 'storageRetrieveInfoOnQueryKeys',
|
||||
text: 'Retrieve Info on query keys',
|
||||
label: { color: 'blue', children: 'Storage', size: 'tiny' },
|
||||
code: `// This example set shows how to make queries and retrieve info on query keys
|
||||
code: `// This example set shows how to make queries at a point
|
||||
const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
|
||||
|
||||
// retrieve the balance, once-off at the latest block
|
||||
const currBalance = await api.query.balances.freeBalance(ALICE);
|
||||
const [nonce, { free }] = await api.query.system.account(ALICE);
|
||||
|
||||
console.log('Alice has a current balance of', currBalance);
|
||||
console.log('Alice has a current balance of', free.toHuman());
|
||||
|
||||
// retrieve balance updates with an optional value callback
|
||||
const balanceUnsub = await api.query.balances.freeBalance(ALICE, (balance) => {
|
||||
console.log('Alice has an updated balance of', balance);
|
||||
const balanceUnsub = await api.query.system.account(ALICE, ([, { free }]) => {
|
||||
console.log('Alice has an updated balance of', free.toHuman());
|
||||
});
|
||||
|
||||
// retrieve the balance at a block hash in the past
|
||||
const header = await api.rpc.chain.getHeader();
|
||||
const prevHash = await api.rpc.chain.getBlockHash(header.blockNumber.subn(42));
|
||||
const prevBalance = await api.query.balances.freeBalance.at(prevHash, ALICE);
|
||||
const [, { free: prev }] = await api.query.system.account.at(prevHash, ALICE);
|
||||
|
||||
console.log('Alice had a balance of', prevBalance, '(42 blocks ago)');
|
||||
console.log('Alice had a balance of', prev.toHuman(), '(42 blocks ago)');
|
||||
|
||||
// useful in some situations - the value hash and storage entry size
|
||||
const currHash = await api.query.balances.freeBalance.hash(ALICE);
|
||||
const currSize = await api.query.balances.freeBalance.size(ALICE);
|
||||
const currHash = await api.query.system.account.hash(ALICE);
|
||||
const currSize = await api.query.system.account.size(ALICE);
|
||||
|
||||
console.log('Alice balance entry has a value hash of', currHash, 'with a size of', currSize);`
|
||||
console.log('Alice account entry has a value hash of', currHash, 'with a size of', currSize);`,
|
||||
label,
|
||||
text: 'Retrieve historic query datas',
|
||||
value: 'storageRetrieveInfoOnQueryKeys'
|
||||
};
|
||||
|
||||
export const storageKeys: Snippet = {
|
||||
code: `// this example shows how to retrieve the hex representation of a storage key
|
||||
|
||||
const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
|
||||
|
||||
// show the key for an entry without arguments
|
||||
console.log(api.query.timestamp.now.key());
|
||||
|
||||
// show the key for a map entry (single argument)
|
||||
console.log(api.query.system.account.key(ALICE));
|
||||
|
||||
// show the key prefix for a map
|
||||
console.log(api.query.system.account.keyPrefix());
|
||||
|
||||
// show the key for a double map
|
||||
console.log(api.query.staking.erasStakers.key(0, ALICE));
|
||||
|
||||
// show the key prefix for a doublemap
|
||||
console.log(api.query.staking.erasStakers.keyPrefix());
|
||||
`,
|
||||
label,
|
||||
text: 'Get underlying storage key hex values',
|
||||
value: 'storageKeys'
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user