diff --git a/src/services/liquidate/BatchLiquidator.ts b/src/services/liquidate/BatchLiquidator.ts index 4e58d57..1b91ff9 100644 --- a/src/services/liquidate/BatchLiquidator.ts +++ b/src/services/liquidate/BatchLiquidator.ts @@ -15,7 +15,10 @@ import { } from "../notifier/messages.js"; import AbstractLiquidator from "./AbstractLiquidator.js"; import type { ILiquidatorService } from "./types.js"; -import type { BatchLiquidationResult } from "./viem-types.js"; +import type { + BatchLiquidationResult, + LiquidateBatchInput, +} from "./viem-types.js"; interface BatchLiquidationOutput { readonly receipt: TransactionReceipt; @@ -78,32 +81,47 @@ export default class BatchLiquidator address: this.batchLiquidator, abi: iBatchLiquidatorAbi, functionName: "estimateBatch", - args: [input] as any, // TODO: types + args: [input], }); const batch: Record = Object.fromEntries( result.map(r => [r.creditAccount.toLowerCase(), r]), ); - this.logger.debug(result, "estimated batch"); + const liquidateBatchInput: LiquidateBatchInput[] = []; + for (const r of result) { + if (r.executed) { + const acc = accounts.find( + a => a.addr === r.creditAccount.toLowerCase(), + ); + if (acc) { + liquidateBatchInput.push({ + calls: r.calls, + creditAccount: r.creditAccount, + creditFacade: acc.creditFacade, + }); + } + } + } + this.logger.debug( + { + accounts: accounts.length, + outputSize: result.length, + executed: liquidateBatchInput.length, + }, + "estimated batch", + ); const { request } = await this.client.pub.simulateContract({ account: this.client.account, address: this.batchLiquidator, abi: iBatchLiquidatorAbi, functionName: "liquidateBatch", - args: [ - result - .filter(i => i.executed) - .map(i => ({ - calls: i.calls, - creditAccount: i.creditAccount, - creditFacade: accounts.find( - ca => ca.addr === i.creditAccount.toLowerCase(), - )?.creditFacade!, // TODO: checks - })), - this.client.address, - ], + args: [liquidateBatchInput, this.client.address], }); const receipt = await this.client.liquidate(request as any, this.logger); // TODO: types + this.logger.debug( + { tx: receipt.transactionHash, gasUsed: receipt.gasUsed }, + "liquidated batch", + ); const logs = parseEventLogs({ abi: iCreditFacadeV3Abi, @@ -113,6 +131,7 @@ export default class BatchLiquidator const liquidated = new Set( logs.map(l => l.args.creditAccount.toLowerCase() as Address), ); + this.logger.debug(`emitted ${liquidated.size} liquidation events`); const getError = (a: CreditAccountData): string | undefined => { if (liquidated.has(a.addr)) { return undefined; @@ -121,10 +140,10 @@ export default class BatchLiquidator if (!item) { return "not found in estimateBatch output"; } - if (item.pathFound) { + if (!item.pathFound) { return "batch path not found"; } - if (item.executed) { + if (!item.executed) { return "cannot execute in estimateBatch"; } return "cannot liquidate in batch"; diff --git a/src/services/liquidate/index.ts b/src/services/liquidate/index.ts index 320bab8..a0a7e6b 100644 --- a/src/services/liquidate/index.ts +++ b/src/services/liquidate/index.ts @@ -1,3 +1,4 @@ export * from "./factory.js"; export * from "./OptimisiticResults.js"; export type * from "./types.js"; +export type * from "./viem-types.js"; diff --git a/src/services/liquidate/viem-types.ts b/src/services/liquidate/viem-types.ts index 8dd5cfd..15cc490 100644 --- a/src/services/liquidate/viem-types.ts +++ b/src/services/liquidate/viem-types.ts @@ -32,3 +32,12 @@ export type BatchLiquidationResult = ArrayElementType< >["outputs"]["0"] > >; + +export type LiquidateBatchInput = ArrayElementType< + AbiParameterToPrimitiveType< + ExtractAbiFunction< + typeof iBatchLiquidatorAbi, + "liquidateBatch" + >["inputs"]["0"] + > +>; diff --git a/src/utils/ethers-6-temp/pathfinder/core.ts b/src/utils/ethers-6-temp/pathfinder/core.ts index 6e2fdd3..1ddaca3 100644 --- a/src/utils/ethers-6-temp/pathfinder/core.ts +++ b/src/utils/ethers-6-temp/pathfinder/core.ts @@ -1,8 +1,6 @@ import type { Address } from "viem"; -import type { Balance } from "../../../data/Balance.js"; import type { MultiCall } from "../../../data/MultiCall.js"; -import type { PathOption } from "./pathOptions.js"; export enum SwapOperation { EXACT_INPUT, @@ -24,17 +22,3 @@ export interface PathFinderOpenStrategyResult extends PathFinderResult { export interface PathFinderCloseResult extends PathFinderResult { underlyingBalance: bigint; } - -/** - * RouterLiqParams in contract - */ -export interface EstimateBatchInput { - creditAccount: Address; - expectedBalances: Balance[]; - leftoverBalances: Balance[]; - connectors: Address[]; - slippage: bigint; - pathOptions: PathOption[]; - iterations: bigint; - force: boolean; -} diff --git a/src/utils/ethers-6-temp/pathfinder/pathfinder.ts b/src/utils/ethers-6-temp/pathfinder/pathfinder.ts index 74a68e2..7d1e6f7 100644 --- a/src/utils/ethers-6-temp/pathfinder/pathfinder.ts +++ b/src/utils/ethers-6-temp/pathfinder/pathfinder.ts @@ -8,9 +8,13 @@ import type { CreditAccountData, CreditManagerData, } from "../../../data/index.js"; -import type { EstimateBatchInput, PathFinderCloseResult } from "./core.js"; +import type { PathFinderCloseResult } from "./core.js"; import { PathOptionFactory } from "./pathOptions.js"; -import type { IRouterV3Contract, RouterResult } from "./viem-types.js"; +import type { + EstimateBatchInput, + IRouterV3Contract, + RouterResult, +} from "./viem-types.js"; const MAX_GAS_PER_ROUTE = 200_000_000n; const GAS_PER_BLOCK = 400_000_000n; diff --git a/src/utils/ethers-6-temp/pathfinder/viem-types.ts b/src/utils/ethers-6-temp/pathfinder/viem-types.ts index 623b13d..8e8956f 100644 --- a/src/utils/ethers-6-temp/pathfinder/viem-types.ts +++ b/src/utils/ethers-6-temp/pathfinder/viem-types.ts @@ -1,7 +1,10 @@ +import type { iBatchLiquidatorAbi } from "@gearbox-protocol/liquidator-v2-contracts/abi"; import type { iRouterV3Abi } from "@gearbox-protocol/types/abi"; import type { AbiParameterToPrimitiveType, ExtractAbiFunction } from "abitype"; import type { GetContractReturnType, PublicClient } from "viem"; +import type { ArrayElementType } from "../../index.js"; + export type IRouterV3Contract = GetContractReturnType< typeof iRouterV3Abi, PublicClient @@ -10,3 +13,12 @@ export type IRouterV3Contract = GetContractReturnType< export type RouterResult = AbiParameterToPrimitiveType< ExtractAbiFunction["outputs"]["0"] >; + +export type EstimateBatchInput = ArrayElementType< + AbiParameterToPrimitiveType< + ExtractAbiFunction< + typeof iBatchLiquidatorAbi, + "estimateBatch" + >["inputs"]["0"] + > +>;