diff --git a/packages/apps-config/src/api/bundle/spec/equilibrium.ts b/packages/apps-config/src/api/bundle/spec/equilibrium.ts new file mode 100644 index 0000000000..bd36460043 --- /dev/null +++ b/packages/apps-config/src/api/bundle/spec/equilibrium.ts @@ -0,0 +1,147 @@ +// Copyright 2017-2020 @polkadot/apps-config authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { Observable } from 'rxjs'; +import type { ApiInterfaceRx } from '@polkadot/api/types'; +import type { Enum, Struct } from '@polkadot/types'; +import type { Option } from '@polkadot/types/codec'; +import type { AccountId, AccountIndex, AccountInfo, Address, Balance, BlockNumber, Index } from '@polkadot/types/interfaces'; + +import { combineLatest, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; + +import { memo } from '@polkadot/api-derive/util/memo'; + +interface VestingInfo extends Struct { + readonly locked: Balance; + readonly perBlock: Balance; + readonly startingBlock: BlockNumber; +} + +interface SignedBalance extends Enum { + readonly isPositive: boolean; + readonly asPositive: Balance; + readonly isNegative: boolean; + readonly asNegative: Balance; +} + +export interface EQDeriveBalancesAll { + freeBalance: Balance; + reservedBalance: Balance; + vestingLocked: Balance; + lockedBalance: Balance; + accountId: AccountId; + accountNonce: Index; + lockedBreakdown: unknown[]; +} + +type Result = [Balance, Balance, Balance, Index]; + +type RawResult = [ + SignedBalance, + Option, + Option, + AccountInfo +]; + +type EQDeriveBalancesQuery = ( + address: AccountIndex | AccountId | Address | string +) => Observable; + +export default { + derives: { + balances: { + all: ( + instanceId: string, + api: ApiInterfaceRx + ): EQDeriveBalancesQuery => + memo( + instanceId, + (address: AccountIndex | AccountId | Address | string) => + api.derive.accounts.accountId(address).pipe( + switchMap( + (accountId): Observable<[AccountId, Result]> => + accountId + ? combineLatest([ + of(accountId), + api + .queryMulti([ + [api.query.balances.account, [accountId, 'EQ']], + [api.query.eqVesting.vested, accountId], + [api.query.eqVesting.vesting, accountId], + [api.query.system.account, accountId] + ]) + .pipe( + map( + (raw): Result => { + if (raw.length < 4) { + throw new Error('4 members expected'); + } + + const res = raw as RawResult; + const freeBalance = res[0].asPositive; + + let reservedBalance = api.registry.createType( + 'Balance' + ); + + let vestingLocked = api.registry.createType( + 'Balance' + ); + + if (res[1].isSome && res[2].isSome) { + const vested = res[1].unwrap(); + const info = res[2].unwrap(); + + vestingLocked = info.locked; + + reservedBalance = api.registry.createType( + 'Balance', + vestingLocked.sub(vested) + ); + } + + return [ + freeBalance, + reservedBalance, + vestingLocked, + res[3].nonce + ]; + } + ) + ) + ]) + : of([ + api.registry.createType('AccountId'), + [ + api.registry.createType('Balance'), + api.registry.createType('Balance'), + api.registry.createType('Balance'), + api.registry.createType('Index') + ] + ]) + ), + map( + ([ + accountId, + [ + freeBalance, + reservedBalance, + vestingLocked, + accountNonce + ] + ]): EQDeriveBalancesAll => ({ + accountId, + accountNonce, + freeBalance, + lockedBalance: vestingLocked, + lockedBreakdown: [], + reservedBalance, + vestingLocked + }) + ) + ) + ) + } + } +}; diff --git a/packages/apps-config/src/api/bundle/spec/index.ts b/packages/apps-config/src/api/bundle/spec/index.ts index 9e94342a1c..bb48ce0ded 100644 --- a/packages/apps-config/src/api/bundle/spec/index.ts +++ b/packages/apps-config/src/api/bundle/spec/index.ts @@ -2,11 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 import acala from './acala'; +import equilibrium from './equilibrium'; import laminar from './laminar'; import moonbeam from './moonbeam'; import soraSubstrate from './soraSubstrate'; export default { + Equilibrium: equilibrium, acala, laminar, mandala: acala, diff --git a/packages/apps-config/src/settings/endpoints.ts b/packages/apps-config/src/settings/endpoints.ts index d24ebe4244..0293efcfba 100644 --- a/packages/apps-config/src/settings/endpoints.ts +++ b/packages/apps-config/src/settings/endpoints.ts @@ -149,7 +149,7 @@ function createLiveNetworks (t: TFunction): LinkOption[] { info: 'equilibrium', text: t('rpc.equilibrium', 'Equilibrium Mainnet', { ns: 'apps-config' }), textBy: t('rpc.hosted.by', 'hosted by {{host}}', { ns: 'apps-config', replace: { host: 'Equilibrium' } }), - value: 'wss://tge.equilibrium.io:9944' + value: 'wss://tge.equilibrium.io' }, { info: 'hanonycash',