|
| 1 | +import { |
| 2 | + type CreditAccountData, |
| 3 | + type GearboxSDK, |
| 4 | + type ICreditAccountsService, |
| 5 | + isVersionRange, |
| 6 | + VERSION_RANGE_310, |
| 7 | +} from "@gearbox-protocol/sdk"; |
| 8 | +import { iBotListV310Abi } from "@gearbox-protocol/sdk/abi/310/generated"; |
| 9 | +import type { |
| 10 | + BotsPlugin, |
| 11 | + BotParameters as TBotParameters, |
| 12 | +} from "@gearbox-protocol/sdk/plugins/bots"; |
| 13 | +import type { Address } from "viem"; |
| 14 | +import type { Config } from "../config/index.js"; |
| 15 | +import { DI } from "../di.js"; |
| 16 | +import { type ILogger, Logger } from "../log/index.js"; |
| 17 | +import { DELEVERAGE_PERMISSIONS } from "../utils/permissions.js"; |
| 18 | +import type { StatusCode } from "../utils/status.js"; |
| 19 | +import type Client from "./Client.js"; |
| 20 | + |
| 21 | +export interface BotParameters extends TBotParameters { |
| 22 | + address: Address; |
| 23 | +} |
| 24 | + |
| 25 | +export interface DeleverageStatus { |
| 26 | + status: StatusCode; |
| 27 | + bots: Array<{ address: Address; status: StatusCode }>; |
| 28 | +} |
| 29 | + |
| 30 | +@DI.Injectable(DI.Deleverage) |
| 31 | +export default class DeleverageService { |
| 32 | + @Logger("DeleverageService") |
| 33 | + log!: ILogger; |
| 34 | + |
| 35 | + @DI.Inject(DI.Config) |
| 36 | + config!: Config; |
| 37 | + |
| 38 | + @DI.Inject(DI.CreditAccountService) |
| 39 | + caService!: ICreditAccountsService; |
| 40 | + |
| 41 | + @DI.Inject(DI.Client) |
| 42 | + client!: Client; |
| 43 | + |
| 44 | + #bots: BotParameters[] = []; |
| 45 | + |
| 46 | + public async launch(): Promise<void> { |
| 47 | + if (this.config.liquidationMode !== "deleverage") { |
| 48 | + return; |
| 49 | + } |
| 50 | + const bots = |
| 51 | + await this.sdk.plugins.bots.findDeployedPartialLiquidationBots(); |
| 52 | + this.#bots = bots.entries().map(([address, bot]) => ({ |
| 53 | + ...bot, |
| 54 | + address, |
| 55 | + })); |
| 56 | + this.log.info( |
| 57 | + `Found ${this.#bots.length} deployed partial liquidation bots`, |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public get bots(): BotParameters[] { |
| 62 | + return this.#bots; |
| 63 | + } |
| 64 | + |
| 65 | + public get bot(): BotParameters { |
| 66 | + // TODO: support multiple bots |
| 67 | + return this.#bots[0]; |
| 68 | + } |
| 69 | + |
| 70 | + public async filterDeleverageAccounts( |
| 71 | + accounts_: CreditAccountData[], |
| 72 | + blockNumber?: bigint, |
| 73 | + ): Promise<CreditAccountData[]> { |
| 74 | + if (this.config.optimistic) { |
| 75 | + return accounts_; |
| 76 | + } |
| 77 | + // TODO: support multiple bots |
| 78 | + const bot = this.#bots[0]; |
| 79 | + |
| 80 | + const accounts = accounts_.filter(ca => { |
| 81 | + const cm = this.caService.sdk.marketRegister.findCreditManager( |
| 82 | + ca.creditManager, |
| 83 | + ); |
| 84 | + return isVersionRange(cm.creditFacade.version, VERSION_RANGE_310); |
| 85 | + }); |
| 86 | + |
| 87 | + const res = await this.client.pub.multicall({ |
| 88 | + contracts: accounts.map(ca => { |
| 89 | + const cm = this.caService.sdk.marketRegister.findCreditManager( |
| 90 | + ca.creditManager, |
| 91 | + ); |
| 92 | + return { |
| 93 | + address: cm.creditFacade.botList, |
| 94 | + abi: iBotListV310Abi, |
| 95 | + functionName: "getBotStatus", |
| 96 | + args: [bot, ca.creditAccount], |
| 97 | + } as const; |
| 98 | + }), |
| 99 | + allowFailure: true, |
| 100 | + blockNumber, |
| 101 | + }); |
| 102 | + const result: CreditAccountData[] = []; |
| 103 | + let errored = 0; |
| 104 | + for (let i = 0; i < accounts.length; i++) { |
| 105 | + const ca = accounts[i]; |
| 106 | + const r = res[i]; |
| 107 | + if (r.status === "success") { |
| 108 | + const [permissions, forbidden] = r.result; |
| 109 | + if ( |
| 110 | + !!permissions && |
| 111 | + !forbidden && |
| 112 | + (permissions & DELEVERAGE_PERMISSIONS) === DELEVERAGE_PERMISSIONS |
| 113 | + ) { |
| 114 | + result.push(ca); |
| 115 | + } |
| 116 | + } else if (r.status === "failure") { |
| 117 | + errored++; |
| 118 | + } |
| 119 | + } |
| 120 | + this.log.debug( |
| 121 | + { errored, before: accounts_.length, after: result.length }, |
| 122 | + "filtered accounts for deleverage", |
| 123 | + ); |
| 124 | + return result; |
| 125 | + } |
| 126 | + |
| 127 | + private get sdk(): GearboxSDK<{ bots: BotsPlugin }> { |
| 128 | + return this.caService.sdk as GearboxSDK<{ bots: BotsPlugin }>; |
| 129 | + } |
| 130 | + |
| 131 | + public get status(): DeleverageStatus | undefined { |
| 132 | + if (this.config.liquidationMode !== "deleverage") { |
| 133 | + return undefined; |
| 134 | + } |
| 135 | + return { |
| 136 | + status: this.bots.length === 1 ? "healthy" : "alert", |
| 137 | + bots: this.bots.map(b => ({ |
| 138 | + address: b.address, |
| 139 | + status: "healthy", |
| 140 | + })), |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
0 commit comments