diff --git a/src/connectors/kujira/kujira.config.ts b/src/connectors/kujira/kujira.config.ts index fdc3f9bdef..a27fa1ba85 100644 --- a/src/connectors/kujira/kujira.config.ts +++ b/src/connectors/kujira/kujira.config.ts @@ -117,7 +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 - coinGeckoCoins: configManager.get('kujira.cache.coinGeckoCoins') || 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 1baa31c7b5..9e36422566 100644 --- a/src/connectors/kujira/kujira.ts +++ b/src/connectors/kujira/kujira.ts @@ -175,7 +175,8 @@ const caches = { instances: new CacheContainer(new MemoryStorage()), tokens: new CacheContainer(new MemoryStorage()), markets: new CacheContainer(new MemoryStorage()), - coinGeckoCoins: new CacheContainer(new MemoryStorage()), + fetchCoinGeckoCoins: new CacheContainer(new MemoryStorage()), + fetchCoinGeckoPrices: new CacheContainer(new MemoryStorage()), }; const config = KujiraConfig.config; @@ -778,20 +779,36 @@ 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.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]; const finalUrl = config.coinGecko.coinsUrl.replace('{apiKey}', apiKey); - const result: any = ( + return ( await runWithRetryAndTimeout( axios, axios.get, @@ -800,6 +817,15 @@ export class Kujira { 0 ) ).data; + } + + async getKujiraTokenSymbolsToCoinGeckoIdsMap( + _options?: any, + _network?: string + ): Promise { + const output = IMap().asMutable(); + + const result: any = await this.fetchCoinGeckoCoins(); const coinGeckoSymbolsToIdsMap = IMap< CoinGeckoSymbol, @@ -1037,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 = { @@ -1138,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'