Skip to content

Commit 457f175

Browse files
committed
feat: use real scanner in optimistic deleverage bot
1 parent 89f974b commit 457f175

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

src/config/deleverage-liquidator.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { zommandRegistry } from "@gearbox-protocol/cli-utils";
2-
import { ZodAddress } from "@gearbox-protocol/sdk";
32
import { z } from "zod/v4";
43
import { CommonSchema } from "./common.js";
54

@@ -13,6 +12,18 @@ export const DeleverageLiquidatorSchema = z.object({
1312
description: "Liquidator mode (full/partial/batch/deleverage)",
1413
env: "LIQUIDATION_MODE",
1514
}),
15+
/**
16+
* In optimistic mode, use real health factor range to scan for accounts
17+
* and do not force-enable deleverage bot on accounts.
18+
*
19+
* This mode can be used to test that deleverage bot is able to detect accounts correctly
20+
*/
21+
useProductionScanner: z.boolean().default(false).register(zommandRegistry, {
22+
flags: "--use-production-scanner",
23+
description:
24+
"In optimistic mode, use real health factor range to scan for accounts and do not force-enable deleverage bot on accounts",
25+
env: "USE_PRODUCTION_SCANNER",
26+
}),
1627
});
1728

1829
export type DeleverageLiquidatorSchema = z.infer<

src/services/DeleverageService.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import type {
1212
BotParameters as TBotParameters,
1313
} from "@gearbox-protocol/sdk/plugins/bots";
1414
import type { Address } from "viem";
15-
import type { Config } from "../config/index.js";
15+
import type {
16+
DeleverageLiquidatorSchema,
17+
LiqduiatorConfig,
18+
} from "../config/index.js";
1619
import { DI } from "../di.js";
1720
import { type ILogger, Logger } from "../log/index.js";
1821
import { DELEVERAGE_PERMISSIONS } from "../utils/permissions.js";
@@ -41,7 +44,7 @@ export default class DeleverageService {
4144
log!: ILogger;
4245

4346
@DI.Inject(DI.Config)
44-
config!: Config;
47+
config!: LiqduiatorConfig<DeleverageLiquidatorSchema>;
4548

4649
@DI.Inject(DI.CreditAccountService)
4750
caService!: ICreditAccountsService;
@@ -63,7 +66,11 @@ export default class DeleverageService {
6366
blockNumber?: bigint,
6467
): Promise<CreditAccountData[]> {
6568
if (this.config.optimistic) {
66-
return accounts_;
69+
if (this.config.useProductionScanner) {
70+
this.log.debug(`checking which accounts have deleverage bot enabled`);
71+
} else {
72+
return accounts_;
73+
}
6774
}
6875

6976
const accounts = accounts_.filter(ca => {

src/services/Scanner.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,32 @@ export class Scanner {
8484
if (this.config.liquidationMode === "deleverage") {
8585
if (this.deleverage.bots.length === 0) {
8686
this.log.error("no deleverage bots found");
87-
// hang indefinitely
87+
// will hang indefinitely
8888
return;
8989
}
90-
// TODO: support multiple bots
91-
if (this.config.optimistic) {
90+
if (this.config.optimistic && !this.config.useProductionScanner) {
9291
this.#minHealthFactor = 0n;
9392
this.#maxHealthFactor = MAX_UINT256;
9493
} else {
94+
// TODO: support multiple bots
9595
this.#minHealthFactor = WAD;
9696
this.#maxHealthFactor =
9797
(BigInt(this.deleverage.bot.minHealthFactor) * WAD) /
9898
PERCENTAGE_FACTOR;
99-
this.log.info(
100-
`deleverage bot max health factor is ${this.deleverage.bot.minHealthFactor / 100}% (${this.#maxHealthFactor})`,
101-
);
10299
}
103100
}
104101

102+
this.log.info(
103+
{
104+
min: this.#minHealthFactor,
105+
max:
106+
this.#maxHealthFactor === MAX_UINT256
107+
? "MAX_UINT256"
108+
: this.#maxHealthFactor,
109+
},
110+
"final health factor range",
111+
);
112+
105113
// we should not pin block during optimistic liquidations
106114
// because during optimistic liquidations we need to call evm_mine to make redstone work
107115
await this.#updateAccounts(
@@ -268,7 +276,6 @@ export class Scanner {
268276
async #getExpiredCreditAccounts(
269277
blockNumber?: bigint,
270278
): Promise<CreditAccountData[]> {
271-
const timestamp = this.caService.sdk.timestamp;
272279
const expiredCMs = new AddressSet();
273280
const expiredCmNames: string[] = [];
274281

src/services/liquidate/LiquidationStrategyDeleverage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export default class LiquidationStrategyDeleverage
4747
throw new Error("warning: deleverage is not supported for v300 accounts");
4848
}
4949
const result = await super.makeLiquidatable(ca);
50+
if (this.config.useProductionScanner) {
51+
this.logger.debug("skipping force-enabling deleverage bot");
52+
return result;
53+
}
5054
const { creditFacade } = this.sdk.marketRegister.findCreditManager(
5155
ca.creditManager,
5256
);

0 commit comments

Comments
 (0)