@haven-fi/solauto-sdk 1.0.457 → 1.0.459
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/transactions/transactionUtils.d.ts +2 -1
- package/dist/transactions/transactionUtils.d.ts.map +1 -1
- package/dist/transactions/transactionUtils.js +16 -2
- package/dist/utils/solauto/rebalanceUtils.d.ts +1 -0
- package/dist/utils/solauto/rebalanceUtils.d.ts.map +1 -1
- package/dist/utils/solauto/rebalanceUtils.js +19 -4
- package/package.json +1 -1
- package/src/transactions/transactionUtils.ts +28 -3
- package/src/utils/solauto/rebalanceUtils.ts +26 -4
- package/tests/transactions/solautoMarginfi.ts +57 -12
@@ -1,11 +1,12 @@
|
|
1
1
|
import { TransactionBuilder, Umi } from "@metaplex-foundation/umi";
|
2
2
|
import { PublicKey } from "@solana/web3.js";
|
3
3
|
import { SolautoClient } from "../clients/solautoClient";
|
4
|
+
import { RebalanceValues } from "../utils/solauto/rebalanceUtils";
|
4
5
|
import { ReferralStateManager } from "../clients";
|
5
6
|
import { TransactionItemInputs } from "../types";
|
6
7
|
export declare function rebalanceChoresBefore(client: SolautoClient, tx: TransactionBuilder, accountsGettingCreated: string[]): Promise<TransactionBuilder>;
|
7
8
|
export declare function getTransactionChores(client: SolautoClient, tx: TransactionBuilder): Promise<[TransactionBuilder, TransactionBuilder]>;
|
8
|
-
export declare function requiresRefreshBeforeRebalance(client: SolautoClient): Promise<boolean>;
|
9
|
+
export declare function requiresRefreshBeforeRebalance(client: SolautoClient, values: RebalanceValues): Promise<boolean>;
|
9
10
|
export declare function buildSolautoRebalanceTransaction(client: SolautoClient, targetLiqUtilizationRateBps?: number, attemptNum?: number): Promise<TransactionItemInputs | undefined>;
|
10
11
|
export declare function convertReferralFeesToDestination(referralManager: ReferralStateManager, tokenAccount: PublicKey, destinationMint: PublicKey): Promise<TransactionItemInputs | undefined>;
|
11
12
|
export declare function getErrorInfo(umi: Umi, txs: TransactionBuilder[], error: Error, simulationSuccessful?: boolean): {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;
|
1
|
+
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AAqBxE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAOzD,OAAO,EAIL,eAAe,EAChB,MAAM,iCAAiC,CAAC;AA2BzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAOlD,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AA6LjD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CAoH7B;AA2LD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA0BnD;AAED,wBAAsB,8BAA8B,CAClD,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,oBAuExB;AAED,wBAAsB,gCAAgC,CACpD,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC,CAqI5C;AAED,wBAAsB,gCAAgC,CACpD,eAAe,EAAE,oBAAoB,EACrC,YAAY,EAAE,SAAS,EACvB,eAAe,EAAE,SAAS,GACzB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC,CAsC5C;AAED,wBAAgB,YAAY,CAC1B,GAAG,EAAE,GAAG,EACR,GAAG,EAAE,kBAAkB,EAAE,EACzB,KAAK,EAAE,KAAK,EACZ,oBAAoB,CAAC,EAAE,OAAO;;;;EAiG/B"}
|
@@ -329,7 +329,20 @@ async function getTransactionChores(client, tx) {
|
|
329
329
|
choresAfter = choresAfter.add(transactionChoresAfter(client, solautoActions, client.livePositionUpdates.cancellingDca));
|
330
330
|
return [choresBefore, choresAfter];
|
331
331
|
}
|
332
|
-
async function requiresRefreshBeforeRebalance(client) {
|
332
|
+
async function requiresRefreshBeforeRebalance(client, values) {
|
333
|
+
// REMOVE ME
|
334
|
+
const state = client.solautoPositionState;
|
335
|
+
const settings = client.solautoPositionSettings();
|
336
|
+
const supplyMint = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.supply.mint);
|
337
|
+
if ((supplyMint.equals(new web3_js_1.PublicKey(constants_1.JUP)) ||
|
338
|
+
supplyMint.equals(new web3_js_1.PublicKey(constants_1.BONK))) &&
|
339
|
+
(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.debt.mint).equals(new web3_js_1.PublicKey(constants_1.USDC)) &&
|
340
|
+
settings &&
|
341
|
+
settings.boostToBps ===
|
342
|
+
(0, numberUtils_1.maxBoostToBps)(state.maxLtvBps, state.liqThresholdBps) &&
|
343
|
+
values.targetRateBps === 6500) {
|
344
|
+
return true;
|
345
|
+
}
|
333
346
|
const neverRefreshedBefore = client.solautoPositionData &&
|
334
347
|
client.solautoPositionData.state.supply.amountCanBeUsed.baseUnit ===
|
335
348
|
BigInt(0) &&
|
@@ -377,7 +390,8 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
377
390
|
const { jupQuote, lookupTableAddresses, setupInstructions, tokenLedgerIx, swapIx, } = await (0, jupiterUtils_1.getJupSwapTransaction)(client.signer, swapDetails, attemptNum);
|
378
391
|
const flashLoan = (0, rebalanceUtils_1.getFlashLoanDetails)(client, values, jupQuote);
|
379
392
|
let tx = (0, umi_1.transactionBuilder)();
|
380
|
-
|
393
|
+
// REVERT ME
|
394
|
+
if (await requiresRefreshBeforeRebalance(client, values)) {
|
381
395
|
tx = tx.add(client.refresh());
|
382
396
|
}
|
383
397
|
if (flashLoan) {
|
@@ -13,6 +13,7 @@ export interface RebalanceValues {
|
|
13
13
|
rebalanceAction: RebalanceAction;
|
14
14
|
rebalanceDirection: RebalanceDirection;
|
15
15
|
feesUsd: number;
|
16
|
+
targetRateBps: number;
|
16
17
|
}
|
17
18
|
export declare function getRebalanceValues(state: PositionState, settings: SolautoSettingsParameters | undefined, dca: DCASettings | undefined, currentUnixTime: number, supplyPrice: number, debtPrice: number, targetLiqUtilizationRateBps?: number): RebalanceValues;
|
18
19
|
export declare function rebalanceRequiresFlashLoan(client: SolautoClient, values: RebalanceValues): {
|
@@ -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,aAAa,EAEb,kBAAkB,EAClB,yBAAyB,EACzB,SAAS,EACV,MAAM,iBAAiB,CAAC;AAOzB,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,aAAa,EAEb,kBAAkB,EAClB,yBAAyB,EACzB,SAAS,EACV,MAAM,iBAAiB,CAAC;AAOzB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAcjD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAoI9C,MAAM,WAAW,eAAe;IAC9B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,OAAO,CAAC;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,SAAS,CAAC;IACzB,eAAe,EAAE,eAAe,CAAC;IACjC,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,aAAa,EACpB,QAAQ,EAAE,yBAAyB,GAAG,SAAS,EAC/C,GAAG,EAAE,WAAW,GAAG,SAAS,EAC5B,eAAe,EAAE,MAAM,EACvB,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EACjB,2BAA2B,CAAC,EAAE,MAAM,GACnC,eAAe,CA2EjB;AAED,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe;;;EAsDxB;AAED,MAAM,WAAW,gBAAgB;IAC/B,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,SAAS,CAAC;IAChB,gBAAgB,EAAE,OAAO,CAAC;CAC3B;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,aAAa,GACtB,gBAAgB,GAAG,SAAS,CAmC9B;AAED,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,cAAc,CA2DhB"}
|
@@ -4,6 +4,7 @@ exports.getRebalanceValues = getRebalanceValues;
|
|
4
4
|
exports.rebalanceRequiresFlashLoan = rebalanceRequiresFlashLoan;
|
5
5
|
exports.getFlashLoanDetails = getFlashLoanDetails;
|
6
6
|
exports.getJupSwapRebalanceDetails = getJupSwapRebalanceDetails;
|
7
|
+
const web3_js_1 = require("@solana/web3.js");
|
7
8
|
const generated_1 = require("../../generated");
|
8
9
|
const generalUtils_1 = require("./generalUtils");
|
9
10
|
const umi_web3js_adapters_1 = require("@metaplex-foundation/umi-web3js-adapters");
|
@@ -11,6 +12,7 @@ const generalUtils_2 = require("../generalUtils");
|
|
11
12
|
const numberUtils_1 = require("../numberUtils");
|
12
13
|
const generalAccounts_1 = require("../../constants/generalAccounts");
|
13
14
|
const priceUtils_1 = require("../priceUtils");
|
15
|
+
const constants_1 = require("../../constants");
|
14
16
|
function getAdditionalAmountToDcaIn(dca) {
|
15
17
|
if (dca.dcaInBaseUnit === BigInt(0)) {
|
16
18
|
return 0;
|
@@ -82,7 +84,17 @@ function getTargetRateAndDcaAmount(state, settings, dca, currentUnixTime, target
|
|
82
84
|
}
|
83
85
|
}
|
84
86
|
function getRebalanceValues(state, settings, dca, currentUnixTime, supplyPrice, debtPrice, targetLiqUtilizationRateBps) {
|
85
|
-
|
87
|
+
let { targetRateBps, amountToDcaIn } = getTargetRateAndDcaAmount(state, settings, dca, currentUnixTime, targetLiqUtilizationRateBps);
|
88
|
+
// REVERT ME AND GET TO THE ROOT OF THIS ISSUE
|
89
|
+
if (((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.supply.mint).equals(new web3_js_1.PublicKey(constants_1.JUP)) ||
|
90
|
+
(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.supply.mint).equals(new web3_js_1.PublicKey(constants_1.BONK))) &&
|
91
|
+
(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(state.debt.mint).equals(new web3_js_1.PublicKey(constants_1.USDC)) &&
|
92
|
+
settings &&
|
93
|
+
settings.boostToBps ===
|
94
|
+
(0, numberUtils_1.maxBoostToBps)(state.maxLtvBps, state.liqThresholdBps) &&
|
95
|
+
targetRateBps === settings.boostToBps) {
|
96
|
+
targetRateBps = 6500;
|
97
|
+
}
|
86
98
|
const amountUsdToDcaIn = (0, numberUtils_1.fromBaseUnit)(BigInt(Math.round(amountToDcaIn ?? 0)), state.debt.decimals) *
|
87
99
|
(dca?.tokenType === generated_1.TokenType.Debt ? debtPrice : supplyPrice);
|
88
100
|
const rebalanceDirection = amountUsdToDcaIn > 0 || state.liqUtilizationRateBps <= targetRateBps
|
@@ -93,6 +105,7 @@ function getRebalanceValues(state, settings, dca, currentUnixTime, supplyPrice,
|
|
93
105
|
amountUsdToDcaIn;
|
94
106
|
const debtUsd = (0, numberUtils_1.fromBaseUnit)(state.debt.amountUsed.baseAmountUsdValue, generalAccounts_1.USD_DECIMALS);
|
95
107
|
let debtAdjustmentUsd = (0, numberUtils_1.getDebtAdjustmentUsd)(state.liqThresholdBps, supplyUsd, debtUsd, targetRateBps, adjustmentFeeBps);
|
108
|
+
(0, generalUtils_2.consoleLog)("Target rate:", targetRateBps, (0, numberUtils_1.maxBoostToBps)(state.maxLtvBps, state.liqThresholdBps));
|
96
109
|
const maxRepayTo = (0, numberUtils_1.maxRepayToBps)(state.maxLtvBps, state.liqThresholdBps);
|
97
110
|
return {
|
98
111
|
debtAdjustmentUsd,
|
@@ -107,6 +120,7 @@ function getRebalanceValues(state, settings, dca, currentUnixTime, supplyPrice,
|
|
107
120
|
: "repay",
|
108
121
|
rebalanceDirection,
|
109
122
|
feesUsd: Math.abs(debtAdjustmentUsd * (0, numberUtils_1.fromBps)(adjustmentFeeBps)),
|
123
|
+
targetRateBps,
|
110
124
|
};
|
111
125
|
}
|
112
126
|
function rebalanceRequiresFlashLoan(client, values) {
|
@@ -125,9 +139,10 @@ function rebalanceRequiresFlashLoan(client, values) {
|
|
125
139
|
const tempLiqUtilizationRateBps = (0, numberUtils_1.getLiqUtilzationRateBps)(supplyUsd, debtUsd, client.solautoPositionState.liqThresholdBps);
|
126
140
|
const maxLiqUtilizationRateBps = (0, numberUtils_1.getMaxLiqUtilizationRateBps)(client.solautoPositionState.maxLtvBps, client.solautoPositionState.liqThresholdBps, 0.02);
|
127
141
|
const requiresFlashLoan = supplyUsd <= 0 || tempLiqUtilizationRateBps > maxLiqUtilizationRateBps;
|
128
|
-
const useDebtLiquidity =
|
129
|
-
|
130
|
-
(
|
142
|
+
const useDebtLiquidity = values.rebalanceDirection === generated_1.RebalanceDirection.Boost ||
|
143
|
+
Math.abs(values.debtAdjustmentUsd) * 0.9 >
|
144
|
+
(0, numberUtils_1.fromBaseUnit)(client.supplyLiquidityAvailable(), generalAccounts_1.USD_DECIMALS) *
|
145
|
+
((0, priceUtils_1.safeGetPrice)(client.supplyMint) ?? 0);
|
131
146
|
(0, generalUtils_2.consoleLog)("Requires flash loan:", requiresFlashLoan);
|
132
147
|
(0, generalUtils_2.consoleLog)("Use debt liquidity:", useDebtLiquidity);
|
133
148
|
(0, generalUtils_2.consoleLog)("Intermediary liq utilization rate:", tempLiqUtilizationRateBps, `$${supplyUsd}`, `$${debtUsd}`, "Max:", maxLiqUtilizationRateBps);
|
package/package.json
CHANGED
@@ -19,6 +19,7 @@ import {
|
|
19
19
|
RebalanceDirection,
|
20
20
|
SolautoAction,
|
21
21
|
SolautoRebalanceType,
|
22
|
+
SolautoSettingsParameters,
|
22
23
|
TokenType,
|
23
24
|
convertReferralFees,
|
24
25
|
createSolautoProgram,
|
@@ -39,6 +40,7 @@ import {
|
|
39
40
|
getFlashLoanDetails,
|
40
41
|
getJupSwapRebalanceDetails,
|
41
42
|
getRebalanceValues,
|
43
|
+
RebalanceValues,
|
42
44
|
} from "../utils/solauto/rebalanceUtils";
|
43
45
|
import {
|
44
46
|
consoleLog,
|
@@ -49,6 +51,7 @@ import {
|
|
49
51
|
import { SolautoMarginfiClient } from "../clients/solautoMarginfiClient";
|
50
52
|
import {
|
51
53
|
getMaxLiqUtilizationRateBps,
|
54
|
+
maxBoostToBps,
|
52
55
|
uint8ArrayToBigInt,
|
53
56
|
} from "../utils/numberUtils";
|
54
57
|
import {
|
@@ -71,10 +74,11 @@ import {
|
|
71
74
|
getJupiterErrorFromCode,
|
72
75
|
JUPITER_PROGRAM_ID,
|
73
76
|
} from "../jupiter-sdk";
|
74
|
-
import { PRICES } from "../constants";
|
77
|
+
import { BONK, JUP, PRICES, USDC } from "../constants";
|
75
78
|
import { TransactionItemInputs } from "../types";
|
76
79
|
import { safeGetPrice } from "../utils";
|
77
80
|
import { BundleSimulationError } from "../types/transactions";
|
81
|
+
import { getPackedSettings } from "http2";
|
78
82
|
|
79
83
|
interface wSolTokenUsage {
|
80
84
|
wSolTokenAccount: PublicKey;
|
@@ -598,7 +602,27 @@ export async function getTransactionChores(
|
|
598
602
|
return [choresBefore, choresAfter];
|
599
603
|
}
|
600
604
|
|
601
|
-
export async function requiresRefreshBeforeRebalance(
|
605
|
+
export async function requiresRefreshBeforeRebalance(
|
606
|
+
client: SolautoClient,
|
607
|
+
values: RebalanceValues
|
608
|
+
) {
|
609
|
+
// REMOVE ME
|
610
|
+
const state = client.solautoPositionState!;
|
611
|
+
const settings = client.solautoPositionSettings();
|
612
|
+
|
613
|
+
const supplyMint = toWeb3JsPublicKey(state.supply.mint);
|
614
|
+
if (
|
615
|
+
(supplyMint.equals(new PublicKey(JUP)) ||
|
616
|
+
supplyMint.equals(new PublicKey(BONK))) &&
|
617
|
+
toWeb3JsPublicKey(state.debt.mint).equals(new PublicKey(USDC)) &&
|
618
|
+
settings &&
|
619
|
+
settings.boostToBps ===
|
620
|
+
maxBoostToBps(state.maxLtvBps, state.liqThresholdBps) &&
|
621
|
+
values.targetRateBps === 6500
|
622
|
+
) {
|
623
|
+
return true;
|
624
|
+
}
|
625
|
+
|
602
626
|
const neverRefreshedBefore =
|
603
627
|
client.solautoPositionData &&
|
604
628
|
client.solautoPositionData.state.supply.amountCanBeUsed.baseUnit ===
|
@@ -707,7 +731,8 @@ export async function buildSolautoRebalanceTransaction(
|
|
707
731
|
|
708
732
|
let tx = transactionBuilder();
|
709
733
|
|
710
|
-
|
734
|
+
// REVERT ME
|
735
|
+
if (await requiresRefreshBeforeRebalance(client, values)) {
|
711
736
|
tx = tx.add(client.refresh());
|
712
737
|
}
|
713
738
|
|
@@ -24,13 +24,14 @@ import {
|
|
24
24
|
getLiqUtilzationRateBps,
|
25
25
|
getMaxLiqUtilizationRateBps,
|
26
26
|
getSolautoFeesBps,
|
27
|
+
maxBoostToBps,
|
27
28
|
maxRepayToBps,
|
28
29
|
toBaseUnit,
|
29
30
|
} from "../numberUtils";
|
30
31
|
import { USD_DECIMALS } from "../../constants/generalAccounts";
|
31
32
|
import { RebalanceAction } from "../../types";
|
32
33
|
import { safeGetPrice } from "../priceUtils";
|
33
|
-
import { TOKEN_INFO } from "../../constants";
|
34
|
+
import { BONK, JUP, TOKEN_INFO, USDC } from "../../constants";
|
34
35
|
|
35
36
|
function getAdditionalAmountToDcaIn(dca: DCASettings): number {
|
36
37
|
if (dca.dcaInBaseUnit === BigInt(0)) {
|
@@ -169,6 +170,7 @@ export interface RebalanceValues {
|
|
169
170
|
rebalanceAction: RebalanceAction;
|
170
171
|
rebalanceDirection: RebalanceDirection;
|
171
172
|
feesUsd: number;
|
173
|
+
targetRateBps: number;
|
172
174
|
}
|
173
175
|
|
174
176
|
export function getRebalanceValues(
|
@@ -180,7 +182,7 @@ export function getRebalanceValues(
|
|
180
182
|
debtPrice: number,
|
181
183
|
targetLiqUtilizationRateBps?: number
|
182
184
|
): RebalanceValues {
|
183
|
-
|
185
|
+
let { targetRateBps, amountToDcaIn } = getTargetRateAndDcaAmount(
|
184
186
|
state,
|
185
187
|
settings,
|
186
188
|
dca,
|
@@ -188,6 +190,19 @@ export function getRebalanceValues(
|
|
188
190
|
targetLiqUtilizationRateBps
|
189
191
|
);
|
190
192
|
|
193
|
+
// REVERT ME AND GET TO THE ROOT OF THIS ISSUE
|
194
|
+
if (
|
195
|
+
(toWeb3JsPublicKey(state.supply.mint).equals(new PublicKey(JUP)) ||
|
196
|
+
toWeb3JsPublicKey(state.supply.mint).equals(new PublicKey(BONK))) &&
|
197
|
+
toWeb3JsPublicKey(state.debt.mint).equals(new PublicKey(USDC)) &&
|
198
|
+
settings &&
|
199
|
+
settings.boostToBps ===
|
200
|
+
maxBoostToBps(state.maxLtvBps, state.liqThresholdBps) &&
|
201
|
+
targetRateBps === settings.boostToBps
|
202
|
+
) {
|
203
|
+
targetRateBps = 6500;
|
204
|
+
}
|
205
|
+
|
191
206
|
const amountUsdToDcaIn =
|
192
207
|
fromBaseUnit(BigInt(Math.round(amountToDcaIn ?? 0)), state.debt.decimals) *
|
193
208
|
(dca?.tokenType === TokenType.Debt ? debtPrice : supplyPrice);
|
@@ -218,6 +233,11 @@ export function getRebalanceValues(
|
|
218
233
|
adjustmentFeeBps
|
219
234
|
);
|
220
235
|
|
236
|
+
consoleLog(
|
237
|
+
"Target rate:",
|
238
|
+
targetRateBps,
|
239
|
+
maxBoostToBps(state.maxLtvBps, state.liqThresholdBps)
|
240
|
+
);
|
221
241
|
const maxRepayTo = maxRepayToBps(state.maxLtvBps, state.liqThresholdBps);
|
222
242
|
return {
|
223
243
|
debtAdjustmentUsd,
|
@@ -234,6 +254,7 @@ export function getRebalanceValues(
|
|
234
254
|
: "repay",
|
235
255
|
rebalanceDirection,
|
236
256
|
feesUsd: Math.abs(debtAdjustmentUsd * fromBps(adjustmentFeeBps)),
|
257
|
+
targetRateBps,
|
237
258
|
};
|
238
259
|
}
|
239
260
|
|
@@ -276,9 +297,10 @@ export function rebalanceRequiresFlashLoan(
|
|
276
297
|
supplyUsd <= 0 || tempLiqUtilizationRateBps > maxLiqUtilizationRateBps;
|
277
298
|
|
278
299
|
const useDebtLiquidity =
|
300
|
+
values.rebalanceDirection === RebalanceDirection.Boost ||
|
279
301
|
Math.abs(values.debtAdjustmentUsd) * 0.9 >
|
280
|
-
|
281
|
-
|
302
|
+
fromBaseUnit(client.supplyLiquidityAvailable(), USD_DECIMALS) *
|
303
|
+
(safeGetPrice(client.supplyMint) ?? 0);
|
282
304
|
|
283
305
|
consoleLog("Requires flash loan:", requiresFlashLoan);
|
284
306
|
consoleLog("Use debt liquidity:", useDebtLiquidity);
|
@@ -1,9 +1,5 @@
|
|
1
1
|
import { describe, it } from "mocha";
|
2
|
-
import {
|
3
|
-
none,
|
4
|
-
publicKey,
|
5
|
-
some,
|
6
|
-
} from "@metaplex-foundation/umi";
|
2
|
+
import { none, publicKey, some } from "@metaplex-foundation/umi";
|
7
3
|
import { setupTest } from "../shared";
|
8
4
|
import { SolautoMarginfiClient } from "../../src/clients/solautoMarginfiClient";
|
9
5
|
import {
|
@@ -13,6 +9,9 @@ import {
|
|
13
9
|
} from "../../src/generated";
|
14
10
|
import { buildSolautoRebalanceTransaction } from "../../src/transactions/transactionUtils";
|
15
11
|
import {
|
12
|
+
bytesToI80F48,
|
13
|
+
getLiqUtilzationRateBps,
|
14
|
+
getMaxLiqUtilizationRateBps,
|
16
15
|
maxBoostToBps,
|
17
16
|
maxRepayFromBps,
|
18
17
|
maxRepayToBps,
|
@@ -25,24 +24,39 @@ import {
|
|
25
24
|
} from "../../src/transactions/transactionsManager";
|
26
25
|
import { PublicKey } from "@solana/web3.js";
|
27
26
|
import {
|
27
|
+
DEFAULT_MARGINFI_GROUP,
|
28
|
+
MARGINFI_ACCOUNTS,
|
28
29
|
SOLAUTO_PROD_PROGRAM,
|
29
30
|
SOLAUTO_TEST_PROGRAM,
|
30
31
|
USDC,
|
32
|
+
USDT,
|
31
33
|
} from "../../src/constants";
|
32
34
|
import {
|
33
35
|
buildHeliusApiUrl,
|
36
|
+
getMarginfiAccountPositionState,
|
34
37
|
getSolautoManagedPositions,
|
38
|
+
retryWithExponentialBackoff,
|
35
39
|
} from "../../src/utils";
|
36
40
|
import { PriorityFeeSetting } from "../../src/types";
|
37
|
-
import { buildIronforgeApiUrl } from "../../dist";
|
41
|
+
import { buildIronforgeApiUrl, fetchSolautoPosition, fetchTokenPrices } from "../../dist";
|
42
|
+
import { safeFetchBank } from "../../src/marginfi-sdk";
|
43
|
+
|
44
|
+
export function getFlooredTimestampByMinute(
|
45
|
+
unixSeconds: number,
|
46
|
+
intervalInMinutes: number = 1
|
47
|
+
): number {
|
48
|
+
const unixTime = Math.floor(new Date(unixSeconds * 1000).getTime() / 1000);
|
49
|
+
const interval = intervalInMinutes * 60;
|
50
|
+
return Math.floor(unixTime / interval) * interval;
|
51
|
+
}
|
38
52
|
|
39
53
|
describe("Solauto Marginfi tests", async () => {
|
40
|
-
|
41
|
-
const signer = setupTest("solauto-manager");
|
54
|
+
const signer = setupTest();
|
55
|
+
// const signer = setupTest("solauto-manager");
|
42
56
|
|
43
57
|
const payForTransactions = false;
|
44
58
|
const testProgram = false;
|
45
|
-
const positionId =
|
59
|
+
const positionId = 3;
|
46
60
|
|
47
61
|
it("open - deposit - borrow - rebalance to 0 - withdraw - close", async () => {
|
48
62
|
const client = new SolautoMarginfiClient(
|
@@ -58,7 +72,7 @@ describe("Solauto Marginfi tests", async () => {
|
|
58
72
|
await client.initialize({
|
59
73
|
signer,
|
60
74
|
positionId,
|
61
|
-
authority: new PublicKey("
|
75
|
+
authority: new PublicKey("7GMmfZdCgJSCawA7roGiGgvueU3XoEHQXUtiCnuDYUde"),
|
62
76
|
// new: true,
|
63
77
|
// marginfiAccount: new PublicKey(
|
64
78
|
// ""
|
@@ -68,7 +82,22 @@ describe("Solauto Marginfi tests", async () => {
|
|
68
82
|
// debtMint: new PublicKey(USDC),
|
69
83
|
});
|
70
84
|
|
71
|
-
// console.log(await
|
85
|
+
// console.log(await client.getFreshPositionState());
|
86
|
+
|
87
|
+
// const debtBank = await safeFetchBank(
|
88
|
+
// client.umi,
|
89
|
+
// publicKey(MARGINFI_ACCOUNTS[DEFAULT_MARGINFI_GROUP][USDC].bank)
|
90
|
+
// );
|
91
|
+
// const state = await getMarginfiAccountPositionState(client.umi, {
|
92
|
+
// pk: new PublicKey("85YaXXB1uyDMLYf4ob6jfV1kr29muVNm7mpmM8qjbVq6"),
|
93
|
+
// });
|
94
|
+
// console.log(state);
|
95
|
+
// console.log(maxBoostToBps(state.maxLtvBps, state.liqThresholdBps));
|
96
|
+
// console.log(
|
97
|
+
// getMaxLiqUtilizationRateBps(state.maxLtvBps, state.liqThresholdBps, 0)
|
98
|
+
// );
|
99
|
+
// console.log(getLiqUtilzationRateBps(18.78, 7.47, state.liqThresholdBps));
|
100
|
+
// return;
|
72
101
|
|
73
102
|
const transactionItems: TransactionItem[] = [];
|
74
103
|
// const settingParams: SolautoSettingsParametersInpArgs = {
|
@@ -86,6 +115,15 @@ describe("Solauto Marginfi tests", async () => {
|
|
86
115
|
// targetBoostToBps: none(),
|
87
116
|
// };
|
88
117
|
|
118
|
+
const settingParams: SolautoSettingsParametersInpArgs = {
|
119
|
+
boostToBps: client.solautoPositionSettings().boostToBps - 150,
|
120
|
+
boostGap: 50,
|
121
|
+
repayToBps: client.solautoPositionSettings().repayToBps - 150,
|
122
|
+
repayGap: 50,
|
123
|
+
automation: none(),
|
124
|
+
targetBoostToBps: none(),
|
125
|
+
};
|
126
|
+
|
89
127
|
// if (client.solautoPositionData === null) {
|
90
128
|
// transactionItems.push(
|
91
129
|
// new TransactionItem(async () => {
|
@@ -131,11 +169,18 @@ describe("Solauto Marginfi tests", async () => {
|
|
131
169
|
transactionItems.push(
|
132
170
|
new TransactionItem(
|
133
171
|
async (attemptNum) =>
|
134
|
-
await buildSolautoRebalanceTransaction(client,
|
172
|
+
await buildSolautoRebalanceTransaction(client, 4000, attemptNum),
|
135
173
|
"rebalance"
|
136
174
|
)
|
137
175
|
);
|
138
176
|
|
177
|
+
// transactionItems.push(
|
178
|
+
// new TransactionItem(
|
179
|
+
// async () => ({ tx: client.refresh() }),
|
180
|
+
// "refresh"
|
181
|
+
// )
|
182
|
+
// );
|
183
|
+
|
139
184
|
// transactionItems.push(
|
140
185
|
// new TransactionItem(
|
141
186
|
// async (attemptNum) =>
|