forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interbtc.ts
80 lines (68 loc) · 2.51 KB
/
interbtc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2017-2023 @polkadot/apps-config authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */
import type { Observable } from 'rxjs';
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { OverrideBundleDefinition } from '@polkadot/types/types';
import interbtc from '@interlay/interbtc-types';
import { combineLatest, map } from 'rxjs';
import { DeriveBalancesAll } from '@polkadot/api-derive/types';
import { memo } from '@polkadot/api-derive/util';
import { TypeRegistry, U128 } from '@polkadot/types';
import { Balance } from '@polkadot/types/interfaces';
import { FrameSystemAccountInfo } from '@polkadot/types/lookup';
import { BN, formatBalance } from '@polkadot/util';
function balanceOf (number: number | string): U128 {
return new U128(new TypeRegistry(), number);
}
function defaultAccountBalance (): DeriveBalancesAll {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
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, FrameSystemAccountInfo]): 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;