Skip to content

Commit

Permalink
fix: apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Grimal committed Feb 11, 2025
1 parent 4b860e8 commit 70647dd
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 95 deletions.
4 changes: 2 additions & 2 deletions packages/liquidation-sdk-viem/examples/whitelistedMarkets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
LiquidationEncoder,
Pendle,
apiSdk,
getPositions,
getRepayDataPreLiquidation,
mainnetAddresses,
} from "@morpho-org/liquidation-sdk-viem";
import {
Expand All @@ -37,8 +39,6 @@ import {
readContract,
sendTransaction,
} from "viem/actions";
import { getPositions } from "../src/positions/getters";
import { getRepayDataPreLiquidation } from "../src/preLiquidation/helpers";

const converter = new BlueSdkConverter({
parseAddress: safeGetAddress,
Expand Down
2 changes: 2 additions & 0 deletions packages/liquidation-sdk-viem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export * from "./api/index.js";
export * from "./addresses.js";
export * from "./flashbots.js";
export * from "./LiquidationEncoder.js";
export * from "./preLiquidation/index.js";
export * from "./swap/index.js";
export * from "./tokens/index.js";
export * from "./abis.js";
export * from "./positions/index.js";
6 changes: 4 additions & 2 deletions packages/liquidation-sdk-viem/src/positions/getters.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { ChainId, MarketId } from "@morpho-org/blue-sdk";
import { fetchAccrualPosition } from "@morpho-org/blue-sdk-viem";
import {
PreLiquidationPosition,
getPreLiquidablePositions,
} from "@morpho-org/liquidation-sdk-viem";
import { Time } from "@morpho-org/morpho-ts";
import type { Account, Chain, Client, Transport } from "viem";
import { apiSdk } from "../api";
import { getPreLiquidablePositions } from "../preLiquidation/positionGetters";
import { PreLiquidationPosition } from "../preLiquidation/types";

export async function getPositions(
client: Client<Transport, Chain, Account>,
Expand Down
1 change: 1 addition & 0 deletions packages/liquidation-sdk-viem/src/positions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./getters.js";
53 changes: 1 addition & 52 deletions packages/liquidation-sdk-viem/src/preLiquidation/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,8 @@ import {
ORACLE_PRICE_SCALE,
SharesMath,
} from "@morpho-org/blue-sdk";
import { parseEther } from "viem";
import type { PreLiquidation } from "./types";

export function getPreSeizableCollateral(
position: AccrualPosition,
preLiquidation: PreLiquidation,
) {
const preLiquidationParams = preLiquidation.preLiquidationParams;
const lltv = position.market.params.lltv;
const preLltv = preLiquidationParams.preLltv;
if (
position.borrowAssets > MathLib.wMulDown(position.collateralValue!, lltv) ||
position.borrowAssets < MathLib.wMulDown(position.collateralValue!, preLltv)
)
return undefined;

const ltv = MathLib.wDivUp(position.borrowAssets, position.collateralValue!);
const quotient = MathLib.wDivDown(ltv - preLltv, lltv - preLltv);
const preLIF =
preLiquidationParams.preLIF1 +
MathLib.wMulDown(
quotient,
preLiquidationParams.preLIF2 - preLiquidationParams.preLIF1,
);
const preLCF =
preLiquidationParams.preLCF1 +
MathLib.wMulDown(
quotient,
preLiquidationParams.preLCF2 - preLiquidationParams.preLCF1,
);

const repayableShares = MathLib.mulDivDown(
position.borrowShares,
preLCF,
parseEther("1.01"), // adding a 1% security to not repay too much
);

const repayableAssets = MathLib.wMulDown(
SharesMath.toAssets(
repayableShares,
position.market.totalBorrowAssets,
position.market.totalBorrowShares,
"Down",
),
preLIF,
);

return MathLib.mulDivDown(
repayableAssets,
ORACLE_PRICE_SCALE,
position.market.price!,
);
}
import type { PreLiquidation } from "./types";

export function getRepayDataPreLiquidation(
position: AccrualPosition,
Expand Down
3 changes: 3 additions & 0 deletions packages/liquidation-sdk-viem/src/preLiquidation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./helpers.js";
export * from "./types.js";
export * from "./positionGetters.js";
60 changes: 54 additions & 6 deletions packages/liquidation-sdk-viem/src/preLiquidation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import {
AccrualPosition,
type Address,
type MarketId,
MathLib,
ORACLE_PRICE_SCALE,
SharesMath,
} from "@morpho-org/blue-sdk";
import { getPreSeizableCollateral } from "./helpers";
import { parseEther } from "viem";

export class PreLiquidationPosition extends AccrualPosition {
public collateralAsset: PartialApiToken;
public loanAsset: PartialApiToken;

public preLiquidation?: PreLiquidation;

public preSeizableCollateral?: bigint;

constructor(
position: AccrualPosition,
collateralAsset: PartialApiToken,
Expand All @@ -25,9 +26,56 @@ export class PreLiquidationPosition extends AccrualPosition {
this.collateralAsset = collateralAsset;
this.loanAsset = loanAsset;
this.preLiquidation = preLiquidation;
this.preSeizableCollateral = preLiquidation
? getPreSeizableCollateral(position, preLiquidation)
: undefined;
}

get preSeizableCollateral() {
if (this.preLiquidation == null) return undefined;

const preLiquidationParams = this.preLiquidation.preLiquidationParams;
const lltv = this.market.params.lltv;
const preLltv = preLiquidationParams.preLltv;
if (
this.borrowAssets > MathLib.wMulDown(this.collateralValue!, lltv) ||
this.borrowAssets < MathLib.wMulDown(this.collateralValue!, preLltv)
)
return undefined;

const ltv = MathLib.wDivUp(this.borrowAssets, this.collateralValue!);
const quotient = MathLib.wDivDown(ltv - preLltv, lltv - preLltv);
const preLIF =
preLiquidationParams.preLIF1 +
MathLib.wMulDown(
quotient,
preLiquidationParams.preLIF2 - preLiquidationParams.preLIF1,
);
const preLCF =
preLiquidationParams.preLCF1 +
MathLib.wMulDown(
quotient,
preLiquidationParams.preLCF2 - preLiquidationParams.preLCF1,
);

const repayableShares = MathLib.mulDivDown(
this.borrowShares,
preLCF,
parseEther("1.01"), // adding a 1% security to not repay too much
);

const repayableAssets = MathLib.wMulDown(
SharesMath.toAssets(
repayableShares,
this.market.totalBorrowAssets,
this.market.totalBorrowShares,
"Down",
),
preLIF,
);

return MathLib.mulDivDown(
repayableAssets,
ORACLE_PRICE_SCALE,
this.market.price!,
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { PreLiquidationPosition } from "../../src/preLiquidation/types.js";
import * as swapMock from "../contracts/SwapMock.js";
import pendleMarketData from "../pendleMockData/pendleMarketData.json";
import pendleTokens from "../pendleMockData/pendleTokens.json";
import { type LiquidationTestContext, test } from "../preLiquidationSetup.js";
import { type LiquidationTestContext, preLiquidationTest } from "../setup.js";

interface SwapAmountConfig {
srcAmount: bigint;
Expand Down Expand Up @@ -269,7 +269,7 @@ describe("pre liquidation", () => {
};

// Cannot run concurrently because `fetch` is mocked globally.
test.sequential(
preLiquidationTest.sequential(
`should pre-liquidate on standard market`,
async ({ client, encoder }) => {
const collateralPriceUsd = 63_300;
Expand Down
31 changes: 0 additions & 31 deletions packages/liquidation-sdk-viem/test/preLiquidationSetup.ts

This file was deleted.

15 changes: 15 additions & 0 deletions packages/liquidation-sdk-viem/test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ export const test = createViemTest(mainnet, {
await use(new LiquidationEncoder(receipt.contractAddress, client));
},
});

export const preLiquidationTest = createViemTest(mainnet, {
forkUrl: process.env.MAINNET_RPC_URL,
forkBlockNumber: 21_429_913,
}).extend<LiquidationEncoderTestContext<typeof mainnet>>({
encoder: async ({ client }, use) => {
const receipt = await client.deployContractWait({
abi: executorAbi,
bytecode,
args: [client.account.address],
});

await use(new LiquidationEncoder(receipt.contractAddress, client));
},
});

0 comments on commit 70647dd

Please sign in to comment.