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

Commit

Permalink
fix(curve): Use virtual price in v2 Pools price calculation (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwele authored Jun 7, 2022
1 parent 05732f9 commit d9d5c04
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/apps/curve/curve.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { CurveFactoryPoolTokenHelper } from './helpers/curve.factory-pool.token-
import { CurveFactoryPoolDefinitionStrategy } from './helpers/curve.factory.pool-definition-strategy';
import { CurveGaugeIsActiveStrategy } from './helpers/curve.gauge.is-active-strategy';
import { CurveGaugeRoiStrategy } from './helpers/curve.gauge.roi-strategy';
import { CurveLiquidityAndVirtualPriceStrategy } from './helpers/curve.liquidity-and-virtual.price-strategy';
import { CurveLiquidityPriceStrategy } from './helpers/curve.liquidity.price-strategy';
import { CurveOnChainCoinStrategy } from './helpers/curve.on-chain.coin-strategy';
import { CurveOnChainReserveStrategy } from './helpers/curve.on-chain.reserve-strategy';
Expand Down Expand Up @@ -107,6 +108,7 @@ import { PolygonCurvePoolTokenFetcher } from './polygon/curve.pool.token-fetcher
CurveOnChainReserveStrategy,
CurveVirtualPriceStrategy,
CurveLiquidityPriceStrategy,
CurveLiquidityAndVirtualPriceStrategy,
CurveFactoryPoolDefinitionStrategy,
CurveCryptoFactoryPoolDefinitionStrategy,
// Gauge Helper Strategies
Expand Down Expand Up @@ -141,6 +143,7 @@ import { PolygonCurvePoolTokenFetcher } from './polygon/curve.pool.token-fetcher
CurveOnChainReserveStrategy,
CurveVirtualPriceStrategy,
CurveLiquidityPriceStrategy,
CurveLiquidityAndVirtualPriceStrategy,
CurveFactoryPoolDefinitionStrategy,
CurveCryptoFactoryPoolDefinitionStrategy,
// Gauge Helper Strategies
Expand Down
14 changes: 10 additions & 4 deletions src/apps/curve/helpers/curve.crypto-factory-pool.token-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Network } from '~types/network.interface';
import { CurveContractFactory, CurveFactoryPool } from '../contracts';

import { CurveCryptoFactoryPoolDefinitionStrategy } from './curve.crypto-factory.pool-definition-strategy';
import { CurveLiquidityPriceStrategy } from './curve.liquidity.price-strategy';
import { CurveLiquidityAndVirtualPriceStrategy } from './curve.liquidity-and-virtual.price-strategy';
import { CurveOnChainCoinStrategy } from './curve.on-chain.coin-strategy';
import { CurveOnChainReserveStrategy } from './curve.on-chain.reserve-strategy';
import { CurveOnChainVolumeStrategy } from './curve.on-chain.volume-strategy';
Expand All @@ -34,8 +34,8 @@ export class CurveCryptoFactoryPoolTokenHelper {
private readonly curveOnChainReserveStrategy: CurveOnChainReserveStrategy,
@Inject(CurveCryptoFactoryPoolDefinitionStrategy)
private readonly curveCryptoFactoryPoolDefinitionStrategy: CurveCryptoFactoryPoolDefinitionStrategy,
@Inject(CurveLiquidityPriceStrategy)
private readonly curveLiquidityPriceStrategy: CurveLiquidityPriceStrategy,
@Inject(CurveLiquidityAndVirtualPriceStrategy)
private readonly curveLiquidityAndVirtualPriceStrategy: CurveLiquidityAndVirtualPriceStrategy,
@Inject(CurveContractFactory)
private readonly curveContractFactory: CurveContractFactory,
@Inject(CurveOnChainVolumeStrategy)
Expand Down Expand Up @@ -71,7 +71,13 @@ export class CurveCryptoFactoryPoolTokenHelper {
.wrap(poolContract)
.fee()
.catch(() => '0'),
resolvePoolTokenPrice: this.curveLiquidityPriceStrategy.build(),
resolvePoolTokenPrice: this.curveLiquidityAndVirtualPriceStrategy.build({
resolveVirtualPrice: ({ multicall, poolContract }) =>
multicall
.wrap(poolContract)
.get_virtual_price()
.catch(() => '0'),
}),
resolvePoolTokenSymbol: ({ multicall, poolTokenContract }) => multicall.wrap(poolTokenContract).symbol(),
resolvePoolTokenSupply: ({ multicall, poolTokenContract }) => multicall.wrap(poolTokenContract).totalSupply(),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Injectable } from '@nestjs/common';
import { BigNumberish } from 'ethers';

import { EthersMulticall as Multicall } from '~multicall/multicall.ethers';
import { Token } from '~position/position.interface';

@Injectable()
export class CurveLiquidityAndVirtualPriceStrategy {
build<T>({
resolveVirtualPrice,
}: {
resolveVirtualPrice: (opts: { multicall: Multicall; poolContract: T }) => Promise<BigNumberish>;
}) {
return async ({
tokens,
reserves,
supply,
multicall,
poolContract,
}: {
tokens: Token[];
reserves: number[];
supply: number;
multicall: Multicall;
poolContract: T;
}) => {
const virtualPriceRaw = await resolveVirtualPrice({ multicall, poolContract });
const virtualPrice = Number(virtualPriceRaw) / 10 ** 18;
const reservesUSD = tokens.map((t, i) => reserves[i] * t.price);
const liquidity = reservesUSD.reduce((total, r) => total + r, 0);
return virtualPrice > 0 ? virtualPrice * (liquidity / supply) : liquidity / supply;
};
}
}
14 changes: 10 additions & 4 deletions src/apps/curve/helpers/curve.v2-pool.token-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CurveContractFactory, CurveV2Pool } from '../contracts';
import { CurvePoolDefinition } from '../curve.types';

