Files
pezkuwi-apps/packages/apps-config/src/api/spec/interbtc.ts
T
Satoshi Qazi Muhammed 45d479d2ec Move to @pezkuwi/dev 0.86.4 (eslint 10 toolchain)
- dev ^0.86.4 at the root and in packages/apps; directives renamed by
  pezkuwi-dev-migrate-eslint10.
- eslint --fix with the 0.86.4 config; one fix broke syntax
  (typescript-eslint prefer-optional-chain turned
  `(isStarted && onStart) && onStart()` into `(isStarted && onStart?.();`
  in TxButton) and is corrected by hand.
- The remaining findings fixed rather than disabled: optional chains
  (each suggestion reviewed), `cause` on rethrown errors, `if` instead of
  `a && promise` in effects, parallel instead of sequential awaits in
  Promise.all, Error rejections, no `delete` on arrays (the storage query
  cache is a Map; an upload's file list no longer keeps a hole), no
  `[object Object]` stringification (TFunction's second argument is text
  or options), dead code removed (referenda's never-shown expand button,
  react-query's unused CoreDescriptor).
2026-10-10 02:06:12 +04:00

77 lines
2.2 KiB
TypeScript

// Copyright 2017-2026 @pezkuwi/apps-config authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { ApiInterfaceRx } from '@pezkuwi/api/types';
import type { DeriveBalancesAll } from '@pezkuwi/api-derive/types';
import type { AccountInfo, Balance } from '@pezkuwi/types/interfaces';
import type { OverrideBundleDefinition } from '@pezkuwi/types/types';
import interbtc from '@interlay/interbtc-types';
import { combineLatest, map } from 'rxjs';
import { memo } from '@pezkuwi/api-derive/util';
import { TypeRegistry, U128 } from '@pezkuwi/types';
import { BN, formatBalance } from '@pezkuwi/util';
function balanceOf (number: number | string): U128 {
return new U128(new TypeRegistry(), number);
}
function defaultAccountBalance (): DeriveBalancesAll {
return {
accountNonce: new BN(1),
additional: [],
availableBalance: balanceOf(0),
freeBalance: balanceOf(0),
lockedBalance: balanceOf(0),
lockedBreakdown: [],
namedReserves: [],
reservedBalance: balanceOf(0)
} as any;
}
interface OrmlAccountData {
free: Balance,
reserved: Balance,
frozen: Balance,
}
export function getBalance (
instanceId: string,
api: ApiInterfaceRx
): () => Observable<DeriveBalancesAll> {
const nativeToken = api.registry.chainTokens[0] || formatBalance.getDefaults().unit;
return memo(
instanceId,
(account: string): Observable<DeriveBalancesAll> =>
combineLatest<[any, any]>([api.query.tokens.accounts(account, { Token: nativeToken }), api.query.system.account(account)]).pipe(
map(([data, systemAccount]: [OrmlAccountData, AccountInfo]): DeriveBalancesAll => {
return {
...defaultAccountBalance(),
accountId: api.registry.createType('AccountId', account),
accountNonce: systemAccount.nonce,
availableBalance: api.registry.createType('Balance', data.free.sub(data.frozen)),
freeBalance: data.free,
lockedBalance: data.frozen,
reservedBalance: data.reserved
};
})
)
);
}
const definitions: OverrideBundleDefinition = {
derives: {
balances: {
account: getBalance,
all: getBalance
}
},
...interbtc
} as any;
export default definitions;