Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions modules/statics/src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,30 @@ export class CoinMap {
* @return {BaseCoin}
*/
public get(key: string): Readonly<BaseCoin> {
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<BaseCoin> | 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) ||
Expand Down
18 changes: 18 additions & 0 deletions modules/statics/test/unit/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
Loading