Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
feat(studio): Eliminate dependencies on subgraphs for volume (#3120)
Browse files Browse the repository at this point in the history
  • Loading branch information
immasandwich authored Dec 11, 2023
1 parent f65f3c8 commit 731d200
Show file tree
Hide file tree
Showing 13 changed files with 11 additions and 291 deletions.
7 changes: 1 addition & 6 deletions src/apps/balancer-v1/balancer-v1.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import { AbstractApp } from '~app/app.dynamic-module';

import { BalancerV1ViemContractFactory } from './contracts';
import { EthereumBalancerV1PoolTokenFetcher } from './ethereum/balancer-v1.pool.token-fetcher';
import { EthereumBalancerV1PoolSubgraphVolumeDataLoader } from './ethereum/balancer-v1.volume.data-loader';

@Module({
providers: [
BalancerV1ViemContractFactory,
EthereumBalancerV1PoolTokenFetcher,
EthereumBalancerV1PoolSubgraphVolumeDataLoader,
],
providers: [BalancerV1ViemContractFactory, EthereumBalancerV1PoolTokenFetcher],
})
export class BalancerV1AppModule extends AbstractApp() {}
58 changes: 0 additions & 58 deletions src/apps/balancer-v1/common/balancer-v1.volume.data-loader.ts

This file was deleted.

23 changes: 1 addition & 22 deletions src/apps/balancer-v1/ethereum/balancer-v1.pool.token-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Inject } from '@nestjs/common';
import DataLoader from 'dataloader';
import { gql } from 'graphql-request';
import { sum } from 'lodash';

