diff --git a/CHANGELOG.md b/CHANGELOG.md index 987cadaafe..0cf73d163e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/api-derive/src/balances/account.ts b/packages/api-derive/src/balances/account.ts index 5ce108566a..0aa05309d4 100644 --- a/packages/api-derive/src/balances/account.ts +++ b/packages/api-derive/src/balances/account.ts @@ -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 { +function queryBalancesFree (api: ApiInterfaceRx, accountId: AccountId): Observable { 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 { +function queryBalancesAccount (api: ApiInterfaceRx, accountId: AccountId): Observable { 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 { + return api.query.system.account>(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')]]) ) diff --git a/packages/api/src/submittable/createClass.ts b/packages/api/src/submittable/createClass.ts index d7ac9ab408..afe2840b96 100644 --- a/packages/api/src/submittable/createClass.ts +++ b/packages/api/src/submittable/createClass.ts @@ -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 ({ 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>(address).pipe( + map(([nonce]): Index => nonce) + ) + : this._api.query.system.accountNonce(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