Skip to content

Commit

Permalink
fix(blue-sdk-ethers-liquidation): INTEG-822 - merge latest main and c…
Browse files Browse the repository at this point in the history
…lean
  • Loading branch information
0x666c6f committed Oct 7, 2024
1 parent e74f471 commit 76ef36e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
pendle,
swap,
} from "@morpho-org/blue-sdk-ethers-liquidation";
import { isPendlePTToken } from "@morpho-org/blue-sdk-ethers-liquidation/src/tokens/pendle";
import { Time } from "@morpho-org/morpho-ts";

const converter = new BlueSdkConverter({
Expand Down Expand Up @@ -171,81 +170,12 @@ export const check = async (
let dstAmount = 0n;
// Handle Pendle Tokens
// To retrieve the tokens, we need to call the Pendle API to get the swap calldata
if (
isPendlePTToken(
market.config.collateralToken,
chainId,
pendleTokens,
)
) {
const pendleMarketResponse =
await pendle.getPendleMarketForPTToken(
chainId,
market.config.collateralToken,
);
if (pendleMarketResponse.total !== 1) {
throw Error("Invalid Pendle market result");
}
const pendleMarketData = pendleMarketResponse.results[0]!;
const maturity = pendleMarketData.pt.expiry!;

srcAmount = seizedAssets;
srcToken = pendleMarketData.underlyingAsset.address;
if (new Date(maturity) < new Date()) {
// Pendle market is expired, we can directly redeem the collateral
// If called before YT's expiry, both PT & YT of equal amounts are needed and will be burned. Else, only PT is needed and will be burned.
const redeemCallData = await pendle.getPendleRedeemCallData(
chainId,
{
receiver: executorAddress,
slippage: 0.04,
yt: pendleMarketData.yt.address,
amountIn: seizedAssets.toString(),
tokenOut: pendleMarketData.underlyingAsset.address,
enableAggregator: true,
},
);

encoder
.erc20Approve(srcToken, redeemCallData.tx.to, MaxUint256)
.erc20Approve(
market.config.collateralToken,
redeemCallData.tx.to,
MaxUint256,
)
.pushCall(
redeemCallData.tx.to,
redeemCallData.tx.value ? redeemCallData.tx.value : 0n,
redeemCallData.tx.data,
);
} else {
// Pendle market is not expired, we need to swap the collateral token (PT) to the underlying token
const swapCallData = await pendle.getPendleSwapCallData(
chainId,
pendleMarketData.address,
{
receiver: executorAddress,
slippage: 0.04,
tokenIn: market.config.collateralToken,
tokenOut: pendleMarketData.underlyingAsset.address,
amountIn: seizedAssets.toString(),
},
);
encoder
.erc20Approve(srcToken, swapCallData.tx.to, MaxUint256)
.erc20Approve(
market.config.collateralToken,
swapCallData.tx.to,
MaxUint256,
)
.pushCall(
swapCallData.tx.to,
swapCallData.tx.value ? swapCallData.tx.value : 0n,
swapCallData.tx.data,
);
srcAmount = BigInt(swapCallData.data.amountOut);
}
}
({ srcAmount, srcToken } = await encoder.handlePendleTokens(
chainId,
market.config.collateralToken,
seizedAssets,
pendleTokens,
));

switch (true) {
// In case of Usual tokens, there aren't much liquidity outside of curve, so we use it instead of 1inch/paraswap
Expand Down
37 changes: 21 additions & 16 deletions packages/blue-sdk-ethers-liquidation/src/LiquidationEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import { Address, ChainId } from "@morpho-org/blue-sdk";
import {
curvePools,
mainnetAddresses,
pendleTokens,
pendle,
} from "@morpho-org/blue-sdk-ethers-liquidation";
import { CurveStableSwapNG__factory } from "@morpho-org/blue-sdk-ethers-liquidation/src/contracts/curve";
import {
getPendleRedeemCallData,
getPendleSwapCallData,
pendleMarkets,
} from "@morpho-org/blue-sdk-ethers-liquidation/src/tokens/pendle";
import {
USD0_USD0PP_USD0_INDEX,
USD0_USD0PP_USDPP_INDEX,
Expand All @@ -32,33 +27,43 @@ export class LiquidationEncoder extends ExecutorEncoder {
chainId: ChainId,
collatToken: string,
seizedAssets: bigint,
pendleTokens: pendle.PendleTokenListResponse,
): Promise<{ srcAmount: bigint; srcToken: string }> {
if (!pendleTokens[chainId].has(collatToken)) {
if (!pendle.isPendlePTToken(collatToken, chainId, pendleTokens)) {
return {
srcAmount: seizedAssets,
srcToken: collatToken,
};
}

const pendleMarketData = pendleMarkets[chainId][collatToken];
const maturity = pendleMarketData?.maturity;
const pendleMarketResponse = await pendle.getPendleMarketForPTToken(
chainId,
collatToken,
);
if (pendleMarketResponse.total !== 1) {
throw Error("Invalid Pendle market result");
}
const pendleMarketData = pendleMarketResponse.results[0]!;
const maturity = pendleMarketData.pt.expiry!;
if (!maturity) {
throw Error("Pendle market not found");
}

let srcAmount = seizedAssets;
let srcToken = pendleMarketData.underlyingTokenAddress;
let srcToken = pendleMarketData.underlyingAsset.address;

if (maturity < new Date()) {
if (new Date(maturity) < new Date()) {
// Pendle market is expired, we can directly redeem the collateral
const redeemCallData = await getPendleRedeemCallData(chainId, {
// If called before YT's expiry, both PT & YT of equal amounts are needed and will be burned. Else, only PT is needed and will be burned.
const redeemCallData = await pendle.getPendleRedeemCallData(chainId, {
receiver: this.address,
slippage: 0.04,
yt: pendleMarketData.yieldTokenAddress,
yt: pendleMarketData.yt.address,
amountIn: seizedAssets.toString(),
tokenOut: pendleMarketData.underlyingTokenAddress,
tokenOut: pendleMarketData.underlyingAsset.address,
enableAggregator: true,
});

this.erc20Approve(srcToken, redeemCallData.tx.to, MaxUint256)
.erc20Approve(collatToken, redeemCallData.tx.to, MaxUint256)
.pushCall(
Expand All @@ -68,14 +73,14 @@ export class LiquidationEncoder extends ExecutorEncoder {
);
} else {
// Pendle market is not expired, we need to swap the collateral token (PT) to the underlying token
const swapCallData = await getPendleSwapCallData(
const swapCallData = await pendle.getPendleSwapCallData(
chainId,
pendleMarketData.address,
{
receiver: this.address,
slippage: 0.04,
tokenIn: collatToken,
tokenOut: pendleMarketData.underlyingTokenAddress,
tokenOut: pendleMarketData.underlyingAsset.address,
amountIn: seizedAssets.toString(),
},
);
Expand Down
20 changes: 10 additions & 10 deletions packages/blue-sdk-ethers-liquidation/src/tokens/pendle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ export type PendleSwapCallData = {
};
};

interface VersionResponse {
type VersionResponse = {
major: number;
minor: number;
patch: number;
}
};

interface TokenInfoResponse {
type TokenInfoResponse = {
chainId: number;
address: string;
decimals: number;
name: string;
symbol: string;
logoURI: string;
tags: string[];
}
};

interface TagDefinitionResponse {
type TagDefinitionResponse = {
name: string;
description: string;
}
};

interface PendleTokenListResponse {
export type PendleTokenListResponse = {
name: string;
timestamp: string;
version: VersionResponse;
Expand All @@ -95,14 +95,14 @@ interface PendleTokenListResponse {
tags: {
[key: string]: TagDefinitionResponse;
};
}
};

interface PendleMarketData {
export type PendleMarketData = {
total: number;
limit: number;
skip: number;
results: MarketResult[];
}
};

interface MarketResult {
id: string;
Expand Down

0 comments on commit 76ef36e

Please sign in to comment.