Query nonce/data from system.account (as available) (#1814)

* Query nonce/data from sustem.account (as available)

* Detect new nonce query, retrieve

* Call system.account for nonce & balance

* Update CHANGELOG
This commit is contained in:
Jaco Greeff
2020-02-07 14:20:34 +01:00
committed by GitHub
parent faa12a68f7
commit fe52484547
3 changed files with 26 additions and 7 deletions
+4
View File
@@ -1,3 +1,7 @@
# 1.2.0-beta.x
- Add support for `system.account` for balance & nonce queries in derives (Substrate composites)
# 1.1.1
- Add support for `balances.account` in all applicable derives (incl. `vesting` module)
+15 -4
View File
@@ -3,6 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, AccountData, AccountIndex, Address, Balance, Index } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DerivedBalancesAccount } from '../types';
import { combineLatest, of, Observable } from 'rxjs';
@@ -27,7 +28,7 @@ function calcBalances (api: ApiInterfaceRx, [accountId, [freeBalance, reservedBa
}
// old
function queryOld (api: ApiInterfaceRx, accountId: AccountId): Observable<Result> {
function queryBalancesFree (api: ApiInterfaceRx, accountId: AccountId): Observable<Result> {
return api.queryMulti<[Balance, Balance, Index]>([
[api.query.balances.freeBalance, accountId],
[api.query.balances.reservedBalance, accountId],
@@ -39,7 +40,7 @@ function queryOld (api: ApiInterfaceRx, accountId: AccountId): Observable<Result
);
}
function queryCurrent (api: ApiInterfaceRx, accountId: AccountId): Observable<Result> {
function queryBalancesAccount (api: ApiInterfaceRx, accountId: AccountId): Observable<Result> {
return api.queryMulti<[AccountData, Index]>([
[api.query.balances.account, accountId],
[api.query.system.accountNonce, accountId]
@@ -50,6 +51,14 @@ function queryCurrent (api: ApiInterfaceRx, accountId: AccountId): Observable<Re
);
}
function queryCurrent (api: ApiInterfaceRx, accountId: AccountId): Observable<Result> {
return api.query.system.account<ITuple<[Index, AccountData]>>(accountId).pipe(
map(([accountNonce, { free, reserved, miscFrozen, feeFrozen }]): Result =>
[free, reserved, feeFrozen, miscFrozen, accountNonce]
)
);
}
/**
* @name account
* @param {( AccountIndex | AccountId | Address | string )} address - An accounts Id in different formats.
@@ -72,9 +81,11 @@ export function account (api: ApiInterfaceRx): (address: AccountIndex | AccountI
(accountId
? combineLatest([
of(accountId),
api.query.balances.account
api.query.system.account
? queryCurrent(api, accountId)
: queryOld(api, accountId)
: api.query.balances.account
? queryBalancesAccount(api, accountId)
: queryBalancesFree(api, accountId)
])
: of([createType(api.registry, 'AccountId'), [createType(api.registry, 'Balance'), createType(api.registry, 'Balance'), createType(api.registry, 'Balance'), createType(api.registry, 'Balance'), createType(api.registry, 'Index')]])
)
+7 -3
View File
@@ -3,8 +3,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Address, Call, ExtrinsicEra, ExtrinsicStatus, Hash, Header, Index, RuntimeDispatchInfo } from '@polkadot/types/interfaces';
import { Callback, Codec, Constructor, IKeyringPair, Registry, SignatureOptions } from '@polkadot/types/types';
import { AccountData, Address, Call, ExtrinsicEra, ExtrinsicStatus, Hash, Header, Index, RuntimeDispatchInfo } from '@polkadot/types/interfaces';
import { Callback, Codec, Constructor, IKeyringPair, ITuple, Registry, SignatureOptions } from '@polkadot/types/types';
import { ApiInterfaceRx, ApiTypes, SignerResult } from '../types';
import { AddressOrPair, SignerOptions, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultImpl, SubmittableResultResult, SubmittableResultSubscription, SubmittableThis } from './types';
@@ -235,7 +235,11 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
// ? this._api.rpc.account.nextIndex
// ? this._api.rpc.account.nextIndex(address)
// : this._api.query.system.accountNonce(address)
? this._api.query.system.accountNonce(address)
? this._api.query.system.account
? this._api.query.system.account<ITuple<[Index, AccountData]>>(address).pipe(
map(([nonce]): Index => nonce)
)
: this._api.query.system.accountNonce<Index>(address)
: of(createType(this.registry, 'Index', options.nonce)),
// if we have an era provided already or eraLength is <= 0 (immortal)
// don't get the latest block, just pass null, handle in mergeMap