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

Added totalLiquidationFees collecting #40

Merged
merged 2 commits into from
Jan 29, 2025
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
3 changes: 3 additions & 0 deletions synthetics-stats/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ type PositionFeesInfoWithPeriod @entity {
totalPositionFeeAmount: BigInt!
totalPositionFeeUsd: BigInt!
totalBorrowingFeeUsd: BigInt!

totalLiquidationFeeAmount: BigInt!
totalLiquidationFeeUsd: BigInt!
}

type LiquidityProviderIncentivesStat @entity {
Expand Down
11 changes: 11 additions & 0 deletions synthetics-stats/src/entities/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export function saveSwapFeesInfoWithPeriod(
export function savePositionFeesInfoWithPeriod(
positionFeeAmount: BigInt,
positionFeeAmountForPool: BigInt,
liquidationFeeAmount: BigInt,
borrowingFeeUsd: BigInt,
tokenPrice: BigInt,
timestamp: i32
Expand All @@ -203,18 +204,26 @@ export function savePositionFeesInfoWithPeriod(
let positionFeeUsd = positionFeeAmount.times(tokenPrice);
let positionFeeUsdForPool = positionFeeAmountForPool.times(tokenPrice);

let liquidationFeeUsd = liquidationFeeAmount.times(tokenPrice);

dailyFees.totalBorrowingFeeUsd = dailyFees.totalBorrowingFeeUsd.plus(borrowingFeeUsd);
dailyFees.totalPositionFeeAmount = dailyFees.totalPositionFeeAmount.plus(positionFeeAmount);
dailyFees.totalPositionFeeUsd = dailyFees.totalPositionFeeUsd.plus(positionFeeUsd);
dailyFees.totalPositionFeeAmountForPool = dailyFees.totalPositionFeeAmountForPool.plus(positionFeeAmountForPool);
dailyFees.totalPositionFeeUsdForPool = dailyFees.totalPositionFeeUsdForPool.plus(positionFeeUsdForPool);

dailyFees.totalLiquidationFeeAmount = dailyFees.totalLiquidationFeeAmount.plus(liquidationFeeAmount);
dailyFees.totalLiquidationFeeUsd = dailyFees.totalLiquidationFeeUsd.plus(liquidationFeeUsd);

totalFees.totalBorrowingFeeUsd = totalFees.totalBorrowingFeeUsd.plus(borrowingFeeUsd);
totalFees.totalPositionFeeAmount = totalFees.totalPositionFeeAmount.plus(positionFeeAmount);
totalFees.totalPositionFeeUsd = totalFees.totalPositionFeeUsd.plus(positionFeeUsd);
totalFees.totalPositionFeeAmountForPool = totalFees.totalPositionFeeAmountForPool.plus(positionFeeAmountForPool);
totalFees.totalPositionFeeUsdForPool = totalFees.totalPositionFeeUsdForPool.plus(positionFeeUsdForPool);

totalFees.totalLiquidationFeeAmount = totalFees.totalLiquidationFeeAmount.plus(liquidationFeeAmount);
totalFees.totalLiquidationFeeUsd = totalFees.totalLiquidationFeeUsd.plus(liquidationFeeUsd);

dailyFees.save();
totalFees.save();
}
Expand Down Expand Up @@ -243,6 +252,8 @@ function getOrCreatePositionFeesInfoWithPeriod(id: string, period: string): Posi
feeInfo.totalPositionFeeUsd = ZERO;
feeInfo.totalPositionFeeAmountForPool = ZERO;
feeInfo.totalPositionFeeUsdForPool = ZERO;
feeInfo.totalLiquidationFeeAmount = ZERO;
feeInfo.totalLiquidationFeeUsd = ZERO;
}

return feeInfo as PositionFeesInfoWithPeriod;
Expand Down
17 changes: 14 additions & 3 deletions synthetics-stats/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ export function handleMarketTokenTransfer(event: Transfer): void {
// `from` user redeems or transfers out GM tokens
if (from != ADDRESS_ZERO) {
// LiquidityProviderIncentivesStat *should* be updated before UserMarketInfo
saveLiquidityProviderIncentivesStat(from, marketAddress, "Market", "1w", value.neg(), event.block.timestamp.toI32());
saveLiquidityProviderIncentivesStat(
from,
marketAddress,
"Market",
"1w",
value.neg(),
event.block.timestamp.toI32()
);
saveLiquidityProviderInfo(from, marketAddress, "Market", value.neg());
let transaction = getOrCreateTransaction(event);
saveUserGmTokensBalanceChange(from, marketAddress, value.neg(), transaction, event.logIndex);
Expand Down Expand Up @@ -215,7 +222,10 @@ function handleEventLog1(event: EventLog1, network: string): void {

if (eventName == "GlvCreated") {
// saveMarketInfo(eventData);
log.warning("block number: {} tx hash: {}", [event.block.number.toHexString(), event.transaction.hash.toHexString()]);
log.warning("block number: {} tx hash: {}", [
event.block.number.toHexString(),
event.transaction.hash.toHexString()
]);
let glvToken = eventData.getAddressItem("glvToken");
if (!glvToken) {
// for fuji
Expand Down Expand Up @@ -366,7 +376,6 @@ function handleEventLog1(event: EventLog1, network: string): void {
if (eventName == "PositionFeesInfo") {
let transaction = getOrCreateTransaction(event);
savePositionFeesInfo(eventData, "PositionFeesInfo", transaction);

return;
}

Expand All @@ -375,6 +384,7 @@ function handleEventLog1(event: EventLog1, network: string): void {
let positionFeeAmount = eventData.getUintItem("positionFeeAmount")!;
let positionFeeAmountForPool = eventData.getUintItem("positionFeeAmountForPool")!;
let collateralTokenPriceMin = eventData.getUintItem("collateralTokenPrice.min")!;
let liquidationFeeAmount = eventData.getUintItem("liquidationFeeAmount")!;
let borrowingFeeUsd = eventData.getUintItem("borrowingFeeUsd")!;
let positionFeesInfo = savePositionFeesInfo(eventData, "PositionFeesCollected", transaction);
let poolValue = getMarketPoolValueFromContract(positionFeesInfo.marketAddress, network, transaction);
Expand All @@ -390,6 +400,7 @@ function handleEventLog1(event: EventLog1, network: string): void {
savePositionFeesInfoWithPeriod(
positionFeeAmount,
positionFeeAmountForPool,
liquidationFeeAmount,
borrowingFeeUsd,
collateralTokenPriceMin,
transaction.timestamp
Expand Down