From 236c0538412593245a3c031ee49bcb00de9d6720 Mon Sep 17 00:00:00 2001 From: Alex D Date: Mon, 5 Jul 2021 18:38:45 +0300 Subject: [PATCH] update eq types (#5769) Co-authored-by: Alex D --- packages/apps-config/package.json | 2 +- .../apps-config/src/api/spec/equilibrium.ts | 41 +++++++++++++++---- packages/apps-config/src/api/spec/genshiro.ts | 12 +----- yarn.lock | 10 ++--- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/packages/apps-config/package.json b/packages/apps-config/package.json index 5a887d537b..9f6b168d02 100644 --- a/packages/apps-config/package.json +++ b/packages/apps-config/package.json @@ -14,7 +14,7 @@ "@digitalnative/type-definitions": "^1.1.9", "@docknetwork/node-types": "^0.4.6", "@edgeware/node-types": "^3.6.1-wako", - "@equilab/definitions": "1.2.5", + "@equilab/definitions": "1.2.6", "@interlay/polkabtc-types": "^0.7.4", "@kiltprotocol/type-definitions": "^0.1.8", "@laminar/type-definitions": "^0.3.1", diff --git a/packages/apps-config/src/api/spec/equilibrium.ts b/packages/apps-config/src/api/spec/equilibrium.ts index ff470d9ff3..d503bfae2d 100644 --- a/packages/apps-config/src/api/spec/equilibrium.ts +++ b/packages/apps-config/src/api/spec/equilibrium.ts @@ -6,7 +6,7 @@ import type { AccountData, AccountId, AccountIndex, Address, Balance } from '@po import type { Codec, OverrideBundleDefinition } from '@polkadot/types/types'; import type { Observable } from '@polkadot/x-rxjs'; -import { equilibrium } from '@equilab/definitions'; +import { equilibrium, equilibriumNext } from '@equilab/definitions'; import BN from 'bn.js'; import { Enum } from '@polkadot/types'; @@ -37,13 +37,24 @@ type EqBalanceDoubleMap = AugmentedQuery< [AccountId, Currency] > +export const u64FromCurrency = (currency: string): number => { + const buf = Buffer.from(currency.toLowerCase()); + const size = buf.length; + + return buf.reduce( + (val, digit, i) => val + Math.pow(256, size - 1 - i) * digit, + 0 + ); +}; + const transformBalanceStorage = ( query: EqBalanceDoubleMap, currency: string, transform: (data: SB) => AccountData, - currencyToAsset: (arg: string) => T + currencyToAsset: (arg: string, api?: ApiInterfaceRx) => T, + api?: ApiInterfaceRx ): CommonBalanceMap => { - const arg = currencyToAsset(currency); + const arg = currencyToAsset(currency, api); // HACK as we cannot properly transform queryMulti result, define AccountData getters on standard Enum if (!(Enum as { hacked?: boolean }).hacked) { @@ -81,7 +92,7 @@ const signedBalancePredicate = (raw: Codec): raw is SignedBalance => Object.prototype.hasOwnProperty.call(raw, key) ); -export const createCustomAccount = (currency: string, currencyToAsset: (curr: string) => A, accountDataType = 'AccountData'): +export const createCustomAccount = (currency: string, currencyToAsset: (curr: string, api?: ApiInterfaceRx) => A, accountDataType = 'AccountData'): (instanceId: string, api: ApiInterfaceRx) => RxResult<(arg: string | Uint8Array | AccountId) => Observable> => (instanceId: string, api: ApiInterfaceRx) => { const registry = api.registry; @@ -106,7 +117,8 @@ export const createCustomAccount = (currency: string, currencyToAsse api.query.eqBalances.account as unknown as EqBalanceDoubleMap, currency, transform, - currencyToAsset + currencyToAsset, + api ); }; @@ -116,7 +128,17 @@ const definitions: OverrideBundleDefinition = { (all, cur) => ({ ...all, [cur]: { - customAccount: createCustomAccount(cur, (currency: string) => currency) + customAccount: createCustomAccount(cur, (currency: string, api?: ApiInterfaceRx) => { + let assetsEnabled = true; + + try { + api?.registry.createType('AssetIdInnerType' as any); + } catch (_) { + assetsEnabled = false; + } + + return assetsEnabled ? { 0: u64FromCurrency(currency) } : currency; + }) } }), {} @@ -127,9 +149,12 @@ const definitions: OverrideBundleDefinition = { types: [ { - // on all versions - minmax: [0, undefined], + minmax: [0, 262], types: equilibrium.types + }, + { + minmax: [263, undefined], + types: equilibriumNext.types } ] }; diff --git a/packages/apps-config/src/api/spec/genshiro.ts b/packages/apps-config/src/api/spec/genshiro.ts index 14bad46952..538cb46972 100644 --- a/packages/apps-config/src/api/spec/genshiro.ts +++ b/packages/apps-config/src/api/spec/genshiro.ts @@ -5,17 +5,7 @@ import type { OverrideBundleDefinition } from '@polkadot/types/types'; import { genshiro } from '@equilab/definitions'; -import { createCustomAccount } from './equilibrium'; - -export const u64FromCurrency = (currency: string): number => { - const buf = Buffer.from(currency.toLowerCase()); - const size = buf.length; - - return buf.reduce( - (val, digit, i) => val + Math.pow(256, size - 1 - i) * digit, - 0 - ); -}; +import { createCustomAccount, u64FromCurrency } from './equilibrium'; const definitions: OverrideBundleDefinition = { derives: { diff --git a/yarn.lock b/yarn.lock index 1e6bcf694c..5d8b77fff5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1604,10 +1604,10 @@ __metadata: languageName: node linkType: hard -"@equilab/definitions@npm:1.2.5": - version: 1.2.5 - resolution: "@equilab/definitions@npm:1.2.5" - checksum: aa8ce21d12b320bd21235e40d3ab2b64ec4520d00d7b55ccfac2c8bd27354a59c025452a13876b24f2222d5aba1b29e1b4b312bbcee84267b731cb74695340ce +"@equilab/definitions@npm:1.2.6": + version: 1.2.6 + resolution: "@equilab/definitions@npm:1.2.6" + checksum: e58599e6611bfab6fe8d108f5afd5607cdbb7118351232144fad74874b964f0b595edee3e38f6f91309c53dc2eeb2803e860db4ece386d8a69a1b43389db81c3 languageName: node linkType: hard @@ -2554,7 +2554,7 @@ __metadata: "@digitalnative/type-definitions": ^1.1.9 "@docknetwork/node-types": ^0.4.6 "@edgeware/node-types": ^3.6.1-wako - "@equilab/definitions": 1.2.5 + "@equilab/definitions": 1.2.6 "@interlay/polkabtc-types": ^0.7.4 "@kiltprotocol/type-definitions": ^0.1.8 "@laminar/type-definitions": ^0.3.1