Skip to content

Commit

Permalink
Updated the cache system
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaro-mota-funttastic committed Nov 29, 2023
1 parent 45dab94 commit 8e4a13c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 56 deletions.
10 changes: 2 additions & 8 deletions src/connectors/kujira/kujira.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
76 changes: 28 additions & 48 deletions src/connectors/kujira/kujira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -783,8 +779,29 @@ export class Kujira {
return output;
}

@Cache(caches.fetchCoinGecko, { ttl: config.cache.fetchCoinGecko })
async fetchCoinGecko(): Promise<any> {
@Cache(caches.fetchCoinGeckoPrices, { ttl: config.cache.fetchCoinGeckoPrices })
async fetchCoinGeckoPrices(coinGeckoIds: string): Promise<any> {
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<any> {
const apiKeys = config.coinGecko.apiKeys;
const randomIndex = Math.floor(Math.random() * apiKeys.length);
const apiKey = apiKeys[randomIndex];
Expand All @@ -802,16 +819,13 @@ export class Kujira {
).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 result: any = await this.fetchCoinGeckoCoins();

const coinGeckoSymbolsToIdsMap = IMap<
CoinGeckoSymbol,
Expand Down Expand Up @@ -994,7 +1008,6 @@ 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 @@ -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 = {
Expand Down Expand Up @@ -1139,7 +1136,6 @@ export class Kujira {
return await this.getTickers({ marketIds });
}

@Cache(caches.getAllTokensQuotationsInUSD, { ttl: config.cache.getTicker })
async getAllTokensQuotationsInUSD(
_options: any
): Promise<IMap<TokenId, Price>> {
Expand All @@ -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(
{},
Expand Down
6 changes: 6 additions & 0 deletions src/services/schema/kujira-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
},
"markets": {
"type": "integer"
},
"coinGeckoCoins": {
"type": "integer"
},
"coinGeckoPrices": {
"type": "integer"
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions src/templates/kujira.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 8e4a13c

Please sign in to comment.