File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,30 @@ export class CoinMap {
5151 }
5252 }
5353
54+ /**
55+ * Replace a Base coin object completely from the CoinMap using its ID.
56+ * @param {string } key key to search the old coin object
57+ * @param {Readonly<BaseCoin> } coin new coin object
58+ */
59+ public replace ( coin : Readonly < BaseCoin > ) : void {
60+ if ( this . has ( coin . id ) ) {
61+ const oldCoin = this . get ( coin . id ) ;
62+ this . _map . delete ( oldCoin . name ) ;
63+ this . _coinByIds . delete ( oldCoin . id ) ;
64+ if ( oldCoin . alias ) {
65+ this . _coinByAliases . delete ( oldCoin . alias ) ;
66+ }
67+ if ( oldCoin . isToken ) {
68+ if ( oldCoin instanceof ContractAddressDefinedToken ) {
69+ this . _coinByContractAddress . delete ( `${ oldCoin . family } :${ oldCoin . contractAddress } ` ) ;
70+ } else if ( oldCoin instanceof NFTCollectionIdDefinedToken ) {
71+ this . _coinByNftCollectionID . delete ( `${ oldCoin . prefix } ${ oldCoin . family } :${ oldCoin . nftCollectionId } ` ) ;
72+ }
73+ }
74+ }
75+ this . addCoin ( coin ) ;
76+ }
77+
5478 static coinNameFromChainId ( chainId : number ) : string {
5579 const ethLikeCoinFromChainId : Record < number , string > = {
5680 1 : 'eth' ,
Original file line number Diff line number Diff line change @@ -792,6 +792,15 @@ describe('CoinMap', function () {
792792 coinMap . addCoin ( token ) ;
793793 coinMap . has ( token . name ) . should . be . true ( ) ;
794794 } ) ;
795+
796+ it ( 'should replace a coin' , ( ) => {
797+ const coinMap = CoinMap . fromCoins ( [ ] ) ;
798+ const coin = coins . get ( 'btc' ) ;
799+ const newCoin = { ...coin , name : 'btc2' } ;
800+ coinMap . replace ( newCoin ) ;
801+ coinMap . has ( coin . name ) . should . be . false ( ) ;
802+ coinMap . has ( newCoin . name ) . should . be . true ( ) ;
803+ } ) ;
795804} ) ;
796805
797806coins . forEach ( ( coin , coinName ) => {
You can’t perform that action at this time.
0 commit comments