Skip to content

Commit

Permalink
fix: improve logging and types
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Jul 15, 2024
1 parent ca0894f commit ce3c397
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 35 deletions.
53 changes: 36 additions & 17 deletions src/services/liquidate/BatchLiquidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Address, BatchLiquidationResult> = 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,
Expand All @@ -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;
Expand All @@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/services/liquidate/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./factory.js";
export * from "./OptimisiticResults.js";
export type * from "./types.js";
export type * from "./viem-types.js";
9 changes: 9 additions & 0 deletions src/services/liquidate/viem-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ export type BatchLiquidationResult = ArrayElementType<
>["outputs"]["0"]
>
>;

export type LiquidateBatchInput = ArrayElementType<
AbiParameterToPrimitiveType<
ExtractAbiFunction<
typeof iBatchLiquidatorAbi,
"liquidateBatch"
>["inputs"]["0"]
>
>;
16 changes: 0 additions & 16 deletions src/utils/ethers-6-temp/pathfinder/core.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
}
8 changes: 6 additions & 2 deletions src/utils/ethers-6-temp/pathfinder/pathfinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/utils/ethers-6-temp/pathfinder/viem-types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,3 +13,12 @@ export type IRouterV3Contract = GetContractReturnType<
export type RouterResult = AbiParameterToPrimitiveType<
ExtractAbiFunction<typeof iRouterV3Abi, "findBestClosePath">["outputs"]["0"]
>;

export type EstimateBatchInput = ArrayElementType<
AbiParameterToPrimitiveType<
ExtractAbiFunction<
typeof iBatchLiquidatorAbi,
"estimateBatch"
>["inputs"]["0"]
>
>;

0 comments on commit ce3c397

Please sign in to comment.