@haven-fi/solauto-sdk 1.0.173 → 1.0.175

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- {"version":3,"file":"solautoMarginfiClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoMarginfiClient.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,MAAM,EACN,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAM1D,OAAO,EACL,kBAAkB,EAElB,aAAa,EAEb,iBAAiB,EACjB,wBAAwB,EACxB,gCAAgC,EAKjC,MAAM,cAAc,CAAC;AAGtB,OAAO,EAEL,eAAe,EAUhB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAUnE,MAAM,WAAW,yBAA0B,SAAQ,iBAAiB;IAClE,eAAe,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC;IACrC,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,aAAa,CAAC,EAAE,SAAS,CAAC;CAC3B;AAED,qBAAa,qBAAsB,SAAQ,aAAa;IACtD,OAAO,CAAC,WAAW,CAAkB;IAE9B,eAAe,EAAG,SAAS,CAAC;IAE5B,sBAAsB,EAAE,MAAM,CAAa;IAC3C,eAAe,EAAG,SAAS,GAAG,MAAM,CAAC;IACrC,iBAAiB,EAAG,SAAS,CAAC;IAC9B,aAAa,EAAG,SAAS,CAAC;IAE1B,sBAAsB,EAAG,qBAAqB,CAAC;IAC/C,oBAAoB,EAAG,qBAAqB,CAAC;IAE7C,iBAAiB,EAAG,SAAS,CAAC;IAC9B,eAAe,EAAG,SAAS,CAAC;IAG5B,iCAAiC,CAAC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IAUzB,wBAAwB,IAAI,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;IAyBvE,yBAAyB,IAAI,kBAAkB;IAS/C,YAAY,CACV,aAAa,CAAC,EAAE,gCAAgC,EAChD,GAAG,CAAC,EAAE,kBAAkB,GACvB,kBAAkB;IAMrB,OAAO,CAAC,sBAAsB;IA+C9B,OAAO,IAAI,kBAAkB;IAc7B,mBAAmB,CAAC,IAAI,EAAE,iBAAiB,GAAG,kBAAkB;IAUhE,OAAO,CAAC,6BAA6B;IAmErC,OAAO,CAAC,oCAAoC;IA0D5C,SAAS,CACP,aAAa,EAAE,GAAG,GAAG,GAAG,EACxB,WAAW,EAAE,cAAc,EAC3B,aAAa,EAAE,wBAAwB,EACvC,SAAS,CAAC,EAAE,gBAAgB,EAC5B,2BAA2B,CAAC,EAAE,MAAM,EACpC,WAAW,CAAC,EAAE,MAAM,GACnB,kBAAkB;IAiErB,WAAW,CACT,gBAAgB,EAAE,gBAAgB,EAClC,uBAAuB,EAAE,SAAS,GACjC,kBAAkB;IA2BrB,UAAU,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,kBAAkB;IAoFlE,iCAAiC,IAAI,kBAAkB;IASjD,qBAAqB,IAAI,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC;CAwClE"}
@@ -334,9 +334,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
334
334
  : undefined,
335
335
  rebalanceType,
336
336
  targetLiqUtilizationRateBps: targetLiqUtilizationRateBps ?? null,
337
- targetInAmountBaseUnit: targetLiqUtilizationRateBps && rebalanceStep === "A"
338
- ? swapDetails.amount
339
- : null,
337
+ targetInAmountBaseUnit: rebalanceStep === "A" ? swapDetails.amount : null,
340
338
  limitGapBps: limitGapBps ?? null,
341
339
  });
342
340
  }
@@ -4,6 +4,7 @@ import { ReferralState } from "../generated";
4
4
  import { SolautoClient } from "../clients/solautoClient";
5
5
  export declare function rebalanceChoresBefore(client: SolautoClient, tx: TransactionBuilder, accountsGettingCreated: string[]): Promise<TransactionBuilder>;
6
6
  export declare function getTransactionChores(client: SolautoClient, tx: TransactionBuilder): Promise<[TransactionBuilder, TransactionBuilder]>;