Expand All @@ -25,8 +24,6 @@ import {
import { BalancerV1ViemContractFactory } from '../contracts';
import { BalancerPoolToken } from '../contracts/viem';

import { EthereumBalancerV1PoolSubgraphVolumeDataLoader } from './balancer-v1.volume.data-loader';

type GetAllPoolsData = {
pools: {
id: string;
Expand Down Expand Up @@ -54,13 +51,9 @@ export class EthereumBalancerV1PoolTokenFetcher extends AppTokenTemplatePosition
> {
groupLabel = 'Pools';

volumeDataLoader: DataLoader<string, number>;

constructor(
@Inject(APP_TOOLKIT) protected readonly appToolkit: IAppToolkit,
@Inject(BalancerV1ViemContractFactory) protected readonly contractFactory: BalancerV1ViemContractFactory,
@Inject(EthereumBalancerV1PoolSubgraphVolumeDataLoader)
protected readonly volumeDataLoaderBuilder: EthereumBalancerV1PoolSubgraphVolumeDataLoader,
) {
super(appToolkit);
}
Expand All @@ -70,8 +63,6 @@ export class EthereumBalancerV1PoolTokenFetcher extends AppTokenTemplatePosition
}

async getAddresses() {
this.volumeDataLoader = this.volumeDataLoaderBuilder.getLoader();

const poolsFromSubgraph = await gqlFetch<GetAllPoolsData>({
endpoint: 'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer?source=zapper',
query: getPoolsQuery,
Expand Down Expand Up @@ -103,26 +94,15 @@ export class EthereumBalancerV1PoolTokenFetcher extends AppTokenTemplatePosition
return liquidity;
}

async getApy({ appToken, contract }: GetDataPropsParams<BalancerPoolToken>) {
const fee = (Number(await contract.read.getSwapFee()) / 10 ** 18) * 100;
const volume = await this.volumeDataLoader.load(appToken.address);
const yearlyFees = volume * fee * 365;
const reserves = (appToken.pricePerShare as number[]).map(pps => pps * appToken.supply);
const liquidity = sum(reserves.map((r, i) => r * appToken.tokens[i].price));
const apy = yearlyFees / liquidity;
return apy;
}

async getDataProps(params: GetDataPropsParams<BalancerPoolToken>) {
const defaultDataProps = await super.getDataProps(params);

const { appToken, contract } = params;
const fee = (Number(await contract.read.getSwapFee()) / 10 ** 18) * 100;
const weightsRaw = await Promise.all(appToken.tokens.map(t => contract.read.getNormalizedWeight([t.address])));
const weight = weightsRaw.map(w => Number(w) / 10 ** 18);
const volume = await this.volumeDataLoader.load(appToken.address);

return { ...defaultDataProps, fee, volume, weight };
return { ...defaultDataProps, fee, weight };
}

async getLabel({
Expand All @@ -143,7 +123,6 @@ export class EthereumBalancerV1PoolTokenFetcher extends AppTokenTemplatePosition
return [
{ label: 'Liquidity', value: buildDollarDisplayItem(appToken.dataProps.liquidity) },
{ label: 'Supply', value: buildNumberDisplayItem(appToken.supply) },
{ label: 'Volume', value: buildDollarDisplayItem(appToken.dataProps.volume) },
{ label: 'Fee', value: buildPercentageDisplayItem(appToken.dataProps.fee) },
{ label: 'Ratio', value: appToken.dataProps.weight.map(p => `${Math.round(p * 100)}%`).join(' / ') },
];
Expand Down
11 changes: 0 additions & 11 deletions src/apps/balancer-v1/ethereum/balancer-v1.volume.data-loader.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ import { CurveFactoryCryptoPoolTokenFetcher } from '../common/curve.factory-cryp
export class BaseCurveFactoryCryptoPoolTokenFetcher extends CurveFactoryCryptoPoolTokenFetcher {
groupLabel = 'Pools';
registryAddress = '0x5ef72230578b3e399e6c6f4f6360edf95e83bbfd';

skipVolume = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ import { CurveFactoryStablePoolTokenFetcher } from '../common/curve.factory-stab
export class BaseCurveFactoryStablePoolTokenFetcher extends CurveFactoryStablePoolTokenFetcher {
groupLabel = 'Pools';
registryAddress = '0x3093f9b57a428f3eb6285a589cb35bea6e78c336';

skipVolume = true;
}
2 changes: 0 additions & 2 deletions src/apps/curve/base/curve.tricrypto-pool.token-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ import { CurveFactoryV2PoolTokenFetcher } from '../common/curve.factory-pool-v2.
export class BaseCurveTricryptoPoolTokenFetcher extends CurveFactoryV2PoolTokenFetcher {
groupLabel = 'Pools';
factoryAddress = '0xa5961898870943c68037f6848d2d866ed2016bcb';

skipVolume = true;
}
26 changes: 4 additions & 22 deletions src/apps/curve/common/curve.pool-dynamic-v2.token-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Inject } from '@nestjs/common';
import DataLoader from 'dataloader';
import { BigNumberish } from 'ethers';
import { compact, range } from 'lodash';
import { Abi, GetContractReturnType, PublicClient } from 'viem';
Expand Down Expand Up @@ -61,13 +60,9 @@ export abstract class CurvePoolDynamicV2TokenFetcher<T extends Abi> extends AppT
CurveTricryptoPool,
CurvePoolTokenDataProps
> {
volumeDataLoader: DataLoader<string, number>;

abstract factoryAddress: string;
blacklistedTokenAddresses: string[] = [];

skipVolume = false;

abstract resolveFactory(address: string): GetContractReturnType<T, PublicClient>;
abstract resolvePoolCount(params: ResolvePoolCountParams<T>): Promise<BigNumberish>;
abstract resolveTokenAddress(params: ResolveTokenAddressParams<T>): Promise<string>;
Expand All @@ -87,10 +82,6 @@ export abstract class CurvePoolDynamicV2TokenFetcher<T extends Abi> extends AppT
}

async getDefinitions({ multicall }: GetDefinitionsParams) {
if (!this.skipVolume) {
this.volumeDataLoader = this.curveVolumeDataLoader.getLoader({ network: this.network });
}

const contract = multicall.wrap(this.resolveFactory(this.factoryAddress));
const poolCount = await this.resolvePoolCount({ contract, multicall });
const poolRange = range(0, Number(poolCount));
Expand Down Expand Up @@ -143,20 +134,11 @@ export abstract class CurvePoolDynamicV2TokenFetcher<T extends Abi> extends AppT
async getDataProps(params: GetDataPropsParams<CurveTricryptoPool, CurvePoolTokenDataProps>) {
const defaultDataProps = await super.getDataProps(params);

const { contract, definition } = params;
let fee: number;

try {
const fees = await contract.read.fee();
fee = Number(fees) / 10 ** 8;
} catch {
fee = 0;
}
const volume = this.skipVolume == false ? await this.volumeDataLoader.load(definition.address) : 0;
const feeVolume = fee * volume;
const apy = defaultDataProps.liquidity > 0 ? (feeVolume / defaultDataProps.liquidity) * 365 : 0;
const { contract } = params;
const feeRaw = await contract.read.fee().catch(() => 0);
const fee = Number(feeRaw) / 10 ** 10;

return { ...defaultDataProps, fee, volume, apy };
return { ...defaultDataProps, fee };
}

async getLabel({ appToken }: GetDisplayPropsParams<CurveTricryptoPool, CurvePoolTokenDataProps>) {
Expand Down
15 changes: 1 addition & 14 deletions src/apps/curve/common/curve.pool-dynamic.token-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Inject } from '@nestjs/common';
import DataLoader from 'dataloader';
import { BigNumberish } from 'ethers';
import { compact, range } from 'lodash';
import { Abi, GetContractReturnType, PublicClient } from 'viem';
Expand Down Expand Up @@ -80,13 +79,9 @@ export abstract class CurvePoolDynamicTokenFetcher<T extends Abi> extends AppTok
CurvePoolTokenDataProps,
CurvePoolDefinition
> {
volumeDataLoader: DataLoader<string, number>;

abstract registryAddress: string;
blacklistedSwapAddresses: string[] = [];

skipVolume = false;

abstract resolveRegistry(address: string): GetContractReturnType<T, PublicClient>;
abstract resolvePoolCount(params: ResolvePoolCountParams<T>): Promise<BigNumberish>;
abstract resolveSwapAddress(params: ResolveSwapAddressParams<T>): Promise<string>;
Expand All @@ -108,10 +103,6 @@ export abstract class CurvePoolDynamicTokenFetcher<T extends Abi> extends AppTok
}

async getDefinitions({ multicall }: GetDefinitionsParams) {
if (!this.skipVolume) {
this.volumeDataLoader = this.curveVolumeDataLoader.getLoader({ network: this.network });
}

const contract = multicall.wrap(this.resolveRegistry(this.registryAddress));
const poolCount = await this.resolvePoolCount({ contract, multicall });
const poolRange = range(0, Number(poolCount));
Expand Down Expand Up @@ -175,11 +166,7 @@ export abstract class CurvePoolDynamicTokenFetcher<T extends Abi> extends AppTok
const fees = await this.resolveFees({ contract, swapAddress, multicall });
const fee = Number(fees[0]) / 10 ** 8;

const volume = this.skipVolume == false ? await this.volumeDataLoader.load(definition.swapAddress) : 0;
const feeVolume = fee * volume;
const apy = defaultDataProps.liquidity > 0 ? (feeVolume / defaultDataProps.liquidity) * 365 : 0;

return { ...defaultDataProps, fee, volume, apy, swapAddress };
return { ...defaultDataProps, fee, swapAddress };
}

async getLabel({ appToken }: GetDisplayPropsParams<Erc20, CurvePoolTokenDataProps, CurvePoolDefinition>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Inject, NotImplementedException } from '@nestjs/common';
import DataLoader from 'dataloader';
import { compact, range } from 'lodash';

import { APP_TOOLKIT, IAppToolkit } from '~app-toolkit/app-toolkit.interface';
Expand Down Expand Up @@ -65,8 +64,6 @@ export abstract class KyberswapElasticLiquidityContractPositionFetcher extends C
abstract blockSubgraphUrl: string;
protected poolFeeMapping: Record<string, number> | null;

apyDataLoader: DataLoader<string, number>;

constructor(
@Inject(APP_TOOLKIT) protected readonly appToolkit: IAppToolkit,
@Inject(KyberswapElasticViemContractFactory)
Expand All @@ -84,11 +81,6 @@ export abstract class KyberswapElasticLiquidityContractPositionFetcher extends C
}

async getDefinitions(): Promise<KyberswapElasticLiquidityPositionDefinition[]> {
this.apyDataLoader = this.apyDataLoaderBuilder.getLoader({
subgraphUrl: this.subgraphUrl,
blockSubgraphUrl: this.blockSubgraphUrl,
});

const data = await gqlFetch<GetTopPoolsResponse>({
endpoint: this.subgraphUrl,
query: GET_TOP_POOLS_QUERY,
Expand Down Expand Up @@ -141,9 +133,8 @@ export abstract class KyberswapElasticLiquidityContractPositionFetcher extends C
const reserves = reservesRaw.map((r, i) => Number(r) / 10 ** tokens[i].decimals);
const liquidity = reserves[0] * tokens[0].price + reserves[1] * tokens[1].price;
const assetStandard = Standard.ERC_721;
const apy = await this.apyDataLoader.load(poolAddress);

return { feeTier, reserves, liquidity, poolAddress, assetStandard, apy, positionKey: `${feeTier}` };
return { feeTier, reserves, liquidity, poolAddress, assetStandard, positionKey: `${feeTier}` };
}

async getLabel({
Expand Down
Loading

0 comments on commit 731d200

Please sign in to comment.