Skip to content

Commit

Permalink
Adding new mechanism to rotate coingecko api keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
danilo-silva-funttastic committed Oct 18, 2023
1 parent b5bbca8 commit a56a5eb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/connectors/kujira/kujira.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export namespace KujiraConfig {
priceUrl:
configManager.get('kujira.coinGecko.priceUrl') ||
'https://api.coingecko.com/api/v3/simple/price?ids={targets}&vs_currencies=usd',
apiKeys: configManager.get('kujira.coinGecko.apiKeys') || [''],
},
};
}
34 changes: 25 additions & 9 deletions src/connectors/kujira/kujira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,17 @@ export class Kujira {
): Promise<GetKujiraTokenSymbolsToCoinGeckoTokenIdsMapResponse> {
const output = IMap<TokenSymbol, CoinGeckoId | undefined>().asMutable();

const url = config.coinGecko.coinsUrl;
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 = (
await runWithRetryAndTimeout(
axios,
axios.get,
[url],
[finalUrl],
config.retry.all.maxNumberOfRetries,
0
)
Expand Down Expand Up @@ -1029,10 +1033,17 @@ export class Kujira {
if (!coinGeckoBaseTokenId || !coinGeckoQuoteTokenId) {
result = {};
} else {
const finalUrl = config.coinGecko.priceUrl.replace(
'{targets}',
coinGeckoBaseTokenId.concat(',').concat(coinGeckoQuoteTokenId)
);
const coinGeckoIds = coinGeckoBaseTokenId
.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(
Expand Down Expand Up @@ -1124,11 +1135,16 @@ export class Kujira {
const coinGeckoIds = kujiraSymbolsToCoinGeckoIdsMap
.valueSeq()
.toArray()
.filter((id: any) => id && id.trim() !== '')
.join(',');

const finalUrl = getNotNullOrThrowError<{ priceUrl: string }>(
config.coinGecko
).priceUrl.replace('{targets}', coinGeckoIds);
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(
Expand Down
11 changes: 9 additions & 2 deletions src/services/schema/kujira-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@
"type": "object",
"required": [
"coinsUrl",
"priceUrl"
"priceUrl",
"apiKeys"
],
"properties": {
"coinsUrl": {
Expand All @@ -210,7 +211,13 @@
"string",
"null"
]
}
},
"apiKeys": {
"type": [
"array",
"null"
]
}
}
},
"transactions": {
Expand Down
5 changes: 3 additions & 2 deletions src/templates/kujira.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ tickers:
# orderBookVolumeWeightedAveragePrice:
# lastFilledOrder:
coinGecko:
coinsUrl: 'https://api.coingecko.com/api/v3/coins/list?x_cg_demo_api_key=<API_KEY>'
priceUrl: "https://api.coingecko.com/api/v3/simple/price?vs_currencies=usd&x_cg_demo_api_key=<API_KEY>&ids={targets}"
coinsUrl: 'https://api.coingecko.com/api/v3/coins/list?x_cg_demo_api_key={apiKey}'
priceUrl: "https://api.coingecko.com/api/v3/simple/price?vs_currencies=usd&x_cg_demo_api_key={apiKey}&ids={targets}"
apiKeys: ["<API_KEY>"] # If you don't have any use null
transactions:
merge:
createOrders: true
Expand Down

0 comments on commit a56a5eb

Please sign in to comment.