Skip to content

Commit

Permalink
fix: handle preview reverts as warnings when isOptimalRepayable is false
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Jun 28, 2024
1 parent e8a0138 commit 7d26dd2
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions src/services/liquidate/LiquidationStrategyV3Partial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,43 +168,50 @@ export default class LiquidationStrategyV3Partial
);
const connectors = this.pathFinder.getAvailableConnectors(ca.allBalances);

const { result: preview } = await this.client.pub.simulateContract({
account: this.client.account,
address: this.partialLiquidator,
abi: [...iLiquidatorAbi, ...exceptionsAbis],
functionName: "previewPartialLiquidation",
args: [
ca.creditManager,
ca.addr,
tokenOut,
optimalAmount,
try {
const { result: preview } = await this.client.pub.simulateContract({
account: this.client.account,
address: this.partialLiquidator,
abi: [...iLiquidatorAbi, ...exceptionsAbis],
functionName: "previewPartialLiquidation",
args: [
ca.creditManager,
ca.addr,
tokenOut,
optimalAmount,
flashLoanAmount,
priceUpdates as any,
connectors,
BigInt(this.config.slippage),
],
});
if (preview.profit < 0n) {
if (isOptimalRepayable) {
throw new Error("optimal liquidation is not profitable or errored");
} else {
throw new Error(
"warning: cannot liquidate while remaining within borrowing limits",
);
}
}
return {
assetOut: tokenOut as Address,
amountOut: optimalAmount,
flashLoanAmount,
priceUpdates as any,
connectors,
BigInt(this.config.slippage),
],
});
if (preview.profit < 0n) {
if (isOptimalRepayable) {
throw new Error("optimal liquidation is not profitable or errored");
} else {
throw new Error(
"warning: cannot liquidate while remaining within borrowing limits",
);
priceUpdates,
calls: preview.calls.map(c => ({
callData: c.callData,
target: c.target,
})),
underlyingBalance: preview.profit,
skipOnFailure: !isOptimalRepayable,
};
} catch (e) {
if (!isOptimalRepayable) {
throw new Error(`warning: ${e}`);
}
throw e;
}
return {
assetOut: tokenOut as Address,
amountOut: optimalAmount,
flashLoanAmount,
priceUpdates,
calls: preview.calls.map(c => ({
callData: c.callData,
target: c.target,
})),
underlyingBalance: preview.profit,
skipOnFailure: !isOptimalRepayable,
};
}

public async simulate(
Expand Down

0 comments on commit 7d26dd2

Please sign in to comment.