import { CurveApiVolumeStrategy } from './curve.api.volume-strategy';
import { CurveLiquidityPriceStrategy } from './curve.liquidity.price-strategy';
import { CurveLiquidityAndVirtualPriceStrategy } from './curve.liquidity-and-virtual.price-strategy';
import { CurveOnChainCoinStrategy } from './curve.on-chain.coin-strategy';
import { CurveOnChainReserveStrategy } from './curve.on-chain.reserve-strategy';
import { CurvePoolTokenHelper } from './curve.pool.token-helper';
Expand All @@ -34,8 +34,8 @@ export class CurveV2PoolTokenHelper {
private readonly curveOnChainReserveStrategy: CurveOnChainReserveStrategy,
@Inject(CurveApiVolumeStrategy)
private readonly curveApiVolumeStrategy: CurveApiVolumeStrategy,
@Inject(CurveLiquidityPriceStrategy)
private readonly curveLiquidityPriceStrategy: CurveLiquidityPriceStrategy,
@Inject(CurveLiquidityAndVirtualPriceStrategy)
private readonly curveLiquidityAndVirtualPriceStrategy: CurveLiquidityAndVirtualPriceStrategy,
@Inject(CurveContractFactory)
private readonly curveContractFactory: CurveContractFactory,
) {}
Expand Down Expand Up @@ -64,7 +64,13 @@ export class CurveV2PoolTokenHelper {
resolvePoolReserves: this.curveOnChainReserveStrategy.build(),
resolvePoolVolume: this.curveApiVolumeStrategy.build({ statsUrl }),
resolvePoolFee: ({ multicall, poolContract }) => multicall.wrap(poolContract).fee(),
resolvePoolTokenPrice: this.curveLiquidityPriceStrategy.build(),
resolvePoolTokenPrice: this.curveLiquidityAndVirtualPriceStrategy.build({
resolveVirtualPrice: ({ multicall, poolContract }) =>
multicall
.wrap(poolContract)
.get_virtual_price()
.catch(() => 0),
}),
resolvePoolTokenSymbol: ({ multicall, poolTokenContract }) => multicall.wrap(poolTokenContract).symbol(),
resolvePoolTokenSupply: ({ multicall, poolTokenContract }) => multicall.wrap(poolTokenContract).totalSupply(),
});
Expand Down

0 comments on commit d9d5c04

Please sign in to comment.