|
| 1 | +import { |
| 2 | + AddressSet, |
| 3 | + type CreditAccountData, |
| 4 | + type GearboxSDK, |
| 5 | + type ICreditAccountsService, |
| 6 | + PERCENTAGE_FACTOR, |
| 7 | +} from "@gearbox-protocol/sdk"; |
| 8 | +import { Create2Deployer } from "@gearbox-protocol/sdk/dev"; |
| 9 | +import type { Address } from "@gearbox-protocol/types/optimist"; |
| 10 | +import type { Hex, SimulateContractReturnType } from "viem"; |
| 11 | +import { encodeFunctionData } from "viem"; |
| 12 | +import type { |
| 13 | + FullLiquidatorSchema, |
| 14 | + LiqduiatorConfig, |
| 15 | +} from "../../config/index.js"; |
| 16 | +import { DI } from "../../di.js"; |
| 17 | +import { errorAbis } from "../../errors/index.js"; |
| 18 | +import { type ILogger, Logger } from "../../log/index.js"; |
| 19 | +import type Client from "../Client.js"; |
| 20 | +import AccountHelper from "./AccountHelper.js"; |
| 21 | +import LlamaThenaLiquidatorJson from "./legacy/LlamaThenaLiquidator.json" with { |
| 22 | + type: "json", |
| 23 | +}; |
| 24 | +import { AAVE_V3_LENDING_POOL } from "./partial/constants.js"; |
| 25 | +import type { |
| 26 | + ILiquidationStrategy, |
| 27 | + LiquidationPreview, |
| 28 | + MakeLiquidatableResult, |
| 29 | +} from "./types.js"; |
| 30 | + |
| 31 | +interface LlamaThenaLiquidationPreview extends LiquidationPreview { |
| 32 | + underlyingAmount: bigint; |
| 33 | + minUnderlyingBack: bigint; |
| 34 | +} |
| 35 | + |
| 36 | +const LLAMATHENA_TOKENS_MAINNET = new AddressSet([ |
| 37 | + "0x72eD19788Bce2971A5ed6401662230ee57e254B7", // stkcvxllamathena |
| 38 | + "0x237926E55f9deee89833a42dEb92d3a6970850B4", // cvxllamathena |
| 39 | + "0xd29f8980852c2c76fC3f6E96a7Aa06E0BedCC1B1", // llamathena |
| 40 | + "0x9D39A5DE30e57443BfF2A8307A4256c8797A3497", // sUSDe |
| 41 | +]); |
| 42 | + |
| 43 | +export default class LiquidationStrategyLlamaThena |
| 44 | + extends AccountHelper |
| 45 | + implements ILiquidationStrategy<LlamaThenaLiquidationPreview> |
| 46 | +{ |
| 47 | + @DI.Inject(DI.CreditAccountService) |
| 48 | + creditAccountService!: ICreditAccountsService; |
| 49 | + |
| 50 | + @DI.Inject(DI.Config) |
| 51 | + config!: LiqduiatorConfig<FullLiquidatorSchema>; |
| 52 | + |
| 53 | + @DI.Inject(DI.Client) |
| 54 | + client!: Client; |
| 55 | + |
| 56 | + @Logger("LlamaThenaStrategy") |
| 57 | + logger!: ILogger; |
| 58 | + |
| 59 | + public readonly name = "llamathena"; |
| 60 | + |
| 61 | + #liquidator: Address | undefined; |
| 62 | + |
| 63 | + public async launch(): Promise<void> { |
| 64 | + const deployer = new Create2Deployer(this.sdk, this.client.wallet); |
| 65 | + const { address } = await deployer.ensureExists({ |
| 66 | + abi: LlamaThenaLiquidatorJson.abi, |
| 67 | + bytecode: LlamaThenaLiquidatorJson.bytecode.object as Hex, |
| 68 | + args: [this.owner, AAVE_V3_LENDING_POOL.Mainnet], |
| 69 | + }); |
| 70 | + this.#liquidator = address; |
| 71 | + this.logger.info(`LlamaThena legacy liquidator deployed at ${address}`); |
| 72 | + } |
| 73 | + |
| 74 | + public async syncState(_blockNumber: bigint): Promise<void> {} |
| 75 | + |
| 76 | + public isApplicable(ca: CreditAccountData): boolean { |
| 77 | + if ( |
| 78 | + this.config.network !== "Mainnet" || |
| 79 | + !this.config.llamathenaWorkaround |
| 80 | + ) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + for (const { token, balance, mask } of ca.tokens) { |
| 84 | + const isEnabled = (mask & ca.enabledTokensMask) !== 0n; |
| 85 | + if (isEnabled && balance > 1n && LLAMATHENA_TOKENS_MAINNET.has(token)) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + } |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + public async makeLiquidatable( |
| 93 | + ca: CreditAccountData, |
| 94 | + ): Promise<MakeLiquidatableResult> { |
| 95 | + return { account: ca }; |
| 96 | + } |
| 97 | + |
| 98 | + public async preview( |
| 99 | + ca: CreditAccountData, |
| 100 | + ): Promise<LlamaThenaLiquidationPreview> { |
| 101 | + const market = this.sdk.marketRegister.findByCreditManager( |
| 102 | + ca.creditManager, |
| 103 | + ); |
| 104 | + const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager); |
| 105 | + const underlying = cm.underlying; |
| 106 | + |
| 107 | + const underlyingValue = market.priceOracle.convertFromUSD( |
| 108 | + underlying, |
| 109 | + ca.totalValueUSD, |
| 110 | + ); |
| 111 | + |
| 112 | + // underlyingAmount = underlyingValue * 9800 / PERCENTAGE_FACTOR (98%) |
| 113 | + const underlyingAmount = (underlyingValue * 9800n) / PERCENTAGE_FACTOR; |
| 114 | + // minUnderlyingBack = underlyingValue * 995 / 1000 (99.5%) |
| 115 | + const minUnderlyingBack = (underlyingValue * 995n) / 1000n; |
| 116 | + |
| 117 | + const callData = encodeFunctionData({ |
| 118 | + abi: LlamaThenaLiquidatorJson.abi, |
| 119 | + functionName: "liquidateCreditAccount", |
| 120 | + args: [ |
| 121 | + ca.creditManager, |
| 122 | + ca.creditAccount, |
| 123 | + underlyingAmount, |
| 124 | + minUnderlyingBack, |
| 125 | + ], |
| 126 | + }); |
| 127 | + |
| 128 | + return { |
| 129 | + calls: [{ target: this.liquidator, callData }], |
| 130 | + underlyingBalance: 0n, |
| 131 | + underlyingAmount, |
| 132 | + minUnderlyingBack, |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + public async simulate( |
| 137 | + account: CreditAccountData, |
| 138 | + preview: LlamaThenaLiquidationPreview, |
| 139 | + ): Promise<SimulateContractReturnType<unknown[], any, any>> { |
| 140 | + const result = await this.client.pub.simulateContract({ |
| 141 | + account: this.client.account, |
| 142 | + abi: [...LlamaThenaLiquidatorJson.abi, ...errorAbis], |
| 143 | + address: this.liquidator, |
| 144 | + functionName: "liquidateCreditAccount", |
| 145 | + args: [ |
| 146 | + account.creditManager, |
| 147 | + account.creditAccount, |
| 148 | + preview.underlyingAmount, |
| 149 | + preview.minUnderlyingBack, |
| 150 | + ], |
| 151 | + }); |
| 152 | + return result as unknown as SimulateContractReturnType<unknown[], any, any>; |
| 153 | + } |
| 154 | + |
| 155 | + private get liquidator(): Address { |
| 156 | + if (!this.#liquidator) { |
| 157 | + throw new Error("LlamaThena liquidator not deployed"); |
| 158 | + } |
| 159 | + return this.#liquidator; |
| 160 | + } |
| 161 | + |
| 162 | + protected get sdk(): GearboxSDK { |
| 163 | + return this.creditAccountService.sdk; |
| 164 | + } |
| 165 | + |
| 166 | + protected get owner(): Address { |
| 167 | + return this.client.wallet.account.address; |
| 168 | + } |
| 169 | +} |
0 commit comments