diff --git a/src/meta-morpho.ts b/src/meta-morpho.ts index 55b16dc..e7ebc3d 100644 --- a/src/meta-morpho.ts +++ b/src/meta-morpho.ts @@ -2,7 +2,8 @@ import { Address, BigInt, Bytes, log } from "@graphprotocol/graph-ts"; import { AllocatorSet, - FeeRecipient, MetaMorpho, + FeeRecipient, + MetaMorpho, MetaMorphoAllocator, MetaMorphoDeposit, MetaMorphoMarket, diff --git a/src/morpho-blue.ts b/src/morpho-blue.ts index 7c57a16..dfd751a 100644 --- a/src/morpho-blue.ts +++ b/src/morpho-blue.ts @@ -33,6 +33,7 @@ import { import { DataManager } from "./sdk/manager"; import { PositionManager } from "./sdk/position"; import { TokenManager } from "./sdk/token"; +import { zeroFlorSub } from "./utils/math"; export function handleAccrueInterest(event: AccrueInterest): void { const market = getMarket(event.params.id); @@ -237,7 +238,7 @@ export function handleRepay(event: Repay): void { event.params.shares ); - market.totalBorrow = market.totalBorrow.minus(event.params.assets); + market.totalBorrow = zeroFlorSub(market.totalBorrow, event.params.assets); market.totalBorrowShares = market.totalBorrowShares.minus( event.params.shares ); diff --git a/src/sdk/position.ts b/src/sdk/position.ts index dbf1bc2..fd549c0 100644 --- a/src/sdk/position.ts +++ b/src/sdk/position.ts @@ -9,6 +9,7 @@ import { } from "../../generated/schema"; import { getProtocol } from "../initializers/protocol"; import { toAssetsDown, toAssetsUp } from "../maths/shares"; +import { zeroFlorSub } from "../utils/math"; import { exponentToBigDecimal, @@ -58,6 +59,7 @@ export class PositionManager { return Position.load(positionID); } + addCollateralPosition( event: ethereum.Event, amountSupplied: BigInt @@ -273,11 +275,8 @@ export class PositionManager { ); position.balance = totalBorrow; - if (position.principal! > amountRepaid) { - position.principal = position.principal!.minus(amountRepaid); - } else { - position.principal = BigInt.zero(); - } + position.principal = zeroFlorSub(position.principal!, amountRepaid); + position.repayCount += 1; if (position.shares!.equals(BigInt.zero())) { diff --git a/src/utils/math.ts b/src/utils/math.ts new file mode 100644 index 0000000..02d36ec --- /dev/null +++ b/src/utils/math.ts @@ -0,0 +1,8 @@ +import { BigInt } from "@graphprotocol/graph-ts"; + +export const zeroFlorSub = (a: BigInt, b: BigInt): BigInt => { + if (a.lt(b)) { + return BigInt.zero(); + } + return a.minus(b); +};