11import { BaseCoin } from './base' ;
22import { DuplicateCoinDefinitionError , CoinNotDefinedError , DuplicateCoinIdDefinitionError } from './errors' ;
3+ import { ContractAddressDefinedToken } from './account' ;
34
45export 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 [ ] {
0 commit comments