-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathmap.ts
More file actions
223 lines (207 loc) · 6.45 KB
/
map.ts
File metadata and controls
223 lines (207 loc) · 6.45 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { BaseCoin } from './base';
import { DuplicateCoinDefinitionError, CoinNotDefinedError, DuplicateCoinIdDefinitionError } from './errors';
import { ContractAddressDefinedToken, NFTCollectionIdDefinedToken } from './account';
export class CoinMap {
private readonly _map = new Map<string, Readonly<BaseCoin>>();
private readonly _coinByIds = new Map<string, Readonly<BaseCoin>>();
// Holds key equivalences used during an asset name migration
private readonly _coinByAliases = new Map<string, Readonly<BaseCoin>>();
// map of coin by address -> the key is the family:contractAddress
// the family is the where the coin is e.g l1 chains like eth, bsc etc. or l2 like arbeth, celo etc.
private readonly _coinByContractAddress = new Map<string, Readonly<BaseCoin>>();
// map of coin by NFT collection ID -> the key is the (t)family:nftCollectionID
private readonly _coinByNftCollectionID = new Map<string, Readonly<BaseCoin>>();
private constructor() {
// Do not instantiate
}
static fromCoins(coins: Readonly<BaseCoin>[]): CoinMap {
const coinMap = new CoinMap();
coins.forEach((coin) => {
coinMap.addCoin(coin);
});
return coinMap;
}
public addCoin(coin: Readonly<BaseCoin>): void {
if (this._map.has(coin.name)) {
throw new DuplicateCoinDefinitionError(coin.name);
}
if (this._coinByIds.has(coin.id)) {
throw new DuplicateCoinIdDefinitionError(coin.id);
}
const alias = coin.alias;
if (alias && this._coinByAliases.has(alias)) {
throw new DuplicateCoinDefinitionError(alias);
}
this._map.set(coin.name, coin);
this._coinByIds.set(coin.id, coin);
if (alias) {
this._coinByAliases.set(coin.alias, coin);
}
if (coin.isToken) {
if (coin instanceof ContractAddressDefinedToken) {
this._coinByContractAddress.set(`${coin.family}:${coin.contractAddress}`, coin);
} else if (coin instanceof NFTCollectionIdDefinedToken) {
this._coinByNftCollectionID.set(`${coin.prefix}${coin.family}:${coin.nftCollectionId}`, coin);
}
}
}
/**
* Replace a Base coin object completely from the CoinMap using its ID.
* @param {string} key key to search the old coin object
* @param {Readonly<BaseCoin>} coin new coin object
*/
public replace(coin: Readonly<BaseCoin>): void {
if (this.has(coin.id)) {
const oldCoin = this.get(coin.id);
this._map.delete(oldCoin.name);
this._coinByIds.delete(oldCoin.id);
if (oldCoin.alias) {
this._coinByAliases.delete(oldCoin.alias);
}
if (oldCoin.isToken) {
if (oldCoin instanceof ContractAddressDefinedToken) {
this._coinByContractAddress.delete(`${oldCoin.family}:${oldCoin.contractAddress}`);
} else if (oldCoin instanceof NFTCollectionIdDefinedToken) {
this._coinByNftCollectionID.delete(`${oldCoin.prefix}${oldCoin.family}:${oldCoin.nftCollectionId}`);
}
}
}
this.addCoin(coin);
}
static coinNameFromChainId(chainId: number): string {
const ethLikeCoinFromChainId: Record<number, string> = {
1: 'eth',
42: 'teth',
5: 'gteth',
560048: 'hteth',
10001: 'ethw',
80002: 'tpolygon',
137: 'polygon',
56: 'bsc',
97: 'tbsc',
42161: 'arbeth',
421614: 'tarbeth',
10: 'opeth',
11155420: 'topeth',
1116: 'coredao',
1114: 'tcoredao',
248: 'oas',
9372: 'toas',
14: 'flr',
114: 'tflr',
19: 'sgb',
16: 'tsgb',
1111: 'wemix',
1112: 'twemix',
50: 'xdc',
51: 'txdc',
80094: 'bera',
80069: 'tbera',
42220: 'celo',
11142220: 'tcelo',
2222: 'kava',
2221: 'tkava',
43114: 'avax',
43113: 'tavax',
100: 'gno',
130: 'uni',
324: 'zketh',
8453: 'baseeth',
84532: 'tbaseeth',
30143: 'mon',
10143: 'tmon',
480: 'world',
4801: 'tworld',
5031: 'somi',
50312: 'tstt',
1868: 'soneium',
1946: 'tsoneium',
33111: 'tapechain',
33139: 'apechain',
688688: 'tphrs',
102030: 'ctc',
102031: 'tctc',
998: 'thypeevm',
999: 'hypeevm',
16602: 'tog',
16661: 'og',
9746: 'txpl',
9745: 'xpl',
14601: 'tsonic',
146: 'sonic',
1328: 'tseievm',
1329: 'seievm',
1001: 'tkaia',
8217: 'kaia',
1270: 'tirys',
59141: 'tlineaeth',
59144: 'lineaeth',
1315: 'tip',
1514: 'ip',
545: 'tflow',
747: 'flow',
98867: 'tplume',
98866: 'plume',
6342: 'tmegaeth',
295: 'hbarevm',
296: 'thbarevm',
};
return ethLikeCoinFromChainId[chainId];
}
/**
* Override `get` to throw if a coin is missing, instead of returning undefined.
* It will honor key equivalences in case given key is missing.
* @param {string} key
* @return {BaseCoin}
*/
public get(key: string): Readonly<BaseCoin> {
const coin =
this._map.get(key) ||
this._coinByIds.get(key) ||
this._coinByAliases.get(key) ||
this._coinByContractAddress.get(key) ||
this._coinByNftCollectionID.get(key);
if (coin) {
return coin;
}
throw new CoinNotDefinedError(key);
}
public has(key: string): boolean {
return (
this._map.has(key) ||
this._coinByIds.has(key) ||
this._coinByAliases.has(key) ||
this._coinByContractAddress.has(key) ||
this._coinByNftCollectionID.has(key)
);
}
public map<T>(mapper: (coin: Readonly<BaseCoin>, coinName: string) => T): T[] {
const mapResult: T[] = [];
this._map.forEach((value, key) => {
mapResult.push(mapper(value, key));
});
return mapResult;
}
public reduce<T>(reducer: (acc: T, coin: Readonly<BaseCoin>, coinName: string) => T, initialValue: T): T {
let acc = initialValue;
this._map.forEach((value, key) => {
acc = reducer(acc, value, key);
});
return acc;
}
public filter(predicate: (coin: Readonly<BaseCoin>, coinName: string) => boolean): CoinMap {
const filterResult: Readonly<BaseCoin>[] = [];
this._map.forEach((value, key) => {
if (predicate(value, key)) {
filterResult.push(value);
}
});
return CoinMap.fromCoins(filterResult);
}
public forEach(callback: (coin: Readonly<BaseCoin>, coinName: string) => void): void {
this._map.forEach(callback);
}
public [Symbol.iterator](): IterableIterator<[string, Readonly<BaseCoin>]> {
return this._map[Symbol.iterator]();
}
}