Skip to content

Commit

Permalink
fix: use zeroFloorPlus to compute totalBorrowAssets
Browse files Browse the repository at this point in the history
  • Loading branch information
yum0e committed Aug 13, 2024
1 parent eb3fa02 commit 1e99ab6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/distribute-market-rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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();
}

0 comments on commit 1e99ab6

Please sign in to comment.