Skip to content

Commit 33b5fe8

Browse files
authored
Merge pull request #5078 from BitGo/SC-512
feat(statics): add new map of address to coin
2 parents da9401e + 220b029 commit 33b5fe8

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

modules/statics/src/map.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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 -> the key is the family:contractAddress
11+
// the family is the where the coin is e.g l1 chains like eth, bsc etc. or l2 like arbeth, celo etc.
12+
private readonly _coinByContractAddress = new Map<string, Readonly<BaseCoin>>();
913

1014
private constructor() {
1115
// Do not instantiate
@@ -30,6 +34,9 @@ export class CoinMap {
3034
}
3135
coinMap._coinByAliases.set(alias, coin);
3236
}
37+
if (coin.isToken && coin instanceof ContractAddressDefinedToken) {
38+
coinMap._coinByContractAddress.set(`${coin.family}:${coin.contractAddress}`, coin);
39+
}
3340
return coinMap;
3441
}, new CoinMap());
3542
}
@@ -60,7 +67,11 @@ export class CoinMap {
6067
* @return {BaseCoin}
6168
*/
6269
public get(key: string): Readonly<BaseCoin> {
63-
const coin = this._map.get(key) || this._coinByIds.get(key) || this._coinByAliases.get(key);
70+
const coin =
71+
this._map.get(key) ||
72+
this._coinByIds.get(key) ||
73+
this._coinByAliases.get(key) ||
74+
this._coinByContractAddress.get(key);
6475

6576
if (coin) {
6677
return coin;
@@ -70,7 +81,12 @@ export class CoinMap {
7081
}
7182

7283
public has(key: string): boolean {
73-
return this._map.has(key) || this._coinByIds.has(key) || this._coinByAliases.has(key);
84+
return (
85+
this._map.has(key) ||
86+
this._coinByIds.has(key) ||
87+
this._coinByAliases.has(key) ||
88+
this._coinByContractAddress.has(key)
89+
);
7490
}
7591

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

modules/statics/test/unit/coins.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,15 @@ describe('CoinMap', function () {
352352
btcById.should.deepEqual(btc);
353353
});
354354

355+
it('should get coin by address', () => {
356+
const weth = coins.get('weth');
357+
const wethByAddress = coins.get(`${weth.family}:${(weth as Erc20Coin).contractAddress}`);
358+
wethByAddress.should.deepEqual(weth);
359+
const tweth = coins.get('tweth');
360+
const twethByAddress = coins.get(`${tweth.family}:${(tweth as Erc20Coin).contractAddress}`);
361+
twethByAddress.should.deepEqual(tweth);
362+
});
363+
355364
it('should find coin by id', () => {
356365
coins.has(btc.id).should.be.true();
357366
});

0 commit comments

Comments
 (0)