From 1e99ab6718199e45a49e32ec525fc339c0eca8fd Mon Sep 17 00:00:00 2001 From: bigq Date: Tue, 13 Aug 2024 14:39:09 +0200 Subject: [PATCH] fix: use `zeroFloorPlus` to compute totalBorrowAssets --- src/distribute-market-rewards.ts | 8 ++++++-- src/utils.ts | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/distribute-market-rewards.ts b/src/distribute-market-rewards.ts index 091f5a2..d5e0d3f 100644 --- a/src/distribute-market-rewards.ts +++ b/src/distribute-market-rewards.ts @@ -4,7 +4,7 @@ import { MorphoTx } from "../generated/schema"; import { getMarket, setupPosition } from "./initializers"; import { snapshotMarket, snapshotPosition } from "./snapshots"; -import { EventType } from "./utils"; +import { EventType, zeroFloorPlus } from "./utils"; export function computeMarketPoints(marketId: Bytes, timestamp: BigInt): void { const market = getMarket(marketId); @@ -72,7 +72,11 @@ export function handleMorphoTx(morphoTx: MorphoTx): void { } else if (morphoTx.type === EventType.BORROW) { position.borrowShares = position.borrowShares.plus(morphoTx.shares); market.totalBorrowShares = market.totalBorrowShares.plus(morphoTx.shares); - market.totalBorrowAssets = market.totalBorrowAssets.plus(morphoTx.assets); + // zeroFloorPlus to avoid negative values when the user repays or get liquidated + market.totalBorrowAssets = zeroFloorPlus( + market.totalBorrowAssets, + morphoTx.assets + ); } else if (morphoTx.type === EventType.COLLATERAL) { position.collateral = position.collateral.plus(morphoTx.assets); market.totalCollateral = market.totalCollateral.plus(morphoTx.assets); diff --git a/src/utils.ts b/src/utils.ts index 2f16ff2..da980d8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import { Bytes, ethereum, log } from "@graphprotocol/graph-ts"; +import { Bytes, ethereum, log, BigInt } from "@graphprotocol/graph-ts"; export namespace EventType { export const SUPPLY = "SUPPLY"; @@ -22,3 +22,8 @@ export function generateLogId(event: ethereum.Event): Bytes { return event.transaction.hash.concat(logIndex); } + +export function zeroFloorPlus(a: BigInt, b: BigInt): BigInt { + const result = a.plus(b); + return result.gt(BigInt.zero()) ? result : BigInt.zero(); +}