@haven-fi/solauto-sdk 1.0.65 → 1.0.67
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/transactions/transactionUtils.js +1 -1
- package/dist/utils/numberUtils.d.ts +3 -3
- package/dist/utils/numberUtils.d.ts.map +1 -1
- package/dist/utils/numberUtils.js +8 -8
- package/dist/utils/solauto/rebalanceUtils.d.ts.map +1 -1
- package/dist/utils/solauto/rebalanceUtils.js +8 -9
- package/package.json +1 -1
- package/src/transactions/transactionUtils.ts +2 -2
- package/src/utils/numberUtils.ts +6 -6
- package/src/utils/solauto/rebalanceUtils.ts +11 -12
- package/tests/transactions/solautoMarginfi.ts +2 -2
@@ -366,7 +366,7 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
366
366
|
]);
|
367
367
|
}
|
368
368
|
if (client.solautoPositionState.liqUtilizationRateBps >
|
369
|
-
(0, numberUtils_1.
|
369
|
+
(0, numberUtils_1.getMaxLiqUtilizationRateBps)(client.solautoPositionState.maxLtvBps, client.solautoPositionState.liqThresholdBps)) {
|
370
370
|
tx = tx.prepend(client.refresh());
|
371
371
|
}
|
372
372
|
return {
|
@@ -6,7 +6,7 @@ export declare function toBps(value: number): number;
|
|
6
6
|
export declare function bytesToI80F48(bytes: number[]): number;
|
7
7
|
export declare function uint8ArrayToBigInt(uint8Array: Uint8Array): bigint;
|
8
8
|
export declare function getDebtAdjustmentUsd(liqThresholdBps: number, supplyUsd: number, debtUsd: number, targetLiqUtilizationRateBps: number, adjustmentFeeBps?: number): number;
|
9
|
-
export declare function
|
10
|
-
export declare function
|
11
|
-
export declare function
|
9
|
+
export declare function getMaxLiqUtilizationRateBps(maxLtvBps: number, liqThresholdBps: number): number;
|
10
|
+
export declare function maxRepayFromBps(maxLtvBps: number, liqThresholdBps: number): number;
|
11
|
+
export declare function maxRepayToBps(maxLtvBps: number, liqThresholdBps: number): number;
|
12
12
|
//# sourceMappingURL=numberUtils.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"numberUtils.d.ts","sourceRoot":"","sources":["../../src/utils/numberUtils.ts"],"names":[],"mappings":"AAEA,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,MAAM,CAM3G;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEhE;AAEH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEpE;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE3C;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAqBrD;AAED,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAajE;AAED,wBAAgB,oBAAoB,CAClC,eAAe,EAAE,MAAM,EACvB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,2BAA2B,EAAE,MAAM,EACnC,gBAAgB,CAAC,EAAE,MAAM,UAQ1B;AAED,wBAAgB,
|
1
|
+
{"version":3,"file":"numberUtils.d.ts","sourceRoot":"","sources":["../../src/utils/numberUtils.ts"],"names":[],"mappings":"AAEA,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,MAAM,CAM3G;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEhE;AAEH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEpE;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE3C;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAqBrD;AAED,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAajE;AAED,wBAAgB,oBAAoB,CAClC,eAAe,EAAE,MAAM,EACvB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,2BAA2B,EAAE,MAAM,EACnC,gBAAgB,CAAC,EAAE,MAAM,UAQ1B;AAED,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,MAAM,EACjB,eAAe,EAAE,MAAM,GACtB,MAAM,CAER;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,UAKzE;AAED,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,UAKvE"}
|
@@ -8,9 +8,9 @@ exports.toBps = toBps;
|
|
8
8
|
exports.bytesToI80F48 = bytesToI80F48;
|
9
9
|
exports.uint8ArrayToBigInt = uint8ArrayToBigInt;
|
10
10
|
exports.getDebtAdjustmentUsd = getDebtAdjustmentUsd;
|
11
|
-
exports.
|
12
|
-
exports.
|
13
|
-
exports.
|
11
|
+
exports.getMaxLiqUtilizationRateBps = getMaxLiqUtilizationRateBps;
|
12
|
+
exports.maxRepayFromBps = maxRepayFromBps;
|
13
|
+
exports.maxRepayToBps = maxRepayToBps;
|
14
14
|
const constants_1 = require("../constants");
|
15
15
|
function getLiqUtilzationRateBps(supplyUsd, debtUsd, liqThresholdBps) {
|
16
16
|
if (supplyUsd === 0) {
|
@@ -63,12 +63,12 @@ function getDebtAdjustmentUsd(liqThresholdBps, supplyUsd, debtUsd, targetLiqUtil
|
|
63
63
|
const debtAdjustmentUsd = (targetLiqUtilizationRate * supplyUsd * liqThreshold - debtUsd) / (1 - targetLiqUtilizationRate * (1 - adjustmentFee) * liqThreshold);
|
64
64
|
return debtAdjustmentUsd;
|
65
65
|
}
|
66
|
-
function
|
66
|
+
function getMaxLiqUtilizationRateBps(maxLtvBps, liqThresholdBps) {
|
67
67
|
return toBps((fromBps(maxLtvBps) - 0.015) / fromBps(liqThresholdBps)) - 1; // -1 to account for any rounding issues
|
68
68
|
}
|
69
|
-
function
|
70
|
-
return Math.min(9000,
|
69
|
+
function maxRepayFromBps(maxLtvBps, liqThresholdBps) {
|
70
|
+
return Math.min(9000, getMaxLiqUtilizationRateBps(maxLtvBps, liqThresholdBps - 1000));
|
71
71
|
}
|
72
|
-
function
|
73
|
-
return Math.min(
|
72
|
+
function maxRepayToBps(maxLtvBps, liqThresholdBps) {
|
73
|
+
return Math.min(maxRepayFromBps(maxLtvBps, liqThresholdBps) - constants_1.MAX_REPAY_GAP_BPS, getMaxLiqUtilizationRateBps(maxLtvBps, liqThresholdBps));
|
74
74
|
}
|
@@ -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,OAAO,EACP,aAAa,EAEb,yBAAyB,EAC1B,MAAM,iBAAiB,CAAC;AAQzB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;
|
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,OAAO,EACP,aAAa,EAEb,yBAAyB,EAC1B,MAAM,iBAAiB,CAAC;AAQzB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAiJjD,MAAM,WAAW,eAAe;IAC9B,kBAAkB,EAAE,OAAO,CAAC;IAC5B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,aAAa,EACpB,QAAQ,EAAE,yBAAyB,GAAG,SAAS,EAC/C,GAAG,EAAE,WAAW,GAAG,SAAS,EAC5B,OAAO,EAAE,OAAO,EAChB,eAAe,EAAE,MAAM,EACvB,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EACjB,2BAA2B,CAAC,EAAE,MAAM,EACpC,WAAW,CAAC,EAAE,MAAM,GACnB,eAAe,CAsEjB;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,SAAS,CAkE9B;AAED,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,cAAc,CAkChB"}
|
@@ -19,21 +19,20 @@ function getAdditionalAmountToDcaIn(dca) {
|
|
19
19
|
}
|
20
20
|
function getStandardTargetLiqUtilizationRateBps(state, settings) {
|
21
21
|
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(settings, (0, generalUtils_2.currentUnixSeconds)());
|
22
|
-
const repayFrom = adjustedSettings.repayToBps
|
23
|
-
const boostFrom = adjustedSettings.boostToBps
|
22
|
+
const repayFrom = adjustedSettings.repayToBps + adjustedSettings.repayGap;
|
23
|
+
const boostFrom = adjustedSettings.boostToBps - adjustedSettings.boostGap;
|
24
24
|
if (state.liqUtilizationRateBps < boostFrom) {
|
25
25
|
return adjustedSettings.boostToBps;
|
26
26
|
}
|
27
|
-
else if (state.liqUtilizationRateBps > repayFrom
|
28
|
-
repayFrom - state.liqUtilizationRateBps < repayFrom * 0.015) {
|
27
|
+
else if (state.liqUtilizationRateBps > repayFrom) {
|
29
28
|
return adjustedSettings.repayToBps;
|
30
29
|
}
|
31
30
|
else {
|
32
31
|
throw new Error("Invalid rebalance condition");
|
33
32
|
}
|
34
33
|
}
|
35
|
-
function targetLiqUtilizationRateBpsFromDCA(state, settings, dca) {
|
36
|
-
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(settings,
|
34
|
+
function targetLiqUtilizationRateBpsFromDCA(state, settings, dca, currentUnixTime) {
|
35
|
+
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(settings, currentUnixTime);
|
37
36
|
let targetRateBps = 0;
|
38
37
|
if (dca.debtToAddBaseUnit > BigInt(0)) {
|
39
38
|
targetRateBps = Math.max(state.liqUtilizationRateBps, adjustedSettings.boostToBps);
|
@@ -47,7 +46,7 @@ function isDcaRebalance(state, settings, dca, currentUnixTime) {
|
|
47
46
|
if (dca === undefined || dca.automation.targetPeriods === 0) {
|
48
47
|
return false;
|
49
48
|
}
|
50
|
-
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(settings,
|
49
|
+
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(settings, currentUnixTime);
|
51
50
|
if (state.liqUtilizationRateBps >
|
52
51
|
adjustedSettings.repayToBps + adjustedSettings.repayGap) {
|
53
52
|
return false;
|
@@ -68,7 +67,7 @@ function getTargetRateAndDcaAmount(state, settings, dca, currentUnixTime, target
|
|
68
67
|
}
|
69
68
|
if (isDcaRebalance(state, settings, dca, currentUnixTime)) {
|
70
69
|
const amountToDcaIn = getAdditionalAmountToDcaIn(dca);
|
71
|
-
const targetLiqUtilizationRateBps = targetLiqUtilizationRateBpsFromDCA(state, settings, dca);
|
70
|
+
const targetLiqUtilizationRateBps = targetLiqUtilizationRateBpsFromDCA(state, settings, dca, currentUnixTime);
|
72
71
|
return {
|
73
72
|
targetRateBps: targetLiqUtilizationRateBps,
|
74
73
|
amountToDcaIn,
|
@@ -134,7 +133,7 @@ function getFlashLoanDetails(client, values, jupQuote) {
|
|
134
133
|
const tempLiqUtilizationRateBps = (0, numberUtils_1.getLiqUtilzationRateBps)(supplyUsd, debtUsd, client.solautoPositionState.liqThresholdBps);
|
135
134
|
const requiresFlashLoan = supplyUsd <= 0 ||
|
136
135
|
tempLiqUtilizationRateBps >
|
137
|
-
(0, numberUtils_1.
|
136
|
+
(0, numberUtils_1.getMaxLiqUtilizationRateBps)(client.solautoPositionState.maxLtvBps, client.solautoPositionState.liqThresholdBps);
|
138
137
|
let flashLoanToken = undefined;
|
139
138
|
let flashLoanTokenPrice = 0;
|
140
139
|
if (values.increasingLeverage) {
|
package/package.json
CHANGED
@@ -45,7 +45,7 @@ import {
|
|
45
45
|
} from "../utils/generalUtils";
|
46
46
|
import { SolautoMarginfiClient } from "../clients/solautoMarginfiClient";
|
47
47
|
import {
|
48
|
-
|
48
|
+
getMaxLiqUtilizationRateBps,
|
49
49
|
uint8ArrayToBigInt,
|
50
50
|
} from "../utils/numberUtils";
|
51
51
|
import { eligibleForRebalance } from "../utils/solauto/generalUtils";
|
@@ -670,7 +670,7 @@ export async function buildSolautoRebalanceTransaction(
|
|
670
670
|
|
671
671
|
if (
|
672
672
|
client.solautoPositionState!.liqUtilizationRateBps >
|
673
|
-
|
673
|
+
getMaxLiqUtilizationRateBps(
|
674
674
|
client.solautoPositionState!.maxLtvBps,
|
675
675
|
client.solautoPositionState!.liqThresholdBps
|
676
676
|
)
|
package/src/utils/numberUtils.ts
CHANGED
@@ -77,23 +77,23 @@ export function getDebtAdjustmentUsd(
|
|
77
77
|
return debtAdjustmentUsd;
|
78
78
|
}
|
79
79
|
|
80
|
-
export function
|
80
|
+
export function getMaxLiqUtilizationRateBps(
|
81
81
|
maxLtvBps: number,
|
82
82
|
liqThresholdBps: number
|
83
83
|
): number {
|
84
84
|
return toBps((fromBps(maxLtvBps) - 0.015) / fromBps(liqThresholdBps)) - 1; // -1 to account for any rounding issues
|
85
85
|
}
|
86
86
|
|
87
|
-
export function
|
87
|
+
export function maxRepayFromBps(maxLtvBps: number, liqThresholdBps: number) {
|
88
88
|
return Math.min(
|
89
89
|
9000,
|
90
|
-
|
90
|
+
getMaxLiqUtilizationRateBps(maxLtvBps, liqThresholdBps - 1000)
|
91
91
|
);
|
92
92
|
}
|
93
93
|
|
94
|
-
export function
|
94
|
+
export function maxRepayToBps(maxLtvBps: number, liqThresholdBps: number) {
|
95
95
|
return Math.min(
|
96
|
-
|
97
|
-
|
96
|
+
maxRepayFromBps(maxLtvBps, liqThresholdBps) - MAX_REPAY_GAP_BPS,
|
97
|
+
getMaxLiqUtilizationRateBps(maxLtvBps, liqThresholdBps)
|
98
98
|
);
|
99
99
|
}
|
@@ -22,7 +22,7 @@ import {
|
|
22
22
|
fromBps,
|
23
23
|
getDebtAdjustmentUsd,
|
24
24
|
getLiqUtilzationRateBps,
|
25
|
-
|
25
|
+
getMaxLiqUtilizationRateBps,
|
26
26
|
toBaseUnit,
|
27
27
|
} from "../numberUtils";
|
28
28
|
import { USD_DECIMALS } from "../../constants/generalAccounts";
|
@@ -57,15 +57,12 @@ function getStandardTargetLiqUtilizationRateBps(
|
|
57
57
|
currentUnixSeconds()
|
58
58
|
);
|
59
59
|
|
60
|
-
const repayFrom = adjustedSettings.repayToBps
|
61
|
-
const boostFrom = adjustedSettings.boostToBps
|
60
|
+
const repayFrom = adjustedSettings.repayToBps + adjustedSettings.repayGap;
|
61
|
+
const boostFrom = adjustedSettings.boostToBps - adjustedSettings.boostGap;
|
62
62
|
|
63
63
|
if (state.liqUtilizationRateBps < boostFrom) {
|
64
64
|
return adjustedSettings.boostToBps;
|
65
|
-
} else if (
|
66
|
-
state.liqUtilizationRateBps > repayFrom ||
|
67
|
-
repayFrom - state.liqUtilizationRateBps < repayFrom * 0.015
|
68
|
-
) {
|
65
|
+
} else if (state.liqUtilizationRateBps > repayFrom) {
|
69
66
|
return adjustedSettings.repayToBps;
|
70
67
|
} else {
|
71
68
|
throw new Error("Invalid rebalance condition");
|
@@ -75,11 +72,12 @@ function getStandardTargetLiqUtilizationRateBps(
|
|
75
72
|
function targetLiqUtilizationRateBpsFromDCA(
|
76
73
|
state: PositionState,
|
77
74
|
settings: SolautoSettingsParameters,
|
78
|
-
dca: DCASettings
|
75
|
+
dca: DCASettings,
|
76
|
+
currentUnixTime: number
|
79
77
|
) {
|
80
78
|
const adjustedSettings = getAdjustedSettingsFromAutomation(
|
81
79
|
settings,
|
82
|
-
|
80
|
+
currentUnixTime
|
83
81
|
);
|
84
82
|
|
85
83
|
let targetRateBps = 0;
|
@@ -106,7 +104,7 @@ function isDcaRebalance(
|
|
106
104
|
|
107
105
|
const adjustedSettings = getAdjustedSettingsFromAutomation(
|
108
106
|
settings,
|
109
|
-
|
107
|
+
currentUnixTime
|
110
108
|
);
|
111
109
|
|
112
110
|
if (
|
@@ -147,7 +145,8 @@ function getTargetRateAndDcaAmount(
|
|
147
145
|
const targetLiqUtilizationRateBps = targetLiqUtilizationRateBpsFromDCA(
|
148
146
|
state,
|
149
147
|
settings,
|
150
|
-
dca
|
148
|
+
dca!,
|
149
|
+
currentUnixTime
|
151
150
|
);
|
152
151
|
|
153
152
|
return {
|
@@ -289,7 +288,7 @@ export function getFlashLoanDetails(
|
|
289
288
|
const requiresFlashLoan =
|
290
289
|
supplyUsd <= 0 ||
|
291
290
|
tempLiqUtilizationRateBps >
|
292
|
-
|
291
|
+
getMaxLiqUtilizationRateBps(
|
293
292
|
client.solautoPositionState!.maxLtvBps,
|
294
293
|
client.solautoPositionState!.liqThresholdBps
|
295
294
|
);
|
@@ -10,7 +10,7 @@ import {
|
|
10
10
|
SolautoSettingsParametersInpArgs,
|
11
11
|
} from "../../src/generated";
|
12
12
|
import { buildSolautoRebalanceTransaction } from "../../src/transactions/transactionUtils";
|
13
|
-
import {
|
13
|
+
import { getMaxLiqUtilizationRateBps, toBaseUnit } from "../../src/utils/numberUtils";
|
14
14
|
import { NATIVE_MINT } from "@solana/spl-token";
|
15
15
|
import { getTokenPrices } from "../../src/utils/generalUtils";
|
16
16
|
import {
|
@@ -82,7 +82,7 @@ describe("Solauto Marginfi tests", async () => {
|
|
82
82
|
// );
|
83
83
|
// }
|
84
84
|
|
85
|
-
// const maxLiqRate =
|
85
|
+
// const maxLiqRate = getMaxLiqUtilizationRateBps(client.solautoPositionState!.maxLtvBps, client.solautoPositionState!.liqThresholdBps);
|
86
86
|
// transactionItems.push(
|
87
87
|
// new TransactionItem(
|
88
88
|
// async () => ({
|