Skip to content

Commit 8758701

Browse files
committed
fix: re-reading account after making it liquidatable
1 parent eb6085d commit 8758701

File tree

4 files changed

+17
-28
lines changed

4 files changed

+17
-28
lines changed

src/services/liquidate/LiquidationStrategyFull.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class LiquidationStrategyFull
6666
ca: CreditAccountData,
6767
): Promise<MakeLiquidatableResult> {
6868
if (!this.#applyLossPolicy) {
69-
return {};
69+
return { account: ca };
7070
}
7171
const { totalValue, debt, accruedInterest } = ca;
7272
if (!this.checkAccountVersion(ca, VERSION_RANGE_310)) {
@@ -81,7 +81,7 @@ export default class LiquidationStrategyFull
8181
(totalValue * discount) / PERCENTAGE_FACTOR - accruedInterest - debt;
8282
if (increaseBy < 0n) {
8383
// already has bad debt, nothing to do
84-
return {};
84+
return { account: ca };
8585
}
8686
increaseBy = (105n * increaseBy) / 100n;
8787
const newDebt = debt + increaseBy;
@@ -96,14 +96,15 @@ export default class LiquidationStrategyFull
9696
const snapshotId = await this.client.anvil.snapshot();
9797

9898
await this.#setDebt(ca, increaseBy, newDebt);
99-
const updCa = await this.creditAccountService.getCreditAccountData(
99+
const account = await this.creditAccountService.getCreditAccountData(
100100
ca.creditAccount,
101101
);
102-
if (!updCa || !this.hasBadDebt(updCa)) {
102+
if (!account || !this.hasBadDebt(account)) {
103103
throw new Error("could not induce bad debt");
104104
}
105105

106106
return {
107+
account,
107108
snapshotId,
108109
};
109110
}
@@ -155,20 +156,7 @@ export default class LiquidationStrategyFull
155156
return true;
156157
}
157158

158-
public async preview(
159-
ca_: CreditAccountData,
160-
): Promise<FullLiquidationPreview> {
161-
let ca = ca_;
162-
if (this.config.optimistic && this.#applyLossPolicy) {
163-
const upd = await this.creditAccountService.getCreditAccountData(
164-
ca.creditAccount,
165-
);
166-
ca = upd ?? ca;
167-
this.logger.debug(
168-
{ hf: ca.healthFactor, badDebt: this.hasBadDebt(ca) },
169-
`re-read credit account data`,
170-
);
171-
}
159+
public async preview(ca: CreditAccountData): Promise<FullLiquidationPreview> {
172160
if (this.#applyLossPolicy && !this.hasBadDebt(ca)) {
173161
throw new Error("cannot apply loss policy: account has no bad debt");
174162
}

src/services/liquidate/LiquidationStrategyPartial.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import {
44
type ICreditAccountsService,
55
VERSION_RANGE_310,
66
} from "@gearbox-protocol/sdk";
7-
import {
8-
calcLiquidatableLTs,
9-
extendAnvilClient,
10-
setLTs,
11-
} from "@gearbox-protocol/sdk/dev";
7+
import { calcLiquidatableLTs, setLTs } from "@gearbox-protocol/sdk/dev";
128
import type { Address, SimulateContractReturnType } from "viem";
139
import type {
1410
DeleverageLiquidatorSchema,
@@ -96,20 +92,21 @@ export default class LiquidationStrategyPartial
9692
const snapshotId = await this.client.anvil.snapshot();
9793

9894
await setLTs(this.client.anvil, cm.state, newLTs, this.logger);
99-
const updCa = await this.creditAccountService.getCreditAccountData(
95+
const account = await this.creditAccountService.getCreditAccountData(
10096
ca.creditAccount,
10197
);
102-
if (!updCa) {
98+
if (!account) {
10399
throw new Error(`cannot find credit account ${ca.creditAccount}`);
104100
}
105101
this.logger.debug({
106-
hfNew: updCa.healthFactor.toString(),
102+
hfNew: account.healthFactor.toString(),
107103
hfOld: ca.healthFactor.toString(),
108104
});
109105
return {
106+
account,
110107
snapshotId,
111108
partialLiquidationCondition: {
112-
hfNew: updCa.healthFactor,
109+
hfNew: account.healthFactor,
113110
ltChanges: Object.fromEntries(
114111
Object.entries(newLTs).map(([t, newLT]) => [
115112
t,

src/services/liquidate/SingularLiquidator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export default class SingularLiquidator
215215

216216
for (const s of this.#strategies) {
217217
strategyResult = await this.#liquidateOneOptimisticStrategy(
218-
acc,
218+
ml.account,
219219
s,
220220
ml.snapshotId,
221221
);

src/services/liquidate/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export interface ILiquidatorService {
6161
}
6262

6363
export interface MakeLiquidatableResult {
64+
/**
65+
* Re-read after making account liquidatable
66+
*/
67+
account: CreditAccountData;
6468
snapshotId?: Hex;
6569
partialLiquidationCondition?: PartialLiquidationCondition<bigint>;
6670
}

0 commit comments

Comments
 (0)