This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(curve): Use virtual price in v2 Pools price calculation (#592)
- Loading branch information
Showing
4 changed files
with
57 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/apps/curve/helpers/curve.liquidity-and-virtual.price-strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters