Skip to content

Commit

Permalink
Added cache for functions that uses Coin Gecko
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaro-mota-funttastic committed Nov 28, 2023
1 parent a56a5eb commit 45dab94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/connectors/kujira/kujira.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ export namespace KujiraConfig {
marketsData: configManager.get('kujira.cache.marketsData') || 3600, // in seconds
markets: configManager.get('kujira.cache.markets') || 3600, // in seconds
tokens: configManager.get('kujira.cache.markets') || 3600, // in seconds
coinGeckoCoins: configManager.get('kujira.cache.coinGeckoCoins') || 3600, // in seconds
fetchCoinGecko: configManager.get('kujira.cache.fetchCoinGecko') || 3600, // in seconds
getTicker: configManager.get('kujira.cache.getTicker') || 60 * 5, // in seconds
getAllTokensQuotationsInUSD:
configManager.get('kujira.cache.getAllTokensQuotationsInUSD') || 3600, // in seconds
getKujiraTokenSymbolsToCoinGeckoIdsMap:
configManager.get(
'kujira.cache.getKujiraTokenSymbolsToCoinGeckoIdsMap'
) || 3600, // in seconds
},
coinGecko: {
coinsUrl:
Expand Down
32 changes: 23 additions & 9 deletions src/connectors/kujira/kujira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,12 @@ const caches = {
instances: new CacheContainer(new MemoryStorage()),
tokens: new CacheContainer(new MemoryStorage()),
markets: new CacheContainer(new MemoryStorage()),
coinGeckoCoins: new CacheContainer(new MemoryStorage()),
fetchCoinGecko: new CacheContainer(new MemoryStorage()),
getTicker: new CacheContainer(new MemoryStorage()),
getAllTokensQuotationsInUSD: new CacheContainer(new MemoryStorage()),
getKujiraTokenSymbolsToCoinGeckoIdsMap: new CacheContainer(
new MemoryStorage()
),
};

const config = KujiraConfig.config;
Expand Down Expand Up @@ -778,20 +783,15 @@ export class Kujira {
return output;
}

@Cache(caches.coinGeckoCoins, { ttl: config.cache.coinGeckoCoins })
async getKujiraTokenSymbolsToCoinGeckoIdsMap(
_options?: any,
_network?: string
): Promise<GetKujiraTokenSymbolsToCoinGeckoTokenIdsMapResponse> {
const output = IMap<TokenSymbol, CoinGeckoId | undefined>().asMutable();

@Cache(caches.fetchCoinGecko, { ttl: config.cache.fetchCoinGecko })
async fetchCoinGecko(): Promise<any> {
const apiKeys = config.coinGecko.apiKeys;
const randomIndex = Math.floor(Math.random() * apiKeys.length);
const apiKey = apiKeys[randomIndex];

const finalUrl = config.coinGecko.coinsUrl.replace('{apiKey}', apiKey);

const result: any = (
return (
await runWithRetryAndTimeout(
axios,
axios.get,
Expand All @@ -800,6 +800,18 @@ export class Kujira {
0
)
).data;
}

@Cache(caches.getKujiraTokenSymbolsToCoinGeckoIdsMap, {
ttl: config.cache.getKujiraTokenSymbolsToCoinGeckoIdsMap,
})
async getKujiraTokenSymbolsToCoinGeckoIdsMap(
_options?: any,
_network?: string
): Promise<GetKujiraTokenSymbolsToCoinGeckoTokenIdsMapResponse> {
const output = IMap<TokenSymbol, CoinGeckoId | undefined>().asMutable();

const result: any = await this.fetchCoinGecko();

const coinGeckoSymbolsToIdsMap = IMap<
CoinGeckoSymbol,
Expand Down Expand Up @@ -982,6 +994,7 @@ export class Kujira {
*
* @param options
*/
@Cache(caches.getTicker, { ttl: config.cache.getTicker })
async getTicker(options: GetTickerRequest): Promise<GetTickerResponse> {
const market = await this.getMarket(
options.marketId ? { id: options.marketId } : { name: options.marketName }
Expand Down Expand Up @@ -1126,6 +1139,7 @@ export class Kujira {
return await this.getTickers({ marketIds });
}

@Cache(caches.getAllTokensQuotationsInUSD, { ttl: config.cache.getTicker })
async getAllTokensQuotationsInUSD(
_options: any
): Promise<IMap<TokenId, Price>> {
Expand Down

0 comments on commit 45dab94

Please sign in to comment.