From 8092561f043daf61215f6fc2b7247d4cfaf1d2c2 Mon Sep 17 00:00:00 2001 From: wphan Date: Thu, 19 Dec 2024 08:19:21 -0800 Subject: [PATCH] liquidator: use SOL routes when swapping LSTs (#322) --- src/bots/liquidator.ts | 26 ++++++++++++++++++++------ src/utils.ts | 11 +++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/bots/liquidator.ts b/src/bots/liquidator.ts index e088ef44..14c38f1c 100644 --- a/src/bots/liquidator.ts +++ b/src/bots/liquidator.ts @@ -82,6 +82,7 @@ import { calculateAccountValueUsd, checkIfAccountExists, handleSimResultError, + isSolLstToken, simulateAndGetTxWithCUs, SimulateAndGetTxWithCUsResponse, } from '../utils'; @@ -1259,10 +1260,17 @@ export class LiquidatorBot implements Bot { let outMarket: SpotMarketAccount | undefined; let inMarket: SpotMarketAccount | undefined; let amountIn: BN | undefined; + const spotMarketIsSolLst = isSolLstToken(spotMarketIndex); if (isVariant(orderDirection, 'long')) { - // sell USDC, buy spotMarketIndex - inMarket = this.driftClient.getSpotMarketAccount(0); - outMarket = this.driftClient.getSpotMarketAccount(spotMarketIndex); + if (spotMarketIsSolLst) { + // sell SOL, buy the LST + inMarket = this.driftClient.getSpotMarketAccount(1); + outMarket = this.driftClient.getSpotMarketAccount(spotMarketIndex); + } else { + // sell USDC, buy spotMarketIndex + inMarket = this.driftClient.getSpotMarketAccount(0); + outMarket = this.driftClient.getSpotMarketAccount(spotMarketIndex); + } if (!inMarket || !outMarket) { logger.error('failed to get spot markets'); return undefined; @@ -1274,9 +1282,15 @@ export class LiquidatorBot implements Bot { .mul(inPrecision) .div(PRICE_PRECISION.mul(outPrecision)); } else { - // sell spotMarketIndex, buy USDC - inMarket = this.driftClient.getSpotMarketAccount(spotMarketIndex); - outMarket = this.driftClient.getSpotMarketAccount(0); + if (spotMarketIsSolLst) { + // sell spotMarketIndex, buy SOL + inMarket = this.driftClient.getSpotMarketAccount(spotMarketIndex); + outMarket = this.driftClient.getSpotMarketAccount(1); + } else { + // sell spotMarketIndex, buy USDC + inMarket = this.driftClient.getSpotMarketAccount(spotMarketIndex); + outMarket = this.driftClient.getSpotMarketAccount(0); + } amountIn = baseAmountIn; } diff --git a/src/utils.ts b/src/utils.ts index b8bfe0f1..d442f263 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1553,3 +1553,14 @@ export function getMarketsAndOracleInfosToLoad( spotIndexes && spotIndexes.length > 0 ? spotIndexes : undefined, }; } + +export function isSolLstToken(spotMarketIndex: number): boolean { + return [ + 2, // mSOL + 6, // jitoSOL + 8, // bSOL + 16, // INF + 17, // dSOL + 25, // BNSOL + ].includes(spotMarketIndex); +}