@haven-fi/solauto-sdk 1.0.174 → 1.0.175

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,CAoIA;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) &&
@@ -336,8 +354,10 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
336
354
  const swapDetails = (0, rebalanceUtils_1.getJupSwapRebalanceDetails)(client, values, targetLiqUtilizationRateBps, attemptNum);
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
- console.log((0, generalUtils_1.safeGetPrice)(client.supplyMint), (0, generalUtils_1.safeGetPrice)(client.debtMint));
340
357
  let tx = (0, umi_1.transactionBuilder)();
358
+ if (requiresRefreshBeforeRebalance(client)) {
359
+ tx = tx.add(client.refresh());
360
+ }
341
361
  if (flashLoan) {
342
362
  client.log("Flash loan details: ", flashLoan);
343
363
  const addFirstRebalance = values.amountUsdToDcaIn > 0;
@@ -368,10 +388,6 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
368
388
  client.rebalance("B", swapDetails, rebalanceType, undefined, targetLiqUtilizationRateBps),
369
389
  ]);
370
390
  }
371
- if (client.solautoPositionState.liqUtilizationRateBps >
372
- (0, numberUtils_1.getMaxLiqUtilizationRateBps)(client.solautoPositionState.maxLtvBps, client.solautoPositionState.liqThresholdBps, 0.01)) {
373
- tx = tx.prepend(client.refresh());
374
- }
375
391
  return {
376
392
  tx,
377
393
  lookupTableAddresses,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haven-fi/solauto-sdk",
3
- "version": "1.0.174",
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",
@@ -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,
@@ -625,13 +652,12 @@ export async function buildSolautoRebalanceTransaction(
625
652
  priceImpactBps
626
653
  );
627
654
 
628
- console.log(
629
- safeGetPrice(client.supplyMint),
630
- safeGetPrice(client.debtMint)
631
- )
632
-
633
655
  let tx = transactionBuilder();
634
656
 
657
+ if (requiresRefreshBeforeRebalance(client)) {
658
+ tx = tx.add(client.refresh());
659
+ }
660
+
635
661
  if (flashLoan) {
636
662
  client.log("Flash loan details: ", flashLoan);
637
663
  const addFirstRebalance = values.amountUsdToDcaIn > 0;
@@ -693,17 +719,6 @@ export async function buildSolautoRebalanceTransaction(
693
719
  ]);
694
720
  }
695
721
 
696
- if (
697
- client.solautoPositionState!.liqUtilizationRateBps >
698
- getMaxLiqUtilizationRateBps(
699
- client.solautoPositionState!.maxLtvBps,
700
- client.solautoPositionState!.liqThresholdBps,
701
- 0.01
702
- )
703
- ) {
704
- tx = tx.prepend(client.refresh());
705
- }
706
-
707
722
  return {
708
723
  tx,
709
724
  lookupTableAddresses,