Skip to content

Commit

Permalink
correct address and method for external pool
Browse files Browse the repository at this point in the history
  • Loading branch information
soilking committed Apr 17, 2024
1 parent c5acdc2 commit ed0964f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 73 deletions.
4 changes: 2 additions & 2 deletions projects/subgraph-bean/src/Bean3CRVHandler_V1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 12 additions & 8 deletions projects/subgraph-bean/src/utils/price/CurvePrice.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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()]);
Expand Down
93 changes: 31 additions & 62 deletions projects/subgraph-core/abis/Bean3CRV.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@
}
],
"stateMutability": "nonpayable",
"type": "function",
"gas": 470049
"type": "function"
},
{
"name": "decimals",
Expand All @@ -390,8 +389,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 291
"type": "function"
},
{
"name": "transfer",
Expand All @@ -412,8 +410,7 @@
}
],
"stateMutability": "nonpayable",
"type": "function",
"gas": 75402
"type": "function"
},
{
"name": "transferFrom",
Expand All @@ -438,8 +435,7 @@
}
],
"stateMutability": "nonpayable",
"type": "function",
"gas": 112037
"type": "function"
},
{
"name": "approve",
Expand All @@ -460,8 +456,7 @@
}
],
"stateMutability": "nonpayable",
"type": "function",
"gas": 37854
"type": "function"
},
{
"name": "get_previous_balances",
Expand All @@ -473,8 +468,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2254
"type": "function"
},
{
"name": "get_balances",
Expand All @@ -486,8 +480,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2284
"type": "function"
},
{
"name": "get_twap_balances",
Expand All @@ -512,8 +505,7 @@
}
],
"stateMutability": "view",
"type": "function",
"gas": 1522
"type": "function"
},
{
"name": "get_price_cumulative_last",
Expand All @@ -525,8 +517,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2344
"type": "function"
},
{
"name": "admin_fee",
Expand All @@ -538,8 +529,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 621
"type": "function"
},
{
"name": "A",
Expand All @@ -551,8 +541,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 5859
"type": "function"
},
{
"name": "A_precise",
Expand All @@ -564,8 +553,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 5821
"type": "function"
},
{
"name": "get_virtual_price",
Expand All @@ -577,8 +565,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 1011891
"type": "function"
},
{
"name": "calc_token_amount",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -1157,8 +1140,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2141
"type": "function"
},
{
"name": "coins",
Expand All @@ -1175,8 +1157,7 @@
}
],
"stateMutability": "view",
"type": "function",
"gas": 2280
"type": "function"
},
{
"name": "balances",
Expand All @@ -1193,8 +1174,7 @@
}
],
"stateMutability": "view",
"type": "function",
"gas": 2310
"type": "function"
},
{
"name": "fee",
Expand All @@ -1206,8 +1186,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2231
"type": "function"
},
{
"name": "block_timestamp_last",
Expand All @@ -1219,8 +1198,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2261
"type": "function"
},
{
"name": "initial_A",
Expand All @@ -1232,8 +1210,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2291
"type": "function"
},
{
"name": "future_A",
Expand All @@ -1245,8 +1222,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2321
"type": "function"
},
{
"name": "initial_A_time",
Expand All @@ -1258,8 +1234,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2351
"type": "function"
},
{
"name": "future_A_time",
Expand All @@ -1271,8 +1246,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2381
"type": "function"
},
{
"name": "name",
Expand All @@ -1284,8 +1258,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 8813
"type": "function"
},
{
"name": "symbol",
Expand All @@ -1297,8 +1270,7 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 7866
"type": "function"
},
{
"name": "balanceOf",
Expand All @@ -1315,8 +1287,7 @@
}
],
"stateMutability": "view",
"type": "function",
"gas": 2686
"type": "function"
},
{
"name": "allowance",
Expand All @@ -1337,8 +1308,7 @@
}
],
"stateMutability": "view",
"type": "function",
"gas": 2931
"type": "function"
},
{
"name": "totalSupply",
Expand All @@ -1350,7 +1320,6 @@
],
"inputs": [],
"stateMutability": "view",
"type": "function",
"gas": 2531
"type": "function"
}
]
2 changes: 1 addition & 1 deletion projects/subgraph-core/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit ed0964f

Please sign in to comment.