From ed0964fdec6115be580486eb7d29169678b0fb0e Mon Sep 17 00:00:00 2001 From: Soil King <157099073+soilking@users.noreply.github.com> Date: Tue, 16 Apr 2024 23:20:34 -0700 Subject: [PATCH] correct address and method for external pool --- .../subgraph-bean/src/Bean3CRVHandler_V1.ts | 4 +- .../src/utils/price/CurvePrice.ts | 20 ++-- projects/subgraph-core/abis/Bean3CRV.json | 93 +++++++------------ projects/subgraph-core/utils/Constants.ts | 2 +- 4 files changed, 46 insertions(+), 73 deletions(-) diff --git a/projects/subgraph-bean/src/Bean3CRVHandler_V1.ts b/projects/subgraph-bean/src/Bean3CRVHandler_V1.ts index 1085cd1017..1ae3f0e6e0 100644 --- a/projects/subgraph-bean/src/Bean3CRVHandler_V1.ts +++ b/projects/subgraph-bean/src/Bean3CRVHandler_V1.ts @@ -8,7 +8,7 @@ import { TokenExchangeUnderlying } from "../generated/Bean3CRV/Bean3CRV"; import { loadBean, updateBeanSupplyPegPercent, updateBeanValues } from "./utils/Bean"; -import { BEAN_3CRV_V1, BEAN_ERC20_V1, CRV3_POOL_V1, LUSD_3POOL } from "../../subgraph-core/utils/Constants"; +import { BEAN_3CRV_V1, BEAN_ERC20_V1, CRV3_POOL, LUSD_3POOL } from "../../subgraph-core/utils/Constants"; import { toDecimal, ZERO_BD, ZERO_BI } from "../../subgraph-core/utils/Decimals"; import { loadOrCreatePool, @@ -209,7 +209,7 @@ function handleSwap( 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; + const otherPool = beanPool == BEAN_3CRV_V1 ? CRV3_POOL : LUSD_3POOL; const twaResult = curveTwaDeltaBAndPrice(twaBalances, beanPool, otherPool); let poolHourly = loadOrCreatePoolHourlySnapshot(poolAddress, timestamp, blockNumber); diff --git a/projects/subgraph-bean/src/utils/price/CurvePrice.ts b/projects/subgraph-bean/src/utils/price/CurvePrice.ts index b5d252773f..96f0d7f0fb 100644 --- a/projects/subgraph-bean/src/utils/price/CurvePrice.ts +++ b/projects/subgraph-bean/src/utils/price/CurvePrice.ts @@ -1,7 +1,7 @@ import { Address, BigDecimal, BigInt, log } from "@graphprotocol/graph-ts"; import { Bean3CRV } from "../../../generated/Bean3CRV-V1/Bean3CRV"; import { BD_10, BI_10, ONE_BI, toDecimal, ZERO_BD, ZERO_BI } from "../../../../subgraph-core/utils/Decimals"; -import { BEAN_3CRV_V1, BEAN_LUSD_V1, CALCULATIONS_CURVE, CRV3_POOL_V1, LUSD, LUSD_3POOL } from "../../../../subgraph-core/utils/Constants"; +import { BEAN_3CRV_V1, BEAN_LUSD_V1, CALCULATIONS_CURVE, CRV3_TOKEN, LUSD, LUSD_3POOL } from "../../../../subgraph-core/utils/Constants"; import { CalculationsCurve } from "../../../generated/Bean3CRV-V1/CalculationsCurve"; import { ERC20 } from "../../../generated/Bean3CRV-V1/ERC20"; import { DeltaBAndPrice, DeltaBPriceLiquidity } from "./Types"; @@ -13,7 +13,7 @@ import { Pool } from "../../../generated/schema"; export function curvePriceAndLp(pool: Address): BigDecimal[] { // Get Curve Price Details let curveCalc = CalculationsCurve.bind(CALCULATIONS_CURVE); - let metapoolPrice = toDecimal(curveCalc.getCurvePriceUsdc(CRV3_POOL_V1)); + let metapoolPrice = toDecimal(curveCalc.getCurvePriceUsdc(CRV3_TOKEN)); let lpContract = Bean3CRV.bind(pool); let beanCrvPrice = ZERO_BD; @@ -22,8 +22,8 @@ export function curvePriceAndLp(pool: Address): BigDecimal[] { if (pool == BEAN_3CRV_V1) { beanCrvPrice = toDecimal(lpContract.get_dy(ZERO_BI, BigInt.fromI32(1), BigInt.fromI32(1000000)), 18); - let crv3PoolContract = ERC20.bind(CRV3_POOL_V1); - let crvHolding = toDecimal(crv3PoolContract.balanceOf(pool), 18); + let crv3Contract = ERC20.bind(CRV3_TOKEN); + let crvHolding = toDecimal(crv3Contract.balanceOf(pool), 18); lpValue = crvHolding.times(metapoolPrice); } else if (pool == BEAN_LUSD_V1) { // price in LUSD @@ -81,16 +81,20 @@ export function curveCumulativePrices(pool: Address, timestamp: BigInt): BigInt[ // beanPool is the pool with beans trading against otherPool's tokens. // otherPool is needed to get the virtual price of that token beans are trading against. export function curveTwaDeltaBAndPrice(twaBalances: BigInt[], beanPool: Address, otherPool: Address): DeltaBAndPrice { - let beanCurve = Bean3CRV.bind(beanPool); - const bean_A = beanCurve.A_precise(); + // In practice we can hardcode bean_A and avoid an unnecessary call since there was no ramping in our pools + // let beanCurve = Bean3CRV.bind(beanPool); + // const bean_A = beanCurve.A_precise(); + const bean_A = beanPool == BEAN_3CRV_V1 ? BigInt.fromU32(1000) : BigInt.fromU32(10000); let otherCurve = Bean3CRV.bind(otherPool); - const other_A = otherCurve.A_precise(); + const other_virtual_price = otherCurve.get_virtual_price(); - const xp = [twaBalances[0].times(BI_10.pow(12)), twaBalances[1].times(other_A).div(BI_10.pow(18))]; + const xp = [twaBalances[0].times(BI_10.pow(12)), twaBalances[1].times(other_virtual_price).div(BI_10.pow(18))]; const D = getD(xp, bean_A); const y = getY(xp[0].plus(BI_10.pow(12)), xp, bean_A, D); + log.debug("curve bean_A {}", [bean_A.toString()]); + log.debug("curve other_virtual_price {}", [other_virtual_price.toString()]); log.debug("curve D {}", [D.toString()]); log.debug("curve y {}", [y.toString()]); log.debug("curve deltaB calculated {}", [D.div(BigInt.fromU32(2)).div(BI_10.pow(12)).minus(twaBalances[0]).toString()]); diff --git a/projects/subgraph-core/abis/Bean3CRV.json b/projects/subgraph-core/abis/Bean3CRV.json index d5ef342ba8..290fa010ba 100644 --- a/projects/subgraph-core/abis/Bean3CRV.json +++ b/projects/subgraph-core/abis/Bean3CRV.json @@ -377,8 +377,7 @@ } ], "stateMutability": "nonpayable", - "type": "function", - "gas": 470049 + "type": "function" }, { "name": "decimals", @@ -390,8 +389,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 291 + "type": "function" }, { "name": "transfer", @@ -412,8 +410,7 @@ } ], "stateMutability": "nonpayable", - "type": "function", - "gas": 75402 + "type": "function" }, { "name": "transferFrom", @@ -438,8 +435,7 @@ } ], "stateMutability": "nonpayable", - "type": "function", - "gas": 112037 + "type": "function" }, { "name": "approve", @@ -460,8 +456,7 @@ } ], "stateMutability": "nonpayable", - "type": "function", - "gas": 37854 + "type": "function" }, { "name": "get_previous_balances", @@ -473,8 +468,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2254 + "type": "function" }, { "name": "get_balances", @@ -486,8 +480,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2284 + "type": "function" }, { "name": "get_twap_balances", @@ -512,8 +505,7 @@ } ], "stateMutability": "view", - "type": "function", - "gas": 1522 + "type": "function" }, { "name": "get_price_cumulative_last", @@ -525,8 +517,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2344 + "type": "function" }, { "name": "admin_fee", @@ -538,8 +529,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 621 + "type": "function" }, { "name": "A", @@ -551,8 +541,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 5859 + "type": "function" }, { "name": "A_precise", @@ -564,8 +553,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 5821 + "type": "function" }, { "name": "get_virtual_price", @@ -577,8 +565,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 1011891 + "type": "function" }, { "name": "calc_token_amount", @@ -1110,16 +1097,14 @@ } ], "stateMutability": "nonpayable", - "type": "function", - "gas": 152464 + "type": "function" }, { "name": "stop_ramp_A", "outputs": [], "inputs": [], "stateMutability": "nonpayable", - "type": "function", - "gas": 149225 + "type": "function" }, { "name": "admin_balances", @@ -1136,16 +1121,14 @@ } ], "stateMutability": "view", - "type": "function", - "gas": 3601 + "type": "function" }, { "name": "withdraw_admin_fees", "outputs": [], "inputs": [], "stateMutability": "nonpayable", - "type": "function", - "gas": 11347 + "type": "function" }, { "name": "admin", @@ -1157,8 +1140,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2141 + "type": "function" }, { "name": "coins", @@ -1175,8 +1157,7 @@ } ], "stateMutability": "view", - "type": "function", - "gas": 2280 + "type": "function" }, { "name": "balances", @@ -1193,8 +1174,7 @@ } ], "stateMutability": "view", - "type": "function", - "gas": 2310 + "type": "function" }, { "name": "fee", @@ -1206,8 +1186,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2231 + "type": "function" }, { "name": "block_timestamp_last", @@ -1219,8 +1198,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2261 + "type": "function" }, { "name": "initial_A", @@ -1232,8 +1210,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2291 + "type": "function" }, { "name": "future_A", @@ -1245,8 +1222,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2321 + "type": "function" }, { "name": "initial_A_time", @@ -1258,8 +1234,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2351 + "type": "function" }, { "name": "future_A_time", @@ -1271,8 +1246,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2381 + "type": "function" }, { "name": "name", @@ -1284,8 +1258,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 8813 + "type": "function" }, { "name": "symbol", @@ -1297,8 +1270,7 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 7866 + "type": "function" }, { "name": "balanceOf", @@ -1315,8 +1287,7 @@ } ], "stateMutability": "view", - "type": "function", - "gas": 2686 + "type": "function" }, { "name": "allowance", @@ -1337,8 +1308,7 @@ } ], "stateMutability": "view", - "type": "function", - "gas": 2931 + "type": "function" }, { "name": "totalSupply", @@ -1350,7 +1320,6 @@ ], "inputs": [], "stateMutability": "view", - "type": "function", - "gas": 2531 + "type": "function" } ] diff --git a/projects/subgraph-core/utils/Constants.ts b/projects/subgraph-core/utils/Constants.ts index d402fddd0d..19d231a2c7 100644 --- a/projects/subgraph-core/utils/Constants.ts +++ b/projects/subgraph-core/utils/Constants.ts @@ -23,7 +23,7 @@ export const BEANSTALK_PRICE = Address.fromString("0xb01CE0008CaD90104651d6A84b6 // LP Addresses export const BEAN_3CRV_V1 = Address.fromString("0x3a70DfA7d2262988064A2D051dd47521E43c9BdD"); -export const CRV3_POOL_V1 = Address.fromString("0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490"); +export const CRV3_TOKEN = Address.fromString("0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490"); export const CRV3_POOL = Address.fromString("0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7"); export const BEAN_WETH_V1 = Address.fromString("0x87898263B6C5BABe34b4ec53F22d98430b91e371"); export const WETH_USDC_PAIR = Address.fromString("0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc");