Skip to content

Commit 7c3a67b

Browse files
committed
fix: correctly calculate premium/profit
1 parent 00bf330 commit 7c3a67b

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/services/liquidate/SingularLiquidator.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import {
1717
LiquidationStartMessage,
1818
LiquidationSuccessMessage,
1919
} from "../notifier/index.js";
20-
import AbstractLiquidator from "./AbstractLiquidator.js";
20+
import AbstractLiquidator, {
21+
type ExecutorBalance,
22+
} from "./AbstractLiquidator.js";
2123
import LiquidationStrategyFull from "./LiquidationStrategyFull.js";
2224
import LiquidationStrategyPartial from "./LiquidationStrategyPartial.js";
2325
import type {
@@ -30,7 +32,7 @@ type OptimisticStrategyResult = {
3032
preview?: LiquidationPreview;
3133
receipt?: TransactionReceipt;
3234
} & (
33-
| { success: true; state: CreditAccountData }
35+
| { success: true; state: CreditAccountData; balancesAfter: ExecutorBalance }
3436
| { success: false; error: Error }
3537
);
3638

@@ -241,19 +243,14 @@ export default class SingularLiquidator
241243
sdk: this.sdk,
242244
});
243245
result.hfAfter = strategyResult.state.healthFactor;
246+
result.liquidatorPremium =
247+
strategyResult.balancesAfter.underlying - balanceBefore.underlying;
248+
result.liquidatorProfit =
249+
strategyResult.balancesAfter.eth - balanceBefore.eth;
244250
}
245251
result.gasUsed = strategyResult.receipt?.gasUsed ?? 0n;
246252
result.isError = !strategyResult.success;
247253

248-
await this.swapper.swap(
249-
acc.underlying,
250-
balanceBefore.underlying + result.liquidatorPremium,
251-
);
252-
const balanceAfter = await this.getExecutorBalance(acc.underlying);
253-
result.liquidatorPremium =
254-
balanceAfter.underlying - balanceBefore.underlying;
255-
result.liquidatorProfit = balanceAfter.eth - balanceBefore.eth;
256-
257254
if (strategyResult?.success === false) {
258255
const decoded = await this.errorHandler.explain(
259256
strategyResult.error,
@@ -291,7 +288,11 @@ export default class SingularLiquidator
291288
): Promise<OptimisticStrategyResult> {
292289
let snapshotId = snapshotId_;
293290
const logger = this.logger.child({ strategy: strategy.name });
294-
let result: OptimisticStrategyResult = { success: true, state: acc };
291+
let result: OptimisticStrategyResult = {
292+
success: true,
293+
state: acc,
294+
balancesAfter: { eth: 0n, underlying: 0n },
295+
};
295296
try {
296297
logger.debug({ snapshotId, strategy: strategy.name }, "previewing...");
297298
result.preview = await strategy.preview(acc);
@@ -322,6 +323,11 @@ export default class SingularLiquidator
322323
);
323324
}
324325
result.state = ca;
326+
// await this.swapper.swap(
327+
// acc.underlying,
328+
// balanceBefore.underlying + result.liquidatorPremium,
329+
// );
330+
result.balancesAfter = await this.getExecutorBalance(acc.underlying);
325331
} catch (e) {
326332
logger.error(e, "strategy failed");
327333
result = { ...result, success: false, error: e as Error };

0 commit comments

Comments
 (0)