Skip to content

Commit 3d86e26

Browse files
committed
feat(statics): add method to replace coin object in coin map
Ticket: WIN-6576
1 parent b14c597 commit 3d86e26

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

modules/statics/src/map.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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',

modules/statics/test/unit/coins.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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

797806
coins.forEach((coin, coinName) => {

0 commit comments

Comments
 (0)