From 45dab949e5779bd4e2c4126ad41b64d09b3cc344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Tue, 28 Nov 2023 19:09:24 -0300 Subject: [PATCH 1/2] Added cache for functions that uses Coin Gecko --- src/connectors/kujira/kujira.config.ts | 9 +++++++- src/connectors/kujira/kujira.ts | 32 ++++++++++++++++++-------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/connectors/kujira/kujira.config.ts b/src/connectors/kujira/kujira.config.ts index fdc3f9bdef..aafd9092db 100644 --- a/src/connectors/kujira/kujira.config.ts +++ b/src/connectors/kujira/kujira.config.ts @@ -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: diff --git a/src/connectors/kujira/kujira.ts b/src/connectors/kujira/kujira.ts index 1baa31c7b5..c76b09cf29 100644 --- a/src/connectors/kujira/kujira.ts +++ b/src/connectors/kujira/kujira.ts @@ -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; @@ -778,20 +783,15 @@ export class Kujira { return output; } - @Cache(caches.coinGeckoCoins, { ttl: config.cache.coinGeckoCoins }) - async getKujiraTokenSymbolsToCoinGeckoIdsMap( - _options?: any, - _network?: string - ): Promise { - const output = IMap().asMutable(); - + @Cache(caches.fetchCoinGecko, { ttl: config.cache.fetchCoinGecko }) + async fetchCoinGecko(): Promise { 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, @@ -800,6 +800,18 @@ export class Kujira { 0 ) ).data; + } + + @Cache(caches.getKujiraTokenSymbolsToCoinGeckoIdsMap, { + ttl: config.cache.getKujiraTokenSymbolsToCoinGeckoIdsMap, + }) + async getKujiraTokenSymbolsToCoinGeckoIdsMap( + _options?: any, + _network?: string + ): Promise { + const output = IMap().asMutable(); + + const result: any = await this.fetchCoinGecko(); const coinGeckoSymbolsToIdsMap = IMap< CoinGeckoSymbol, @@ -982,6 +994,7 @@ export class Kujira { * * @param options */ + @Cache(caches.getTicker, { ttl: config.cache.getTicker }) async getTicker(options: GetTickerRequest): Promise { const market = await this.getMarket( options.marketId ? { id: options.marketId } : { name: options.marketName } @@ -1126,6 +1139,7 @@ export class Kujira { return await this.getTickers({ marketIds }); } + @Cache(caches.getAllTokensQuotationsInUSD, { ttl: config.cache.getTicker }) async getAllTokensQuotationsInUSD( _options: any ): Promise> { From 8e4a13cbafb510043e30205b93ba27e55421889a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20E=2E=20F=2E=20Mota?= Date: Wed, 29 Nov 2023 17:59:55 -0300 Subject: [PATCH 2/2] Updated the cache system --- src/connectors/kujira/kujira.config.ts | 10 +--- src/connectors/kujira/kujira.ts | 76 ++++++++++---------------- src/services/schema/kujira-schema.json | 6 ++ src/templates/kujira.yml | 2 + 4 files changed, 38 insertions(+), 56 deletions(-) diff --git a/src/connectors/kujira/kujira.config.ts b/src/connectors/kujira/kujira.config.ts index aafd9092db..a27fa1ba85 100644 --- a/src/connectors/kujira/kujira.config.ts +++ b/src/connectors/kujira/kujira.config.ts @@ -117,14 +117,8 @@ 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 - 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 + fetchCoinGeckoCoins: configManager.get('kujira.cache.coinGeckoCoins') || 86400, // in seconds + fetchCoinGeckoPrices: configManager.get('kujira.cache.coinGeckoPrices') || 300 , // in seconds }, coinGecko: { coinsUrl: diff --git a/src/connectors/kujira/kujira.ts b/src/connectors/kujira/kujira.ts index c76b09cf29..9e36422566 100644 --- a/src/connectors/kujira/kujira.ts +++ b/src/connectors/kujira/kujira.ts @@ -175,12 +175,8 @@ const caches = { instances: new CacheContainer(new MemoryStorage()), tokens: new CacheContainer(new MemoryStorage()), markets: new CacheContainer(new MemoryStorage()), - fetchCoinGecko: new CacheContainer(new MemoryStorage()), - getTicker: new CacheContainer(new MemoryStorage()), - getAllTokensQuotationsInUSD: new CacheContainer(new MemoryStorage()), - getKujiraTokenSymbolsToCoinGeckoIdsMap: new CacheContainer( - new MemoryStorage() - ), + fetchCoinGeckoCoins: new CacheContainer(new MemoryStorage()), + fetchCoinGeckoPrices: new CacheContainer(new MemoryStorage()), }; const config = KujiraConfig.config; @@ -783,8 +779,29 @@ export class Kujira { return output; } - @Cache(caches.fetchCoinGecko, { ttl: config.cache.fetchCoinGecko }) - async fetchCoinGecko(): Promise { + @Cache(caches.fetchCoinGeckoPrices, { ttl: config.cache.fetchCoinGeckoPrices }) + async fetchCoinGeckoPrices(coinGeckoIds: string): Promise { + const apiKeys = config.coinGecko.apiKeys; + const randomIndex = Math.floor(Math.random() * apiKeys.length); + const apiKey = apiKeys[randomIndex]; + + const finalUrl = config.coinGecko.priceUrl + .replace('{apiKey}', apiKey) + .replace('{targets}', coinGeckoIds); + + return ( + await runWithRetryAndTimeout( + axios, + axios.get, + [finalUrl], + config.retry.all.maxNumberOfRetries, + 0 + ) + ).data; + } + + @Cache(caches.fetchCoinGeckoCoins, { ttl: config.cache.fetchCoinGeckoCoins }) + async fetchCoinGeckoCoins(): Promise { const apiKeys = config.coinGecko.apiKeys; const randomIndex = Math.floor(Math.random() * apiKeys.length); const apiKey = apiKeys[randomIndex]; @@ -802,16 +819,13 @@ export class Kujira { ).data; } - @Cache(caches.getKujiraTokenSymbolsToCoinGeckoIdsMap, { - ttl: config.cache.getKujiraTokenSymbolsToCoinGeckoIdsMap, - }) async getKujiraTokenSymbolsToCoinGeckoIdsMap( _options?: any, _network?: string ): Promise { const output = IMap().asMutable(); - const result: any = await this.fetchCoinGecko(); + const result: any = await this.fetchCoinGeckoCoins(); const coinGeckoSymbolsToIdsMap = IMap< CoinGeckoSymbol, @@ -994,7 +1008,6 @@ export class Kujira { * * @param options */ - @Cache(caches.getTicker, { ttl: config.cache.getTicker }) async getTicker(options: GetTickerRequest): Promise { const market = await this.getMarket( options.marketId ? { id: options.marketId } : { name: options.marketName } @@ -1050,23 +1063,7 @@ export class Kujira { .concat(',') .concat(coinGeckoQuoteTokenId); - const apiKeys = config.coinGecko.apiKeys; - const randomIndex = Math.floor(Math.random() * apiKeys.length); - const apiKey = apiKeys[randomIndex]; - - const finalUrl = config.coinGecko.priceUrl - .replace('{apiKey}', apiKey) - .replace('{targets}', coinGeckoIds); - - result = ( - await runWithRetryAndTimeout( - axios, - axios.get, - [finalUrl], - config.retry.all.maxNumberOfRetries, - 0 - ) - ).data; + result = await this.fetchCoinGeckoPrices(coinGeckoIds) } const tokens = { @@ -1139,7 +1136,6 @@ export class Kujira { return await this.getTickers({ marketIds }); } - @Cache(caches.getAllTokensQuotationsInUSD, { ttl: config.cache.getTicker }) async getAllTokensQuotationsInUSD( _options: any ): Promise> { @@ -1152,23 +1148,7 @@ export class Kujira { .filter((id: any) => id && id.trim() !== '') .join(','); - const apiKeys = config.coinGecko.apiKeys; - const randomIndex = Math.floor(Math.random() * apiKeys.length); - const apiKey = apiKeys[randomIndex]; - - const finalUrl = config.coinGecko.priceUrl - .replace('{apiKey}', apiKey) - .replace('{targets}', coinGeckoIds); - - const result: any = ( - await runWithRetryAndTimeout( - axios, - axios.get, - [finalUrl], - config.retry.all.maxNumberOfRetries, - 0 - ) - ).data; + const result: any = await this.fetchCoinGeckoPrices(coinGeckoIds) const tokensSymbolsToTokensIdsMap = await this.getTokenSymbolsToTokenIdsMap( {}, diff --git a/src/services/schema/kujira-schema.json b/src/services/schema/kujira-schema.json index 23e89d3b1c..170a3685e5 100644 --- a/src/services/schema/kujira-schema.json +++ b/src/services/schema/kujira-schema.json @@ -44,6 +44,12 @@ }, "markets": { "type": "integer" + }, + "coinGeckoCoins": { + "type": "integer" + }, + "coinGeckoPrices": { + "type": "integer" } } }, diff --git a/src/templates/kujira.yml b/src/templates/kujira.yml index 9966cda9ca..66a178d876 100644 --- a/src/templates/kujira.yml +++ b/src/templates/kujira.yml @@ -40,6 +40,8 @@ orderBook: cache: marketsData: 3600 # in seconds markets: 3600 # in seconds + coinGeckoCoins: 86400 # in seconds + coinGeckoPrices: 300 # in seconds orders: create: fee: 'auto'