From c5acdc26517da41dd7b7e64714f0a204d2ce3c8a Mon Sep 17 00:00:00 2001 From: Soil King <157099073+soilking@users.noreply.github.com> Date: Tue, 16 Apr 2024 19:18:05 -0700 Subject: [PATCH] refactor --- .../subgraph-bean/src/Bean3CRVHandler_V1.ts | 2 +- .../subgraph-bean/src/BeanstalkHandler.ts | 38 ++++++++----------- .../subgraph-bean/src/UniswapV2Handler.ts | 6 ++- .../src/utils/price/CurvePrice.ts | 14 ++++++- .../subgraph-bean/src/utils/price/Types.ts | 6 +++ .../src/utils/price/UniswapPrice.ts | 15 +++++++- 6 files changed, 51 insertions(+), 30 deletions(-) diff --git a/projects/subgraph-bean/src/Bean3CRVHandler_V1.ts b/projects/subgraph-bean/src/Bean3CRVHandler_V1.ts index b3ad674106..1085cd1017 100644 --- a/projects/subgraph-bean/src/Bean3CRVHandler_V1.ts +++ b/projects/subgraph-bean/src/Bean3CRVHandler_V1.ts @@ -206,7 +206,7 @@ function handleSwap( checkBeanCross(BEAN_ERC20_V1.toHexString(), timestamp, blockNumber, oldBeanPrice, newPrice); } -export function onSunriseSetCurveTwa(poolAddress: string, timestamp: BigInt, blockNumber: BigInt): void { +export function setCurveTwa(poolAddress: string, timestamp: BigInt, blockNumber: BigInt): void { const twaBalances = getTWAPrices(poolAddress, TWAType.CURVE, timestamp); const beanPool = Address.fromString(poolAddress); const otherPool = beanPool == BEAN_3CRV_V1 ? CRV3_POOL_V1 : LUSD_3POOL; diff --git a/projects/subgraph-bean/src/BeanstalkHandler.ts b/projects/subgraph-bean/src/BeanstalkHandler.ts index 670491dac9..3423d8eb49 100644 --- a/projects/subgraph-bean/src/BeanstalkHandler.ts +++ b/projects/subgraph-bean/src/BeanstalkHandler.ts @@ -15,10 +15,12 @@ import { import { ZERO_BD, ZERO_BI, toDecimal } from "../../subgraph-core/utils/Decimals"; import { CurvePrice } from "../generated/Beanstalk/CurvePrice"; import { checkBeanCross } from "./utils/Cross"; -import { uniswapV2DeltaB, uniswapV2Price, updatePreReplantPriceETH } from "./utils/price/UniswapPrice"; -import { curveDeltaB, curvePriceAndLp } from "./utils/price/CurvePrice"; +import { calcUniswapV2Inst, uniswapV2DeltaB, uniswapV2Price, updatePreReplantPriceETH } from "./utils/price/UniswapPrice"; +import { calcCurveInst, curveDeltaB, curvePriceAndLp } from "./utils/price/CurvePrice"; import { MetapoolOracle, WellOracle } from "../generated/TWAPOracles/BIP37"; -import { onSunriseSetUniswapV2Twa } from "./UniswapV2Handler"; +import { setUniswapV2Twa } from "./UniswapV2Handler"; +import { setCurveTwa } from "./Bean3CRVHandler_V1"; +import { DeltaBPriceLiquidity } from "./utils/price/Types"; export function handleSunrise(event: Sunrise): void { // Update the season for hourly and daily liquidity metrics @@ -107,23 +109,13 @@ export function handleSunrise(event: Sunrise): void { let totalLiquidity = ZERO_BD; for (let i = 0; i < bean.pools.length; i++) { const pool = loadOrCreatePool(bean.pools[i], event.block.number); - let price = ZERO_BD; - let liquidity = ZERO_BD; - let deltaB = ZERO_BI; + let inst: DeltaBPriceLiquidity; if (bean.pools[i] == BEAN_WETH_V1.toHexString()) { - const wethToken = updatePreReplantPriceETH(); - const weth_bd = toDecimal(pool.reserves[0], 18); - const bean_bd = toDecimal(pool.reserves[1]); - price = uniswapV2Price(bean_bd, weth_bd, wethToken.lastPriceUSD); - liquidity = weth_bd.times(wethToken.lastPriceUSD); - deltaB = uniswapV2DeltaB(bean_bd, weth_bd, wethToken.lastPriceUSD); - - onSunriseSetUniswapV2Twa(bean.pools[i], event.block.timestamp, event.block.number); + inst = calcUniswapV2Inst(pool); + setUniswapV2Twa(bean.pools[i], event.block.timestamp, event.block.number); } else { - const priceAndLp = curvePriceAndLp(Address.fromString(bean.pools[i])); - price = priceAndLp[0]; - liquidity = priceAndLp[1]; - deltaB = curveDeltaB(Address.fromString(bean.pools[i]), pool.reserves[0]); + inst = calcCurveInst(pool); + setCurveTwa(bean.pools[i], event.block.timestamp, event.block.number); } // Update price, liquidity, and deltaB in the pool @@ -133,13 +125,13 @@ export function handleSunrise(event: Sunrise): void { event.block.number, ZERO_BI, ZERO_BD, - liquidity.minus(pool.liquidityUSD), - deltaB + inst.liquidity.minus(pool.liquidityUSD), + inst.deltaB ); - updatePoolPrice(bean.pools[i], event.block.timestamp, event.block.number, price); + updatePoolPrice(bean.pools[i], event.block.timestamp, event.block.number, inst.price); - weightedPrice = weightedPrice.plus(price.times(liquidity)); - totalLiquidity = totalLiquidity.plus(liquidity); + weightedPrice = weightedPrice.plus(inst.price.times(inst.liquidity)); + totalLiquidity = totalLiquidity.plus(inst.liquidity); } const totalPrice = weightedPrice.div(totalLiquidity); diff --git a/projects/subgraph-bean/src/UniswapV2Handler.ts b/projects/subgraph-bean/src/UniswapV2Handler.ts index 9c835d3cb3..092d042af2 100644 --- a/projects/subgraph-bean/src/UniswapV2Handler.ts +++ b/projects/subgraph-bean/src/UniswapV2Handler.ts @@ -21,7 +21,7 @@ import { updatePreReplantPriceETH } from "./utils/price/UniswapPrice"; import { getTWAPrices } from "./utils/price/TwaOracle"; -import { TWAType } from "./utils/price/Types"; +import { DeltaBAndPrice, TWAType } from "./utils/price/Types"; // export function handleMint(event: Mint): void { // updatePoolReserves(event.address.toHexString(), event.params.amount0, event.params.amount1, event.block.number); @@ -115,7 +115,7 @@ export function handleSync(event: Sync): void { updateBeanValues(BEAN_ERC20_V1.toHexString(), event.block.timestamp, currentBeanPrice, ZERO_BI, ZERO_BI, ZERO_BD, deltaLiquidityUSD); } -export function onSunriseSetUniswapV2Twa(poolAddress: string, timestamp: BigInt, blockNumber: BigInt): void { +export function setUniswapV2Twa(poolAddress: string, timestamp: BigInt, blockNumber: BigInt): DeltaBAndPrice { const twaPrices = getTWAPrices(poolAddress, TWAType.UNISWAP, timestamp); const twaResult = uniswapTwaDeltaBAndPrice(twaPrices, blockNumber); @@ -127,4 +127,6 @@ export function onSunriseSetUniswapV2Twa(poolAddress: string, timestamp: BigInt, poolDaily.twaPrice = twaResult.price; poolHourly.save(); poolDaily.save(); + + return twaResult; } diff --git a/projects/subgraph-bean/src/utils/price/CurvePrice.ts b/projects/subgraph-bean/src/utils/price/CurvePrice.ts index 02bf64db25..b5d252773f 100644 --- a/projects/subgraph-bean/src/utils/price/CurvePrice.ts +++ b/projects/subgraph-bean/src/utils/price/CurvePrice.ts @@ -4,7 +4,8 @@ import { BD_10, BI_10, ONE_BI, toDecimal, ZERO_BD, ZERO_BI } from "../../../../s import { BEAN_3CRV_V1, BEAN_LUSD_V1, CALCULATIONS_CURVE, CRV3_POOL_V1, LUSD, LUSD_3POOL } from "../../../../subgraph-core/utils/Constants"; import { CalculationsCurve } from "../../../generated/Bean3CRV-V1/CalculationsCurve"; import { ERC20 } from "../../../generated/Bean3CRV-V1/ERC20"; -import { DeltaBAndPrice } from "./Types"; +import { DeltaBAndPrice, DeltaBPriceLiquidity } from "./Types"; +import { Pool } from "../../../generated/schema"; // Note that the Bean3CRV type applies to any curve pool (including lusd) @@ -42,6 +43,15 @@ export function curvePriceAndLp(pool: Address): BigDecimal[] { return [beanPrice, lpValue]; } +export function calcCurveInst(pool: Pool): DeltaBPriceLiquidity { + const priceAndLp = curvePriceAndLp(Address.fromString(pool.id)); + return { + price: priceAndLp[0], + liquidity: priceAndLp[1], + deltaB: curveDeltaB(Address.fromString(pool.id), pool.reserves[0]) + }; +} + // TODO: this logic can be refactored to remove the contract calls and instead use getD method. // Returns the deltaB in the given curve pool export function curveDeltaB(pool: Address, beanReserves: BigInt): BigInt { @@ -143,7 +153,7 @@ function getY(x: BigInt, xp: BigInt[], a: BigInt, D: BigInt): BigInt { let Ann: BigInt = a.times(N_COINS); // Calculate c considering each element in xp - for (let _i = 0; _i < N_COINS.toU32(); ++_i) { + for (let _i = 0; _i < N_COINS.toI32(); ++_i) { if (_i == i) _x = x; else if (_i != j) _x = xp[_i]; else continue; diff --git a/projects/subgraph-bean/src/utils/price/Types.ts b/projects/subgraph-bean/src/utils/price/Types.ts index dc4ab13963..2557dfb9b9 100644 --- a/projects/subgraph-bean/src/utils/price/Types.ts +++ b/projects/subgraph-bean/src/utils/price/Types.ts @@ -9,3 +9,9 @@ export class DeltaBAndPrice { deltaB: BigInt; price: BigDecimal; } + +export class DeltaBPriceLiquidity { + deltaB: BigInt; + price: BigDecimal; + liquidity: BigDecimal; +} diff --git a/projects/subgraph-bean/src/utils/price/UniswapPrice.ts b/projects/subgraph-bean/src/utils/price/UniswapPrice.ts index 13d37d89b1..9721d5e26f 100644 --- a/projects/subgraph-bean/src/utils/price/UniswapPrice.ts +++ b/projects/subgraph-bean/src/utils/price/UniswapPrice.ts @@ -1,11 +1,11 @@ import { Address, BigDecimal, BigInt, log } from "@graphprotocol/graph-ts"; import { BD_10, BI_10, ONE_BI, pow, sqrt, toDecimal, ZERO_BD, ZERO_BI } from "../../../../subgraph-core/utils/Decimals"; -import { Token } from "../../../generated/schema"; +import { Pool, Token } from "../../../generated/schema"; import { loadOrCreateToken } from "../Token"; import { UniswapV2Pair } from "../../../generated/BeanUniswapV2Pair/UniswapV2Pair"; import { BEANSTALK, WETH, WETH_USDC_PAIR } from "../../../../subgraph-core/utils/Constants"; import { PreReplant } from "../../../generated/Beanstalk/PreReplant"; -import { DeltaBAndPrice } from "./Types"; +import { DeltaBAndPrice, DeltaBPriceLiquidity } from "./Types"; export function updatePreReplantPriceETH(): Token { let token = loadOrCreateToken(WETH.toHexString()); @@ -22,6 +22,17 @@ export function updatePreReplantPriceETH(): Token { return token; } +export function calcUniswapV2Inst(pool: Pool): DeltaBPriceLiquidity { + const wethToken = updatePreReplantPriceETH(); + const weth_bd = toDecimal(pool.reserves[0], 18); + const bean_bd = toDecimal(pool.reserves[1]); + return { + price: uniswapV2Price(bean_bd, weth_bd, wethToken.lastPriceUSD), + liquidity: weth_bd.times(wethToken.lastPriceUSD), + deltaB: uniswapV2DeltaB(bean_bd, weth_bd, wethToken.lastPriceUSD) + }; +} + // For our single uniswapv2 pool, token 0 is WETH and token 1 is BEAN export function uniswapV2Reserves(pool: Address): BigInt[] { let pair = UniswapV2Pair.bind(pool);