@haven-fi/solauto-sdk 1.0.174 → 1.0.176
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/transactions/transactionUtils.d.ts +1 -0
- package/dist/transactions/transactionUtils.d.ts.map +1 -1
- package/dist/transactions/transactionUtils.js +24 -5
- package/dist/transactions/transactionsManager.d.ts.map +1 -1
- package/dist/transactions/transactionsManager.js +18 -0
- package/package.json +1 -1
- package/src/transactions/transactionUtils.ts +35 -16
- package/src/transactions/transactionsManager.ts +28 -0
- package/tests/transactions/solautoMarginfi.ts +2 -2
@@ -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,
|
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,WA6BnE;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,26 @@ 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
|
+
if (client.livePositionUpdates.supplyAdjustment > BigInt(0) || client.livePositionUpdates.debtAdjustment > BigInt(0)) {
|
333
|
+
return false;
|
334
|
+
}
|
335
|
+
const oldSupply = client.solautoPositionData.state.supply.amountUsed.baseUnit;
|
336
|
+
const oldDebt = client.solautoPositionData.state.debt.amountUsed.baseUnit;
|
337
|
+
const supplyDiff = (client.solautoPositionState?.supply.amountUsed.baseUnit ?? BigInt(0)) - oldSupply;
|
338
|
+
const debtDiff = (client.solautoPositionState?.debt.amountUsed.baseUnit ?? BigInt(0)) - oldDebt;
|
339
|
+
if (Math.abs(Number(supplyDiff)) / Number(oldSupply) >= 0.01 || Math.abs(Number(debtDiff)) / Number(oldDebt) >= 0.01) {
|
340
|
+
return true;
|
341
|
+
}
|
342
|
+
}
|
343
|
+
// Rebalance ix will already refresh internally if position is self managed, has automation to update, or position state last updated >= 1 day ago
|
344
|
+
return false;
|
345
|
+
}
|
325
346
|
async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRateBps, attemptNum) {
|
326
347
|
client.solautoPositionState = await client.getFreshPositionState();
|
327
348
|
if ((client.solautoPositionState?.supply.amountUsed.baseUnit === BigInt(0) &&
|
@@ -336,8 +357,10 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
336
357
|
const swapDetails = (0, rebalanceUtils_1.getJupSwapRebalanceDetails)(client, values, targetLiqUtilizationRateBps, attemptNum);
|
337
358
|
const { jupQuote, priceImpactBps, lookupTableAddresses, setupInstructions, tokenLedgerIx, swapIx, } = await (0, jupiterUtils_1.getJupSwapTransaction)(client.signer, swapDetails, attemptNum);
|
338
359
|
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
360
|
let tx = (0, umi_1.transactionBuilder)();
|
361
|
+
if (requiresRefreshBeforeRebalance(client)) {
|
362
|
+
tx = tx.add(client.refresh());
|
363
|
+
}
|
341
364
|
if (flashLoan) {
|
342
365
|
client.log("Flash loan details: ", flashLoan);
|
343
366
|
const addFirstRebalance = values.amountUsdToDcaIn > 0;
|
@@ -368,10 +391,6 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
368
391
|
client.rebalance("B", swapDetails, rebalanceType, undefined, targetLiqUtilizationRateBps),
|
369
392
|
]);
|
370
393
|
}
|
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
394
|
return {
|
376
395
|
tx,
|
377
396
|
lookupTableAddresses,
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionsManager.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,kBAAkB,EAEnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAKzD,OAAO,EACL,aAAa,EAEd,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAClE,OAAO,EAAE,oBAAoB,EAAa,MAAM,YAAY,CAAC;
|
1
|
+
{"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionsManager.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,kBAAkB,EAEnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAKzD,OAAO,EACL,aAAa,EAEd,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAClE,OAAO,EAAE,oBAAoB,EAAa,MAAM,YAAY,CAAC;AA0C7D,qBAAa,eAAe;IAKjB,OAAO,EAAE,CACd,UAAU,EAAE,MAAM,KACf,OAAO,CACV;QAAE,EAAE,EAAE,kBAAkB,CAAC;QAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,SAAS,CACxE;IACM,IAAI,CAAC,EAAE,MAAM;IATtB,oBAAoB,EAAG,MAAM,EAAE,CAAC;IAChC,EAAE,CAAC,EAAE,kBAAkB,CAAC;gBAGf,OAAO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,YAAY,EAAE,eAAe,EAAE,EAC/B,eAAe,CAAC,EAAE,kBAAkB,GACnC,OAAO,CAAC,0BAA0B,CAAC;IAyEzB,IAAI,CACf,KAAK,EAAE,eAAe,EAAE,EACxB,eAAe,CAAC,EAAE,kBAAkB,EACpC,WAAW,CAAC,EAAE,OAAO,GACpB,OAAO,CAAC,0BAA0B,CAAC;YA0GxB,eAAe;CA2D9B"}
|
@@ -9,6 +9,7 @@ const umi_1 = require("@metaplex-foundation/umi");
|
|
9
9
|
const solanaUtils_1 = require("../utils/solanaUtils");
|
10
10
|
const generalUtils_1 = require("../utils/generalUtils");
|
11
11
|
const transactionUtils_1 = require("./transactionUtils");
|
12
|
+
const generated_1 = require("../generated");
|
12
13
|
// import { sendJitoBundledTransactions } from "../utils/jitoUtils";
|
13
14
|
class LookupTables {
|
14
15
|
constructor(defaultLuts, umi) {
|
@@ -331,6 +332,23 @@ class TransactionsManager {
|
|
331
332
|
this.updateStatus(txName, TransactionStatus.Successful, attemptNum, txSig ? bs58_1.default.encode(txSig) : undefined);
|
332
333
|
}
|
333
334
|
catch (e) {
|
335
|
+
try {
|
336
|
+
if (typeof e === "object" && e["InstructionError"]) {
|
337
|
+
const err = e["InstructionError"];
|
338
|
+
const errIx = err[0];
|
339
|
+
const errCode = err[1]["Custom"];
|
340
|
+
const solautoProgram = this.txHandler.umi.programs.get(generated_1.SOLAUTO_PROGRAM_ID);
|
341
|
+
const invalidRebalanceError = new generated_1.InvalidRebalanceConditionError(solautoProgram);
|
342
|
+
if (tx.getInstructions()[Math.max(0, errIx - 2)].programId ===
|
343
|
+
generated_1.SOLAUTO_PROGRAM_ID &&
|
344
|
+
solautoProgram.getErrorFromCode(errCode)?.name ===
|
345
|
+
invalidRebalanceError.name) {
|
346
|
+
this.updateStatus(txName, TransactionStatus.Skipped, attemptNum);
|
347
|
+
return;
|
348
|
+
}
|
349
|
+
}
|
350
|
+
}
|
351
|
+
catch { }
|
334
352
|
this.updateStatus(txName, TransactionStatus.Failed, attemptNum);
|
335
353
|
throw e;
|
336
354
|
}
|
package/package.json
CHANGED
@@ -566,6 +566,37 @@ 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
|
+
if (client.livePositionUpdates.supplyAdjustment > BigInt(0) || client.livePositionUpdates.debtAdjustment > BigInt(0)) {
|
581
|
+
return false;
|
582
|
+
}
|
583
|
+
|
584
|
+
const oldSupply = client.solautoPositionData.state.supply.amountUsed.baseUnit;
|
585
|
+
const oldDebt = client.solautoPositionData.state.debt.amountUsed.baseUnit;
|
586
|
+
|
587
|
+
const supplyDiff = (client.solautoPositionState?.supply.amountUsed.baseUnit ?? BigInt(0)) - oldSupply;
|
588
|
+
const debtDiff = (client.solautoPositionState?.debt.amountUsed.baseUnit ?? BigInt(0)) - oldDebt;
|
589
|
+
|
590
|
+
if (Math.abs(Number(supplyDiff)) / Number(oldSupply) >= 0.01 || Math.abs(Number(debtDiff)) / Number(oldDebt) >= 0.01) {
|
591
|
+
return true;
|
592
|
+
}
|
593
|
+
}
|
594
|
+
|
595
|
+
// Rebalance ix will already refresh internally if position is self managed, has automation to update, or position state last updated >= 1 day ago
|
596
|
+
|
597
|
+
return false;
|
598
|
+
}
|
599
|
+
|
569
600
|
export async function buildSolautoRebalanceTransaction(
|
570
601
|
client: SolautoClient,
|
571
602
|
targetLiqUtilizationRateBps?: number,
|
@@ -625,13 +656,12 @@ export async function buildSolautoRebalanceTransaction(
|
|
625
656
|
priceImpactBps
|
626
657
|
);
|
627
658
|
|
628
|
-
console.log(
|
629
|
-
safeGetPrice(client.supplyMint),
|
630
|
-
safeGetPrice(client.debtMint)
|
631
|
-
)
|
632
|
-
|
633
659
|
let tx = transactionBuilder();
|
634
660
|
|
661
|
+
if (requiresRefreshBeforeRebalance(client)) {
|
662
|
+
tx = tx.add(client.refresh());
|
663
|
+
}
|
664
|
+
|
635
665
|
if (flashLoan) {
|
636
666
|
client.log("Flash loan details: ", flashLoan);
|
637
667
|
const addFirstRebalance = values.amountUsdToDcaIn > 0;
|
@@ -693,17 +723,6 @@ export async function buildSolautoRebalanceTransaction(
|
|
693
723
|
]);
|
694
724
|
}
|
695
725
|
|
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
726
|
return {
|
708
727
|
tx,
|
709
728
|
lookupTableAddresses,
|
@@ -17,6 +17,10 @@ import {
|
|
17
17
|
import { getTransactionChores } from "./transactionUtils";
|
18
18
|
import { PriorityFeeSetting, TransactionRunType } from "../types";
|
19
19
|
import { ReferralStateManager, TxHandler } from "../clients";
|
20
|
+
import {
|
21
|
+
InvalidRebalanceConditionError,
|
22
|
+
SOLAUTO_PROGRAM_ID,
|
23
|
+
} from "../generated";
|
20
24
|
// import { sendJitoBundledTransactions } from "../utils/jitoUtils";
|
21
25
|
|
22
26
|
class LookupTables {
|
@@ -521,6 +525,30 @@ export class TransactionsManager {
|
|
521
525
|
txSig ? bs58.encode(txSig) : undefined
|
522
526
|
);
|
523
527
|
} catch (e) {
|
528
|
+
try {
|
529
|
+
if (typeof e === "object" && (e as any)["InstructionError"]) {
|
530
|
+
const err = (e as any)["InstructionError"];
|
531
|
+
const errIx = err[0];
|
532
|
+
const errCode = err[1]["Custom"];
|
533
|
+
|
534
|
+
const solautoProgram =
|
535
|
+
this.txHandler.umi.programs.get(SOLAUTO_PROGRAM_ID);
|
536
|
+
const invalidRebalanceError = new InvalidRebalanceConditionError(
|
537
|
+
solautoProgram
|
538
|
+
);
|
539
|
+
|
540
|
+
if (
|
541
|
+
tx.getInstructions()[Math.max(0, errIx - 2)].programId ===
|
542
|
+
SOLAUTO_PROGRAM_ID &&
|
543
|
+
solautoProgram.getErrorFromCode(errCode)?.name ===
|
544
|
+
invalidRebalanceError.name
|
545
|
+
) {
|
546
|
+
this.updateStatus(txName, TransactionStatus.Skipped, attemptNum);
|
547
|
+
return;
|
548
|
+
}
|
549
|
+
}
|
550
|
+
} catch {}
|
551
|
+
|
524
552
|
this.updateStatus(txName, TransactionStatus.Failed, attemptNum);
|
525
553
|
throw e;
|
526
554
|
}
|
@@ -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 =
|
27
|
+
const payForTransactions = false;
|
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("
|
43
|
+
authority: new PublicKey("AprYCPiVeKMCgjQ2ZufwChMzvQ5kFjJo2ekTLSkXsQDm")
|
44
44
|
// marginfiAccount: new PublicKey(
|
45
45
|
// "4nNvUXF5YqHFcH2nGweSiuvy1ct7V5FXfoCLKFYUN36z"
|
46
46
|
// ),
|