Skip to content

Commit 366fef5

Browse files
yashvanthbl137-cryptoclaude
authored andcommitted
feat(statics): add CoinMap.getOrUndefined() safe accessor
CoinMap.get() throws CoinNotDefinedError for unknown coins, meaning optional-chaining (?.) provides no protection. Add getOrUndefined() which returns undefined for unknown coins and delegates to it from get(), making the missing-coin case explicit in the type system. Ticket: CGD-255 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Session-Id: 6a81412d-5c4e-462d-959a-3eda803f708c Task-Id: 562723cc-ae5d-41b5-96c7-d8a533be687c
1 parent 10b7ff0 commit 366fef5

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

modules/statics/src/map.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,30 @@ export class CoinMap {
215215
* @return {BaseCoin}
216216
*/
217217
public get(key: string): Readonly<BaseCoin> {
218-
const coin =
219-
this._map.get(key) ||
220-
this._coinByIds.get(key) ||
221-
this._coinByAliases.get(key) ||
222-
this._coinByContractAddress.get(key) ||
223-
this._coinByNftCollectionID.get(key);
224-
218+
const coin = this.getOrUndefined(key);
225219
if (coin) {
226220
return coin;
227221
}
228-
229222
throw new CoinNotDefinedError(key);
230223
}
231224

225+
/**
226+
* Safe accessor that returns undefined instead of throwing when a coin is not found.
227+
* Prefer this over `get` at call sites where the coin name is not guaranteed to be
228+
* registered — it makes the missing-coin case explicit in the type system.
229+
* @param {string} key
230+
* @return {BaseCoin | undefined}
231+
*/
232+
public getOrUndefined(key: string): Readonly<BaseCoin> | undefined {
233+
return (
234+
this._map.get(key) ||
235+
this._coinByIds.get(key) ||
236+
this._coinByAliases.get(key) ||
237+
this._coinByContractAddress.get(key) ||
238+
this._coinByNftCollectionID.get(key)
239+
);
240+
}
241+
232242
public has(key: string): boolean {
233243
return (
234244
this._map.has(key) ||

modules/statics/test/unit/coins.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,24 @@ describe('CoinMap', function () {
781781
btcById.should.deepEqual(btc);
782782
});
783783

784+
it('getOrUndefined should return coin for known key', () => {
785+
const btc = coins.getOrUndefined('btc');
786+
should(btc).not.be.undefined();
787+
btc!.name.should.equal('btc');
788+
});
789+
790+
it('getOrUndefined should return undefined for unknown key', () => {
791+
const result = coins.getOrUndefined('zzzz:TBD:no_such_coin');
792+
should(result).be.undefined();
793+
});
794+
795+
it('getOrUndefined should return coin by id', () => {
796+
const btc = coins.get('btc');
797+
const btcById = coins.getOrUndefined(btc.id);
798+
should(btcById).not.be.undefined();
799+
btcById!.should.deepEqual(btc);
800+
});
801+
784802
it('should get coin by address', () => {
785803
const weth = coins.get('weth');
786804
const wethByAddress = coins.get(`${weth.family}:${(weth as Erc20Coin).contractAddress}`);

0 commit comments

Comments
 (0)