Skip to content

Commit

Permalink
fix: tokenSrv <> coingeckoSrv circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Oct 10, 2023
1 parent 0fa01dd commit ecb8c5f
Show file tree
Hide file tree
Showing 22 changed files with 89 additions and 73 deletions.
8 changes: 4 additions & 4 deletions modules/coingecko/coingecko.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosError } from 'axios';
import { twentyFourHoursInSecs } from '../common/time';
import _ from 'lodash';
import moment from 'moment-timezone';
import { tokenService } from '../token/token.service';
import { getTokenDefinitions } from '@/modules/token/token.model';
import { TokenDefinition } from '../token/token-types';
import { isAddress } from 'ethers/lib/utils';
import { RateLimiter } from 'limiter';
Expand Down Expand Up @@ -75,7 +75,7 @@ interface CoinId {
const tokensPerInterval = env.COINGECKO_API_KEY ? ((env.DEPLOYMENT_ENV as DeploymentEnv) === 'main' ? 10 : 5) : 3;
const requestRateLimiter = new RateLimiter({ tokensPerInterval, interval: 'minute' });

export class CoingeckoService {
class CoingeckoService {
private readonly baseUrl: string;
private readonly fiatParam: string;
private readonly apiKeyParam: string;
Expand Down Expand Up @@ -109,7 +109,7 @@ export class CoingeckoService {
try {
if (addresses.length / addressesPerRequest > 10) throw new Error('Too many requests for rate limit.');

const tokenDefinitions = await tokenService.getTokenDefinitions();
const tokenDefinitions = await getTokenDefinitions();
const mapped = addresses.map((address) => this.getMappedTokenDetails(address, tokenDefinitions));
const groupedByPlatform = _.groupBy(mapped, 'platform');

Expand Down Expand Up @@ -144,7 +144,7 @@ export class CoingeckoService {
const now = Math.floor(Date.now() / 1000);
const end = now;
const start = end - days * twentyFourHoursInSecs;
const tokenDefinitions = await tokenService.getTokenDefinitions();
const tokenDefinitions = await getTokenDefinitions();
const mapped = this.getMappedTokenDetails(address, tokenDefinitions);

const endpoint = `/coins/${mapped.platform}/contract/${mapped.address}/market_chart/range?vs_currency=${this.fiatParam}&from=${start}&to=${end}`;
Expand Down
3 changes: 1 addition & 2 deletions modules/network/arbitrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { every } from '../../worker/intervals';
import { GithubContentService } from '../content/github-content.service';
import { gaugeSubgraphService } from '../subgraphs/gauge-subgraph/gauge-subgraph.service';
import { CoingeckoPriceHandlerService } from '../token/lib/token-price-handlers/coingecko-price-handler.service';
import { coingeckoService } from '../coingecko/coingecko.service';
import { IbTokensAprService } from '../pool/lib/apr-data-sources/ib-tokens-apr.service';
import { env } from '../../app/env';

Expand Down Expand Up @@ -233,7 +232,7 @@ export const arbitrumNetworkConfig: NetworkConfig = {
],
poolStakingServices: [new GaugeStakingService(gaugeSubgraphService, arbitrumNetworkData.bal!.address)],
tokenPriceHandlers: [
new CoingeckoPriceHandlerService(coingeckoService),
new CoingeckoPriceHandlerService(),
new BptPriceHandlerService(),
new LinearWrappedTokenPriceHandlerService(),
new SwapsPriceHandlerService(),
Expand Down
3 changes: 1 addition & 2 deletions modules/network/avalanche.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { UserSyncGaugeBalanceService } from '../user/lib/user-sync-gauge-balance
import { every } from '../../worker/intervals';
import { GithubContentService } from '../content/github-content.service';
import { gaugeSubgraphService } from '../subgraphs/gauge-subgraph/gauge-subgraph.service';
import { coingeckoService } from '../coingecko/coingecko.service';
import { CoingeckoPriceHandlerService } from '../token/lib/token-price-handlers/coingecko-price-handler.service';
import { env } from '../../app/env';
import { IbTokensAprService } from '../pool/lib/apr-data-sources/ib-tokens-apr.service';
Expand Down Expand Up @@ -225,7 +224,7 @@ export const avalancheNetworkConfig: NetworkConfig = {
],
poolStakingServices: [new GaugeStakingService(gaugeSubgraphService, avalancheNetworkData.bal!.address)],
tokenPriceHandlers: [
new CoingeckoPriceHandlerService(coingeckoService),
new CoingeckoPriceHandlerService(),
new BptPriceHandlerService(),
new LinearWrappedTokenPriceHandlerService(),
new SwapsPriceHandlerService(),
Expand Down
3 changes: 1 addition & 2 deletions modules/network/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { every } from '../../worker/intervals';
import { GithubContentService } from '../content/github-content.service';
import { gaugeSubgraphService } from '../subgraphs/gauge-subgraph/gauge-subgraph.service';
import { CoingeckoPriceHandlerService } from '../token/lib/token-price-handlers/coingecko-price-handler.service';
import { coingeckoService } from '../coingecko/coingecko.service';
import { env } from '../../app/env';
import { IbTokensAprService } from '../pool/lib/apr-data-sources/ib-tokens-apr.service';

Expand Down Expand Up @@ -126,7 +125,7 @@ export const baseNetworkConfig: NetworkConfig = {
],
poolStakingServices: [new GaugeStakingService(gaugeSubgraphService, baseNetworkData.bal!.address)],
tokenPriceHandlers: [
new CoingeckoPriceHandlerService(coingeckoService),
new CoingeckoPriceHandlerService(),
new BptPriceHandlerService(),
new LinearWrappedTokenPriceHandlerService(),
new SwapsPriceHandlerService(),
Expand Down
3 changes: 1 addition & 2 deletions modules/network/fantom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { UserSyncReliquaryFarmBalanceService } from '../user/lib/user-sync-reliq
import { every } from '../../worker/intervals';
import { SanityContentService } from '../content/sanity-content.service';
import { CoingeckoPriceHandlerService } from '../token/lib/token-price-handlers/coingecko-price-handler.service';
import { coingeckoService } from '../coingecko/coingecko.service';
import { env } from '../../app/env';
import { IbTokensAprService } from '../pool/lib/apr-data-sources/ib-tokens-apr.service';
import { BeetswarsGaugeVotingAprService } from '../pool/lib/apr-data-sources/fantom/beetswars-gauge-voting-apr';
Expand Down Expand Up @@ -320,7 +319,7 @@ export const fantomNetworkConfig: NetworkConfig = {
),
new FbeetsPriceHandlerService(fantomNetworkData.fbeets!.address, fantomNetworkData.fbeets!.poolId),
new ClqdrPriceHandlerService(),
new CoingeckoPriceHandlerService(coingeckoService),
new CoingeckoPriceHandlerService(),
new BptPriceHandlerService(),
new LinearWrappedTokenPriceHandlerService(),
new SwapsPriceHandlerService(),
Expand Down
3 changes: 1 addition & 2 deletions modules/network/gnosis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { UserSyncGaugeBalanceService } from '../user/lib/user-sync-gauge-balance
import { every } from '../../worker/intervals';
import { GithubContentService } from '../content/github-content.service';
import { gaugeSubgraphService } from '../subgraphs/gauge-subgraph/gauge-subgraph.service';
import { coingeckoService } from '../coingecko/coingecko.service';
import { CoingeckoPriceHandlerService } from '../token/lib/token-price-handlers/coingecko-price-handler.service';
import { env } from '../../app/env';
import { IbTokensAprService } from '../pool/lib/apr-data-sources/ib-tokens-apr.service';
Expand Down Expand Up @@ -151,7 +150,7 @@ export const gnosisNetworkConfig: NetworkConfig = {
],
poolStakingServices: [new GaugeStakingService(gaugeSubgraphService, gnosisNetworkData.bal!.address)],
tokenPriceHandlers: [
new CoingeckoPriceHandlerService(coingeckoService),
new CoingeckoPriceHandlerService(),
new BptPriceHandlerService(),
new LinearWrappedTokenPriceHandlerService(),
new SwapsPriceHandlerService(),
Expand Down
3 changes: 1 addition & 2 deletions modules/network/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { UserSyncGaugeBalanceService } from '../user/lib/user-sync-gauge-balance
import { every } from '../../worker/intervals';
import { GithubContentService } from '../content/github-content.service';
import { gaugeSubgraphService } from '../subgraphs/gauge-subgraph/gauge-subgraph.service';
import { coingeckoService } from '../coingecko/coingecko.service';
import { CoingeckoPriceHandlerService } from '../token/lib/token-price-handlers/coingecko-price-handler.service';
import { IbTokensAprService } from '../pool/lib/apr-data-sources/ib-tokens-apr.service';
import { env } from '../../app/env';
Expand Down Expand Up @@ -372,7 +371,7 @@ export const mainnetNetworkConfig: NetworkConfig = {
],
poolStakingServices: [new GaugeStakingService(gaugeSubgraphService, mainnetNetworkData.bal!.address)],
tokenPriceHandlers: [
new CoingeckoPriceHandlerService(coingeckoService),
new CoingeckoPriceHandlerService(),
new BptPriceHandlerService(),
new LinearWrappedTokenPriceHandlerService(),
new SwapsPriceHandlerService(),
Expand Down
3 changes: 1 addition & 2 deletions modules/network/optimism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { UserSyncGaugeBalanceService } from '../user/lib/user-sync-gauge-balance
import { every } from '../../worker/intervals';
import { SanityContentService } from '../content/sanity-content.service';
import { gaugeSubgraphService } from '../subgraphs/gauge-subgraph/gauge-subgraph.service';
import { coingeckoService } from '../coingecko/coingecko.service';
import { CoingeckoPriceHandlerService } from '../token/lib/token-price-handlers/coingecko-price-handler.service';
import { IbTokensAprService } from '../pool/lib/apr-data-sources/ib-tokens-apr.service';
import { env } from '../../app/env';
Expand Down Expand Up @@ -267,7 +266,7 @@ export const optimismNetworkConfig: NetworkConfig = {
optimismNetworkData.beets!.address,
optimismNetworkData.beets!.beetsPriceProviderRpcUrl,
),
new CoingeckoPriceHandlerService(coingeckoService),
new CoingeckoPriceHandlerService(),
new BptPriceHandlerService(),
new LinearWrappedTokenPriceHandlerService(),
new SwapsPriceHandlerService(),
Expand Down
3 changes: 1 addition & 2 deletions modules/network/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { UserSyncGaugeBalanceService } from '../user/lib/user-sync-gauge-balance
import { every } from '../../worker/intervals';
import { GithubContentService } from '../content/github-content.service';
import { gaugeSubgraphService } from '../subgraphs/gauge-subgraph/gauge-subgraph.service';
import { coingeckoService } from '../coingecko/coingecko.service';
import { CoingeckoPriceHandlerService } from '../token/lib/token-price-handlers/coingecko-price-handler.service';
import { IbTokensAprService } from '../pool/lib/apr-data-sources/ib-tokens-apr.service';
import { env } from '../../app/env';
Expand Down Expand Up @@ -270,7 +269,7 @@ export const polygonNetworkConfig: NetworkConfig = {
],
poolStakingServices: [new GaugeStakingService(gaugeSubgraphService, polygonNetworkData.bal!.address)],
tokenPriceHandlers: [
new CoingeckoPriceHandlerService(coingeckoService),
new CoingeckoPriceHandlerService(),
new BptPriceHandlerService(),
new LinearWrappedTokenPriceHandlerService(),
new SwapsPriceHandlerService(),
Expand Down
3 changes: 1 addition & 2 deletions modules/network/zkevm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { every } from '../../worker/intervals';
import { GithubContentService } from '../content/github-content.service';
import { gaugeSubgraphService } from '../subgraphs/gauge-subgraph/gauge-subgraph.service';
import { CoingeckoPriceHandlerService } from '../token/lib/token-price-handlers/coingecko-price-handler.service';
import { coingeckoService } from '../coingecko/coingecko.service';
import { env } from '../../app/env';
import { IbTokensAprService } from '../pool/lib/apr-data-sources/ib-tokens-apr.service';

Expand Down Expand Up @@ -170,7 +169,7 @@ export const zkevmNetworkConfig: NetworkConfig = {
],
poolStakingServices: [new GaugeStakingService(gaugeSubgraphService, zkevmNetworkData.bal!.address)],
tokenPriceHandlers: [
new CoingeckoPriceHandlerService(coingeckoService),
new CoingeckoPriceHandlerService(),
new BptPriceHandlerService(),
new LinearWrappedTokenPriceHandlerService(),
new SwapsPriceHandlerService(),
Expand Down
5 changes: 2 additions & 3 deletions modules/pool/lib/pool-snapshot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import _ from 'lodash';
import { PrismaPoolSnapshot } from '@prisma/client';
import { prismaBulkExecuteOperations } from '../../../prisma/prisma-util';
import { prismaPoolWithExpandedNesting } from '../../../prisma/prisma-types';
import { CoingeckoService } from '../../coingecko/coingecko.service';
import { coingeckoService } from '@/modules/coingecko/coingecko.service';
import { blocksSubgraphService } from '../../subgraphs/blocks-subgraph/blocks-subgraph.service';
import { sleep } from '../../common/promise';
import { networkContext } from '../../network/network-context.service';
Expand All @@ -24,7 +24,6 @@ import { TokenHistoricalPrices } from '../../coingecko/coingecko-types';
export class PoolSnapshotService {
constructor(
private readonly balancerSubgraphService: BalancerSubgraphService,
private readonly coingeckoService: CoingeckoService,
) {}

public async getSnapshotsForPool(poolId: string, range: GqlPoolSnapshotDataRange) {
Expand Down Expand Up @@ -213,7 +212,7 @@ export class PoolSnapshotService {
});
if (priceForDays.length === 0) {
try {
tokenPriceMap[token.address] = await this.coingeckoService.getTokenHistoricalPrices(
tokenPriceMap[token.address] = await coingeckoService.getTokenHistoricalPrices(
token.address,
numDays,
);
Expand Down
2 changes: 0 additions & 2 deletions modules/pool/lib/staking/gauge-staking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ export class GaugeStakingService implements PoolStakingService {
totalSupply: string;
}[];

console.log(onchainRates)

return onchainRates;
}

Expand Down
3 changes: 1 addition & 2 deletions modules/pool/pool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
QueryPoolGetSwapsArgs,
QueryPoolGetUserSwapVolumeArgs,
} from '../../schema';
import { coingeckoService } from '../coingecko/coingecko.service';
import { balancerSubgraphService } from '../subgraphs/balancer-subgraph/balancer-subgraph.service';
import { blocksSubgraphService } from '../subgraphs/blocks-subgraph/blocks-subgraph.service';
import { tokenService } from '../token/token.service';
Expand Down Expand Up @@ -536,6 +535,6 @@ export const poolService = new PoolService(
new PoolAprUpdaterService(),
new PoolSyncService(),
new PoolSwapService(tokenService, balancerSubgraphService),
new PoolSnapshotService(balancerSubgraphService, coingeckoService),
new PoolSnapshotService(balancerSubgraphService),
new ReliquarySnapshotService(reliquarySubgraphService),
);
12 changes: 6 additions & 6 deletions modules/token/lib/coingecko-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import _ from 'lodash';
import moment from 'moment-timezone';
import { prismaBulkExecuteOperations } from '../../../prisma/prisma-util';
import { timestampRoundedUpToNearestHour } from '../../common/time';
import { CoingeckoService } from '../../coingecko/coingecko.service';
import { coingeckoService } from '@/modules/coingecko/coingecko.service';
import { networkContext } from '../../network/network-context.service';
import { AllNetworkConfigs } from '../../network/network-config';
import { PrismaToken } from '.prisma/client';

export class CoingeckoDataService {
constructor(private readonly conigeckoService: CoingeckoService) {}
constructor() {}

public async syncCoingeckoPricesForAllChains() {
const timestamp = timestampRoundedUpToNearestHour();
Expand Down Expand Up @@ -41,7 +41,7 @@ export class CoingeckoDataService {
const chunks = _.chunk(uniqueTokensWithIds, 250); //max page size is 250

for (const chunk of chunks) {
const response = await this.conigeckoService.getMarketDataForTokenIds(
const response = await coingeckoService.getMarketDataForTokenIds(
chunk.map((item) => item.coingeckoTokenId || ''),
);
let operations: any[] = [];
Expand Down Expand Up @@ -139,7 +139,7 @@ export class CoingeckoDataService {
public async syncCoingeckoIds() {
const allTokens = await prisma.prismaToken.findMany({ where: { chain: networkContext.chain } });

const coinIds = await this.conigeckoService.getCoinIdList();
const coinIds = await coingeckoService.getCoinIdList();

for (const token of allTokens) {
const coinId = coinIds.find((coinId) => {
Expand Down Expand Up @@ -180,8 +180,8 @@ export class CoingeckoDataService {
throw new Error('Missing token or token is missing coingecko token id');
}

const monthData = await this.conigeckoService.getCoinCandlestickData(token.coingeckoTokenId, 30);
const twentyFourHourData = await this.conigeckoService.getCoinCandlestickData(token.coingeckoTokenId, 1);
const monthData = await coingeckoService.getCoinCandlestickData(token.coingeckoTokenId, 30);
const twentyFourHourData = await coingeckoService.getCoinCandlestickData(token.coingeckoTokenId, 1);

//merge 30 min data into hourly data
const hourlyData = Object.values(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { TokenPriceHandler } from '../../token-types';
import { PrismaTokenWithTypes } from '../../../../prisma/prisma-types';
import { prisma } from '../../../../prisma/prisma-client';
import { timestampRoundedUpToNearestHour } from '../../../common/time';
import { CoingeckoService } from '../../../coingecko/coingecko.service';
import { coingeckoService } from '@/modules/coingecko/coingecko.service';
import { networkContext } from '../../../network/network-context.service';

export class CoingeckoPriceHandlerService implements TokenPriceHandler {
public readonly exitIfFails = true;
public readonly id = 'CoingeckoPriceHandlerService';

constructor(private readonly coingeckoService: CoingeckoService) {}
constructor() {}

public async getAcceptedTokens(tokens: PrismaTokenWithTypes[]): Promise<string[]> {
return tokens
Expand All @@ -30,7 +30,7 @@ export class CoingeckoPriceHandlerService implements TokenPriceHandler {

const tokenAddresses = tokens.map((item) => item.address);

const tokenPricesByAddress = await this.coingeckoService.getTokenPrices(tokenAddresses);
const tokenPricesByAddress = await coingeckoService.getTokenPrices(tokenAddresses);

let operations: any[] = [];
for (let tokenAddress of Object.keys(tokenPricesByAddress)) {
Expand Down
30 changes: 30 additions & 0 deletions modules/token/token.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { prisma } from '@/prisma/prisma-client';
import { networkContext } from '@/modules/network/network-context.service';

export const getTokenDefinitions = async () => {
const tokens = await prisma.prismaToken.findMany({
where: { types: { some: { type: 'WHITE_LISTED' } }, chain: networkContext.chain },
include: { types: true },
orderBy: { priority: 'desc' },
});

const weth = tokens.find((token) => token.address === networkContext.data.weth.address);

if (weth) {
tokens.push({
...weth,
name: networkContext.data.eth.name,
address: networkContext.data.eth.address,
symbol: networkContext.data.eth.symbol,
});
}

return tokens.map((token) => ({
...token,
chainId: networkContext.data.chain.id,
//TODO: some linear wrapped tokens are tradable. ie: xBOO
tradable: !token.types.find(
(type) => type.type === 'PHANTOM_BPT' || type.type === 'BPT' || type.type === 'LINEAR_WRAPPED_TOKEN',
),
}));
}
3 changes: 2 additions & 1 deletion modules/token/token.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Resolvers } from '../../schema';
import _ from 'lodash';
import { isAdminRoute } from '../auth/auth-context';
import { tokenService } from './token.service';
import { getTokenDefinitions } from './token.model';

const resolvers: Resolvers = {
Query: {
tokenGetTokens: async () => {
return tokenService.getTokenDefinitions();
return getTokenDefinitions();
},
tokenGetCurrentPrices: async (parent, {}, context) => {
const prices = await tokenService.getWhiteListedTokenPrices();
Expand Down
Loading

0 comments on commit ecb8c5f

Please sign in to comment.