Skip to content

Commit e910a7e

Browse files
committed
feat(statics): add new map of address to coin
TICKET: SC-512
1 parent da9401e commit e910a7e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

modules/statics/src/map.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { BaseCoin } from './base';
22
import { DuplicateCoinDefinitionError, CoinNotDefinedError, DuplicateCoinIdDefinitionError } from './errors';
3+
import { ContractAddressDefinedToken } from './account';
34

45
export class CoinMap {
56
private readonly _map = new Map<string, Readonly<BaseCoin>>();
67
private readonly _coinByIds = new Map<string, Readonly<BaseCoin>>();
78
// Holds key equivalences used during an asset name migration
89
private readonly _coinByAliases = new Map<string, Readonly<BaseCoin>>();
10+
// map of coin by address -> l1:address
11+
private readonly _coinByContractAddress = new Map<string, Readonly<BaseCoin>>();
912

1013
private constructor() {
1114
// Do not instantiate
@@ -30,6 +33,9 @@ export class CoinMap {
3033
}
3134
coinMap._coinByAliases.set(alias, coin);
3235
}
36+
if (coin.isToken && coin instanceof ContractAddressDefinedToken) {
37+
coinMap._coinByContractAddress.set(`${coin.family}:${coin.contractAddress}`, coin);
38+
}
3339
return coinMap;
3440
}, new CoinMap());
3541
}
@@ -60,7 +66,11 @@ export class CoinMap {
6066
* @return {BaseCoin}
6167
*/
6268
public get(key: string): Readonly<BaseCoin> {
63-
const coin = this._map.get(key) || this._coinByIds.get(key) || this._coinByAliases.get(key);
69+
const coin =
70+
this._map.get(key) ||
71+
this._coinByIds.get(key) ||
72+
this._coinByAliases.get(key) ||
73+
this._coinByContractAddress.get(key);
6474

6575
if (coin) {
6676
return coin;
@@ -70,7 +80,12 @@ export class CoinMap {
7080
}
7181

7282
public has(key: string): boolean {
73-
return this._map.has(key) || this._coinByIds.has(key) || this._coinByAliases.has(key);
83+
return (
84+
this._map.has(key) ||
85+
this._coinByIds.has(key) ||
86+
this._coinByAliases.has(key) ||
87+
this._coinByContractAddress.has(key)
88+
);
7489
}
7590

7691
public map<T>(mapper: (coin: Readonly<BaseCoin>, coinName: string) => T): T[] {

0 commit comments

Comments
 (0)