11import { BaseCoin } from './base' ;
2- import {
3- DuplicateCoinDefinitionError ,
4- CoinNotDefinedError ,
5- DuplicateCoinIdDefinitionError ,
6- DuplicateContractAddressDefinitionError ,
7- DuplicateNftCollectionIdDefinitionError ,
8- } from './errors' ;
2+ import { DuplicateCoinDefinitionError , CoinNotDefinedError , DuplicateCoinIdDefinitionError } from './errors' ;
93import { ContractAddressDefinedToken , NFTCollectionIdDefinedToken } from './account' ;
104import { EthereumNetwork } from './networks' ;
115
@@ -14,10 +8,10 @@ export class CoinMap {
148 private readonly _coinByIds = new Map < string , Readonly < BaseCoin > > ( ) ;
159 // Holds key equivalences used during an asset name migration
1610 private readonly _coinByAliases = new Map < string , Readonly < BaseCoin > > ( ) ;
17- // map of coin by address -> the key is the family:networkType: contractAddress
11+ // map of coin by address -> the key is the family:contractAddress
1812 // the family is the where the coin is e.g l1 chains like eth, bsc etc. or l2 like arbeth, celo etc.
1913 private readonly _coinByContractAddress = new Map < string , Readonly < BaseCoin > > ( ) ;
20- // map of coin by NFT collection ID -> the key is the (t)family:networkType: nftCollectionID
14+ // map of coin by NFT collection ID -> the key is the (t)family:nftCollectionID
2115 private readonly _coinByNftCollectionID = new Map < string , Readonly < BaseCoin > > ( ) ;
2216 // Lazily initialized cache for chainId to coin name mapping (derived from network definitions)
2317 private _coinByChainId : Map < number , string > | null = null ;
@@ -26,14 +20,6 @@ export class CoinMap {
2620 // Do not instantiate
2721 }
2822
29- private static contractAddressKey ( coin : ContractAddressDefinedToken ) : string {
30- return `${ coin . family } :${ coin . network . type } :${ coin . contractAddress } ` ;
31- }
32-
33- private static nftCollectionIdKey ( coin : NFTCollectionIdDefinedToken ) : string {
34- return `${ coin . prefix } ${ coin . family } :${ coin . network . type } :${ coin . nftCollectionId } ` ;
35- }
36-
3723 static fromCoins ( coins : Readonly < BaseCoin > [ ] ) : CoinMap {
3824 const coinMap = new CoinMap ( ) ;
3925 coins . forEach ( ( coin ) => {
@@ -61,19 +47,9 @@ export class CoinMap {
6147
6248 if ( coin . isToken ) {
6349 if ( coin instanceof ContractAddressDefinedToken ) {
64- const contractAddressKey = CoinMap . contractAddressKey ( coin ) ;
65- const existingByContractAddress = this . _coinByContractAddress . get ( contractAddressKey ) ;
66- if ( existingByContractAddress ) {
67- throw new DuplicateContractAddressDefinitionError ( contractAddressKey , existingByContractAddress . name ) ;
68- }
69- this . _coinByContractAddress . set ( contractAddressKey , coin ) ;
50+ this . _coinByContractAddress . set ( `${ coin . family } :${ coin . contractAddress } ` , coin ) ;
7051 } else if ( coin instanceof NFTCollectionIdDefinedToken ) {
71- const nftCollectionKey = CoinMap . nftCollectionIdKey ( coin ) ;
72- const existingByNftCollectionId = this . _coinByNftCollectionID . get ( nftCollectionKey ) ;
73- if ( existingByNftCollectionId ) {
74- throw new DuplicateNftCollectionIdDefinitionError ( nftCollectionKey , existingByNftCollectionId . name ) ;
75- }
76- this . _coinByNftCollectionID . set ( nftCollectionKey , coin ) ;
52+ this . _coinByNftCollectionID . set ( `${ coin . prefix } ${ coin . family } :${ coin . nftCollectionId } ` , coin ) ;
7753 }
7854 }
7955 }
@@ -93,9 +69,9 @@ export class CoinMap {
9369 }
9470 if ( oldCoin . isToken ) {
9571 if ( oldCoin instanceof ContractAddressDefinedToken ) {
96- this . _coinByContractAddress . delete ( CoinMap . contractAddressKey ( oldCoin ) ) ;
72+ this . _coinByContractAddress . delete ( ` ${ oldCoin . family } : ${ oldCoin . contractAddress } ` ) ;
9773 } else if ( oldCoin instanceof NFTCollectionIdDefinedToken ) {
98- this . _coinByNftCollectionID . delete ( CoinMap . nftCollectionIdKey ( oldCoin ) ) ;
74+ this . _coinByNftCollectionID . delete ( ` ${ oldCoin . prefix } ${ oldCoin . family } : ${ oldCoin . nftCollectionId } ` ) ;
9975 }
10076 }
10177 }
0 commit comments