diff --git a/projects/subgraph-basin/schema.graphql b/projects/subgraph-basin/schema.graphql index 3b16b57b8..37ffcf985 100644 --- a/projects/subgraph-basin/schema.graphql +++ b/projects/subgraph-basin/schema.graphql @@ -55,14 +55,12 @@ type Aquifer @entity { } type WellFunction @entity { - " {Well address}-{Well Function address}" - id: ID! - " Contract address of the well function" - target: Bytes! + " Well Function address " + id: Bytes! " Calldata passed to the well function " data: Bytes! - " Well associated with this well function" - well: Well! + " Wells associated with this well function" + wells: [Well!]! @derivedFrom(field: "wellFunction") } type Pump @entity { @@ -80,9 +78,15 @@ type Well @entity { " Smart contract address of the well " id: Bytes! - " The aquifer this well belongs to " + " The aquifer used to bore this well " aquifer: Aquifer! + " Pricing function contract used with this well " + wellFunction: WellFunction! + + " The well implementation used to deploy this well " + implementation: Bytes! + " Name of liquidity well (e.g. Curve.fi DAI/USDC/USDT) " name: String @@ -95,15 +99,9 @@ type Well @entity { " The order of the tokens in the Well. The above `tokens` association will be sorted by id on any retrieval. " tokenOrder: [Bytes!]! - " Pricing function contract used with this well " - wellFunction: WellFunction! @derivedFrom(field: "well") - " Pumps associated with this well " pumps: [Pump!]! @derivedFrom(field: "well") - " The well implementation used to deploy this well " - implementation: Bytes! - " Creation timestamp " createdTimestamp: BigInt! diff --git a/projects/subgraph-basin/src/entities/Well.ts b/projects/subgraph-basin/src/entities/Well.ts index 90b371a2d..309e834c9 100644 --- a/projects/subgraph-basin/src/entities/Well.ts +++ b/projects/subgraph-basin/src/entities/Well.ts @@ -9,9 +9,8 @@ import { ZERO_BD, ZERO_BI } from "../../../subgraph-core/utils/Decimals"; -import { BoreWellWellFunctionStruct } from "../../generated/Basin-ABIs/Aquifer"; -export function createWell(wellAddress: Address, implementation: Address, inputTokens: Address[]): Well { +export function createWell(wellAddress: Address, inputTokens: Address[]): Well { let well = Well.load(wellAddress); if (well !== null) { return well as Well; @@ -36,7 +35,8 @@ export function createWell(wellAddress: Address, implementation: Address, inputT } well.aquifer = Bytes.empty(); - well.implementation = implementation; + well.wellFunction = Bytes.empty(); + well.implementation = Bytes.empty(); well.tokens = []; // This is currently set in the `handleBoreWell` function well.tokenOrder = []; well.createdTimestamp = ZERO_BI; @@ -83,14 +83,11 @@ export function loadWell(wellAddress: Address): Well { return Well.load(wellAddress) as Well; } -export function loadOrCreateWellFunction(functionData: BoreWellWellFunctionStruct, wellAddress: Address): WellFunction { - let id = wellAddress.toHexString() + "-" + functionData.target.toHexString(); - let wellFunction = WellFunction.load(id); +export function loadOrCreateWellFunction(wellFnAddress: Address): WellFunction { + let wellFunction = WellFunction.load(wellFnAddress); if (wellFunction == null) { - wellFunction = new WellFunction(id); - wellFunction.target = functionData.target; - wellFunction.data = functionData.data; - wellFunction.well = wellAddress; + wellFunction = new WellFunction(wellFnAddress); + wellFunction.data = Bytes.empty(); wellFunction.save(); } return wellFunction as WellFunction; diff --git a/projects/subgraph-basin/src/handlers/AquiferTemplateHandler.ts b/projects/subgraph-basin/src/handlers/AquiferTemplateHandler.ts index 8357e3645..e83f1a2d6 100644 --- a/projects/subgraph-basin/src/handlers/AquiferTemplateHandler.ts +++ b/projects/subgraph-basin/src/handlers/AquiferTemplateHandler.ts @@ -7,15 +7,11 @@ import { createWell, loadOrCreateWellFunction } from "../entities/Well"; import { loadOrCreateToken } from "../entities/Token"; export function handleBoreWell(event: BoreWell): void { - if (event.params.well == Address.fromString("0x875b1da8dcba757398db2bc35043a72b4b62195d")) { - // Ignore well with incorrect price function - return; - } let aquifer = loadOrCreateAquifer(event.address); Well.create(event.params.well); - let well = createWell(event.params.well, event.params.implementation, event.params.tokens); + let well = createWell(event.params.well, event.params.tokens); well.aquifer = event.address; const tokens: Bytes[] = []; @@ -29,8 +25,11 @@ export function handleBoreWell(event: BoreWell): void { loadOrCreatePump(event.params.pumps[i], event.params.well); } - loadOrCreateWellFunction(event.params.wellFunction, event.params.well); + const wellFn = loadOrCreateWellFunction(event.params.wellFunction.target); + wellFn.data = event.params.wellFunction.data; + wellFn.save(); + well.wellFunction = event.params.wellFunction.target; well.implementation = event.params.implementation; well.createdTimestamp = event.block.timestamp; well.createdBlockNumber = event.block.number; diff --git a/projects/subgraph-basin/src/utils/Volume.ts b/projects/subgraph-basin/src/utils/Volume.ts index bbbfb25b4..7db352d3c 100644 --- a/projects/subgraph-basin/src/utils/Volume.ts +++ b/projects/subgraph-basin/src/utils/Volume.ts @@ -1,7 +1,7 @@ import { Address, BigInt, ethereum, log } from "@graphprotocol/graph-ts"; import { emptyBigIntArray, toDecimal, ZERO_BD, ZERO_BI } from "../../../subgraph-core/utils/Decimals"; import { Well } from "../../generated/schema"; -import { loadWell } from "../entities/Well"; +import { loadOrCreateWellFunction, loadWell } from "../entities/Well"; import { loadToken } from "../entities/Token"; import { WellFunction } from "../../generated/Basin-ABIs/WellFunction"; import { toAddress } from "../../../subgraph-core/utils/Bytes"; @@ -69,8 +69,8 @@ export function updateWellVolumesAfterLiquidity( * @returns a list of tokens and the amount bought of each. the purchased token is positive, the sold token negative. */ export function calcLiquidityVolume(well: Well, deltaReserves: BigInt[], deltaLpSupply: BigInt): BigInt[] { - const wellFn = well.wellFunction.load()[0]; - const wellFnContract = WellFunction.bind(toAddress(wellFn.target)); + const wellFn = loadOrCreateWellFunction(toAddress(well.wellFunction)); + const wellFnContract = WellFunction.bind(toAddress(wellFn.id)); const doubleSided = wellFnContract.calcLPTokenUnderlying(deltaLpSupply.abs(), well.reserves, well.lpTokenSupply, wellFn.data); const tokenAmountBought = [doubleSided[0].minus(deltaReserves[0]), doubleSided[1].minus(deltaReserves[1])]; diff --git a/projects/subgraph-basin/src/utils/Well.ts b/projects/subgraph-basin/src/utils/Well.ts index 0b785a3f9..078958287 100644 --- a/projects/subgraph-basin/src/utils/Well.ts +++ b/projects/subgraph-basin/src/utils/Well.ts @@ -1,7 +1,7 @@ import { Address, BigDecimal, BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts"; import { dayFromTimestamp, hourFromTimestamp } from "../../../subgraph-core/utils/Dates"; import { BI_10, emptyBigDecimalArray, getBigDecimalArrayTotal, ONE_BI, toDecimal, ZERO_BI } from "../../../subgraph-core/utils/Decimals"; -import { loadWell, takeWellDailySnapshot, takeWellHourlySnapshot } from "../entities/Well"; +import { loadOrCreateWellFunction, loadWell, takeWellDailySnapshot, takeWellHourlySnapshot } from "../entities/Well"; import { getTokenDecimals, updateTokenUSD } from "./Token"; import { getProtocolToken, isStable2WellFn, wellFnSupportsRate } from "../../../subgraph-core/constants/RuntimeConstants"; import { v } from "./constants/Version"; @@ -50,8 +50,8 @@ export function updateWellTokenUSDPrices(wellAddress: Address, blockNumber: BigI // Value at index i is how much of token i is received in exchange for one of token 1 - i. export function getTokenPrices(well: Well): BigInt[] { - const wellFn = well.wellFunction.load()[0]; - const wellFnAddress = toAddress(wellFn.target); + const wellFn = loadOrCreateWellFunction(toAddress(well.wellFunction)); + const wellFnAddress = toAddress(wellFn.id); const wellFnContract = WellFunction.bind(wellFnAddress); let rates: BigInt[] = []; diff --git a/projects/subgraph-basin/tests/Liquidity.test.ts b/projects/subgraph-basin/tests/Liquidity.test.ts index 6c170d1a3..05368636f 100644 --- a/projects/subgraph-basin/tests/Liquidity.test.ts +++ b/projects/subgraph-basin/tests/Liquidity.test.ts @@ -19,7 +19,7 @@ import { mockSync } from "./helpers/Liquidity"; import { initL1Version } from "./entity-mocking/MockVersion"; -import { loadWell } from "../src/entities/Well"; +import { loadOrCreateWellFunction, loadWell } from "../src/entities/Well"; import { calcLiquidityVolume } from "../src/utils/Volume"; import { toAddress } from "../../subgraph-core/utils/Bytes"; import { mockWellLpTokenUnderlying } from "../../subgraph-core/tests/event-mocking/Tokens"; @@ -314,13 +314,13 @@ describe("Well Entity: Liquidity Event Tests", () => { }); test("Liquidity Volume Calculation", () => { const well = loadWell(WELL); - const wellFn = well.wellFunction.load()[0]; + const wellFn = loadOrCreateWellFunction(toAddress(well.wellFunction)); well.lpTokenSupply = ONE_BI; well.reserves = [BigInt.fromI32(3000).times(BI_10.pow(6)), BigInt.fromU32(1).times(BI_10.pow(18))]; let deltaReserves = [BigInt.fromI32(1500).times(BI_10.pow(6)), ZERO_BI]; let deltaLp = ONE_BI; - mockWellLpTokenUnderlying(toAddress(wellFn.target), deltaLp.abs(), well.reserves, well.lpTokenSupply, wellFn.data, [ + mockWellLpTokenUnderlying(toAddress(wellFn.id), deltaLp.abs(), well.reserves, well.lpTokenSupply, wellFn.data, [ BigInt.fromString("878679656"), BigInt.fromString("292893218813452475") ]); @@ -332,7 +332,7 @@ describe("Well Entity: Liquidity Event Tests", () => { well.reserves = [BigInt.fromI32(1200).times(BI_10.pow(6)), BigInt.fromU32(1).times(BI_10.pow(18))]; deltaReserves = [BigInt.fromI32(-1800).times(BI_10.pow(6)), ZERO_BI]; deltaLp = ONE_BI.neg(); - mockWellLpTokenUnderlying(toAddress(wellFn.target), deltaLp.abs(), well.reserves, well.lpTokenSupply, wellFn.data, [ + mockWellLpTokenUnderlying(toAddress(wellFn.id), deltaLp.abs(), well.reserves, well.lpTokenSupply, wellFn.data, [ BigInt.fromString("-697366596"), BigInt.fromString("-581138830084189666") ]); diff --git a/projects/subgraph-basin/tests/helpers/Liquidity.ts b/projects/subgraph-basin/tests/helpers/Liquidity.ts index 9cd51ff88..48f5ba744 100644 --- a/projects/subgraph-basin/tests/helpers/Liquidity.ts +++ b/projects/subgraph-basin/tests/helpers/Liquidity.ts @@ -66,7 +66,7 @@ export function mockRemoveLiquidityOneWeth(lpAmount: BigInt = WELL_LP_AMOUNT, be function mockCalcLPTokenUnderlying(deltaReserves: BigInt[], lpDelta: BigInt): void { const well = loadWell(WELL); mockWellLpTokenUnderlying( - toAddress(well.wellFunction.load()[0].target), + toAddress(well.wellFunction), lpDelta.abs(), [well.reserves[0].plus(deltaReserves[0]), well.reserves[1].plus(deltaReserves[1])], well.lpTokenSupply.plus(lpDelta),