Skip to content

Commit

Permalink
fix: add pendle fix to findOneTokenPath
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Dec 26, 2024
1 parent 1e4846f commit 9b81693
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
22
119 changes: 119 additions & 0 deletions src/sdk/abi/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21415,3 +21415,122 @@ export const yearnWithdrawerAbi = [
name: "UnknownToken",
},
] as const;

export const iSwapperAbi = [
{
type: "function",
name: "getBestDirectPairSwap",
inputs: [
{
name: "swapTask",
type: "tuple",
internalType: "struct SwapTask",
components: [
{
name: "swapOperation",
type: "uint8",
internalType: "enum SwapOperation",
},
{
name: "creditAccount",
type: "address",
internalType: "address",
},
{
name: "tokenIn",
type: "address",
internalType: "address",
},
{
name: "tokenOut",
type: "address",
internalType: "address",
},
{
name: "connectors",
type: "address[]",
internalType: "address[]",
},
{
name: "amount",
type: "uint256",
internalType: "uint256",
},
{
name: "leftoverAmount",
type: "uint256",
internalType: "uint256",
},
],
},
{
name: "adapter",
type: "address",
internalType: "address",
},
],
outputs: [
{
name: "quote",
type: "tuple",
internalType: "struct SwapQuote",
components: [
{
name: "multiCall",
type: "tuple",
internalType: "struct MultiCall",
components: [
{
name: "target",
type: "address",
internalType: "address",
},
{
name: "callData",
type: "bytes",
internalType: "bytes",
},
],
},
{
name: "amount",
type: "uint256",
internalType: "uint256",
},
{
name: "found",
type: "bool",
internalType: "bool",
},
],
},
],
stateMutability: "nonpayable",
},
{
type: "function",
name: "getComponentId",
inputs: [],
outputs: [
{
name: "",
type: "uint8",
internalType: "uint8",
},
],
stateMutability: "view",
},
{
type: "function",
name: "version",
inputs: [],
outputs: [
{
name: "",
type: "uint256",
internalType: "uint256",
},
],
stateMutability: "view",
},
] as const;
136 changes: 114 additions & 22 deletions src/sdk/router/RouterV3Contract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Address } from "viem";
import { type Address, encodeFunctionData, getContract } from "viem";

import { routerV3Abi } from "../abi";
import { iCreditFacadeV3MulticallAbi, iSwapperAbi, routerV3Abi } from "../abi";
import { BaseContract } from "../base";
import type { CreditAccountData } from "../base/types";
import { PERCENTAGE_FACTOR } from "../constants";
import type { GearboxSDK } from "../GearboxSDK";
import { getConnectors } from "../sdk-gov-legacy";
import { contractsByNetwork, getConnectors } from "../sdk-gov-legacy";
import { AddressMap } from "../utils";
import type { IHooks } from "../utils/internal";
import { Hooks } from "../utils/internal";
Expand Down Expand Up @@ -94,6 +95,7 @@ export type RouterHooks = {
*/
interface CreditManagerSlice {
address: Address;
creditFacade: Address;
collateralTokens: Array<Address>;
}

Expand All @@ -108,6 +110,17 @@ export type CreditAccountDataSlice = Pick<
| "creditManager"
>;

const PT_IN = {
["0xEe9085fC268F6727d5D4293dBABccF901ffDCC29".toLowerCase()]:
"PT_sUSDe_26DEC2024",
["0xE00bd3Df25fb187d6ABBB620b3dfd19839947b81".toLowerCase()]:
"PT_sUSDe_27MAR20251",
};

const OUT = {
["0x9D39A5DE30e57443BfF2A8307A4256c8797A3497".toLowerCase()]: "sUSDe",
};

export class RouterV3Contract
extends BaseContract<abi>
implements IHooks<RouterHooks>
Expand Down Expand Up @@ -195,34 +208,113 @@ export class RouterV3Contract
* @param slippage
* @returns
*/
public async findOneTokenPath({
creditAccount: ca,
creditManager: cm,
async findOneTokenPath(props: FindOneTokenPathProps): Promise<RouterResult> {
const {
creditAccount,
creditManager,
tokenIn,
tokenOut,
amount,
slippage,
} = props;

const connectors = this.getAvailableConnectors(
creditManager.collateralTokens,
);

const isPTOverrideRedeem =
PT_IN[tokenIn.toLowerCase()] && OUT[tokenOut.toLowerCase()];

const { result } = await (isPTOverrideRedeem
? this.overridePTRedeem(props)
: this.contract.simulate.findOneTokenPath(
[
tokenIn,
amount,
tokenOut,
creditAccount.creditAccount,
connectors,
BigInt(slippage),
],
{
gas: GAS_PER_BLOCK,
},
));

return {
amount: result.amount,
minAmount: result.minAmount,
calls: [...result.calls],
};
}

async overridePTRedeem({
creditAccount,
creditManager,
tokenIn,
tokenOut,
amount,
slippage,
}: FindOneTokenPathProps): Promise<RouterResult> {
const connectors = this.getAvailableConnectors(cm.collateralTokens);
}: FindOneTokenPathProps) {
const pendleSwapperAddress = await this.contract.read.componentAddressById([
37,
]);
const cm = this.sdk.marketRegister.findCreditManager(creditManager.address);
const pendleRouter =
contractsByNetwork[this.sdk.provider.networkType].PENDLE_ROUTER;
const pendleAdapter = cm.creditManager.adapters.mustGet(pendleRouter);

const pendleSwapper = getContract({
address: pendleSwapperAddress,
abi: iSwapperAbi,
client: this.sdk.provider.publicClient,
});

const { result } = await this.contract.simulate.findOneTokenPath(
[
tokenIn,
amount,
tokenOut,
ca.creditAccount as Address,
connectors,
BigInt(slippage),
],
const result = await pendleSwapper.simulate.getBestDirectPairSwap([
{
gas: GAS_PER_BLOCK,
swapOperation: 1,
creditAccount: creditAccount.creditAccount,
tokenIn: tokenIn,
tokenOut: tokenOut,
connectors: [],
amount,
leftoverAmount: 0n,
},
);
pendleAdapter.address,
]);

const minAmount =
(result.result.amount * (PERCENTAGE_FACTOR - BigInt(slippage))) /
PERCENTAGE_FACTOR;

const storeExpectedBalances = {
target: creditManager.creditFacade,
callData: encodeFunctionData({
abi: iCreditFacadeV3MulticallAbi,
functionName: "storeExpectedBalances",
args: [[{ token: tokenOut, amount: minAmount }]],
}),
};

const compareBalances = {
target: creditManager.creditFacade,
callData: encodeFunctionData({
abi: iCreditFacadeV3MulticallAbi,
functionName: "compareBalances",
args: [],
}),
};

return {
amount: result.amount,
minAmount: result.minAmount,
calls: [...result.calls],
result: {
amount: result.result.amount,
minAmount,
calls: [
storeExpectedBalances,
result.result.multiCall,
compareBalances,
],
},
};
}

Expand Down

0 comments on commit 9b81693

Please sign in to comment.