Skip to content

Commit

Permalink
track separate volume on both sides of trade
Browse files Browse the repository at this point in the history
  • Loading branch information
soilking committed Jun 13, 2024
1 parent 8d14971 commit 6aca74c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
21 changes: 21 additions & 0 deletions projects/subgraph-basin/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!]!

Expand Down Expand Up @@ -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!]!

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

Expand Down Expand Up @@ -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!]!

Expand Down Expand Up @@ -270,6 +282,9 @@ type WellDailySnapshot @entity {
" Delta of cumulativeTradeVolumeUSD "
deltaTradeVolumeUSD: BigDecimal!

" Delta of cumulativeBiTradeVolumeReserves "
deltaBiTradeVolumeReserves: [BigInt!]!

" Delta of cumulativeTransferVolumeReserves "
deltaTransferVolumeReserves: [BigInt!]!

Expand Down Expand Up @@ -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!]!

Expand Down Expand Up @@ -357,6 +375,9 @@ type WellHourlySnapshot @entity {
" Delta of cumulativeTradeVolumeUSD "
deltaTradeVolumeUSD: BigDecimal!

" Delta of cumulativeBiTradeVolumeReserves "
deltaBiTradeVolumeReserves: [BigInt!]!

" Delta of cumulativeTransferVolumeReserves "
deltaTransferVolumeReserves: [BigInt!]!

Expand Down
9 changes: 9 additions & 0 deletions projects/subgraph-basin/src/utils/VolumeCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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;
Expand All @@ -173,13 +180,15 @@ 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);

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);
Expand Down
20 changes: 20 additions & 0 deletions projects/subgraph-basin/src/utils/Well.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 6aca74c

Please sign in to comment.