diff --git a/modules/statics/src/map.ts b/modules/statics/src/map.ts index 31314c9b22..c2577637f1 100644 --- a/modules/statics/src/map.ts +++ b/modules/statics/src/map.ts @@ -215,20 +215,30 @@ export class CoinMap { * @return {BaseCoin} */ public get(key: string): Readonly { - const coin = - this._map.get(key) || - this._coinByIds.get(key) || - this._coinByAliases.get(key) || - this._coinByContractAddress.get(key) || - this._coinByNftCollectionID.get(key); - + const coin = this.getOrUndefined(key); if (coin) { return coin; } - throw new CoinNotDefinedError(key); } + /** + * Safe accessor that returns undefined instead of throwing when a coin is not found. + * Prefer this over `get` at call sites where the coin name is not guaranteed to be + * registered — it makes the missing-coin case explicit in the type system. + * @param {string} key + * @return {BaseCoin | undefined} + */ + public getOrUndefined(key: string): Readonly | undefined { + return ( + this._map.get(key) || + this._coinByIds.get(key) || + this._coinByAliases.get(key) || + this._coinByContractAddress.get(key) || + this._coinByNftCollectionID.get(key) + ); + } + public has(key: string): boolean { return ( this._map.has(key) || diff --git a/modules/statics/test/unit/coins.ts b/modules/statics/test/unit/coins.ts index 8ebbda4fa1..aa3726b07d 100644 --- a/modules/statics/test/unit/coins.ts +++ b/modules/statics/test/unit/coins.ts @@ -781,6 +781,24 @@ describe('CoinMap', function () { btcById.should.deepEqual(btc); }); + it('getOrUndefined should return coin for known key', () => { + const btc = coins.getOrUndefined('btc'); + should(btc).not.be.undefined(); + btc!.name.should.equal('btc'); + }); + + it('getOrUndefined should return undefined for unknown key', () => { + const result = coins.getOrUndefined('zzzz:TBD:no_such_coin'); + should(result).be.undefined(); + }); + + it('getOrUndefined should return coin by id', () => { + const btc = coins.get('btc'); + const btcById = coins.getOrUndefined(btc.id); + should(btcById).not.be.undefined(); + btcById!.should.deepEqual(btc); + }); + it('should get coin by address', () => { const weth = coins.get('weth'); const wethByAddress = coins.get(`${weth.family}:${(weth as Erc20Coin).contractAddress}`);