forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mangata.ts
79 lines (69 loc) · 2.41 KB
/
mangata.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
// Copyright 2017-2024 @polkadot/apps-config authors & contributors
// SPDX-License-Identifier: Apache-2.0
// structs need to be in order
/* eslint-disable sort-keys */
import type { Observable } from 'rxjs';
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { DeriveBalancesAll } from '@polkadot/api-derive/types';
import type { Balance } from '@polkadot/types/interfaces';
import type { FrameSystemAccountInfo } from '@polkadot/types/lookup';
import type { OverrideBundleDefinition } from '@polkadot/types/types';
import { mangataTypesBundleForPolkadotApps } from '@mangata-finance/type-definitions';
import { combineLatest, map } from 'rxjs';
import { memo } from '@polkadot/api-derive/util';
import { TypeRegistry, U128 } from '@polkadot/types';
import { BN } 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),
vestingLocked: balanceOf(0)
} as any;
}
interface OrmlAccountData {
free: Balance,
reserved: Balance,
frozen: Balance,
}
export function getBalance (
instanceId: string,
api: ApiInterfaceRx
): () => Observable<DeriveBalancesAll> {
return memo(
instanceId,
(account: string): Observable<DeriveBalancesAll> =>
combineLatest<[any, any]>([api.query.tokens.accounts(account, 0), 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
}
},
...mangataTypesBundleForPolkadotApps
};
export default definitions;