Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
soilking committed Apr 17, 2024
1 parent 20d7d9c commit c5acdc2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion projects/subgraph-bean/src/Bean3CRVHandler_V1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 15 additions & 23 deletions projects/subgraph-bean/src/BeanstalkHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions projects/subgraph-bean/src/UniswapV2Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -127,4 +127,6 @@ export function onSunriseSetUniswapV2Twa(poolAddress: string, timestamp: BigInt,
poolDaily.twaPrice = twaResult.price;
poolHourly.save();
poolDaily.save();

return twaResult;
}
14 changes: 12 additions & 2 deletions projects/subgraph-bean/src/utils/price/CurvePrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions projects/subgraph-bean/src/utils/price/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ export class DeltaBAndPrice {
deltaB: BigInt;
price: BigDecimal;
}

export class DeltaBPriceLiquidity {
deltaB: BigInt;
price: BigDecimal;
liquidity: BigDecimal;
}
15 changes: 13 additions & 2 deletions projects/subgraph-bean/src/utils/price/UniswapPrice.ts
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -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);
Expand Down

0 comments on commit c5acdc2

Please sign in to comment.