Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cache for functions that uses Coin Gecko #1

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/connectors/kujira/kujira.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
78 changes: 36 additions & 42 deletions src/connectors/kujira/kujira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -778,20 +779,36 @@ 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.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];

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

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

async getKujiraTokenSymbolsToCoinGeckoIdsMap(
_options?: any,
_network?: string
): Promise<GetKujiraTokenSymbolsToCoinGeckoTokenIdsMapResponse> {
const output = IMap<TokenSymbol, CoinGeckoId | undefined>().asMutable();

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

const coinGeckoSymbolsToIdsMap = IMap<
CoinGeckoSymbol,
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(
{},
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
Loading