Skip to content

Commit

Permalink
fix wellfunction duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
soilking committed Sep 27, 2024
1 parent 18cf3cd commit 68a845a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 40 deletions.
24 changes: 11 additions & 13 deletions projects/subgraph-basin/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand All @@ -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!

Expand Down
17 changes: 7 additions & 10 deletions projects/subgraph-basin/src/entities/Well.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions projects/subgraph-basin/src/handlers/AquiferTemplateHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions projects/subgraph-basin/src/utils/Volume.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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])];
Expand Down
6 changes: 3 additions & 3 deletions projects/subgraph-basin/src/utils/Well.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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[] = [];
Expand Down
8 changes: 4 additions & 4 deletions projects/subgraph-basin/tests/Liquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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")
]);
Expand All @@ -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")
]);
Expand Down
2 changes: 1 addition & 1 deletion projects/subgraph-basin/tests/helpers/Liquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 68a845a

Please sign in to comment.