From 6aca74cb3c704616c77381d5b7f700bf6bbc4d8f Mon Sep 17 00:00:00 2001 From: Soil King <157099073+soilking@users.noreply.github.com> Date: Thu, 13 Jun 2024 12:25:59 -0700 Subject: [PATCH] track separate volume on both sides of trade --- projects/subgraph-basin/schema.graphql | 21 +++++++++++++++++++ projects/subgraph-basin/src/utils/VolumeCP.ts | 9 ++++++++ projects/subgraph-basin/src/utils/Well.ts | 20 ++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/projects/subgraph-basin/schema.graphql b/projects/subgraph-basin/schema.graphql index a4b7e614f2..f0be49e93c 100644 --- a/projects/subgraph-basin/schema.graphql +++ b/projects/subgraph-basin/schema.graphql @@ -122,6 +122,9 @@ type Well @entity { " All trade volume occurred in this well, in USD. This includes any net trading activity as a result of add/remove liquidity. Should be equal to the sum of all entries in cumulativeTradeVolumeReservesUSD " cumulativeTradeVolumeUSD: BigDecimal! + " All trade volume occurred for a specific token, in native amount. This includes absolute tokens on both sides of the trade unlike cumulativeTradeVolumeReserves. This includes any net trading activity as a result of add/remove liquidity. The ordering should be the same as the well's `tokens` field. " + cumulativeBiTradeVolumeReserves: [BigInt!]! + " All transfer volume occurred for a specific token, in native amount. This includes the full amount of tokens transferred in ad/remove liquidity. The ordering should be the same as the well's `tokens` field. " cumulativeTransferVolumeReserves: [BigInt!]! @@ -149,6 +152,9 @@ type Well @entity { " Current rolling 24h trade volume in USD " rollingDailyTradeVolumeUSD: BigDecimal! + " Current rolling 24h reserve trade volume in token amounts, including absolute tokens on both side of the trade unlike rollingDailyTradeVolumeReserves. " + rollingDailyBiTradeVolumeReserves: [BigInt!]! + " Current rolling 24h reserve transfer volume in token amounts " rollingDailyTransferVolumeReserves: [BigInt!]! @@ -167,6 +173,9 @@ type Well @entity { " Current rolling weekly trade volume in USD " rollingWeeklyTradeVolumeUSD: BigDecimal! + " Current rolling weekly reserve trade volume in token amounts, including absolute tokens on both side of the trade unlike rollingWeeklyTradeVolumeReserves. " + rollingWeeklyBiTradeVolumeReserves: [BigInt!]! + " Current rolling weekly reserve transfer volume in token amounts " rollingWeeklyTransferVolumeReserves: [BigInt!]! @@ -235,6 +244,9 @@ type WellDailySnapshot @entity { " All trade volume occurred in this well, in USD. This includes any net trading activity as a result of add/remove liquidity. Should be equal to the sum of all entries in cumulativeTradeVolumeReservesUSD " cumulativeTradeVolumeUSD: BigDecimal! + " All trade volume occurred for a specific token, in native amount. This includes absolute tokens on both sides of the trade unlike cumulativeTradeVolumeReserves. This includes any net trading activity as a result of add/remove liquidity. The ordering should be the same as the well's `tokens` field. " + cumulativeBiTradeVolumeReserves: [BigInt!]! + " All transfer volume occurred for a specific token, in native amount. This includes the full amount of tokens transferred in ad/remove liquidity. The ordering should be the same as the well's `tokens` field. " cumulativeTransferVolumeReserves: [BigInt!]! @@ -270,6 +282,9 @@ type WellDailySnapshot @entity { " Delta of cumulativeTradeVolumeUSD " deltaTradeVolumeUSD: BigDecimal! + " Delta of cumulativeBiTradeVolumeReserves " + deltaBiTradeVolumeReserves: [BigInt!]! + " Delta of cumulativeTransferVolumeReserves " deltaTransferVolumeReserves: [BigInt!]! @@ -322,6 +337,9 @@ type WellHourlySnapshot @entity { " All trade volume occurred in this well, in USD. This includes any net trading activity as a result of add/remove liquidity. Should be equal to the sum of all entries in cumulativeTradeVolumeReservesUSD " cumulativeTradeVolumeUSD: BigDecimal! + " All trade volume occurred for a specific token, in native amount. This includes absolute tokens on both sides of the trade unlike cumulativeTradeVolumeReserves. This includes any net trading activity as a result of add/remove liquidity. The ordering should be the same as the well's `tokens` field. " + cumulativeBiTradeVolumeReserves: [BigInt!]! + " All transfer volume occurred for a specific token, in native amount. This includes the full amount of tokens transferred in ad/remove liquidity. The ordering should be the same as the well's `tokens` field. " cumulativeTransferVolumeReserves: [BigInt!]! @@ -357,6 +375,9 @@ type WellHourlySnapshot @entity { " Delta of cumulativeTradeVolumeUSD " deltaTradeVolumeUSD: BigDecimal! + " Delta of cumulativeBiTradeVolumeReserves " + deltaBiTradeVolumeReserves: [BigInt!]! + " Delta of cumulativeTransferVolumeReserves " deltaTransferVolumeReserves: [BigInt!]! diff --git a/projects/subgraph-basin/src/utils/VolumeCP.ts b/projects/subgraph-basin/src/utils/VolumeCP.ts index 337a4647c2..f28c00d3f3 100644 --- a/projects/subgraph-basin/src/utils/VolumeCP.ts +++ b/projects/subgraph-basin/src/utils/VolumeCP.ts @@ -118,10 +118,13 @@ export function calcLiquidityVolume(currentReserves: BigInt[], addedReserves: Bi function updateVolumeStats(well: Well, deltaTradeVolumeReserves: BigInt[], deltaTransferVolumeReserves: BigInt[]): void { let tradeVolumeReserves = well.cumulativeTradeVolumeReserves; let tradeVolumeReservesUSD = well.cumulativeTradeVolumeReservesUSD; + let biTradeVolumeReserves = well.cumulativeBiTradeVolumeReserves; let rollingDailyTradeVolumeReserves = well.rollingDailyTradeVolumeReserves; let rollingDailyTradeVolumeReservesUSD = well.rollingDailyTradeVolumeReservesUSD; + let rollingDailyBiTradeVolumeReserves = well.rollingDailyBiTradeVolumeReserves; let rollingWeeklyTradeVolumeReserves = well.rollingWeeklyTradeVolumeReserves; let rollingWeeklyTradeVolumeReservesUSD = well.rollingWeeklyTradeVolumeReservesUSD; + let rollingWeeklyBiTradeVolumeReserves = well.rollingWeeklyBiTradeVolumeReserves; let transferVolumeReserves = well.cumulativeTransferVolumeReserves; let transferVolumeReservesUSD = well.cumulativeTransferVolumeReservesUSD; @@ -141,6 +144,9 @@ function updateVolumeStats(well: Well, deltaTradeVolumeReserves: BigInt[], delta rollingWeeklyTradeVolumeReserves[i] = rollingWeeklyTradeVolumeReserves[i].plus(deltaTradeVolumeReserves[i]); usdTradeAmount = toDecimal(deltaTradeVolumeReserves[i], tokenInfo.decimals).times(tokenInfo.lastPriceUSD); } + biTradeVolumeReserves[i] = biTradeVolumeReserves[i].plus(deltaTradeVolumeReserves[i].abs()); + rollingDailyBiTradeVolumeReserves[i] = rollingDailyBiTradeVolumeReserves[i].plus(deltaTradeVolumeReserves[i].abs()); + rollingWeeklyBiTradeVolumeReserves[i] = rollingWeeklyBiTradeVolumeReserves[i].plus(deltaTradeVolumeReserves[i].abs()); transferVolumeReserves[i] = transferVolumeReserves[i].plus(deltaTransferVolumeReserves[i].abs()); rollingDailyTransferVolumeReserves[i] = rollingDailyTransferVolumeReserves[i].plus(deltaTransferVolumeReserves[i].abs()); @@ -162,6 +168,7 @@ function updateVolumeStats(well: Well, deltaTradeVolumeReserves: BigInt[], delta well.cumulativeTradeVolumeReserves = tradeVolumeReserves; well.cumulativeTradeVolumeReservesUSD = tradeVolumeReservesUSD; well.cumulativeTradeVolumeUSD = well.cumulativeTradeVolumeUSD.plus(totalTradeUSD); + well.cumulativeBiTradeVolumeReserves = biTradeVolumeReserves; well.cumulativeTransferVolumeReserves = transferVolumeReserves; well.cumulativeTransferVolumeReservesUSD = transferVolumeReservesUSD; @@ -173,6 +180,7 @@ function updateVolumeStats(well: Well, deltaTradeVolumeReserves: BigInt[], delta well.rollingDailyTradeVolumeReserves = rollingDailyTradeVolumeReserves; well.rollingDailyTradeVolumeReservesUSD = rollingDailyTradeVolumeReservesUSD; well.rollingDailyTradeVolumeUSD = well.rollingDailyTradeVolumeUSD.plus(totalTradeUSD); + well.rollingDailyBiTradeVolumeReserves = rollingDailyBiTradeVolumeReserves; well.rollingDailyTransferVolumeReserves = rollingDailyTransferVolumeReserves; well.rollingDailyTransferVolumeReservesUSD = rollingDailyTransferVolumeReservesUSD; well.rollingDailyTransferVolumeUSD = well.rollingDailyTransferVolumeUSD.plus(totalTransferUSD); @@ -180,6 +188,7 @@ function updateVolumeStats(well: Well, deltaTradeVolumeReserves: BigInt[], delta well.rollingWeeklyTradeVolumeReserves = rollingWeeklyTradeVolumeReserves; well.rollingWeeklyTradeVolumeReservesUSD = rollingWeeklyTradeVolumeReservesUSD; well.rollingWeeklyTradeVolumeUSD = well.rollingWeeklyTradeVolumeUSD.plus(totalTradeUSD); + well.rollingWeeklyBiTradeVolumeReserves = rollingWeeklyBiTradeVolumeReserves; well.rollingWeeklyTransferVolumeReserves = rollingWeeklyTransferVolumeReserves; well.rollingWeeklyTransferVolumeReservesUSD = rollingWeeklyTransferVolumeReservesUSD; well.rollingWeeklyTransferVolumeUSD = well.rollingWeeklyTransferVolumeUSD.plus(totalTransferUSD); diff --git a/projects/subgraph-basin/src/utils/Well.ts b/projects/subgraph-basin/src/utils/Well.ts index f103e9b1d9..4c7badccde 100644 --- a/projects/subgraph-basin/src/utils/Well.ts +++ b/projects/subgraph-basin/src/utils/Well.ts @@ -52,6 +52,7 @@ export function createWell(wellAddress: Address, implementation: Address, inputT well.cumulativeTradeVolumeReserves = emptyBigIntArray(inputTokens.length); well.cumulativeTradeVolumeReservesUSD = emptyBigDecimalArray(inputTokens.length); well.cumulativeTradeVolumeUSD = ZERO_BD; + well.cumulativeBiTradeVolumeReserves = emptyBigIntArray(inputTokens.length); well.cumulativeTransferVolumeReserves = emptyBigIntArray(inputTokens.length); well.cumulativeTransferVolumeReservesUSD = emptyBigDecimalArray(inputTokens.length); well.cumulativeTransferVolumeUSD = ZERO_BD; @@ -61,12 +62,14 @@ export function createWell(wellAddress: Address, implementation: Address, inputT well.rollingDailyTradeVolumeReserves = emptyBigIntArray(inputTokens.length); well.rollingDailyTradeVolumeReservesUSD = emptyBigDecimalArray(inputTokens.length); well.rollingDailyTradeVolumeUSD = ZERO_BD; + well.rollingDailyBiTradeVolumeReserves = emptyBigIntArray(inputTokens.length); well.rollingDailyTransferVolumeReserves = emptyBigIntArray(inputTokens.length); well.rollingDailyTransferVolumeReservesUSD = emptyBigDecimalArray(inputTokens.length); well.rollingDailyTransferVolumeUSD = ZERO_BD; well.rollingWeeklyTradeVolumeReserves = emptyBigIntArray(inputTokens.length); well.rollingWeeklyTradeVolumeReservesUSD = emptyBigDecimalArray(inputTokens.length); well.rollingWeeklyTradeVolumeUSD = ZERO_BD; + well.rollingWeeklyBiTradeVolumeReserves = emptyBigIntArray(inputTokens.length); well.rollingWeeklyTransferVolumeReserves = emptyBigIntArray(inputTokens.length); well.rollingWeeklyTransferVolumeReservesUSD = emptyBigDecimalArray(inputTokens.length); well.rollingWeeklyTransferVolumeUSD = ZERO_BD; @@ -218,6 +221,10 @@ export function takeWellDailySnapshot(wellAddress: Address, dayID: i32, timestam priorSnapshot.cumulativeTradeVolumeReservesUSD ); newSnapshot.deltaTradeVolumeUSD = newSnapshot.cumulativeTradeVolumeUSD.minus(priorSnapshot.cumulativeTradeVolumeUSD); + newSnapshot.deltaBiTradeVolumeReserves = deltaBigIntArray( + newSnapshot.cumulativeBiTradeVolumeReserves, + priorSnapshot.cumulativeBiTradeVolumeReserves + ); newSnapshot.deltaTransferVolumeReserves = deltaBigIntArray( newSnapshot.cumulativeTransferVolumeReserves, priorSnapshot.cumulativeTransferVolumeReserves @@ -249,6 +256,7 @@ export function loadOrCreateWellDailySnapshot(wellAddress: Address, dayID: i32, snapshot.cumulativeTradeVolumeReserves = well.cumulativeTradeVolumeReserves; snapshot.cumulativeTradeVolumeReservesUSD = well.cumulativeTradeVolumeReservesUSD; snapshot.cumulativeTradeVolumeUSD = well.cumulativeTradeVolumeUSD; + snapshot.cumulativeBiTradeVolumeReserves = well.cumulativeBiTradeVolumeReserves; snapshot.cumulativeTransferVolumeReserves = well.cumulativeTransferVolumeReserves; snapshot.cumulativeTransferVolumeReservesUSD = well.cumulativeTransferVolumeReservesUSD; snapshot.cumulativeTransferVolumeUSD = well.cumulativeTransferVolumeUSD; @@ -260,6 +268,7 @@ export function loadOrCreateWellDailySnapshot(wellAddress: Address, dayID: i32, snapshot.deltaTradeVolumeReserves = emptyBigIntArray(well.tokens.length); snapshot.deltaTradeVolumeReservesUSD = emptyBigDecimalArray(well.tokens.length); snapshot.deltaTradeVolumeUSD = ZERO_BD; + snapshot.deltaBiTradeVolumeReserves = emptyBigIntArray(well.tokens.length); snapshot.deltaTransferVolumeReserves = emptyBigIntArray(well.tokens.length); snapshot.deltaTransferVolumeReservesUSD = emptyBigDecimalArray(well.tokens.length); snapshot.deltaTransferVolumeUSD = ZERO_BD; @@ -295,6 +304,10 @@ export function takeWellHourlySnapshot(wellAddress: Address, hourID: i32, timest priorSnapshot.cumulativeTradeVolumeReservesUSD ); newSnapshot.deltaTradeVolumeUSD = newSnapshot.cumulativeTradeVolumeUSD.minus(priorSnapshot.cumulativeTradeVolumeUSD); + newSnapshot.deltaBiTradeVolumeReserves = deltaBigIntArray( + newSnapshot.cumulativeBiTradeVolumeReserves, + priorSnapshot.cumulativeBiTradeVolumeReserves + ); newSnapshot.deltaTransferVolumeReserves = deltaBigIntArray( newSnapshot.cumulativeTransferVolumeReserves, priorSnapshot.cumulativeTransferVolumeReserves @@ -323,6 +336,7 @@ export function takeWellHourlySnapshot(wellAddress: Address, hourID: i32, timest oldest24h.deltaTradeVolumeReservesUSD ); well.rollingDailyTradeVolumeUSD = well.rollingDailyTradeVolumeUSD.minus(oldest24h.deltaTradeVolumeUSD); + well.rollingDailyBiTradeVolumeReserves = deltaBigIntArray(well.rollingDailyBiTradeVolumeReserves, oldest24h.deltaBiTradeVolumeReserves); well.rollingDailyTransferVolumeReserves = deltaBigIntArray( well.rollingDailyTransferVolumeReserves, oldest24h.deltaTransferVolumeReserves @@ -339,6 +353,10 @@ export function takeWellHourlySnapshot(wellAddress: Address, hourID: i32, timest oldest7d.deltaTradeVolumeReservesUSD ); well.rollingWeeklyTradeVolumeUSD = well.rollingWeeklyTradeVolumeUSD.minus(oldest7d.deltaTradeVolumeUSD); + well.rollingWeeklyBiTradeVolumeReserves = deltaBigIntArray( + well.rollingWeeklyBiTradeVolumeReserves, + oldest7d.deltaBiTradeVolumeReserves + ); well.rollingWeeklyTransferVolumeReserves = deltaBigIntArray( well.rollingWeeklyTransferVolumeReserves, oldest7d.deltaTransferVolumeReserves @@ -370,6 +388,7 @@ export function loadOrCreateWellHourlySnapshot( snapshot.cumulativeTradeVolumeReserves = well.cumulativeTradeVolumeReserves; snapshot.cumulativeTradeVolumeReservesUSD = well.cumulativeTradeVolumeReservesUSD; snapshot.cumulativeTradeVolumeUSD = well.cumulativeTradeVolumeUSD; + snapshot.cumulativeBiTradeVolumeReserves = well.cumulativeBiTradeVolumeReserves; snapshot.cumulativeTransferVolumeReserves = well.cumulativeTransferVolumeReserves; snapshot.cumulativeTransferVolumeReservesUSD = well.cumulativeTransferVolumeReservesUSD; snapshot.cumulativeTransferVolumeUSD = well.cumulativeTransferVolumeUSD; @@ -381,6 +400,7 @@ export function loadOrCreateWellHourlySnapshot( snapshot.deltaTradeVolumeReserves = emptyBigIntArray(well.tokens.length); snapshot.deltaTradeVolumeReservesUSD = emptyBigDecimalArray(well.tokens.length); snapshot.deltaTradeVolumeUSD = ZERO_BD; + snapshot.deltaBiTradeVolumeReserves = emptyBigIntArray(well.tokens.length); snapshot.deltaTransferVolumeReserves = emptyBigIntArray(well.tokens.length); snapshot.deltaTransferVolumeReservesUSD = emptyBigDecimalArray(well.tokens.length); snapshot.deltaTransferVolumeUSD = ZERO_BD;