7
+ export declare function requiresRefreshBeforeRebalance(client: SolautoClient): boolean;
7
8
  export declare function buildSolautoRebalanceTransaction(client: SolautoClient, targetLiqUtilizationRateBps?: number, attemptNum?: number): Promise<{
8
9
  tx: TransactionBuilder;
9
10
  lookupTableAddresses: string[];
@@ -1 +1 @@
1
- {"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AAKxE,OAAO,EAEL,aAAa,EAUd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAyNzD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CA+G7B;AAmLD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA0BnD;AAED,wBAAsB,gCAAgC,CACpD,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CACN;IACE,EAAE,EAAE,kBAAkB,CAAC;IACvB,oBAAoB,EAAE,MAAM,EAAE,CAAC;CAChC,GACD,SAAS,CACZ,CA+HA;AAED,wBAAsB,gCAAgC,CACpD,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,SAAS,GACtB,OAAO,CAAC,CAAC,kBAAkB,EAAE,MAAM,EAAE,CAAC,GAAG,SAAS,CAAC,CAmCrD"}
1
+ {"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AAKxE,OAAO,EAEL,aAAa,EAUd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAyNzD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CA+G7B;AAmLD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA0BnD;AAED,wBAAgB,8BAA8B,CAAC,MAAM,EAAE,aAAa,WAyBnE;AAED,wBAAsB,gCAAgC,CACpD,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CACN;IACE,EAAE,EAAE,kBAAkB,CAAC;IACvB,oBAAoB,EAAE,MAAM,EAAE,CAAC;CAChC,GACD,SAAS,CACZ,CAwHA;AAED,wBAAsB,gCAAgC,CACpD,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,SAAS,GACtB,OAAO,CAAC,CAAC,kBAAkB,EAAE,MAAM,EAAE,CAAC,GAAG,SAAS,CAAC,CAmCrD"}
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.rebalanceChoresBefore = rebalanceChoresBefore;
4
4
  exports.getTransactionChores = getTransactionChores;
5
+ exports.requiresRefreshBeforeRebalance = requiresRefreshBeforeRebalance;
5
6
  exports.buildSolautoRebalanceTransaction = buildSolautoRebalanceTransaction;
6
7
  exports.convertReferralFeesToDestination = convertReferralFeesToDestination;
7
8
  const umi_1 = require("@metaplex-foundation/umi");
@@ -322,6 +323,23 @@ async function getTransactionChores(client, tx) {
322
323
  choresAfter = choresAfter.add(transactionChoresAfter(client, solautoActions, client.livePositionUpdates.cancellingDca));
323
324
  return [choresBefore, choresAfter];
324
325
  }
326
+ function requiresRefreshBeforeRebalance(client) {
327
+ if (client.solautoPositionState.liqUtilizationRateBps >
328
+ (0, numberUtils_1.getMaxLiqUtilizationRateBps)(client.solautoPositionState.maxLtvBps, client.solautoPositionState.liqThresholdBps, 0.01)) {
329
+ return true;
330
+ }
331
+ else if (client.solautoPositionData && !client.selfManaged) {
332
+ const oldSupply = client.solautoPositionData.state.supply.amountUsed.baseUnit;
333
+ const oldDebt = client.solautoPositionData.state.debt.amountUsed.baseUnit;
334
+ const supplyDiff = (client.solautoPositionState?.supply.amountUsed.baseUnit ?? BigInt(0)) - oldSupply;
335
+ const debtDiff = (client.solautoPositionState?.debt.amountUsed.baseUnit ?? BigInt(0)) - oldDebt;
336
+ if (Math.abs(Number(supplyDiff)) / Number(oldSupply) >= 0.01 || Math.abs(Number(debtDiff)) / Number(oldDebt) >= 0.01) {
337
+ return true;
338
+ }
339
+ }
340
+ // Rebalance ix will already refresh internally if position is self managed, has automation to update, or position state last updated >= 1 day ago
341
+ return false;
342
+ }
325
343
  async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRateBps, attemptNum) {
326
344
  client.solautoPositionState = await client.getFreshPositionState();
327
345
  if ((client.solautoPositionState?.supply.amountUsed.baseUnit === BigInt(0) &&
@@ -337,6 +355,9 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
337
355
  const { jupQuote, priceImpactBps, lookupTableAddresses, setupInstructions, tokenLedgerIx, swapIx, } = await (0, jupiterUtils_1.getJupSwapTransaction)(client.signer, swapDetails, attemptNum);
338
356
  const flashLoan = (0, rebalanceUtils_1.getFlashLoanDetails)(client, values, jupQuote, priceImpactBps);
339
357
  let tx = (0, umi_1.transactionBuilder)();
358
+ if (requiresRefreshBeforeRebalance(client)) {
359
+ tx = tx.add(client.refresh());
360
+ }
340
361
  if (flashLoan) {
341
362
  client.log("Flash loan details: ", flashLoan);
342
363
  const addFirstRebalance = values.amountUsdToDcaIn > 0;
@@ -367,10 +388,6 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
367
388
  client.rebalance("B", swapDetails, rebalanceType, undefined, targetLiqUtilizationRateBps),
368
389
  ]);
369
390
  }
370
- if (client.solautoPositionState.liqUtilizationRateBps >
371
- (0, numberUtils_1.getMaxLiqUtilizationRateBps)(client.solautoPositionState.maxLtvBps, client.solautoPositionState.liqThresholdBps, 0.01)) {
372
- tx = tx.prepend(client.refresh());
373
- }
374
391
  return {
375
392
  tx,
376
393
  lookupTableAddresses,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haven-fi/solauto-sdk",
3
- "version": "1.0.173",
3
+ "version": "1.0.175",
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",
@@ -514,10 +514,7 @@ export class SolautoMarginfiClient extends SolautoClient {
514
514
  : undefined,
515
515
  rebalanceType,
516
516
  targetLiqUtilizationRateBps: targetLiqUtilizationRateBps ?? null,
517
- targetInAmountBaseUnit:
518
- targetLiqUtilizationRateBps && rebalanceStep === "A"
519
- ? swapDetails.amount
520
- : null,
517
+ targetInAmountBaseUnit: rebalanceStep === "A" ? swapDetails.amount : null,
521
518
  limitGapBps: limitGapBps ?? null,
522
519
  });
523
520
  }
