From e6e96aa9c9818a1b9a307aa4ccc8b33b3ce45890 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 6 Feb 2020 14:09:29 +0100 Subject: [PATCH] Make indices module optional (#1812) * Make indices optional * Updtae contracts with optional creation * Flatten contract fees --- CHANGELOG.md | 2 + packages/api-derive/src/accounts/indexToId.ts | 16 ++++---- packages/api-derive/src/accounts/indexes.ts | 22 ++++++---- packages/api-derive/src/balances/fees.ts | 8 ++-- packages/api-derive/src/contracts/fees.ts | 41 ++++++++----------- 5 files changed, 45 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a34951849..562d4535ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - Add `.isMax()` on int values, an opposite of `isZero()` (e.g. `u128`, as well as derived, e.g. `Balances`) - Add `.keyPrefix()` to all `api.query.*` complementing the `.key()` call, both returning a hex string - Council derives now takes bootstrapping (member, no elections) into account +- Account derives now allow for an optional indices module (not required) +- Adjust balances fees derivation with optional transfer and creation fees (removed in Substrate) - Adjust enum `.toJSON()` to return type name as specified # 1.0.1 diff --git a/packages/api-derive/src/accounts/indexToId.ts b/packages/api-derive/src/accounts/indexToId.ts index 1978ed7517..5641173a47 100644 --- a/packages/api-derive/src/accounts/indexToId.ts +++ b/packages/api-derive/src/accounts/indexToId.ts @@ -4,7 +4,7 @@ import { AccountId, AccountIndex } from '@polkadot/types/interfaces'; -import { Observable } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { map, startWith } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { ENUMSET_SIZE } from '@polkadot/types/primitive/Generic/AccountIndex'; @@ -26,18 +26,18 @@ import { memo } from '../util'; * ``` */ export function indexToId (api: ApiInterfaceRx): (accountIndex: AccountIndex | string) => Observable { - const querySection = api.query.indices || api.query.balances; - return memo((_accountIndex: AccountIndex | string): Observable => { const accountIndex = _accountIndex instanceof ClassOf(api.registry, 'AccountIndex') ? _accountIndex : createType(api.registry, 'AccountIndex', _accountIndex); - return querySection.enumSet>(accountIndex.div(ENUMSET_SIZE)).pipe( - startWith([]), - map((accounts): AccountId | undefined => - (accounts || [])[accountIndex.mod(ENUMSET_SIZE).toNumber()] + return api.query.indices.enumSet + ? api.query.indices.enumSet>(accountIndex.div(ENUMSET_SIZE)).pipe( + startWith([]), + map((accounts): AccountId | undefined => + (accounts || [])[accountIndex.mod(ENUMSET_SIZE).toNumber()] + ) ) - ); + : of(undefined); }); } diff --git a/packages/api-derive/src/accounts/indexes.ts b/packages/api-derive/src/accounts/indexes.ts index 10b8c856e6..607b8b7f57 100644 --- a/packages/api-derive/src/accounts/indexes.ts +++ b/packages/api-derive/src/accounts/indexes.ts @@ -6,7 +6,7 @@ import { ApiInterfaceRx } from '@polkadot/api/types'; import { AccountId, AccountIndex } from '@polkadot/types/interfaces'; import { AccountIndexes } from '../types'; -import { Observable } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { ENUMSET_SIZE } from '@polkadot/types/primitive/Generic/AccountIndex'; import { Vec, createType } from '@polkadot/types'; @@ -32,14 +32,18 @@ const enumsetSize = ENUMSET_SIZE.toNumber(); */ export function indexes (api: ApiInterfaceRx): () => Observable { return memo((): Observable => - api.query.indices.nextEnumSet().pipe( - // use the nextEnumSet (which is a counter of the number of sets) to construct - // a range of values to query [0, 1, 2, ...]. Retrieve the full enum set for the - // specific index - each query can return up to ENUMSET_SIZE (64) records, each - // containing an AccountId - switchMap((next: AccountIndex): Observable[]> => - api.query.indices.enumSet.multi>([...Array(next.toNumber() + 1).keys()]) - ), + (api.query.indices.nextEnumSet + ? api.query.indices.nextEnumSet().pipe( + // use the nextEnumSet (which is a counter of the number of sets) to construct + // a range of values to query [0, 1, 2, ...]. Retrieve the full enum set for the + // specific index - each query can return up to ENUMSET_SIZE (64) records, each + // containing an AccountId + switchMap((next: AccountIndex): Observable[]> => + api.query.indices.enumSet.multi>([...Array(next.toNumber() + 1).keys()]) + ) + ) + : of([] as AccountId[][]) + ).pipe( map((all: AccountId[][]): AccountIndexes => all.reduce((result: AccountIndexes, list, outerIndex): AccountIndexes => { (list || []).forEach((accountId, innerIndex): void => { diff --git a/packages/api-derive/src/balances/fees.ts b/packages/api-derive/src/balances/fees.ts index 98ff042b0c..c50ec55807 100644 --- a/packages/api-derive/src/balances/fees.ts +++ b/packages/api-derive/src/balances/fees.ts @@ -16,10 +16,12 @@ type Result = [Balance, Balance, Balance, Balance, Balance]; function query (api: ApiInterfaceRx): Observable { return of([ - api.consts.balances.creationFee, - api.consts.balances.existentialDeposit, // deprecated - remove + (api.consts.balances.creationFee as Balance) || createType(api.registry, 'Balance'), (api.consts.balances.transferFee as Balance) || createType(api.registry, 'Balance'), + + // current + api.consts.balances.existentialDeposit, api.consts.transactionPayment.transactionBaseFee, api.consts.transactionPayment.transactionByteFee ]); @@ -41,7 +43,7 @@ function query (api: ApiInterfaceRx): Observable { export function fees (api: ApiInterfaceRx): () => Observable { return memo((): Observable => query(api).pipe( - map(([creationFee, existentialDeposit, transferFee, transactionBaseFee, transactionByteFee]): DerivedFees => ({ + map(([creationFee, transferFee, existentialDeposit, transactionBaseFee, transactionByteFee]): DerivedFees => ({ creationFee, existentialDeposit, transactionBaseFee, diff --git a/packages/api-derive/src/contracts/fees.ts b/packages/api-derive/src/contracts/fees.ts index 5a2f036d34..7d5050bba2 100644 --- a/packages/api-derive/src/contracts/fees.ts +++ b/packages/api-derive/src/contracts/fees.ts @@ -14,33 +14,21 @@ import { memo } from '../util'; type ResultV2 = [BN, BN, BN, BN, BN, BN, BN, BN, BN]; -// parse the result -function parseResult ([callBaseFee, contractFee, creationFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee, transferFee]: ResultV2): DerivedContractFees { - return { - callBaseFee, - contractFee, - creationFee, - rentByteFee, - rentDepositOffset, - tombstoneDeposit, - transactionBaseFee, - transactionByteFee, - transferFee - }; -} - // query via constants (current applicable path) function queryConstants (api: ApiInterfaceRx): Observable { return of([ + // deprecated + api.consts.contracts.creationFee || createType(api.registry, 'Balance'), + api.consts.contracts.transferFee || createType(api.registry, 'Balance'), + + // current api.consts.contracts.callBaseFee, api.consts.contracts.contractFee, - api.consts.contracts.creationFee, api.consts.contracts.rentByteFee, api.consts.contracts.rentDepositOffset, api.consts.contracts.tombstoneDeposit, api.consts.contracts.transactionBaseFee, - api.consts.contracts.transactionByteFee, - api.consts.contracts.transferFee || createType(api.registry, 'Balance') + api.consts.contracts.transactionByteFee ]) as unknown as Observable; } @@ -60,12 +48,17 @@ function queryConstants (api: ApiInterfaceRx): Observable { export function fees (api: ApiInterfaceRx): () => Observable { return memo((): Observable => { return queryConstants(api).pipe( - map(([callBaseFee, contractFee, creationFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee, transferFee]): DerivedContractFees => - // We've done this on purpose, i.e. so we can just copy the name/order from the parse above and see gaps - parseResult([ - callBaseFee, contractFee, creationFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee, transferFee - ]) - ) + map(([creationFee, transferFee, callBaseFee, contractFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee]): DerivedContractFees => ({ + callBaseFee, + contractFee, + creationFee, + rentByteFee, + rentDepositOffset, + tombstoneDeposit, + transactionBaseFee, + transactionByteFee, + transferFee + })) ); }); }