Skip to content

Commit

Permalink
fix(repay): negative total borrow
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-devatom committed Feb 3, 2025
1 parent 32b0431 commit daf584e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/meta-morpho.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Address, BigInt, Bytes, log } from "@graphprotocol/graph-ts";

import {
AllocatorSet,
FeeRecipient, MetaMorpho,
FeeRecipient,
MetaMorpho,
MetaMorphoAllocator,
MetaMorphoDeposit,
MetaMorphoMarket,
Expand Down
3 changes: 2 additions & 1 deletion src/morpho-blue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
);
Expand Down
9 changes: 4 additions & 5 deletions src/sdk/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -58,6 +59,7 @@ export class PositionManager {

return Position.load(positionID);
}

addCollateralPosition(
event: ethereum.Event,
amountSupplied: BigInt
Expand Down Expand Up @@ -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())) {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/math.ts
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit daf584e

Please sign in to comment.