@@ -566,6 +566,33 @@ export async function getTransactionChores(
566
566
  return [choresBefore, choresAfter];
567
567
  }
568
568
 
569
+ export function requiresRefreshBeforeRebalance(client: SolautoClient) {
570
+ if (
571
+ client.solautoPositionState!.liqUtilizationRateBps >
572
+ getMaxLiqUtilizationRateBps(
573
+ client.solautoPositionState!.maxLtvBps,
574
+ client.solautoPositionState!.liqThresholdBps,
575
+ 0.01
576
+ )
577
+ ) {
578
+ return true;
579
+ } else if (client.solautoPositionData && !client.selfManaged) {
580
+ const oldSupply = client.solautoPositionData.state.supply.amountUsed.baseUnit;
581
+ const oldDebt = client.solautoPositionData.state.debt.amountUsed.baseUnit;
582
+
583
+ const supplyDiff = (client.solautoPositionState?.supply.amountUsed.baseUnit ?? BigInt(0)) - oldSupply;
584
+ const debtDiff = (client.solautoPositionState?.debt.amountUsed.baseUnit ?? BigInt(0)) - oldDebt;
585
+
586
+ if (Math.abs(Number(supplyDiff)) / Number(oldSupply) >= 0.01 || Math.abs(Number(debtDiff)) / Number(oldDebt) >= 0.01) {
587
+ return true;
588
+ }
589
+ }
590
+
591
+ // Rebalance ix will already refresh internally if position is self managed, has automation to update, or position state last updated >= 1 day ago
592
+
593
+ return false;
594
+ }
595
+
569
596
  export async function buildSolautoRebalanceTransaction(
570
597
  client: SolautoClient,
571
598
  targetLiqUtilizationRateBps?: number,
@@ -627,6 +654,10 @@ export async function buildSolautoRebalanceTransaction(
627
654
 
628
655
  let tx = transactionBuilder();
629
656
 
657
+ if (requiresRefreshBeforeRebalance(client)) {
658
+ tx = tx.add(client.refresh());
659
+ }
660
+
630
661
  if (flashLoan) {
631
662
  client.log("Flash loan details: ", flashLoan);
632
663
  const addFirstRebalance = values.amountUsdToDcaIn > 0;
@@ -688,17 +719,6 @@ export async function buildSolautoRebalanceTransaction(
688
719
  ]);
689
720
  }
690
721
 
691
- if (
692
- client.solautoPositionState!.liqUtilizationRateBps >
693
- getMaxLiqUtilizationRateBps(
694
- client.solautoPositionState!.maxLtvBps,
695
- client.solautoPositionState!.liqThresholdBps,
696
- 0.01
697
- )
698
- ) {
699
- tx = tx.prepend(client.refresh());
700
- }
701
-
702
722
  return {
703
723
  tx,
704
724
  lookupTableAddresses,
@@ -24,7 +24,7 @@ describe("Solauto Marginfi tests", async () => {
24
24
  // const signer = setupTest();
25
25
  const signer = setupTest("solauto-manager");
26
26
 
27
- const payForTransactions = false;
27
+ const payForTransactions = true;
28
28
  const useJitoBundle = false;
29
29
  const positionId = 1;
30
30
 
@@ -40,7 +40,7 @@ describe("Solauto Marginfi tests", async () => {
40
40
  {
41
41
  signer,
42
42
  positionId,
43
- authority: new PublicKey("AprYCPiVeKMCgjQ2ZufwChMzvQ5kFjJo2ekTLSkXsQDm")
43
+ authority: new PublicKey("E5BBsR1sUToPc3jXVwhrK5ttSiy6xhWJDMdQLvkgNppe")
44
44
  // marginfiAccount: new PublicKey(
45
45
  // "4nNvUXF5YqHFcH2nGweSiuvy1ct7V5FXfoCLKFYUN36z"
46
46
  // ),