@haven-fi/solauto-sdk 1.0.433 → 1.0.435

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -384,9 +384,9 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
384
384
  }));
385
385
  }
386
386
  flashRepay(flashLoanDetails) {
387
- const accounts = flashLoanDetails.mint.equals(this.supplyMint)
388
- ? { data: this.marginfiSupplyAccounts, oracle: this.supplyPriceOracle }
389
- : { data: this.marginfiDebtAccounts, oracle: this.debtPriceOracle };
387
+ const accounts = flashLoanDetails.useDebtLiquidity
388
+ ? { data: this.marginfiDebtAccounts, oracle: this.debtPriceOracle }
389
+ : { data: this.marginfiSupplyAccounts, oracle: this.supplyPriceOracle };
390
390
  const remainingAccounts = [];
391
391
  let includedFlashLoanToken = false;
392
392
  if (this.intermediaryMarginfiAccount) {
@@ -10,7 +10,8 @@ export declare enum SolautoRebalanceType {
10
10
  None = 0,
11
11
  Regular = 1,
12
12
  DoubleRebalanceWithFL = 2,
13
- SingleRebalanceWithFL = 3
13
+ FLSwapThenRebalance = 3,
14
+ FLRebalanceThenSwap = 4
14
15
  }
15
16
  export type SolautoRebalanceTypeArgs = SolautoRebalanceType;
16
17
  export declare function getSolautoRebalanceTypeSerializer(): Serializer<SolautoRebalanceTypeArgs, SolautoRebalanceType>;
@@ -1 +1 @@
1
- {"version":3,"file":"solautoRebalanceType.d.ts","sourceRoot":"","sources":["../../../src/generated/types/solautoRebalanceType.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAc,MAAM,sCAAsC,CAAC;AAE9E,oBAAY,oBAAoB;IAC9B,IAAI,IAAA;IACJ,OAAO,IAAA;IACP,qBAAqB,IAAA;IACrB,qBAAqB,IAAA;CACtB;AAED,MAAM,MAAM,wBAAwB,GAAG,oBAAoB,CAAC;AAE5D,wBAAgB,iCAAiC,IAAI,UAAU,CAC7D,wBAAwB,EACxB,oBAAoB,CACrB,CAIA"}
1
+ {"version":3,"file":"solautoRebalanceType.d.ts","sourceRoot":"","sources":["../../../src/generated/types/solautoRebalanceType.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAc,MAAM,sCAAsC,CAAC;AAE9E,oBAAY,oBAAoB;IAC9B,IAAI,IAAA;IACJ,OAAO,IAAA;IACP,qBAAqB,IAAA;IACrB,mBAAmB,IAAA;IACnB,mBAAmB,IAAA;CACpB;AAED,MAAM,MAAM,wBAAwB,GAAG,oBAAoB,CAAC;AAE5D,wBAAgB,iCAAiC,IAAI,UAAU,CAC7D,wBAAwB,EACxB,oBAAoB,CACrB,CAIA"}
@@ -15,7 +15,8 @@ var SolautoRebalanceType;
15
15
  SolautoRebalanceType[SolautoRebalanceType["None"] = 0] = "None";
16
16
  SolautoRebalanceType[SolautoRebalanceType["Regular"] = 1] = "Regular";
17
17
  SolautoRebalanceType[SolautoRebalanceType["DoubleRebalanceWithFL"] = 2] = "DoubleRebalanceWithFL";
18
- SolautoRebalanceType[SolautoRebalanceType["SingleRebalanceWithFL"] = 3] = "SingleRebalanceWithFL";
18
+ SolautoRebalanceType[SolautoRebalanceType["FLSwapThenRebalance"] = 3] = "FLSwapThenRebalance";
19
+ SolautoRebalanceType[SolautoRebalanceType["FLRebalanceThenSwap"] = 4] = "FLRebalanceThenSwap";
19
20
  })(SolautoRebalanceType || (exports.SolautoRebalanceType = SolautoRebalanceType = {}));
20
21
  function getSolautoRebalanceTypeSerializer() {
21
22
  return (0, serializers_1.scalarEnum)(SolautoRebalanceType, {
@@ -1 +1 @@
1
- {"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AAmBxE,OAAO,EAAE,aAAa,EAA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,CAAC;AAOlD,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AA4LjD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CAoH7B;AA2LD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA0BnD;AAED,wBAAsB,8BAA8B,CAAC,MAAM,EAAE,aAAa,oBAqDzE;AAED,wBAAsB,gCAAgC,CACpD,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC,CAoI5C;AAED,wBAAsB,gCAAgC,CACpD,eAAe,EAAE,oBAAoB,EACrC,YAAY,EAAE,SAAS,EACvB,eAAe,EAAE,SAAS,GACzB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC,CAsC5C;AAED,wBAAgB,YAAY,CAC1B,GAAG,EAAE,GAAG,EACR,GAAG,EAAE,kBAAkB,EAAE,EACzB,KAAK,EAAE,KAAK,EACZ,oBAAoB,CAAC,EAAE,OAAO;;;;EAiG/B"}
@@ -383,20 +383,27 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
383
383
  if (flashLoan) {
384
384
  client.log("Flash loan details: ", flashLoan);
385
385
  const addFirstRebalance = values.amountUsdToDcaIn > 0;
386
+ const rebalanceThenSwap = values.rebalanceDirection === generated_1.RebalanceDirection.Repay &&
387
+ flashLoan.useDebtLiquidity;
386
388
  const rebalanceType = addFirstRebalance
387
389
  ? generated_1.SolautoRebalanceType.DoubleRebalanceWithFL
388
- : generated_1.SolautoRebalanceType.SingleRebalanceWithFL;
390
+ : rebalanceThenSwap
391
+ ? generated_1.SolautoRebalanceType.FLRebalanceThenSwap
392
+ : generated_1.SolautoRebalanceType.FLSwapThenRebalance;
393
+ client.log("Rebalance type:", rebalanceType);
394
+ const firstRebalance = client.rebalance("A", jupQuote, rebalanceType, values, flashLoan, targetLiqUtilizationRateBps);
395
+ const lastRebalance = client.rebalance("B", jupQuote, rebalanceType, values, flashLoan, targetLiqUtilizationRateBps);
396
+ const flashBorrowDest = (0, accountUtils_1.getTokenAccount)(rebalanceThenSwap
397
+ ? client.solautoPosition
398
+ : (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(client.signer.publicKey), rebalanceThenSwap ? swapDetails.outputMint : swapDetails.inputMint);
389
399
  tx = tx.add([
390
400
  setupInstructions,
391
401
  tokenLedgerIx,
392
- client.flashBorrow(flashLoan, (0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(client.signer.publicKey), swapDetails.inputMint)),
393
- ...(addFirstRebalance
394
- ? [
395
- client.rebalance("A", jupQuote, rebalanceType, values, flashLoan, targetLiqUtilizationRateBps),
396
- ]
397
- : []),
398
- swapIx,
399
- client.rebalance("B", jupQuote, rebalanceType, values, flashLoan, targetLiqUtilizationRateBps),
402
+ client.flashBorrow(flashLoan, flashBorrowDest),
403
+ ...(addFirstRebalance ? [firstRebalance] : []),
404
+ ...(rebalanceThenSwap
405
+ ? [lastRebalance, swapIx]
406
+ : [swapIx, lastRebalance]),
400
407
  client.flashRepay(flashLoan),
401
408
  ]);
402
409
  }
@@ -18,6 +18,7 @@ export declare function getRebalanceValues(state: PositionState, settings: Solau
18
18
  export interface FlashLoanDetails {
19
19
  baseUnitAmount: bigint;
20
20
  mint: PublicKey;
21
+ useDebtLiquidity: boolean;
21
22
  }
22
23
  export declare function getFlashLoanDetails(client: SolautoClient, values: RebalanceValues, jupQuote: QuoteResponse): FlashLoanDetails | undefined;
23
24
  export declare function getJupSwapRebalanceDetails(client: SolautoClient, values: RebalanceValues, targetLiqUtilizationRateBps?: number, attemptNum?: number): JupSwapDetails;
@@ -1 +1 @@
1
- {"version":3,"file":"rebalanceUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/rebalanceUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EACL,WAAW,EACX,aAAa,EAEb,kBAAkB,EAClB,yBAAyB,EACzB,SAAS,EACV,MAAM,iBAAiB,CAAC;AAOzB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAajD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAmI9C,MAAM,WAAW,eAAe;IAC9B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,OAAO,CAAC;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,SAAS,CAAC;IACzB,eAAe,EAAE,eAAe,CAAC;IACjC,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,aAAa,EACpB,QAAQ,EAAE,yBAAyB,GAAG,SAAS,EAC/C,GAAG,EAAE,WAAW,GAAG,SAAS,EAC5B,eAAe,EAAE,MAAM,EACvB,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EACjB,2BAA2B,CAAC,EAAE,MAAM,GACnC,eAAe,CAwDjB;AAED,MAAM,WAAW,gBAAgB;IAC/B,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,SAAS,CAAC;CACjB;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,aAAa,GACtB,gBAAgB,GAAG,SAA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AS,CAAC;IAChB,gBAAgB,EAAE,OAAO,CAAC;CAC3B;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,aAAa,GACtB,gBAAgB,GAAG,SAAS,CAiF9B;AAED,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,cAAc,CA8ChB"}
@@ -128,7 +128,12 @@ function getFlashLoanDetails(client, values, jupQuote) {
128
128
  (0, generalUtils_2.consoleLog)("Intermediary liq utilization rate:", tempLiqUtilizationRateBps, `$${supplyUsd}`, `$${debtUsd}`, "Max:", maxLiqUtilizationRateBps);
129
129
  let flashLoanToken = undefined;
130
130
  let flashLoanTokenPrice = 0;
131
- if (values.rebalanceDirection === generated_1.RebalanceDirection.Boost) {
131
+ const baseUnitAmount = BigInt(parseInt(jupQuote.inAmount));
132
+ const sufficientDebtLiquidity = debtAdjustmentUsdAbs * 1.1 <=
133
+ (0, numberUtils_1.fromBaseUnit)(client.solautoPositionState?.debt.amountCanBeUsed.baseAmountUsdValue ??
134
+ BigInt(0), generalAccounts_1.USD_DECIMALS);
135
+ if (values.rebalanceDirection === generated_1.RebalanceDirection.Boost ||
136
+ sufficientDebtLiquidity) {
132
137
  flashLoanToken = client.solautoPositionState.debt;
133
138
  flashLoanTokenPrice = (0, priceUtils_1.safeGetPrice)(client.debtMint);
134
139
  }
@@ -136,15 +141,14 @@ function getFlashLoanDetails(client, values, jupQuote) {
136
141
  flashLoanToken = client.solautoPositionState.supply;
137
142
  flashLoanTokenPrice = (0, priceUtils_1.safeGetPrice)(client.supplyMint);
138
143
  }
139
- const exactAmountBaseUnit = jupQuote.swapMode === "ExactOut" || jupQuote.swapMode === "ExactIn"
140
- ? BigInt(parseInt(jupQuote.inAmount))
141
- : undefined;
144
+ if (jupQuote.swapMode !== "ExactOut" && jupQuote.swapMode !== "ExactIn") {
145
+ throw new Error("Token ledger swap not currently supported");
146
+ }
142
147
  return requiresFlashLoan
143
148
  ? {
144
- baseUnitAmount: exactAmountBaseUnit
145
- ? exactAmountBaseUnit
146
- : (0, numberUtils_1.toBaseUnit)(debtAdjustmentUsdAbs / flashLoanTokenPrice, flashLoanToken.decimals),
149
+ baseUnitAmount,
147
150
  mint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(flashLoanToken.mint),
151
+ useDebtLiquidity: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(flashLoanToken.mint).equals(client.debtMint),
148
152
  }
149
153
  : undefined;
150
154
  }
@@ -7,7 +7,6 @@ import {
7
7
  getSolautoManagedPositions,
8
8
  PositionState,
9
9
  positionStateWithLatestPrices,
10
- PRICES,
11
10
  retryWithExponentialBackoff,
12
11
  safeFetchAllSolautoPosition,
13
12
  safeGetPrice,
@@ -15,7 +14,7 @@ import {
15
14
  TOKEN_INFO,
16
15
  USD_DECIMALS,
17
16
  } from "../src";
18
- import { PublicKey, SystemProgram, VersionedTransaction } from "@solana/web3.js";
17
+ import { PublicKey } from "@solana/web3.js";
19
18
  import { NATIVE_MINT } from "@solana/spl-token";
20
19
  import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
21
20
  import path from "path";
@@ -187,6 +186,7 @@ async function main(filterWhitelist: boolean) {
187
186
  console.log("\n\n");
188
187
 
189
188
  const latestStates: PositionState[] = [];
189
+ let unhealthyPositions = 0;
190
190
  for (const pos of solautoPositionsData) {
191
191
  const latestState = await positionStateWithLatestPrices(
192
192
  pos.state,
@@ -200,15 +200,20 @@ async function main(filterWhitelist: boolean) {
200
200
  toWeb3JsPublicKey(pos.state.debt.mint)
201
201
  );
202
202
 
203
- console.log("Position:", pos.publicKey.toString());
204
- console.log("Authority:", pos.authority.toString());
203
+ const repayFrom = pos.position.settingParams.repayToBps + pos.position.settingParams.repayGap;
204
+ const unhealthy = pos.state.liqUtilizationRateBps > repayFrom;
205
+ const healthText = unhealthy ? `(Unhealthy: ${pos.state.liqUtilizationRateBps - repayFrom}` : "";
206
+ if (unhealthy) {
207
+ unhealthyPositions += 1;
208
+ }
209
+
210
+ console.log(pos.publicKey.toString(), `(${pos.authority.toString()})`);
205
211
  console.log(
206
- `${strategy}: $${formatNumber(fromBaseUnit(latestState.netWorth.baseAmountUsdValue, USD_DECIMALS), 2, 10000, 2)}`
212
+ `${strategy}: $${formatNumber(fromBaseUnit(latestState.netWorth.baseAmountUsdValue, USD_DECIMALS), 2, 10000, 2)} ${healthText}`
207
213
  );
208
- console.log("\n");
209
214
  }
210
215
 
211
- console.log("Total positions:", solautoPositionsData.length);
216
+ console.log("\nTotal positions:", solautoPositionsData.length, unhealthyPositions ? ` (unhealthy: ${unhealthyPositions})` : "");
212
217
  console.log(
213
218
  "Total users:",
214
219
  Array.from(new Set(solautoPositionsData.map((x) => x.authority.toString())))
@@ -221,37 +226,5 @@ async function main(filterWhitelist: boolean) {
221
226
  console.log(`Total TVL: $${formatNumber(tvl, 2, 10000, 2)}`);
222
227
  }
223
228
 
224
- import { Transaction } from "@solana/web3.js";
225
- import * as bs58 from "bs58";
226
-
227
- function decodeVersionedTx(base58Tx: string) {
228
- // 1) Decode base58 into raw bytes
229
- const txBuffer = bs58.decode(base58Tx);
230
-
231
- // 2) Parse as versioned transaction
232
- const versionedTx = VersionedTransaction.deserialize(txBuffer);
233
-
234
- // 3) Extract signatures or do other things with `versionedTx`
235
- const signatures = versionedTx.signatures.map((sigBytes, index) => ({
236
- index,
237
- signature: bs58.encode(sigBytes), // convert signature bytes to base58 string
238
- }));
239
-
240
- return { versionedTx, signatures };
241
- }
242
-
243
229
  const filterWhitelist = true;
244
- main(filterWhitelist).then((x) => x);
245
-
246
- // console.log(
247
- // decodeVersionedTx(
248
- // "mQkqpyEB1JEkGUsowzhUXvn2v1kwdGYApNGDUsRZ1BmpmKJhMUXQhmyZoDXKPtVhcjgWQHqN6S94p2fNiPxyR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// console.log(SystemProgram.programId.toString());
230
+ main(filterWhitelist).then((x) => x);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haven-fi/solauto-sdk",
3
- "version": "1.0.433",
3
+ "version": "1.0.435",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "Typescript SDK for the Solauto program on the Solana blockchain",
@@ -594,9 +594,9 @@ export class SolautoMarginfiClient extends SolautoClient {
594
594
  }
595
595
 
596
596
  flashRepay(flashLoanDetails: FlashLoanDetails): TransactionBuilder {
597
- const accounts = flashLoanDetails.mint.equals(this.supplyMint)
598
- ? { data: this.marginfiSupplyAccounts, oracle: this.supplyPriceOracle }
599
- : { data: this.marginfiDebtAccounts, oracle: this.debtPriceOracle };
597
+ const accounts = flashLoanDetails.useDebtLiquidity
598
+ ? { data: this.marginfiDebtAccounts, oracle: this.debtPriceOracle }
599
+ : { data: this.marginfiSupplyAccounts, oracle: this.supplyPriceOracle };
600
600
 
601
601
  const remainingAccounts: AccountMeta[] = [];
602
602
  let includedFlashLoanToken = false;
@@ -12,7 +12,8 @@ export enum SolautoRebalanceType {
12
12
  None,
13
13
  Regular,
14
14
  DoubleRebalanceWithFL,
15
- SingleRebalanceWithFL,
15
+ FLSwapThenRebalance,
16
+ FLRebalanceThenSwap,
16
17
  }
17
18
 
18
19
  export type SolautoRebalanceTypeArgs = SolautoRebalanceType;
@@ -16,6 +16,7 @@ import {
16
16
  import {
17
17
  InvalidRebalanceConditionError,
18
18
  LendingPlatform,
19
+ RebalanceDirection,
19
20
  SolautoAction,
20
21
  SolautoRebalanceType,
21
22
  TokenType,
@@ -713,41 +714,50 @@ export async function buildSolautoRebalanceTransaction(
713
714
  if (flashLoan) {
714
715
  client.log("Flash loan details: ", flashLoan);
715
716
  const addFirstRebalance = values.amountUsdToDcaIn > 0;
717
+
718
+ const rebalanceThenSwap =
719
+ values.rebalanceDirection === RebalanceDirection.Repay &&
720
+ flashLoan.useDebtLiquidity;
721
+
716
722
  const rebalanceType = addFirstRebalance
717
723
  ? SolautoRebalanceType.DoubleRebalanceWithFL
718
- : SolautoRebalanceType.SingleRebalanceWithFL;
724
+ : rebalanceThenSwap
725
+ ? SolautoRebalanceType.FLRebalanceThenSwap
726
+ : SolautoRebalanceType.FLSwapThenRebalance;
727
+
728
+ client.log("Rebalance type:", rebalanceType);
729
+ const firstRebalance = client.rebalance(
730
+ "A",
731
+ jupQuote,
732
+ rebalanceType,
733
+ values,
734
+ flashLoan,
735
+ targetLiqUtilizationRateBps
736
+ );
737
+ const lastRebalance = client.rebalance(
738
+ "B",
739
+ jupQuote,
740
+ rebalanceType,
741
+ values,
742
+ flashLoan,
743
+ targetLiqUtilizationRateBps
744
+ );
745
+
746
+ const flashBorrowDest = getTokenAccount(
747
+ rebalanceThenSwap
748
+ ? client.solautoPosition
749
+ : toWeb3JsPublicKey(client.signer.publicKey),
750
+ rebalanceThenSwap ? swapDetails.outputMint : swapDetails.inputMint
751
+ );
719
752
 
720
753
  tx = tx.add([
721
754
  setupInstructions,
722
755
  tokenLedgerIx,
723
- client.flashBorrow(
724
- flashLoan,
725
- getTokenAccount(
726
- toWeb3JsPublicKey(client.signer.publicKey),
727
- swapDetails.inputMint
728
- )
729
- ),
730
- ...(addFirstRebalance
731
- ? [
732
- client.rebalance(
733
- "A",
734
- jupQuote,
735
- rebalanceType,
736
- values,
737
- flashLoan,
738
- targetLiqUtilizationRateBps
739
- ),
740
- ]
741
- : []),
742
- swapIx,
743
- client.rebalance(
744
- "B",
745
- jupQuote,
746
- rebalanceType,
747
- values,
748
- flashLoan,
749
- targetLiqUtilizationRateBps
750
- ),
756
+ client.flashBorrow(flashLoan, flashBorrowDest),
757
+ ...(addFirstRebalance ? [firstRebalance] : []),
758
+ ...(rebalanceThenSwap
759
+ ? [lastRebalance, swapIx]
760
+ : [swapIx, lastRebalance]),
751
761
  client.flashRepay(flashLoan),
752
762
  ]);
753
763
  } else {
@@ -880,8 +890,14 @@ export function getErrorInfo(
880
890
  consoleLog("Error code:", errCode);
881
891
  consoleLog("Error instruction program:", errIx?.programId.toString());
882
892
 
883
- const solautoError = getSolautoErrorFromCode(errCode ?? -1, createSolautoProgram());
884
- const marginfiError = getMarginfiErrorFromCode(errCode ?? -1, createMarginfiProgram());
893
+ const solautoError = getSolautoErrorFromCode(
894
+ errCode ?? -1,
895
+ createSolautoProgram()
896
+ );
897
+ const marginfiError = getMarginfiErrorFromCode(
898
+ errCode ?? -1,
899
+ createMarginfiProgram()
900
+ );
885
901
 
886
902
  if (
887
903
  errCode &&
@@ -30,6 +30,7 @@ import {
30
30
  import { USD_DECIMALS } from "../../constants/generalAccounts";
31
31
  import { RebalanceAction } from "../../types";
32
32
  import { safeGetPrice } from "../priceUtils";
33
+ import { TOKEN_INFO } from "../../constants";
33
34
 
34
35
  function getAdditionalAmountToDcaIn(dca: DCASettings): number {
35
36
  if (dca.dcaInBaseUnit === BigInt(0)) {
@@ -239,6 +240,7 @@ export function getRebalanceValues(
239
240
  export interface FlashLoanDetails {
240
241
  baseUnitAmount: bigint;
241
242
  mint: PublicKey;
243
+ useDebtLiquidity: boolean;
242
244
  }
243
245
 
244
246
  export function getFlashLoanDetails(
@@ -292,7 +294,20 @@ export function getFlashLoanDetails(
292
294
 
293
295
  let flashLoanToken: PositionTokenUsage | undefined = undefined;
294
296
  let flashLoanTokenPrice = 0;
295
- if (values.rebalanceDirection === RebalanceDirection.Boost) {
297
+
298
+ const baseUnitAmount = BigInt(parseInt(jupQuote.inAmount));
299
+ const sufficientDebtLiquidity =
300
+ debtAdjustmentUsdAbs * 1.1 <=
301
+ fromBaseUnit(
302
+ client.solautoPositionState?.debt.amountCanBeUsed.baseAmountUsdValue ??
303
+ BigInt(0),
304
+ USD_DECIMALS
305
+ );
306
+
307
+ if (
308
+ values.rebalanceDirection === RebalanceDirection.Boost ||
309
+ sufficientDebtLiquidity
310
+ ) {
296
311
  flashLoanToken = client.solautoPositionState!.debt;
297
312
  flashLoanTokenPrice = safeGetPrice(client.debtMint)!;
298
313
  } else {
@@ -300,20 +315,17 @@ export function getFlashLoanDetails(
300
315
  flashLoanTokenPrice = safeGetPrice(client.supplyMint)!;
301
316
  }
302
317
 
303
- const exactAmountBaseUnit =
304
- jupQuote.swapMode === "ExactOut" || jupQuote.swapMode === "ExactIn"
305
- ? BigInt(parseInt(jupQuote.inAmount))
306
- : undefined;
318
+ if (jupQuote.swapMode !== "ExactOut" && jupQuote.swapMode !== "ExactIn") {
319
+ throw new Error("Token ledger swap not currently supported");
320
+ }
307
321
 
308
322
  return requiresFlashLoan
309
323
  ? {
310
- baseUnitAmount: exactAmountBaseUnit
311
- ? exactAmountBaseUnit
312
- : toBaseUnit(
313
- debtAdjustmentUsdAbs / flashLoanTokenPrice,
314
- flashLoanToken.decimals
315
- ),
324
+ baseUnitAmount,
316
325
  mint: toWeb3JsPublicKey(flashLoanToken.mint),
326
+ useDebtLiquidity: toWeb3JsPublicKey(flashLoanToken.mint).equals(
327
+ client.debtMint
328
+ ),
317
329
  }
318
330
  : undefined;
319
331
  }