Skip to content

Commit de2afb1

Browse files
committed
fix: improve optimistic reporting in case of loss policy revert
1 parent 13d56ce commit de2afb1

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

src/errors/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from "./ErrorHandler.js";
2-
export * from "./isCreditAccountNotLiquidatableException.js";
2+
export * from "./isRevertedWith.js";
33
export * from "./TransactionRevertedError.js";

src/errors/isCreditAccountNotLiquidatableException.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/errors/isRevertedWith.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { BaseError, ContractFunctionRevertedError, type Hex } from "viem";
2+
3+
// export function isCreditAccountNotLiquidatableException(e: Error): boolean {
4+
// if (!(e instanceof BaseError)) {
5+
// return false;
6+
// }
7+
8+
// const err = e.walk(i => i instanceof ContractFunctionRevertedError);
9+
// if (err instanceof ContractFunctionRevertedError) {
10+
// // CreditAccountNotLiquidatableException()
11+
// return err.raw === "0x234b893b";
12+
// }
13+
// return false;
14+
// }
15+
16+
// export function isCreditAccountNotLiquidatableWithLossExceptionException(
17+
// e: Error,
18+
// ): boolean {
19+
// if (!(e instanceof BaseError)) {
20+
// return false;
21+
// }
22+
23+
// const err = e.walk(i => i instanceof ContractFunctionRevertedError);
24+
// if (err instanceof ContractFunctionRevertedError) {
25+
// // CreditAccountNotLiquidatableWithLossException()
26+
// return err.raw === "0x6b8c2b8c";
27+
// }
28+
// return false;
29+
// }
30+
31+
export function isRevertedWith(e: Error, revertCode: Hex): boolean {
32+
if (!(e instanceof BaseError)) {
33+
return false;
34+
}
35+
36+
const err = e.walk(i => i instanceof ContractFunctionRevertedError);
37+
if (err instanceof ContractFunctionRevertedError) {
38+
// CreditAccountNotLiquidatableWithLossException()
39+
return err.raw === revertCode;
40+
}
41+
return false;
42+
}

src/services/liquidate/LiquidationStrategyFull.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type {
2323
LiqduiatorConfig,
2424
} from "../../config/index.js";
2525
import { DI } from "../../di.js";
26-
import { isCreditAccountNotLiquidatableException } from "../../errors/index.js";
26+
import { isRevertedWith } from "../../errors/index.js";
2727
import { type ILogger, Logger } from "../../log/index.js";
2828
import type Client from "../Client.js";
2929
import AccountHelper from "./AccountHelper.js";
@@ -215,14 +215,23 @@ export default class LiquidationStrategyFull
215215
>;
216216
} catch (e) {
217217
// in optimistic mode, it's possible to encounter accounts with underlying only and HF > 0
218-
if (
219-
this.config.optimistic &&
220-
account.healthFactor > WAD &&
221-
isCreditAccountNotLiquidatableException(e as Error)
222-
) {
223-
throw new Error("warning: credit account is not liquidatable", {
224-
cause: e,
225-
});
218+
if (this.config.optimistic) {
219+
if (
220+
account.healthFactor > WAD &&
221+
isRevertedWith(e as Error, "0x234b893b") // CreditAccountNotLiquidatableException())
222+
) {
223+
throw new Error("warning: credit account is not liquidatable", {
224+
cause: e,
225+
});
226+
} else if (isRevertedWith(e as Error, "0x6b8c2b8c")) {
227+
// CreditAccountNotLiquidatableWithLossException()
228+
throw new Error(
229+
"warning: credit account is not liquidatable with loss",
230+
{
231+
cause: e,
232+
},
233+
);
234+
}
226235
}
227236
throw e;
228237
}

0 commit comments

Comments
 (0)