Skip to content

Commit

Permalink
Merge pull request #254 from balancer/remove-coingecko
Browse files Browse the repository at this point in the history
removing coingecko
  • Loading branch information
gmbronco authored Jan 30, 2024
2 parents 42ba312 + 674ebfc commit d618d8b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/lambdas/update-prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const handler = wrapHandler(async (): Promise<any> => {
log('Fetching all tokens.');
const tokens = await getTokens();
log(`Fetched ${tokens.length} tokens. Updating token prices`);
await updateTokenPrices(tokens, true);
await updateTokenPrices(tokens);
log(`Updated prices`);
return { statusCode: 201, body: '' };
} catch (e) {
Expand Down
38 changes: 38 additions & 0 deletions src/modules/prices/beets-price-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@ import axios from 'axios';
const BEETS_API_URL =
process.env.BEETS_API_URL || 'https://api-v3.balancer.fi/graphql';

export const nativeAssetMap = {
1: {
symbol: 'eth',
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
},
10: {
symbol: 'eth',
address: '0x4200000000000000000000000000000000000006',
},
100: {
symbol: 'xdai',
address: '0xe91d153e0b41518a2ce8dd3d7944fa863463a97d',
},
137: {
symbol: 'matic',
address: '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270',
},
1101: {
symbol: 'eth',
address: '0x4f9a0e7fd2bf6067db6994cf12e4495df938e6e9',
},
8453: {
symbol: 'eth',
address: '0x4200000000000000000000000000000000000006',
},
42161: {
symbol: 'eth',
address: '0x82af49447d8a07e3bd95bd0d56f35241523fbab1',
},
43114: {
symbol: 'avax',
address: '0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7',
},
}

class BeetsPriceFetcher {
async fetch(tokens: Token[]): Promise<Token[]> {
const tokenPricesByChain: Record<number, Record<string, number>> = {};
Expand All @@ -26,6 +61,9 @@ class BeetsPriceFetcher {
token.price = token.price || {};
token.price.usd =
tokenPricesByChain[token.chainId][token.address].toString();
token.price[nativeAssetMap[token.chainId].symbol] =
(tokenPricesByChain[token.chainId][token.address] /
tokenPricesByChain[token.chainId][nativeAssetMap[token.chainId].address]).toString();
}
return token;
});
Expand Down
15 changes: 8 additions & 7 deletions src/modules/tokens/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BigNumber, ethers } from 'ethers';
import { Token } from './types';
import { Contract } from '@ethersproject/contracts';
import PriceFetcher from '@/modules/prices/price-fetcher';
import BeetsPriceFetcher from '@/modules/prices/beets-price-fetcher';
import { getToken, updateTokens } from '@/modules/dynamodb';
import { TokenPrices } from '@balancer-labs/sdk';
Expand All @@ -10,18 +9,20 @@ const log = console.log;

export async function updateTokenPrices(
tokens: Token[],
abortOnRateLimit = false
) {
const beetsPriceFetcher = new BeetsPriceFetcher();
log(`fetching prices for ${tokens.length} tokens from beets API`);
const beetsTokensWithPrices = await beetsPriceFetcher.fetch(tokens);
log(`Saving ${beetsTokensWithPrices.length} updated tokens to DB`);
await updateTokens(beetsTokensWithPrices);
const priceFetcher = new PriceFetcher(abortOnRateLimit);
log(`fetching prices for ${tokens.length} tokens from coingecko`);
const tokensWithPrices = await priceFetcher.fetch(tokens);
log(`Saving ${tokensWithPrices.length} updated tokens to DB`);
await updateTokens(tokensWithPrices);

// Commenting out coingecko price fetcher, as it is not working because of rate limiting
// const priceFetcher = new PriceFetcher(abortOnRateLimit);
// log(`fetching prices for ${tokens.length} tokens from coingecko`);
// const tokensWithPrices = await priceFetcher.fetch(tokens);
// log(`Saving ${tokensWithPrices.length} updated tokens to DB`);
// await updateTokens(tokensWithPrices);

log('finished updating token prices');
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async function decorateAndSavePools(chainId: number) {
async function updatePrices() {
const tokens = await getTokens();
log('Updating token prices');
await updateTokenPrices(tokens, false);
await updateTokenPrices(tokens);
log('Updated token prices');
setTimeout(updatePrices, UPDATE_PRICES_INTERVAL);
}
Expand Down

0 comments on commit d618d8b

Please sign in to comment.