Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subgraph - Bean and Basin USD Volume #649

Merged
merged 4 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions projects/subgraph-basin/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ type Well @entity {
" Total number of trades (swaps) "
cumulativeSwapCount: Int!

" Current rolling 24 volume in USD "
rollingDailyVolumeUSD: BigDecimal!

" Current rolling weekly volume in USD "
rollingWeeklyVolumeUSD: BigDecimal!

" Day ID of the most recent daily snapshot "
lastSnapshotDayID: Int!

Expand Down
26 changes: 26 additions & 0 deletions projects/subgraph-basin/src/utils/Well.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export function createWell(wellAddress: Address, implementation: Address, inputT
well.cumulativeDepositCount = 0;
well.cumulativeWithdrawCount = 0;
well.cumulativeSwapCount = 0;
well.rollingDailyVolumeUSD = ZERO_BD;
well.rollingWeeklyVolumeUSD = ZERO_BD;
well.lastSnapshotDayID = 0;
well.lastSnapshotHourID = 0;
well.lastUpdateTimestamp = ZERO_BI;
Expand Down Expand Up @@ -92,6 +94,12 @@ export function updateWellVolumes(

let usdVolume = toDecimal(amountIn, swapToken.decimals).times(swapToken.lastPriceUSD);

// Remove liquidity one token has no input token amount to calculate volume.
if (amountIn == ZERO_BI) {
swapToken = loadToken(toToken);
usdVolume = toDecimal(amountOut.div(BigInt.fromI32(2)), swapToken.decimals).times(swapToken.lastPriceUSD);
}

// Update fromToken amounts

let volumeReserves = well.cumulativeVolumeReserves;
Expand Down Expand Up @@ -286,6 +294,24 @@ export function takeWellHourlySnapshot(wellAddress: Address, hourID: i32, timest
newSnapshot.lastUpdateTimestamp = timestamp;
newSnapshot.lastUpdateBlockNumber = blockNumber;
newSnapshot.save();

// Update the rolling daily and weekly volumes
well.rollingDailyVolumeUSD = newSnapshot.deltaVolumeUSD;
well.rollingWeeklyVolumeUSD = newSnapshot.deltaVolumeUSD;
for (let i = 1; i < 168; i++) {
let snapshot = WellHourlySnapshot.load(wellAddress.concatI32(hourID - i));
if (snapshot == null) {
// We hit the last snapshot to total
break;
}
if (i < 24) {
well.rollingDailyVolumeUSD = well.rollingDailyVolumeUSD.plus(snapshot.deltaVolumeUSD);
well.rollingWeeklyVolumeUSD = well.rollingWeeklyVolumeUSD.plus(snapshot.deltaVolumeUSD);
} else {
well.rollingWeeklyVolumeUSD = well.rollingWeeklyVolumeUSD.plus(snapshot.deltaVolumeUSD);
}
}
well.save();
}

export function loadOrCreateWellHourlySnapshot(
Expand Down
21 changes: 13 additions & 8 deletions projects/subgraph-bean/src/BeanWellHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function handleAddLiquidity(event: AddLiquidity): void {
event.block.timestamp,
event.block.number,
event.params.tokenAmountsIn[0],
event.params.tokenAmountsIn[1]
event.params.tokenAmountsIn[1],
false
);
}

Expand All @@ -23,7 +24,8 @@ export function handleRemoveLiquidity(event: RemoveLiquidity): void {
event.block.timestamp,
event.block.number,
event.params.tokenAmountsOut[0],
event.params.tokenAmountsOut[1]
event.params.tokenAmountsOut[1],
true
);
}

Expand All @@ -33,7 +35,8 @@ export function handleRemoveLiquidityOneToken(event: RemoveLiquidityOneToken): v
event.block.timestamp,
event.block.number,
event.params.tokenOut == BEAN_ERC20 ? event.params.tokenAmountOut : ZERO_BI,
event.params.tokenOut != BEAN_ERC20 ? event.params.tokenAmountOut : ZERO_BI
event.params.tokenOut != BEAN_ERC20 ? event.params.tokenAmountOut : ZERO_BI,
true
);
}

Expand All @@ -42,13 +45,13 @@ export function handleSync(event: Sync): void {

let deltaReserves = deltaBigIntArray(event.params.reserves, pool.reserves);

handleLiquidityChange(event.address.toHexString(), event.block.timestamp, event.block.number, deltaReserves[0], deltaReserves[1]);
handleLiquidityChange(event.address.toHexString(), event.block.timestamp, event.block.number, deltaReserves[0], deltaReserves[1], false);
}

export function handleSwap(event: Swap): void {
handleSwapEvent(
event.address.toHexString(),
event.params.fromToken,
event.params.toToken,
event.params.amountIn,
event.params.amountOut,
event.block.timestamp,
Expand Down Expand Up @@ -76,7 +79,8 @@ function handleLiquidityChange(
timestamp: BigInt,
blockNumber: BigInt,
token0Amount: BigInt,
token1Amount: BigInt
token1Amount: BigInt,
removal: boolean
): void {
// Get Price Details via Price contract
let beanstalkPrice = BeanstalkPrice.bind(BEANSTALK_PRICE);
Expand All @@ -97,8 +101,9 @@ function handleLiquidityChange(

let volumeUSD = ZERO_BD;
let volumeBean = ZERO_BI;
if (token0Amount == ZERO_BI || token1Amount == ZERO_BI) {
volumeUSD = deltaLiquidityUSD;
if ((token0Amount == ZERO_BI || token1Amount == ZERO_BI) && removal) {
// This is a removal and delta liquidity should always be negative.
volumeUSD = deltaLiquidityUSD.times(BigDecimal.fromString("-1")).div(BigDecimal.fromString("2"));
volumeBean = BigInt.fromString(volumeUSD.div(newPrice).times(BigDecimal.fromString("1000000")).truncate(0).toString());
}

Expand Down
Loading