forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 5
/
equilibrium.ts
108 lines (85 loc) · 3.27 KB
/
equilibrium.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2017-2024 @polkadot/apps-config authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { Enum } from '@polkadot/types';
import type { AccountId, AccountIndex, Address, Balance } from '@polkadot/types/interfaces';
import type { OverrideBundleDefinition } from '@polkadot/types/types';
import type { Struct, u64, u128, Vec } from '@polkadot/types-codec';
import type { ITuple } from '@polkadot/types-codec/types';
import { map, of } from 'rxjs';
import { memo } from '@polkadot/api-derive/util';
import { U8aFixed } from '@polkadot/types-codec';
export interface SignedBalance extends Enum {
readonly isPositive: boolean;
readonly asPositive: Balance;
readonly isNegative: boolean;
readonly asNegative: Balance;
}
export const u64FromCurrency = (currency: string): bigint => {
const buf = Buffer.from(currency.toLowerCase());
const size = buf.length;
return buf.reduce((val, digit, i) => {
const exp = BigInt(size - 1 - i);
return BigInt(val) + BigInt(256) ** exp * BigInt(digit);
}, BigInt(0));
};
const TOKENS = ['eq'];
interface EqPrimitivesBalanceAccountData extends Enum {
readonly isV0: boolean;
readonly asV0: {
readonly lock: u128;
readonly balance: Vec<ITuple<[u64, EqPrimitivesSignedBalance]>>;
} & Struct;
readonly type: 'V0';
}
interface EqPrimitivesSignedBalance extends Enum {
readonly isPositive: boolean;
readonly asPositive: u128;
readonly isNegative: boolean;
readonly asNegative: u128;
readonly type: 'Positive' | 'Negative';
}
export const createDerives = (tokens: string[]) => tokens.reduce((prev, token, i) => {
const isNative = !i;
return {
...prev,
[token]: { customAccount: (instanceId: string, api: ApiInterfaceRx) => {
const { registry } = api;
const asset = u64FromCurrency(token);
return memo(instanceId, (address: AccountIndex | AccountId | Address | string) => api.query.system.account(address).pipe(map((v) => {
const data = (v as unknown as { data: EqPrimitivesBalanceAccountData }).data;
const miscFrozen = isNative ? data.asV0.lock : registry.createType('u128', 0);
const feeFrozen = miscFrozen;
const reserved = registry.createType('u128', 0);
const entry = data.asV0.balance.find(([assetId]) => {
return assetId.toBigInt() === asset;
});
const balance = entry?.[1];
const free = balance?.isPositive
? balance.asPositive
: registry.createType('u128', 0);
return {
feeFrozen, free, miscFrozen, reserved
};
})));
},
customLocks: (instanceId: string, api: ApiInterfaceRx) => {
const { registry } = api;
return memo(instanceId, (address: AccountIndex | AccountId | Address | string) => isNative
? api.query.system.account(address).pipe(map((v) => {
const data = (v as unknown as { data: EqPrimitivesBalanceAccountData }).data;
return [{
amount: data.asV0.lock,
id: new U8aFixed(registry),
reasons: ''
}];
}))
: of([]));
} }
};
}, {});
const definitions: OverrideBundleDefinition = {
derives: createDerives(TOKENS),
instances: { balances: TOKENS }
};
export default definitions;