@haven-fi/solauto-sdk 1.0.58 → 1.0.60
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/clients/solautoClient.d.ts.map +1 -1
- package/dist/transactions/transactionUtils.d.ts.map +1 -1
- package/dist/transactions/transactionUtils.js +3 -2
- package/dist/utils/solauto/generalUtils.d.ts +20 -5
- package/dist/utils/solauto/generalUtils.d.ts.map +1 -1
- package/dist/utils/solauto/generalUtils.js +72 -13
- package/dist/utils/solauto/rebalanceUtils.d.ts +3 -1
- package/dist/utils/solauto/rebalanceUtils.d.ts.map +1 -1
- package/dist/utils/solauto/rebalanceUtils.js +47 -58
- package/package.json +1 -1
- package/src/clients/solautoClient.ts +0 -3
- package/src/transactions/transactionUtils.ts +15 -3
- package/src/utils/solauto/generalUtils.ts +123 -21
- package/src/utils/solauto/rebalanceUtils.ts +91 -80
- package/tests/shared.ts +6 -3
- package/tests/unit/rebalanceCalculations.ts +77 -46
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"solautoClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoClient.ts"],"names":[],"mappings":"AAAA,OAAO,gCAAgC,CAAC;AACxC,OAAO,EAEL,UAAU,EACV,SAAS,EACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,MAAM,EACN,kBAAkB,EAClB,GAAG,EAQJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACL,aAAa,EAEd,MAAM,iDAAiD,CAAC;AACzD,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,wBAAwB,EACxB,yBAAyB,EACzB,gCAAgC,EAChC,sBAAsB,EASvB,MAAM,cAAc,CAAC;
|
1
|
+
{"version":3,"file":"solautoClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoClient.ts"],"names":[],"mappings":"AAAA,OAAO,gCAAgC,CAAC;AACxC,OAAO,EAEL,UAAU,EACV,SAAS,EACV,MAAM,iBAAiB,CAAC;AACzB,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GACvB,kBAAkB;IAuBrB,gBAAgB,CAAC,IAAI,EAAE,sBAAsB,GAAG,kBAAkB;IA2DlE,eAAe,IAAI,kBAAkB;IAWrC,WAAW,IAAI,kBAAkB;IA4BjC,QAAQ,CAAC,OAAO,IAAI,kBAAkB;IAEtC,mBAAmB,CAAC,IAAI,EAAE,iBAAiB,GAAG,kBAAkB;IAuFhE,QAAQ,CAAC,WAAW,CAClB,gBAAgB,EAAE,gBAAgB,EAClC,uBAAuB,EAAE,SAAS,GACjC,kBAAkB;IAErB,QAAQ,CAAC,UAAU,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,kBAAkB;IAE3E,QAAQ,CAAC,SAAS,CAChB,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;IAEf,qBAAqB,IAAI,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC;CAYlE"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EAGL,OAAO,IAAI,eAAe,EAC3B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,
|
1
|
+
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EAGL,OAAO,IAAI,eAAe,EAC3B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAGL,aAAa,EASd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAuMzD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CA8G7B;AAkLD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA4BnD;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,CA0HA;AAED,wBAAsB,gCAAgC,CACpD,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,eAAe,GAC5B,OAAO,CAAC,CAAC,kBAAkB,EAAE,MAAM,EAAE,CAAC,CAAC,CA8BzC"}
|
@@ -17,6 +17,7 @@ const numberUtils_1 = require("../utils/numberUtils");
|
|
17
17
|
const generalUtils_2 = require("../utils/solauto/generalUtils");
|
18
18
|
const accountUtils_1 = require("../utils/accountUtils");
|
19
19
|
const marginfi_sdk_1 = require("../marginfi-sdk");
|
20
|
+
const constants_1 = require("../constants");
|
20
21
|
function getWSolUsage(client, solautoActions, initiatingDcaIn, cancellingDcaIn) {
|
21
22
|
const supplyIsWsol = client.supplyMint.equals(spl_token_1.NATIVE_MINT);
|
22
23
|
const debtIsWsol = client.debtMint.equals(spl_token_1.NATIVE_MINT);
|
@@ -324,11 +325,11 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
324
325
|
(targetLiqUtilizationRateBps === undefined &&
|
325
326
|
!(0, generalUtils_2.eligibleForRebalance)(client.solautoPositionState, client.livePositionUpdates.settings ??
|
326
327
|
client.solautoPositionData?.position.settingParams, client.livePositionUpdates.activeDca ??
|
327
|
-
client.solautoPositionData?.position.dca))) {
|
328
|
+
client.solautoPositionData?.position.dca, (0, generalUtils_1.currentUnixSeconds)()))) {
|
328
329
|
client.log("Not eligible for a rebalance");
|
329
330
|
return undefined;
|
330
331
|
}
|
331
|
-
const values = (0, rebalanceUtils_1.getRebalanceValues)(client, targetLiqUtilizationRateBps);
|
332
|
+
const values = (0, rebalanceUtils_1.getRebalanceValues)(client.solautoPositionState, client.solautoPositionSettings(), client.solautoPositionActiveDca(), client.solautoPositionData?.feeType ?? generated_1.FeeType.Small, (0, generalUtils_1.currentUnixSeconds)(), constants_1.PRICES[client.supplyMint.toString()].price, constants_1.PRICES[client.debtMint.toString()].price, targetLiqUtilizationRateBps);
|
332
333
|
client.log("Rebalance values: ", values);
|
333
334
|
const swapDetails = (0, rebalanceUtils_1.getJupSwapRebalanceDetails)(client, values, targetLiqUtilizationRateBps, attemptNum);
|
334
335
|
const { jupQuote, lookupTableAddresses, setupInstructions, tokenLedgerIx, swapIx, } = await (0, jupiterUtils_1.getJupSwapTransaction)(client.signer, swapDetails, attemptNum);
|
@@ -3,21 +3,36 @@ import { Umi } from "@metaplex-foundation/umi";
|
|
3
3
|
import { AutomationSettings, DCASettings, DCASettingsInpArgs, FeeType, LendingPlatform, PositionState, SolautoSettingsParameters, SolautoSettingsParametersInpArgs } from "../../generated";
|
4
4
|
import { RebalanceAction, SolautoPositionDetails } from "../../types/solauto";
|
5
5
|
export declare function nextAutomationPeriodTimestamp(automation: AutomationSettings): number;
|
6
|
-
export declare function eligibleForNextAutomationPeriod(automation: AutomationSettings): boolean;
|
6
|
+
export declare function eligibleForNextAutomationPeriod(automation: AutomationSettings, currentUnixTime: number): boolean;
|
7
7
|
export declare function getUpdatedValueFromAutomation(currValue: number, targetValue: number, automation: AutomationSettings, currentUnixTimestamp: number): number;
|
8
|
-
export declare function getAdjustedSettingsFromAutomation(settings: SolautoSettingsParameters,
|
8
|
+
export declare function getAdjustedSettingsFromAutomation(settings: SolautoSettingsParameters, currentUnixTime: number): SolautoSettingsParameters;
|
9
9
|
export declare function getSolautoFeesBps(isReferred: boolean, feeType: FeeType): {
|
10
10
|
solauto: number;
|
11
11
|
referrer: number;
|
12
12
|
total: number;
|
13
13
|
};
|
14
|
-
export declare function eligibleForRebalance(positionState: PositionState, positionSettings: SolautoSettingsParameters, positionDca: DCASettings): RebalanceAction | undefined;
|
15
|
-
export declare function eligibleForRefresh(positionState: PositionState, positionSettings: SolautoSettingsParameters): boolean;
|
14
|
+
export declare function eligibleForRebalance(positionState: PositionState, positionSettings: SolautoSettingsParameters, positionDca: DCASettings, currentUnixSecs: number): RebalanceAction | undefined;
|
15
|
+
export declare function eligibleForRefresh(positionState: PositionState, positionSettings: SolautoSettingsParameters, currentUnixTime: number): boolean;
|
16
16
|
export declare function getSolautoManagedPositions(umi: Umi, authority?: PublicKey): Promise<SolautoPositionDetails[]>;
|
17
17
|
export declare function getAllReferralStates(umi: Umi): Promise<PublicKey[]>;
|
18
18
|
export declare function getReferralsByUser(umi: Umi, user: PublicKey): Promise<PublicKey[]>;
|
19
19
|
export declare function getAllPositionsByAuthority(umi: Umi, user: PublicKey): Promise<SolautoPositionDetails[]>;
|
20
|
-
|
20
|
+
interface GetLatestStateProps {
|
21
|
+
state: PositionState;
|
22
|
+
umi?: Umi;
|
23
|
+
protocolAccount?: PublicKey;
|
24
|
+
lendingPlatform?: LendingPlatform;
|
25
|
+
supplyPrice?: number;
|
26
|
+
debtPrice?: number;
|
27
|
+
}
|
28
|
+
export declare function positionStateWithPrices({ state, supplyPrice, debtPrice, umi, protocolAccount, lendingPlatform, }: GetLatestStateProps): Promise<PositionState | undefined>;
|
29
|
+
interface AssetProps {
|
30
|
+
amountUsedBaseUnit: bigint;
|
31
|
+
decimals: number;
|
32
|
+
price: number;
|
33
|
+
mint: PublicKey;
|
34
|
+
}
|
35
|
+
export declare function createFakePositionState(supply: AssetProps, debt: AssetProps, maxLtvBps: number, liqThresholdBps: number): PositionState;
|
21
36
|
type PositionAdjustment = {
|
22
37
|
type: "supply";
|
23
38
|
value: bigint;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"generalUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/generalUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,
|
1
|
+
{"version":3,"file":"generalUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/generalUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAA+B,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,kBAAkB,EAClB,OAAO,EACP,eAAe,EACf,aAAa,EAEb,yBAAyB,EACzB,gCAAgC,EAIjC,MAAM,iBAAiB,CAAC;AAczB,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAgB9E,wBAAgB,6BAA6B,CAC3C,UAAU,EAAE,kBAAkB,GAC7B,MAAM,CAKR;AAED,wBAAgB,+BAA+B,CAC7C,UAAU,EAAE,kBAAkB,EAC9B,eAAe,EAAE,MAAM,GACtB,OAAO,CAET;AAED,wBAAgB,6BAA6B,CAC3C,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,kBAAkB,EAC9B,oBAAoB,EAAE,MAAM,UAY7B;AAED,wBAAgB,iCAAiC,CAC/C,QAAQ,EAAE,yBAAyB,EACnC,eAAe,EAAE,MAAM,GACtB,yBAAyB,CAgB3B;AAED,wBAAgB,iBAAiB,CAC/B,UAAU,EAAE,OAAO,EACnB,OAAO,EAAE,OAAO,GACf;IACD,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,M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,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,SAAS,CAAC;CACjB;AAED,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,UAAU,EAClB,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,MAAM,EACjB,eAAe,EAAE,MAAM,GACtB,aAAa,CA8Df;AAED,KAAK,kBAAkB,GACnB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC/B;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACpC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,gCAAgC,CAAA;CAAE,GAC7D;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,kBAAkB,CAAA;CAAE,CAAC;AAE/C,qBAAa,mBAAmB;IACvB,gBAAgB,EAAE,MAAM,CAAa;IACrC,cAAc,EAAE,MAAM,CAAa;IACnC,uBAAuB,EAAE,MAAM,CAAa;IAC5C,QAAQ,EAAE,yBAAyB,GAAG,SAAS,CAAa;IAC5D,SAAS,EAAE,WAAW,GAAG,SAAS,CAAa;IAEtD,GAAG,CAAC,MAAM,EAAE,kBAAkB;IAyD9B,KAAK;IAQL,UAAU,IAAI,OAAO;CAQtB"}
|
@@ -13,6 +13,7 @@ exports.getAllReferralStates = getAllReferralStates;
|
|
13
13
|
exports.getReferralsByUser = getReferralsByUser;
|
14
14
|
exports.getAllPositionsByAuthority = getAllPositionsByAuthority;
|
15
15
|
exports.positionStateWithPrices = positionStateWithPrices;
|
16
|
+
exports.createFakePositionState = createFakePositionState;
|
16
17
|
const web3_js_1 = require("@solana/web3.js");
|
17
18
|
const umi_1 = require("@metaplex-foundation/umi");
|
18
19
|
const generated_1 = require("../../generated");
|
@@ -33,8 +34,8 @@ function nextAutomationPeriodTimestamp(automation) {
|
|
33
34
|
: Number(automation.unixStartDate) +
|
34
35
|
automation.periodsPassed * Number(automation.intervalSeconds);
|
35
36
|
}
|
36
|
-
function eligibleForNextAutomationPeriod(automation) {
|
37
|
-
return
|
37
|
+
function eligibleForNextAutomationPeriod(automation, currentUnixTime) {
|
38
|
+
return currentUnixTime >= nextAutomationPeriodTimestamp(automation);
|
38
39
|
}
|
39
40
|
function getUpdatedValueFromAutomation(currValue, targetValue, automation, currentUnixTimestamp) {
|
40
41
|
const currRateDiff = currValue - targetValue;
|
@@ -44,10 +45,10 @@ function getUpdatedValueFromAutomation(currValue, targetValue, automation, curre
|
|
44
45
|
const newValue = currValue - currRateDiff * progressPct;
|
45
46
|
return newValue;
|
46
47
|
}
|
47
|
-
function getAdjustedSettingsFromAutomation(settings,
|
48
|
+
function getAdjustedSettingsFromAutomation(settings, currentUnixTime) {
|
48
49
|
const boostToBps = settings.automation.targetPeriods > 0 &&
|
49
|
-
eligibleForNextAutomationPeriod(settings.automation)
|
50
|
-
? getUpdatedValueFromAutomation(settings.boostToBps, settings.targetBoostToBps, settings.automation,
|
50
|
+
eligibleForNextAutomationPeriod(settings.automation, currentUnixTime)
|
51
|
+
? getUpdatedValueFromAutomation(settings.boostToBps, settings.targetBoostToBps, settings.automation, currentUnixTime)
|
51
52
|
: settings.boostToBps;
|
52
53
|
return {
|
53
54
|
...settings,
|
@@ -66,17 +67,17 @@ function getSolautoFeesBps(isReferred, feeType) {
|
|
66
67
|
total: fees,
|
67
68
|
};
|
68
69
|
}
|
69
|
-
function eligibleForRebalance(positionState, positionSettings, positionDca) {
|
70
|
+
function eligibleForRebalance(positionState, positionSettings, positionDca, currentUnixSecs) {
|
70
71
|
if (positionDca.automation.targetPeriods > 0 &&
|
71
|
-
eligibleForNextAutomationPeriod(positionDca.automation)) {
|
72
|
+
eligibleForNextAutomationPeriod(positionDca.automation, currentUnixSecs)) {
|
72
73
|
return "dca";
|
73
74
|
}
|
74
75
|
if (positionState.supply.amountUsed.baseUnit === BigInt(0)) {
|
75
76
|
return undefined;
|
76
77
|
}
|
77
|
-
const boostToBps = eligibleForRefresh(positionState, positionSettings) &&
|
78
|
+
const boostToBps = eligibleForRefresh(positionState, positionSettings, currentUnixSecs) &&
|
78
79
|
positionSettings.automation.targetPeriods > 0
|
79
|
-
? getUpdatedValueFromAutomation(positionSettings.boostToBps, positionSettings.targetBoostToBps, positionSettings.automation,
|
80
|
+
? getUpdatedValueFromAutomation(positionSettings.boostToBps, positionSettings.targetBoostToBps, positionSettings.automation, currentUnixSecs)
|
80
81
|
: positionSettings.boostToBps;
|
81
82
|
const repayFrom = positionSettings.repayToBps + positionSettings.repayGap;
|
82
83
|
const boostFrom = boostToBps - positionSettings.boostGap;
|
@@ -88,9 +89,9 @@ function eligibleForRebalance(positionState, positionSettings, positionDca) {
|
|
88
89
|
}
|
89
90
|
return undefined;
|
90
91
|
}
|
91
|
-
function eligibleForRefresh(positionState, positionSettings) {
|
92
|
+
function eligibleForRefresh(positionState, positionSettings, currentUnixTime) {
|
92
93
|
if (positionSettings.automation.targetPeriods > 0) {
|
93
|
-
return eligibleForNextAutomationPeriod(positionSettings.automation);
|
94
|
+
return eligibleForNextAutomationPeriod(positionSettings.automation, currentUnixTime);
|
94
95
|
}
|
95
96
|
else {
|
96
97
|
return ((0, generalUtils_1.currentUnixSeconds)() - Number(positionState.lastUpdated) >
|
@@ -211,8 +212,13 @@ async function getAllPositionsByAuthority(umi, user) {
|
|
211
212
|
// TODO support other platforms
|
212
213
|
return allPositions;
|
213
214
|
}
|
214
|
-
async function positionStateWithPrices(
|
215
|
+
async function positionStateWithPrices({ state, supplyPrice, debtPrice, umi, protocolAccount, lendingPlatform, }) {
|
215
216
|
if ((0, generalUtils_1.currentUnixSeconds)() - Number(state.lastUpdated) > 60 * 60 * 24 * 7) {
|
217
|
+
if (umi === undefined ||
|
218
|
+
protocolAccount === undefined ||
|
219
|
+
lendingPlatform === undefined) {
|
220
|
+
throw new Error("Missing required parameters");
|
221
|
+
}
|
216
222
|
if (lendingPlatform === generated_1.LendingPlatform.Marginfi) {
|
217
223
|
return await (0, marginfiUtils_1.getMarginfiAccountPositionState)(umi, protocolAccount, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.supply.mint), (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.debt.mint));
|
218
224
|
}
|
@@ -234,7 +240,7 @@ async function positionStateWithPrices(umi, state, protocolAccount, lendingPlatf
|
|
234
240
|
...state,
|
235
241
|
liqUtilizationRateBps: (0, numberUtils_1.getLiqUtilzationRateBps)(supplyUsd, debtUsd, state.liqThresholdBps),
|
236
242
|
netWorth: {
|
237
|
-
|
243
|
+
baseUnit: (0, numberUtils_1.toBaseUnit)((supplyUsd - debtUsd) / supplyPrice, state.supply.decimals),
|
238
244
|
baseAmountUsdValue: (0, numberUtils_1.toBaseUnit)(supplyUsd - debtUsd, constants_1.USD_DECIMALS),
|
239
245
|
},
|
240
246
|
supply: {
|
@@ -253,6 +259,59 @@ async function positionStateWithPrices(umi, state, protocolAccount, lendingPlatf
|
|
253
259
|
},
|
254
260
|
};
|
255
261
|
}
|
262
|
+
function createFakePositionState(supply, debt, maxLtvBps, liqThresholdBps) {
|
263
|
+
const supplyUsd = (0, numberUtils_1.fromBaseUnit)(supply.amountUsedBaseUnit, supply.decimals) * supply.price;
|
264
|
+
const debtUsd = (0, numberUtils_1.fromBaseUnit)(debt.amountUsedBaseUnit, debt.decimals) * debt.price;
|
265
|
+
return {
|
266
|
+
liqUtilizationRateBps: (0, numberUtils_1.getLiqUtilzationRateBps)(supplyUsd, debtUsd, liqThresholdBps),
|
267
|
+
supply: {
|
268
|
+
amountUsed: {
|
269
|
+
baseUnit: supply.amountUsedBaseUnit,
|
270
|
+
baseAmountUsdValue: (0, numberUtils_1.toBaseUnit)(supplyUsd, constants_1.USD_DECIMALS),
|
271
|
+
},
|
272
|
+
amountCanBeUsed: {
|
273
|
+
baseUnit: (0, numberUtils_1.toBaseUnit)(1000000, supply.decimals),
|
274
|
+
baseAmountUsdValue: BigInt(Math.round(1000000 * supply.price)),
|
275
|
+
},
|
276
|
+
baseAmountMarketPriceUsd: (0, numberUtils_1.toBaseUnit)(supply.price, constants_1.USD_DECIMALS),
|
277
|
+
borrowFeeBps: 0,
|
278
|
+
decimals: supply.decimals,
|
279
|
+
flashLoanFeeBps: 0,
|
280
|
+
mint: (0, umi_1.publicKey)(supply.mint),
|
281
|
+
padding1: [],
|
282
|
+
padding2: [],
|
283
|
+
padding: new Uint8Array([]),
|
284
|
+
},
|
285
|
+
debt: {
|
286
|
+
amountUsed: {
|
287
|
+
baseUnit: debt.amountUsedBaseUnit,
|
288
|
+
baseAmountUsdValue: (0, numberUtils_1.toBaseUnit)(debtUsd, constants_1.USD_DECIMALS),
|
289
|
+
},
|
290
|
+
amountCanBeUsed: {
|
291
|
+
baseUnit: (0, numberUtils_1.toBaseUnit)(1000000, debt.decimals),
|
292
|
+
baseAmountUsdValue: BigInt(Math.round(1000000 * debt.price)),
|
293
|
+
},
|
294
|
+
baseAmountMarketPriceUsd: (0, numberUtils_1.toBaseUnit)(debt.price, constants_1.USD_DECIMALS),
|
295
|
+
borrowFeeBps: 0,
|
296
|
+
decimals: debt.decimals,
|
297
|
+
flashLoanFeeBps: 0,
|
298
|
+
mint: (0, umi_1.publicKey)(debt.mint),
|
299
|
+
padding1: [],
|
300
|
+
padding2: [],
|
301
|
+
padding: new Uint8Array([]),
|
302
|
+
},
|
303
|
+
netWorth: {
|
304
|
+
baseUnit: (0, numberUtils_1.toBaseUnit)((supplyUsd - debtUsd) / supply.price, supply.decimals),
|
305
|
+
baseAmountUsdValue: (0, numberUtils_1.toBaseUnit)(supplyUsd - debtUsd, constants_1.USD_DECIMALS),
|
306
|
+
},
|
307
|
+
maxLtvBps,
|
308
|
+
liqThresholdBps,
|
309
|
+
lastUpdated: BigInt((0, generalUtils_1.currentUnixSeconds)()),
|
310
|
+
padding1: [],
|
311
|
+
padding2: [],
|
312
|
+
padding: [],
|
313
|
+
};
|
314
|
+
}
|
256
315
|
class LivePositionUpdates {
|
257
316
|
constructor() {
|
258
317
|
this.supplyAdjustment = BigInt(0);
|
@@ -1,13 +1,15 @@
|
|
1
1
|
import { PublicKey } from "@solana/web3.js";
|
2
2
|
import { SolautoClient } from "../../clients/solautoClient";
|
3
|
+
import { DCASettings, FeeType, PositionState, SolautoSettingsParameters } from "../../generated";
|
3
4
|
import { QuoteResponse } from "@jup-ag/api";
|
4
5
|
import { JupSwapDetails } from "../jupiterUtils";
|
5
6
|
export interface RebalanceValues {
|
6
7
|
increasingLeverage: boolean;
|
7
8
|
debtAdjustmentUsd: number;
|
9
|
+
amountToDcaIn: number;
|
8
10
|
amountUsdToDcaIn: number;
|
9
11
|
}
|
10
|
-
export declare function getRebalanceValues(
|
12
|
+
export declare function getRebalanceValues(state: PositionState, settings: SolautoSettingsParameters | undefined, dca: DCASettings | undefined, feeType: FeeType, currentUnixTime: number, supplyPrice: number, debtPrice: number, targetLiqUtilizationRateBps?: number, limitGapBps?: number): RebalanceValues;
|
11
13
|
export interface FlashLoanDetails {
|
12
14
|
baseUnitAmount: bigint;
|
13
15
|
mint: PublicKey;
|
@@ -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;
|
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;AAkJjD,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"}
|
@@ -3,81 +3,72 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getRebalanceValues = getRebalanceValues;
|
4
4
|
exports.getFlashLoanDetails = getFlashLoanDetails;
|
5
5
|
exports.getJupSwapRebalanceDetails = getJupSwapRebalanceDetails;
|
6
|
-
const generated_1 = require("../../generated");
|
7
6
|
const generalUtils_1 = require("./generalUtils");
|
8
7
|
const umi_web3js_adapters_1 = require("@metaplex-foundation/umi-web3js-adapters");
|
9
8
|
const generalUtils_2 = require("../generalUtils");
|
10
9
|
const numberUtils_1 = require("../numberUtils");
|
11
10
|
const generalAccounts_1 = require("../../constants/generalAccounts");
|
12
11
|
const solautoConstants_1 = require("../../constants/solautoConstants");
|
13
|
-
function getAdditionalAmountToDcaIn(
|
14
|
-
const dca = client.solautoPositionActiveDca();
|
12
|
+
function getAdditionalAmountToDcaIn(dca) {
|
15
13
|
if (dca.debtToAddBaseUnit === BigInt(0)) {
|
16
14
|
return 0;
|
17
15
|
}
|
18
|
-
const debtBalance = Number(
|
19
|
-
Number(client.livePositionUpdates.debtTaBalanceAdjustment ?? 0);
|
16
|
+
const debtBalance = Number(dca.debtToAddBaseUnit);
|
20
17
|
const updatedDebtBalance = (0, generalUtils_1.getUpdatedValueFromAutomation)(debtBalance, 0, dca.automation, (0, generalUtils_2.currentUnixSeconds)());
|
21
18
|
return debtBalance - updatedDebtBalance;
|
22
19
|
}
|
23
|
-
function getStandardTargetLiqUtilizationRateBps(
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
repayFrom * 0.015) {
|
34
|
-
return adjustedSettings.repayToBps;
|
35
|
-
}
|
36
|
-
else {
|
37
|
-
throw new Error("Invalid rebalance condition");
|
38
|
-
}
|
20
|
+
function getStandardTargetLiqUtilizationRateBps(state, settings) {
|
21
|
+
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(settings, (0, generalUtils_2.currentUnixSeconds)());
|
22
|
+
const repayFrom = adjustedSettings.repayToBps - adjustedSettings.repayGap;
|
23
|
+
const boostFrom = adjustedSettings.boostToBps + adjustedSettings.boostGap;
|
24
|
+
if (state.liqUtilizationRateBps < boostFrom) {
|
25
|
+
return adjustedSettings.boostToBps;
|
26
|
+
}
|
27
|
+
else if (state.liqUtilizationRateBps > repayFrom ||
|
28
|
+
repayFrom - state.liqUtilizationRateBps < repayFrom * 0.015) {
|
29
|
+
return adjustedSettings.repayToBps;
|
39
30
|
}
|
40
31
|
else {
|
41
|
-
throw new Error("
|
32
|
+
throw new Error("Invalid rebalance condition");
|
42
33
|
}
|
43
34
|
}
|
44
|
-
function targetLiqUtilizationRateBpsFromDCA(
|
45
|
-
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(
|
35
|
+
function targetLiqUtilizationRateBpsFromDCA(state, settings, dca) {
|
36
|
+
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(settings, (0, generalUtils_2.currentUnixSeconds)());
|
46
37
|
let targetRateBps = 0;
|
47
|
-
if (
|
48
|
-
targetRateBps = Math.max(
|
38
|
+
if (dca.debtToAddBaseUnit > BigInt(0)) {
|
39
|
+
targetRateBps = Math.max(state.liqUtilizationRateBps, adjustedSettings.boostToBps);
|
49
40
|
}
|
50
41
|
else {
|
51
42
|
targetRateBps = adjustedSettings.boostToBps;
|
52
43
|
}
|
53
44
|
return targetRateBps;
|
54
45
|
}
|
55
|
-
function isDcaRebalance(
|
56
|
-
if (
|
46
|
+
function isDcaRebalance(state, settings, dca, currentUnixTime) {
|
47
|
+
if (dca === undefined || dca.automation.targetPeriods === 0) {
|
57
48
|
return false;
|
58
49
|
}
|
59
|
-
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(
|
60
|
-
if (
|
50
|
+
const adjustedSettings = (0, generalUtils_1.getAdjustedSettingsFromAutomation)(settings, (0, generalUtils_2.currentUnixSeconds)());
|
51
|
+
if (state.liqUtilizationRateBps >
|
61
52
|
adjustedSettings.repayToBps + adjustedSettings.repayGap) {
|
62
53
|
return false;
|
63
54
|
}
|
64
|
-
if (
|
65
|
-
return false;
|
66
|
-
}
|
67
|
-
if (!(0, generalUtils_1.eligibleForNextAutomationPeriod)(client.solautoPositionActiveDca().automation)) {
|
55
|
+
if (!(0, generalUtils_1.eligibleForNextAutomationPeriod)(dca.automation, currentUnixTime)) {
|
68
56
|
return false;
|
69
57
|
}
|
70
58
|
return true;
|
71
59
|
}
|
72
|
-
function getTargetRateAndDcaAmount(
|
60
|
+
function getTargetRateAndDcaAmount(state, settings, dca, currentUnixTime, targetLiqUtilizationRateBps) {
|
73
61
|
if (targetLiqUtilizationRateBps !== undefined) {
|
74
62
|
return {
|
75
63
|
targetRateBps: targetLiqUtilizationRateBps,
|
76
64
|
};
|
77
65
|
}
|
78
|
-
if (
|
79
|
-
|
80
|
-
|
66
|
+
if (settings === undefined) {
|
67
|
+
throw new Error("If rebalancing a self-managed position, settings, and DCA should be provided");
|
68
|
+
}
|
69
|
+
if (isDcaRebalance(state, settings, dca, currentUnixTime)) {
|
70
|
+
const amountToDcaIn = getAdditionalAmountToDcaIn(dca);
|
71
|
+
const targetLiqUtilizationRateBps = targetLiqUtilizationRateBpsFromDCA(state, settings, dca);
|
81
72
|
return {
|
82
73
|
targetRateBps: targetLiqUtilizationRateBps,
|
83
74
|
amountToDcaIn,
|
@@ -85,33 +76,30 @@ function getTargetRateAndDcaAmount(client, targetLiqUtilizationRateBps) {
|
|
85
76
|
}
|
86
77
|
else {
|
87
78
|
return {
|
88
|
-
targetRateBps: getStandardTargetLiqUtilizationRateBps(
|
79
|
+
targetRateBps: getStandardTargetLiqUtilizationRateBps(state, settings),
|
89
80
|
};
|
90
81
|
}
|
91
82
|
}
|
92
|
-
function getRebalanceValues(
|
93
|
-
if (
|
94
|
-
|
83
|
+
function getRebalanceValues(state, settings, dca, feeType, currentUnixTime, supplyPrice, debtPrice, targetLiqUtilizationRateBps, limitGapBps) {
|
84
|
+
if (state === undefined ||
|
85
|
+
state.lastUpdated <
|
95
86
|
BigInt(Math.round((0, generalUtils_2.currentUnixSeconds)() - solautoConstants_1.MIN_POSITION_STATE_FRESHNESS_SECS))) {
|
96
87
|
throw new Error("Requires a fresh position state to get rebalance details");
|
97
88
|
}
|
98
|
-
const { targetRateBps, amountToDcaIn } = getTargetRateAndDcaAmount(
|
99
|
-
const amountUsdToDcaIn = (0, numberUtils_1.fromBaseUnit)(BigInt(Math.round(amountToDcaIn ?? 0)),
|
100
|
-
|
101
|
-
|
89
|
+
const { targetRateBps, amountToDcaIn } = getTargetRateAndDcaAmount(state, settings, dca, currentUnixTime, targetLiqUtilizationRateBps);
|
90
|
+
const amountUsdToDcaIn = (0, numberUtils_1.fromBaseUnit)(BigInt(Math.round(amountToDcaIn ?? 0)), state.debt.decimals) *
|
91
|
+
debtPrice;
|
92
|
+
const increasingLeverage = amountUsdToDcaIn > 0 || state.liqUtilizationRateBps < targetRateBps;
|
102
93
|
let adjustmentFeeBps = 0;
|
103
94
|
if (increasingLeverage) {
|
104
|
-
adjustmentFeeBps = (0, generalUtils_1.getSolautoFeesBps)(
|
105
|
-
}
|
106
|
-
const supplyUsd = (0, numberUtils_1.fromBaseUnit)(
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
const inputMarketPrice = increasingLeverage
|
113
|
-
? solautoConstants_1.PRICES[client.debtMint.toString()].price
|
114
|
-
: solautoConstants_1.PRICES[client.supplyMint.toString()].price;
|
95
|
+
adjustmentFeeBps = (0, generalUtils_1.getSolautoFeesBps)(false, feeType).total;
|
96
|
+
}
|
97
|
+
const supplyUsd = (0, numberUtils_1.fromBaseUnit)(state.supply.amountUsed.baseAmountUsdValue, generalAccounts_1.USD_DECIMALS) +
|
98
|
+
amountUsdToDcaIn;
|
99
|
+
const debtUsd = (0, numberUtils_1.fromBaseUnit)(state.debt.amountUsed.baseAmountUsdValue, generalAccounts_1.USD_DECIMALS);
|
100
|
+
let debtAdjustmentUsd = (0, numberUtils_1.getDebtAdjustmentUsd)(state.liqThresholdBps, supplyUsd, debtUsd, targetRateBps, adjustmentFeeBps);
|
101
|
+
const input = increasingLeverage ? state.debt : state.supply;
|
102
|
+
const inputMarketPrice = increasingLeverage ? debtPrice : supplyPrice;
|
115
103
|
const limitGap = limitGapBps
|
116
104
|
? (0, numberUtils_1.fromBps)(limitGapBps)
|
117
105
|
: (0, numberUtils_1.fromBps)(solautoConstants_1.DEFAULT_LIMIT_GAP_BPS);
|
@@ -126,6 +114,7 @@ function getRebalanceValues(client, targetLiqUtilizationRateBps, limitGapBps) {
|
|
126
114
|
return {
|
127
115
|
increasingLeverage,
|
128
116
|
debtAdjustmentUsd,
|
117
|
+
amountToDcaIn: amountToDcaIn ?? 0,
|
129
118
|
amountUsdToDcaIn,
|
130
119
|
};
|
131
120
|
}
|
@@ -186,7 +175,7 @@ function getJupSwapRebalanceDetails(client, values, targetLiqUtilizationRateBps,
|
|
186
175
|
inputMint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(input.mint),
|
187
176
|
outputMint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(output.mint),
|
188
177
|
destinationWallet: client.solautoPosition,
|
189
|
-
slippageBpsIncFactor: 0.25 + (
|
178
|
+
slippageBpsIncFactor: 0.25 + (attemptNum ?? 0) * 0.2,
|
190
179
|
amount: rebalancingToZero
|
191
180
|
? client.solautoPositionState.debt.amountUsed.baseUnit +
|
192
181
|
BigInt(Math.round(Number(client.solautoPositionState.debt.amountUsed.baseUnit) *
|
package/package.json
CHANGED
@@ -49,14 +49,12 @@ import {
|
|
49
49
|
} from "../utils/accountUtils";
|
50
50
|
import {
|
51
51
|
SOLAUTO_FEES_WALLET,
|
52
|
-
SOLAUTO_MANAGER,
|
53
52
|
} from "../constants/generalAccounts";
|
54
53
|
import { JupSwapDetails } from "../utils/jupiterUtils";
|
55
54
|
import {
|
56
55
|
getSolanaRpcConnection,
|
57
56
|
getWrappedInstruction,
|
58
57
|
splTokenTransferUmiIx,
|
59
|
-
systemTransferUmiIx,
|
60
58
|
} from "../utils/solanaUtils";
|
61
59
|
import { FlashLoanDetails } from "../utils/solauto/rebalanceUtils";
|
62
60
|
import { NATIVE_MINT } from "@solana/spl-token";
|
@@ -66,7 +64,6 @@ import {
|
|
66
64
|
} from "../constants/solautoConstants";
|
67
65
|
import { currentUnixSeconds } from "../utils/generalUtils";
|
68
66
|
import { LivePositionUpdates } from "../utils/solauto/generalUtils";
|
69
|
-
import { toBaseUnit } from "../utils";
|
70
67
|
|
71
68
|
export interface SolautoClientArgs {
|
72
69
|
authority?: PublicKey;
|
@@ -14,6 +14,7 @@ import {
|
|
14
14
|
Account as SplTokenAccount,
|
15
15
|
} from "@solana/spl-token";
|
16
16
|
import {
|
17
|
+
FeeType,
|
17
18
|
LendingPlatform,
|
18
19
|
ReferralState,
|
19
20
|
SOLAUTO_PROGRAM_ID,
|
@@ -38,6 +39,7 @@ import {
|
|
38
39
|
getRebalanceValues,
|
39
40
|
} from "../utils/solauto/rebalanceUtils";
|
40
41
|
import {
|
42
|
+
currentUnixSeconds,
|
41
43
|
getSolanaAccountCreated,
|
42
44
|
rpcAccountCreated,
|
43
45
|
} from "../utils/generalUtils";
|
@@ -55,6 +57,7 @@ import {
|
|
55
57
|
getLendingAccountWithdrawInstructionDataSerializer,
|
56
58
|
MARGINFI_PROGRAM_ID,
|
57
59
|
} from "../marginfi-sdk";
|
60
|
+
import { PRICES } from "../constants";
|
58
61
|
|
59
62
|
interface wSolTokenUsage {
|
60
63
|
wSolTokenAccount: PublicKey;
|
@@ -547,7 +550,6 @@ export async function getTransactionChores(
|
|
547
550
|
return [choresBefore, choresAfter];
|
548
551
|
}
|
549
552
|
|
550
|
-
|
551
553
|
export async function buildSolautoRebalanceTransaction(
|
552
554
|
client: SolautoClient,
|
553
555
|
targetLiqUtilizationRateBps?: number,
|
@@ -568,14 +570,24 @@ export async function buildSolautoRebalanceTransaction(
|
|
568
570
|
client.livePositionUpdates.settings ??
|
569
571
|
client.solautoPositionData?.position.settingParams!,
|
570
572
|
client.livePositionUpdates.activeDca ??
|
571
|
-
client.solautoPositionData?.position.dca
|
573
|
+
client.solautoPositionData?.position.dca!,
|
574
|
+
currentUnixSeconds()
|
572
575
|
))
|
573
576
|
) {
|
574
577
|
client.log("Not eligible for a rebalance");
|
575
578
|
return undefined;
|
576
579
|
}
|
577
580
|
|
578
|
-
const values = getRebalanceValues(
|
581
|
+
const values = getRebalanceValues(
|
582
|
+
client.solautoPositionState!,
|
583
|
+
client.solautoPositionSettings(),
|
584
|
+
client.solautoPositionActiveDca(),
|
585
|
+
client.solautoPositionData?.feeType ?? FeeType.Small,
|
586
|
+
currentUnixSeconds(),
|
587
|
+
PRICES[client.supplyMint.toString()].price,
|
588
|
+
PRICES[client.debtMint.toString()].price,
|
589
|
+
targetLiqUtilizationRateBps
|
590
|
+
);
|
579
591
|
client.log("Rebalance values: ", values);
|
580
592
|
|
581
593
|
const swapDetails = getJupSwapRebalanceDetails(
|