Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion packages/currency/src/currencyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class CurrencyManager<TMeta = unknown> implements CurrencyTypes.ICurrency
private readonly legacyTokens: CurrencyTypes.LegacyTokenMap;
private readonly conversionPairs: CurrencyTypes.AggregatorsMap;

private readonly knownCurrenciesById: Map<string, CurrencyTypes.CurrencyDefinition<TMeta>>;

private static defaultInstance: CurrencyManager;

/**
Expand All @@ -41,6 +43,10 @@ export class CurrencyManager<TMeta = unknown> implements CurrencyTypes.ICurrency
}
this.knownCurrencies.push(currency);
}

this.knownCurrenciesById = new Map(
this.knownCurrencies.map((knownCurrency) => [knownCurrency.id, knownCurrency]),
);
this.legacyTokens = legacyTokens || CurrencyManager.getDefaultLegacyTokens();
this.conversionPairs = conversionPairs || CurrencyManager.getDefaultConversionPairs();
}
Expand Down Expand Up @@ -84,7 +90,7 @@ export class CurrencyManager<TMeta = unknown> implements CurrencyTypes.ICurrency
* Gets a supported currency from its CurrencyTypes.CurrencyDefinition id
*/
fromId(id: string): CurrencyTypes.CurrencyDefinition<TMeta> | undefined {
return this.knownCurrencies.find((knownCurrency) => knownCurrency.id === id);
return this.knownCurrenciesById.get(id);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/currency/test/currencyManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ describe('CurrencyManager', () => {
});

describe('Accessing currencies', () => {
it('access a common token by its id', () => {
expect(currencyManager.fromId('USDC-multichain-moonbeam')).toMatchObject({
symbol: 'USDC-multichain',
decimals: 6,
});
});

it('access a common token by its symbol', () => {
expect(currencyManager.from('DAI')).toMatchObject({
symbol: 'DAI',
Expand Down