This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 454
/
supported_tokens.ts
200 lines (180 loc) · 5.91 KB
/
supported_tokens.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright © 2022 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { NotFoundError } from '@liskhq/lisk-db';
import { BaseStore, ImmutableStoreGetter, StoreGetter } from '../../base_store';
import { getMainchainID } from '../../interoperability/utils';
import { CHAIN_ID_LENGTH, LOCAL_ID_LENGTH, TOKEN_ID_LENGTH } from '../constants';
import { splitTokenID } from '../utils';
export interface SupportedTokensStoreData {
supportedTokenIDs: Buffer[];
}
export const supportedTokensStoreSchema = {
$id: '/token/store/supportedTokens',
type: 'object',
required: ['supportedTokenIDs'],
properties: {
supportedTokenIDs: {
type: 'array',
fieldNumber: 1,
items: {
dataType: 'bytes',
minLength: TOKEN_ID_LENGTH,
maxLength: TOKEN_ID_LENGTH,
},
},
},
};
export const ALL_SUPPORTED_TOKENS_KEY = Buffer.alloc(0);
export class SupportedTokensStore extends BaseStore<SupportedTokensStoreData> {
public schema = supportedTokensStoreSchema;
private _ownChainID!: Buffer;
public registerOwnChainID(ownChainID: Buffer) {
this._ownChainID = ownChainID;
}
public async allSupported(context: ImmutableStoreGetter): Promise<boolean> {
return this.has(context, ALL_SUPPORTED_TOKENS_KEY);
}
public async isSupported(context: ImmutableStoreGetter, tokenID: Buffer): Promise<boolean> {
if (this._isMainchainOrNative(tokenID)) {
return true;
}
const allSupported = await this.allSupported(context);
if (allSupported) {
return true;
}
const chainID = tokenID.slice(0, CHAIN_ID_LENGTH);
try {
const supported = await this.get(context, chainID);
if (
supported.supportedTokenIDs.length === 0 ||
supported.supportedTokenIDs.findIndex(id => id.equals(tokenID)) > -1
) {
return true;
}
} catch (error) {
if (!(error instanceof NotFoundError)) {
throw error;
}
}
return false;
}
public async getAll(
context: ImmutableStoreGetter,
): Promise<{ key: Buffer; value: SupportedTokensStoreData }[]> {
return this.iterate(context, {
gte: Buffer.alloc(TOKEN_ID_LENGTH, 0),
lte: Buffer.alloc(TOKEN_ID_LENGTH, 255),
});
}
public async removeAll(context: StoreGetter): Promise<void> {
const allSupportedTokens = await this.getAll(context);
for (const { key } of allSupportedTokens) {
await this.del(context, key);
}
}
public async supportAll(context: StoreGetter): Promise<void> {
await this.removeAll(context);
await this.set(context, ALL_SUPPORTED_TOKENS_KEY, { supportedTokenIDs: [] });
}
public async supportChain(context: StoreGetter, chainID: Buffer): Promise<void> {
// LSK or native token do not need entry
if (this._isMainchainOrNative(Buffer.concat([chainID, Buffer.alloc(LOCAL_ID_LENGTH)]))) {
return;
}
const allSupported = await this.allSupported(context);
if (allSupported) {
return;
}
await this.set(context, chainID, { supportedTokenIDs: [] });
}
public async removeSupportForChain(context: StoreGetter, chainID: Buffer): Promise<void> {
// LSK or native token do not need entry
if (this._isMainchainOrNative(Buffer.concat([chainID, Buffer.alloc(LOCAL_ID_LENGTH)]))) {
return;
}
const allSupported = await this.allSupported(context);
if (allSupported) {
throw new Error('Invalid operation. All tokens from all chains are supported.');
}
if (chainID.equals(this._ownChainID)) {
throw new Error(
'Invalid operation. All tokens from all the specified chain should be supported.',
);
}
const supportExist = await this.has(context, chainID);
if (!supportExist) {
return;
}
await this.del(context, chainID);
}
public async supportToken(context: StoreGetter, tokenID: Buffer): Promise<void> {
// LSK or native token do not need entry
if (this._isMainchainOrNative(tokenID)) {
return;
}
const allSupported = await this.allSupported(context);
if (allSupported) {
return;
}
const [chainID] = splitTokenID(tokenID);
let supported: SupportedTokensStoreData = { supportedTokenIDs: [] };
try {
supported = await this.get(context, chainID);
if (supported.supportedTokenIDs.length === 0) {
return;
}
} catch (error) {
if (!(error instanceof NotFoundError)) {
throw error;
}
}
supported.supportedTokenIDs.push(tokenID);
supported.supportedTokenIDs.sort((a, b) => a.compare(b));
await this.del(context, ALL_SUPPORTED_TOKENS_KEY);
await this.set(context, chainID, supported);
}
public async removeSupportForToken(context: StoreGetter, tokenID: Buffer): Promise<void> {
if (this._isMainchainOrNative(tokenID)) {
throw new Error('Cannot remove support for LSK or native token.');
}
const allSupported = await this.allSupported(context);
if (allSupported) {
throw new Error('All tokens are supported.');
}
const [chainID] = splitTokenID(tokenID);
const supportExist = await this.has(context, chainID);
if (!supportExist) {
return;
}
const supported = await this.get(context, chainID);
if (supported.supportedTokenIDs.length === 0) {
throw new Error('All tokens from the specified chain are supported.');
}
const index = supported.supportedTokenIDs.findIndex(id => id.equals(tokenID));
if (index >= 0) {
supported.supportedTokenIDs.splice(index, 1);
await this.set(context, chainID, supported);
if (supported.supportedTokenIDs.length === 0) {
await this.del(context, chainID);
}
}
}
private _isMainchainOrNative(tokenID: Buffer): boolean {
const [chainID] = splitTokenID(tokenID);
if (chainID.equals(this._ownChainID)) {
return true;
}
return chainID[0] === this._ownChainID[0] && getMainchainID(chainID).equals(chainID);
}
}