@haven-fi/solauto-sdk 1.0.581 → 1.0.583
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/constants/solautoConstants.d.ts +1 -0
- package/dist/constants/solautoConstants.d.ts.map +1 -1
- package/dist/constants/solautoConstants.js +4 -10
- package/dist/constants/switchboardConstants.d.ts.map +1 -1
- package/dist/constants/switchboardConstants.js +5 -5
- package/dist/generated/accounts/solautoPosition.js +2 -2
- package/dist/generated/errors/solauto.d.ts +14 -2
- package/dist/generated/errors/solauto.d.ts.map +1 -1
- package/dist/generated/errors/solauto.js +27 -5
- package/dist/generated/instructions/claimReferralFees.d.ts +1 -1
- package/dist/generated/instructions/claimReferralFees.d.ts.map +1 -1
- package/dist/generated/instructions/closePosition.d.ts +1 -1
- package/dist/generated/instructions/closePosition.d.ts.map +1 -1
- package/dist/generated/instructions/closePosition.js +2 -2
- package/dist/generated/instructions/marginfiOpenPosition.d.ts +1 -3
- package/dist/generated/instructions/marginfiOpenPosition.d.ts.map +1 -1
- package/dist/generated/instructions/marginfiOpenPosition.js +0 -1
- package/dist/generated/instructions/marginfiRebalance.d.ts +7 -3
- package/dist/generated/instructions/marginfiRebalance.d.ts.map +1 -1
- package/dist/generated/instructions/marginfiRebalance.js +3 -1
- package/dist/generated/types/index.d.ts +7 -1
- package/dist/generated/types/index.d.ts.map +1 -1
- package/dist/generated/types/index.js +7 -1
- package/dist/generated/types/positionData.d.ts +2 -2
- package/dist/generated/types/positionData.d.ts.map +1 -1
- package/dist/generated/types/positionData.js +1 -1
- package/dist/generated/types/positionState.d.ts +7 -7
- package/dist/generated/types/positionState.d.ts.map +1 -1
- package/dist/generated/types/positionState.js +3 -3
- package/dist/generated/types/{positionTokenUsage.d.ts → positionTokenState.d.ts} +6 -8
- package/dist/generated/types/positionTokenState.d.ts.map +1 -0
- package/dist/generated/types/{positionTokenUsage.js → positionTokenState.js} +6 -7
- package/dist/generated/types/rebalanceData.d.ts +7 -13
- package/dist/generated/types/rebalanceData.d.ts.map +1 -1
- package/dist/generated/types/rebalanceData.js +3 -6
- package/dist/generated/types/rebalanceDirection.d.ts +3 -2
- package/dist/generated/types/rebalanceDirection.d.ts.map +1 -1
- package/dist/generated/types/rebalanceDirection.js +3 -2
- package/dist/generated/types/rebalanceInstructionData.d.ts +27 -0
- package/dist/generated/types/rebalanceInstructionData.d.ts.map +1 -0
- package/dist/generated/types/rebalanceInstructionData.js +22 -0
- package/dist/generated/types/rebalanceStateValues.d.ts +27 -0
- package/dist/generated/types/rebalanceStateValues.d.ts.map +1 -0
- package/dist/generated/types/rebalanceStateValues.js +22 -0
- package/dist/generated/types/rebalanceStep.d.ts +15 -0
- package/dist/generated/types/rebalanceStep.d.ts.map +1 -0
- package/dist/generated/types/rebalanceStep.js +22 -0
- package/dist/generated/types/solautoRebalanceType.d.ts +4 -5
- package/dist/generated/types/solautoRebalanceType.d.ts.map +1 -1
- package/dist/generated/types/solautoRebalanceType.js +4 -5
- package/dist/generated/types/solautoSettingsParameters.d.ts +2 -15
- package/dist/generated/types/solautoSettingsParameters.d.ts.map +1 -1
- package/dist/generated/types/solautoSettingsParameters.js +1 -5
- package/dist/generated/types/solautoSettingsParametersInp.d.ts +1 -12
- package/dist/generated/types/solautoSettingsParametersInp.d.ts.map +1 -1
- package/dist/generated/types/solautoSettingsParametersInp.js +0 -3
- package/dist/generated/types/swapType.d.ts +15 -0
- package/dist/generated/types/swapType.d.ts.map +1 -0
- package/dist/generated/types/swapType.js +22 -0
- package/dist/generated/types/tokenBalanceChange.d.ts +21 -0
- package/dist/generated/types/tokenBalanceChange.d.ts.map +1 -0
- package/dist/generated/types/tokenBalanceChange.js +19 -0
- package/dist/generated/types/tokenBalanceChangeType.d.ts +18 -0
- package/dist/generated/types/tokenBalanceChangeType.d.ts.map +1 -0
- package/dist/generated/types/tokenBalanceChangeType.js +25 -0
- package/dist/generated/types/updatePositionData.d.ts +2 -2
- package/dist/generated/types/updatePositionData.d.ts.map +1 -1
- package/dist/generated/types/updatePositionData.js +1 -1
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/marginfi-sdk/types/interestRateConfig.d.ts +3 -1
- package/dist/marginfi-sdk/types/interestRateConfig.d.ts.map +1 -1
- package/dist/marginfi-sdk/types/interestRateConfig.js +2 -1
- package/dist/services/flashLoans/flProviderAggregator.d.ts +19 -0
- package/dist/services/flashLoans/flProviderAggregator.d.ts.map +1 -0
- package/dist/services/flashLoans/flProviderAggregator.js +46 -0
- package/dist/services/flashLoans/flProviderBase.d.ts +23 -0
- package/dist/services/flashLoans/flProviderBase.d.ts.map +1 -0
- package/dist/services/flashLoans/flProviderBase.js +37 -0
- package/dist/services/flashLoans/index.d.ts +4 -0
- package/dist/services/flashLoans/index.d.ts.map +1 -0
- package/dist/services/flashLoans/index.js +19 -0
- package/dist/services/flashLoans/marginfiFlProvider.d.ts +25 -0
- package/dist/services/flashLoans/marginfiFlProvider.d.ts.map +1 -0
- package/dist/services/flashLoans/marginfiFlProvider.js +246 -0
- package/dist/services/index.d.ts +6 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +21 -0
- package/dist/services/rebalance/index.d.ts +3 -0
- package/dist/services/rebalance/index.d.ts.map +1 -0
- package/dist/services/rebalance/index.js +18 -0
- package/dist/services/rebalance/rebalanceSwapManager.d.ts +30 -0
- package/dist/services/rebalance/rebalanceSwapManager.d.ts.map +1 -0
- package/dist/services/rebalance/rebalanceSwapManager.js +144 -0
- package/dist/services/rebalance/rebalanceTxBuilder.d.ts +22 -0
- package/dist/services/rebalance/rebalanceTxBuilder.d.ts.map +1 -0
- package/dist/services/rebalance/rebalanceTxBuilder.js +200 -0
- package/dist/services/rebalance/rebalanceValues.d.ts +31 -0
- package/dist/services/rebalance/rebalanceValues.d.ts.map +1 -0
- package/dist/services/rebalance/rebalanceValues.js +118 -0
- package/dist/services/rebalance/solautoFees.d.ts +13 -0
- package/dist/services/rebalance/solautoFees.d.ts.map +1 -0
- package/dist/services/rebalance/solautoFees.js +54 -0
- package/dist/services/solauto/index.d.ts +5 -0
- package/dist/services/solauto/index.d.ts.map +1 -0
- package/dist/{clients → services/solauto}/referralStateManager.d.ts +4 -5
- package/dist/services/solauto/referralStateManager.d.ts.map +1 -0
- package/dist/{clients → services/solauto}/referralStateManager.js +10 -12
- package/dist/services/solauto/solautoClient.d.ts +59 -0
- package/dist/services/solauto/solautoClient.d.ts.map +1 -0
- package/dist/{clients → services/solauto}/solautoClient.js +78 -109
- package/dist/services/solauto/solautoMarginfiClient.d.ts +34 -0
- package/dist/services/solauto/solautoMarginfiClient.d.ts.map +1 -0
- package/dist/services/solauto/solautoMarginfiClient.js +322 -0
- package/dist/{clients → services/solauto}/txHandler.d.ts +11 -2
- package/dist/services/solauto/txHandler.d.ts.map +1 -0
- package/dist/services/solauto/txHandler.js +38 -0
- package/dist/services/swap/index.d.ts +2 -0
- package/dist/services/swap/index.d.ts.map +1 -0
- package/dist/{utils/solauto → services/swap}/index.js +1 -2
- package/dist/services/swap/jupSwapManager.d.ts +37 -0
- package/dist/services/swap/jupSwapManager.d.ts.map +1 -0
- package/dist/services/swap/jupSwapManager.js +108 -0
- package/dist/services/transactions/index.d.ts.map +1 -0
- package/dist/{transactions → services/transactions}/transactionUtils.d.ts +3 -5
- package/dist/services/transactions/transactionUtils.d.ts.map +1 -0
- package/dist/{transactions → services/transactions}/transactionUtils.js +29 -137
- package/dist/{transactions → services/transactions}/transactionsManager.d.ts +4 -4
- package/dist/services/transactions/transactionsManager.d.ts.map +1 -0
- package/dist/{transactions → services/transactions}/transactionsManager.js +10 -8
- package/dist/solautoPosition/index.d.ts +4 -0
- package/dist/solautoPosition/index.d.ts.map +1 -0
- package/dist/solautoPosition/index.js +19 -0
- package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts +14 -0
- package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts.map +1 -0
- package/dist/solautoPosition/marginfiSolautoPositionEx.js +73 -0
- package/dist/solautoPosition/solautoPositionEx.d.ts +67 -0
- package/dist/solautoPosition/solautoPositionEx.d.ts.map +1 -0
- package/dist/solautoPosition/solautoPositionEx.js +143 -0
- package/dist/solautoPosition/utils.d.ts +17 -0
- package/dist/solautoPosition/utils.d.ts.map +1 -0
- package/dist/solautoPosition/utils.js +115 -0
- package/dist/types/solauto.d.ts +19 -1
- package/dist/types/solauto.d.ts.map +1 -1
- package/dist/utils/generalUtils.d.ts +1 -0
- package/dist/utils/generalUtils.d.ts.map +1 -1
- package/dist/utils/generalUtils.js +9 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/jitoUtils.d.ts.map +1 -1
- package/dist/utils/jupiterUtils.d.ts +3 -28
- package/dist/utils/jupiterUtils.d.ts.map +1 -1
- package/dist/utils/jupiterUtils.js +2 -73
- package/dist/utils/marginfiUtils.d.ts +4 -3
- package/dist/utils/marginfiUtils.d.ts.map +1 -1
- package/dist/utils/marginfiUtils.js +47 -18
- package/dist/utils/numberUtils.d.ts +3 -6
- package/dist/utils/numberUtils.d.ts.map +1 -1
- package/dist/utils/numberUtils.js +13 -48
- package/dist/utils/priceUtils.js +1 -1
- package/dist/utils/solanaUtils.js +2 -2
- package/dist/utils/{solauto/generalUtils.d.ts → solautoUtils.d.ts} +9 -16
- package/dist/utils/solautoUtils.d.ts.map +1 -0
- package/dist/utils/{solauto/generalUtils.js → solautoUtils.js} +48 -173
- package/dist/utils/switchboardUtils.d.ts +1 -1
- package/dist/utils/switchboardUtils.d.ts.map +1 -1
- package/dist/utils/switchboardUtils.js +10 -9
- package/local/createTokenAccounts.ts +1 -5
- package/local/logPositions.ts +22 -68
- package/local/shared.ts +29 -24
- package/local/txSandbox.ts +27 -0
- package/local/updateMarginfiLUT.ts +80 -4
- package/package.json +4 -7
- package/src/constants/solautoConstants.ts +4 -9
- package/src/constants/switchboardConstants.ts +10 -5
- package/src/generated/accounts/solautoPosition.ts +2 -2
- package/src/generated/errors/solauto.ts +45 -4
- package/src/generated/instructions/claimReferralFees.ts +1 -1
- package/src/generated/instructions/closePosition.ts +3 -3
- package/src/generated/instructions/marginfiOpenPosition.ts +0 -7
- package/src/generated/instructions/marginfiRebalance.ts +12 -3
- package/src/generated/types/index.ts +7 -1
- package/src/generated/types/positionData.ts +3 -3
- package/src/generated/types/positionState.ts +12 -12
- package/src/generated/types/{positionTokenUsage.ts → positionTokenState.ts} +13 -16
- package/src/generated/types/rebalanceData.ts +16 -27
- package/src/generated/types/rebalanceDirection.ts +1 -0
- package/src/generated/types/rebalanceInstructionData.ts +62 -0
- package/src/generated/types/rebalanceStateValues.ts +59 -0
- package/src/generated/types/rebalanceStep.ts +25 -0
- package/src/generated/types/solautoRebalanceType.ts +0 -1
- package/src/generated/types/solautoSettingsParameters.ts +4 -25
- package/src/generated/types/solautoSettingsParametersInp.ts +2 -24
- package/src/generated/types/swapType.ts +22 -0
- package/src/generated/types/tokenBalanceChange.ts +46 -0
- package/src/generated/types/tokenBalanceChangeType.ts +28 -0
- package/src/generated/types/updatePositionData.ts +3 -3
- package/src/index.ts +8 -8
- package/src/marginfi-sdk/types/interestRateConfig.ts +6 -2
- package/src/services/flashLoans/flProviderAggregator.ts +74 -0
- package/src/services/flashLoans/flProviderBase.ts +88 -0
- package/src/services/flashLoans/index.ts +3 -0
- package/src/services/flashLoans/marginfiFlProvider.ts +392 -0
- package/src/services/index.ts +5 -0
- package/src/services/rebalance/index.ts +2 -0
- package/src/services/rebalance/rebalanceSwapManager.ts +218 -0
- package/src/services/rebalance/rebalanceTxBuilder.ts +330 -0
- package/src/services/rebalance/rebalanceValues.ts +232 -0
- package/src/services/rebalance/solautoFees.ts +61 -0
- package/src/services/solauto/index.ts +4 -0
- package/src/{clients → services/solauto}/referralStateManager.ts +17 -31
- package/src/{clients → services/solauto}/solautoClient.ts +142 -205
- package/src/services/solauto/solautoMarginfiClient.ts +472 -0
- package/src/services/solauto/txHandler.ts +69 -0
- package/src/services/swap/index.ts +1 -0
- package/src/services/swap/jupSwapManager.ts +189 -0
- package/src/{transactions → services/transactions}/transactionUtils.ts +40 -280
- package/src/{transactions → services/transactions}/transactionsManager.ts +15 -10
- package/src/solautoPosition/index.ts +3 -0
- package/src/solautoPosition/marginfiSolautoPositionEx.ts +111 -0
- package/src/solautoPosition/solautoPositionEx.ts +281 -0
- package/src/solautoPosition/utils.ts +188 -0
- package/src/types/solauto.ts +30 -2
- package/src/utils/generalUtils.ts +9 -1
- package/src/utils/index.ts +2 -2
- package/src/utils/jitoUtils.ts +0 -4
- package/src/utils/jupiterUtils.ts +3 -164
- package/src/utils/marginfiUtils.ts +77 -43
- package/src/utils/numberUtils.ts +18 -77
- package/src/utils/solanaUtils.ts +1 -1
- package/src/utils/{solauto/generalUtils.ts → solautoUtils.ts} +66 -288
- package/src/utils/switchboardUtils.ts +15 -17
- package/tests/transactions/shared.ts +135 -0
- package/tests/transactions/solautoMarginfi.ts +8 -198
- package/tests/unit/accounts.ts +3 -10
- package/tests/unit/lookupTables.ts +23 -2
- package/tests/unit/rebalanceCalculations.ts +65 -343
- package/dist/clients/index.d.ts +0 -5
- package/dist/clients/index.d.ts.map +0 -1
- package/dist/clients/referralStateManager.d.ts.map +0 -1
- package/dist/clients/solautoClient.d.ts +0 -71
- package/dist/clients/solautoClient.d.ts.map +0 -1
- package/dist/clients/solautoMarginfiClient.d.ts +0 -51
- package/dist/clients/solautoMarginfiClient.d.ts.map +0 -1
- package/dist/clients/solautoMarginfiClient.js +0 -497
- package/dist/clients/txHandler.d.ts.map +0 -1
- package/dist/clients/txHandler.js +0 -23
- package/dist/generated/types/positionTokenUsage.d.ts.map +0 -1
- package/dist/transactions/index.d.ts.map +0 -1
- package/dist/transactions/transactionUtils.d.ts.map +0 -1
- package/dist/transactions/transactionsManager.d.ts.map +0 -1
- package/dist/utils/solauto/generalUtils.d.ts.map +0 -1
- package/dist/utils/solauto/index.d.ts +0 -3
- package/dist/utils/solauto/index.d.ts.map +0 -1
- package/dist/utils/solauto/rebalanceUtils.d.ts +0 -30
- package/dist/utils/solauto/rebalanceUtils.d.ts.map +0 -1
- package/dist/utils/solauto/rebalanceUtils.js +0 -287
- package/src/clients/index.ts +0 -4
- package/src/clients/solautoMarginfiClient.ts +0 -774
- package/src/clients/txHandler.ts +0 -38
- package/src/utils/solauto/index.ts +0 -2
- package/src/utils/solauto/rebalanceUtils.ts +0 -562
- package/dist/{clients → services/solauto}/index.js +2 -2
- /package/dist/{transactions → services/transactions}/index.d.ts +0 -0
- /package/dist/{transactions → services/transactions}/index.js +0 -0
- /package/src/{transactions → services/transactions}/index.ts +0 -0
@@ -0,0 +1,144 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.RebalanceSwapManager = void 0;
|
4
|
+
const swap_1 = require("../swap");
|
5
|
+
const generated_1 = require("../../generated");
|
6
|
+
const utils_1 = require("../../utils");
|
7
|
+
const umi_web3js_adapters_1 = require("@metaplex-foundation/umi-web3js-adapters");
|
8
|
+
class RebalanceSwapManager {
|
9
|
+
constructor(client, values, flRequirements, targetLiqUtilizationRateBps) {
|
10
|
+
this.client = client;
|
11
|
+
this.values = values;
|
12
|
+
this.flRequirements = flRequirements;
|
13
|
+
this.targetLiqUtilizationRateBps = targetLiqUtilizationRateBps;
|
14
|
+
this.jupSwapManager = new swap_1.JupSwapManager(client.signer);
|
15
|
+
}
|
16
|
+
isBoost() {
|
17
|
+
return this.values.rebalanceDirection === generated_1.RebalanceDirection.Boost;
|
18
|
+
}
|
19
|
+
usdToSwap() {
|
20
|
+
return Math.abs(this.values.debtAdjustmentUsd);
|
21
|
+
}
|
22
|
+
postRebalanceLiqUtilizationRateBps(swapOutputAmount) {
|
23
|
+
let supplyUsd = this.client.solautoPosition.supplyUsd();
|
24
|
+
// TODO: add token balance change
|
25
|
+
let debtUsd = this.client.solautoPosition.debtUsd();
|
26
|
+
const outputToken = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.isBoost()
|
27
|
+
? this.client.solautoPosition.state().supply.mint
|
28
|
+
: this.client.solautoPosition.state().debt.mint);
|
29
|
+
const swapOutputUsd = swapOutputAmount
|
30
|
+
? (0, utils_1.fromBaseUnit)(swapOutputAmount, (0, utils_1.tokenInfo)(outputToken).decimals) *
|
31
|
+
((0, utils_1.safeGetPrice)(outputToken) ?? 0)
|
32
|
+
: this.usdToSwap();
|
33
|
+
supplyUsd = this.isBoost()
|
34
|
+
? supplyUsd + swapOutputUsd
|
35
|
+
: supplyUsd - this.usdToSwap();
|
36
|
+
debtUsd = this.isBoost()
|
37
|
+
? debtUsd + this.usdToSwap()
|
38
|
+
: debtUsd - swapOutputUsd;
|
39
|
+
return (0, utils_1.getLiqUtilzationRateBps)(supplyUsd, debtUsd, this.client.solautoPosition.state().liqThresholdBps ?? 0);
|
40
|
+
}
|
41
|
+
async findSufficientQuote(swapInput, criteria) {
|
42
|
+
let swapQuote;
|
43
|
+
let insufficient = false;
|
44
|
+
for (let i = 0; i < 10; i++) {
|
45
|
+
(0, utils_1.consoleLog)("Finding sufficient quote...");
|
46
|
+
swapQuote = await this.jupSwapManager.getQuote(swapInput);
|
47
|
+
const outputAmount = parseInt(swapQuote.outAmount);
|
48
|
+
const postRebalanceRate = this.postRebalanceLiqUtilizationRateBps(BigInt(outputAmount));
|
49
|
+
insufficient = criteria.minOutputAmount
|
50
|
+
? outputAmount < Number(criteria.minOutputAmount)
|
51
|
+
: criteria.minLiqUtilizationRateBps
|
52
|
+
? postRebalanceRate < criteria.minLiqUtilizationRateBps
|
53
|
+
: postRebalanceRate > criteria.maxLiqUtilizationRateBps;
|
54
|
+
if (insufficient) {
|
55
|
+
(0, utils_1.consoleLog)("Insufficient swap quote:", swapQuote);
|
56
|
+
swapInput.amount = this.bigIntWithIncrement(swapInput.amount, 0.01);
|
57
|
+
}
|
58
|
+
else {
|
59
|
+
break;
|
60
|
+
}
|
61
|
+
}
|
62
|
+
return swapQuote;
|
63
|
+
}
|
64
|
+
swapDetails() {
|
65
|
+
const input = this.isBoost()
|
66
|
+
? this.client.solautoPosition.state().debt
|
67
|
+
: this.client.solautoPosition.state().supply;
|
68
|
+
const output = this.isBoost()
|
69
|
+
? this.client.solautoPosition.state().supply
|
70
|
+
: this.client.solautoPosition.state().debt;
|
71
|
+
let inputAmount = (0, utils_1.toBaseUnit)(this.usdToSwap() / (0, utils_1.safeGetPrice)(input.mint), input.decimals);
|
72
|
+
return {
|
73
|
+
input,
|
74
|
+
output,
|
75
|
+
inputAmount,
|
76
|
+
};
|
77
|
+
}
|
78
|
+
bigIntWithIncrement(num, inc) {
|
79
|
+
return num + BigInt(Math.round(Number(num) * inc));
|
80
|
+
}
|
81
|
+
async setSwapParams(attemptNum) {
|
82
|
+
const rebalanceToZero = this.targetLiqUtilizationRateBps === 0;
|
83
|
+
let { input, output, inputAmount } = this.swapDetails();
|
84
|
+
let outputAmount = rebalanceToZero
|
85
|
+
? output.amountUsed.baseUnit +
|
86
|
+
BigInt(Math.round(Number(output.amountUsed.baseUnit) *
|
87
|
+
// Add this small percentage to account for the APR on the debt between now and the transaction
|
88
|
+
0.0001))
|
89
|
+
: (0, utils_1.toBaseUnit)(this.usdToSwap() / (0, utils_1.safeGetPrice)(output.mint), output.decimals);
|
90
|
+
const flashLoanRepayFromDebt = !this.isBoost() &&
|
91
|
+
this.flRequirements &&
|
92
|
+
this.flRequirements.liquiditySource === generated_1.TokenType.Debt;
|
93
|
+
const exactOut = flashLoanRepayFromDebt && !rebalanceToZero;
|
94
|
+
const exactIn = !exactOut;
|
95
|
+
if (exactIn && (rebalanceToZero || this.values.repayingCloseToMaxLtv)) {
|
96
|
+
inputAmount = this.bigIntWithIncrement(inputAmount, 0.005);
|
97
|
+
}
|
98
|
+
const swapAmount = exactOut
|
99
|
+
? this.flRequirements
|
100
|
+
? this.bigIntWithIncrement(outputAmount, this.flRequirements.flFeeBps ?? 0)
|
101
|
+
: outputAmount
|
102
|
+
: inputAmount;
|
103
|
+
const swapInput = {
|
104
|
+
inputMint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(input.mint),
|
105
|
+
outputMint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(output.mint),
|
106
|
+
exactIn,
|
107
|
+
exactOut,
|
108
|
+
amount: swapAmount,
|
109
|
+
};
|
110
|
+
(0, utils_1.consoleLog)("Swap input:", swapInput);
|
111
|
+
if (exactIn && (rebalanceToZero || this.values.repayingCloseToMaxLtv)) {
|
112
|
+
this.swapQuote = await this.findSufficientQuote(swapInput, {
|
113
|
+
minOutputAmount: rebalanceToZero ? outputAmount : undefined,
|
114
|
+
maxLiqUtilizationRateBps: this.values.repayingCloseToMaxLtv
|
115
|
+
? (0, utils_1.maxRepayToBps)(this.client.solautoPosition.state().maxLtvBps ?? 0, this.client.solautoPosition.state().liqThresholdBps ?? 0) - 15
|
116
|
+
: undefined,
|
117
|
+
});
|
118
|
+
}
|
119
|
+
if (this.flRequirements) {
|
120
|
+
this.flBorrowAmount = exactOut
|
121
|
+
? outputAmount
|
122
|
+
: this.swapQuote
|
123
|
+
? BigInt(parseInt(this.swapQuote.inAmount))
|
124
|
+
: inputAmount;
|
125
|
+
}
|
126
|
+
this.swapParams = {
|
127
|
+
...swapInput,
|
128
|
+
destinationWallet: flashLoanRepayFromDebt
|
129
|
+
? (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.client.signer.publicKey)
|
130
|
+
: this.client.solautoPosition.publicKey,
|
131
|
+
slippageIncFactor: 0.2 + attemptNum * 0.25,
|
132
|
+
};
|
133
|
+
}
|
134
|
+
async getSwapTxData() {
|
135
|
+
const { jupQuote, lookupTableAddresses, setupInstructions, swapIx } = await this.jupSwapManager.getJupSwapTxData(this.swapParams);
|
136
|
+
return {
|
137
|
+
swapQuote: jupQuote,
|
138
|
+
lookupTableAddresses,
|
139
|
+
setupInstructions,
|
140
|
+
swapIx,
|
141
|
+
};
|
142
|
+
}
|
143
|
+
}
|
144
|
+
exports.RebalanceSwapManager = RebalanceSwapManager;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { SolautoClient } from "../solauto";
|
2
|
+
import { TransactionItemInputs } from "../../types";
|
3
|
+
export declare class RebalanceTxBuilder {
|
4
|
+
private client;
|
5
|
+
private targetLiqUtilizationRateBps?;
|
6
|
+
private values;
|
7
|
+
private rebalanceType;
|
8
|
+
private swapManager;
|
9
|
+
private flRequirements?;
|
10
|
+
constructor(client: SolautoClient, targetLiqUtilizationRateBps?: number | undefined);
|
11
|
+
private shouldProceedWithRebalance;
|
12
|
+
private getRebalanceValues;
|
13
|
+
private getFlLiquiditySource;
|
14
|
+
private flashLoanRequirements;
|
15
|
+
private getFlashLoanDetails;
|
16
|
+
private setRebalanceType;
|
17
|
+
private setRebalanceDetails;
|
18
|
+
private refreshBeforeRebalance;
|
19
|
+
private assembleTransaction;
|
20
|
+
buildRebalanceTx(attemptNum: number): Promise<TransactionItemInputs | undefined>;
|
21
|
+
}
|
22
|
+
//# sourceMappingURL=rebalanceTxBuilder.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"rebalanceTxBuilder.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/rebalanceTxBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAGL,qBAAqB,EACtB,MAAM,aAAa,CAAC;AA2BrB,qBAAa,kBAAkB;IAO3B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,2BAA2B,CAAC;IAPtC,OAAO,CAAC,MAAM,CAAmB;IACjC,OAAO,CAAC,aAAa,CAAwB;IAC7C,OAAO,CAAC,WAAW,CAAwB;IAC3C,OAAO,CAAC,cAAc,CAAC,CAAwB;gBAGrC,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,YAAA;YAGhC,0BAA0B;IAQxC,OAAO,CAAC,kBAAkB;IAa1B,OAAO,CAAC,oBAAoB;YAyCd,qBAAqB;IAyCnC,OAAO,CAAC,mBAAmB;IAwB3B,OAAO,CAAC,gBAAgB;YAgCV,mBAAmB;YAoBnB,sBAAsB;YA4BtB,mBAAmB;IAkEpB,gBAAgB,CAC3B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC;CAW9C"}
|
@@ -0,0 +1,200 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.RebalanceTxBuilder = void 0;
|
4
|
+
const utils_1 = require("../../utils");
|
5
|
+
const rebalanceValues_1 = require("./rebalanceValues");
|
6
|
+
const solautoFees_1 = require("./solautoFees");
|
7
|
+
const generated_1 = require("../../generated");
|
8
|
+
const web3_js_1 = require("@solana/web3.js");
|
9
|
+
const rebalanceSwapManager_1 = require("./rebalanceSwapManager");
|
10
|
+
const umi_web3js_adapters_1 = require("@metaplex-foundation/umi-web3js-adapters");
|
11
|
+
const umi_1 = require("@metaplex-foundation/umi");
|
12
|
+
class RebalanceTxBuilder {
|
13
|
+
constructor(client, targetLiqUtilizationRateBps) {
|
14
|
+
this.client = client;
|
15
|
+
this.targetLiqUtilizationRateBps = targetLiqUtilizationRateBps;
|
16
|
+
}
|
17
|
+
async shouldProceedWithRebalance() {
|
18
|
+
return (this.client.solautoPosition.supplyUsd() > 0 &&
|
19
|
+
(this.targetLiqUtilizationRateBps !== undefined ||
|
20
|
+
this.client.solautoPosition.eligibleForRebalance()));
|
21
|
+
}
|
22
|
+
getRebalanceValues(flFee) {
|
23
|
+
return (0, rebalanceValues_1.getRebalanceValues)(this.client.solautoPosition, new solautoFees_1.SolautoFeesBps(this.client.isReferred(), this.targetLiqUtilizationRateBps, this.client.solautoPosition.netWorthUsd()), flFee ?? 0, this.targetLiqUtilizationRateBps);
|
24
|
+
}
|
25
|
+
getFlLiquiditySource(supplyLiquidityAvailable, debtLiquidityAvailable) {
|
26
|
+
const debtAdjustmentUsd = Math.abs(this.values.debtAdjustmentUsd);
|
27
|
+
const insufficientLiquidity = (amountNeededUsd, liquidityAvailable, tokenMint) => {
|
28
|
+
return (amountNeededUsd >
|
29
|
+
(0, utils_1.fromBaseUnit)(liquidityAvailable, (0, utils_1.tokenInfo)(tokenMint).decimals) *
|
30
|
+
((0, utils_1.safeGetPrice)(tokenMint) ?? 0) *
|
31
|
+
0.95);
|
32
|
+
};
|
33
|
+
const insufficientSupplyLiquidity = insufficientLiquidity(debtAdjustmentUsd, supplyLiquidityAvailable, this.client.solautoPosition.supplyMint());
|
34
|
+
const insufficientDebtLiquidity = insufficientLiquidity(debtAdjustmentUsd, debtLiquidityAvailable, this.client.solautoPosition.debtMint());
|
35
|
+
let useDebtLiquidity = this.values.rebalanceDirection === generated_1.RebalanceDirection.Boost ||
|
36
|
+
insufficientSupplyLiquidity;
|
37
|
+
if (useDebtLiquidity) {
|
38
|
+
return !insufficientDebtLiquidity ? generated_1.TokenType.Debt : undefined;
|
39
|
+
}
|
40
|
+
else {
|
41
|
+
return !insufficientSupplyLiquidity ? generated_1.TokenType.Supply : undefined;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
async flashLoanRequirements(attemptNum) {
|
45
|
+
const maxLtvRateBps = (0, utils_1.getMaxLiqUtilizationRateBps)(this.client.solautoPosition.state().maxLtvBps, this.client.solautoPosition.state().liqThresholdBps, 0.02);
|
46
|
+
if (this.values.intermediaryLiqUtilizationRateBps < maxLtvRateBps) {
|
47
|
+
return undefined;
|
48
|
+
}
|
49
|
+
const stdFlLiquiditySource = this.getFlLiquiditySource(this.client.flProvider.liquidityAvailable(generated_1.TokenType.Supply), this.client.flProvider.liquidityAvailable(generated_1.TokenType.Debt));
|
50
|
+
if ((attemptNum ?? 0) >= 3 || stdFlLiquiditySource === undefined) {
|
51
|
+
const { supplyBalance, debtBalance } = await this.client.signerBalances();
|
52
|
+
const signerFlLiquiditySource = this.getFlLiquiditySource(supplyBalance, debtBalance);
|
53
|
+
if (signerFlLiquiditySource) {
|
54
|
+
return {
|
55
|
+
liquiditySource: signerFlLiquiditySource,
|
56
|
+
signerFlashLoan: true,
|
57
|
+
};
|
58
|
+
}
|
59
|
+
else {
|
60
|
+
throw new Error(`Insufficient liquidity to perform the transaction`);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
return {
|
65
|
+
liquiditySource: stdFlLiquiditySource,
|
66
|
+
flFeeBps: this.client.flProvider.flFeeBps(stdFlLiquiditySource),
|
67
|
+
};
|
68
|
+
}
|
69
|
+
}
|
70
|
+
getFlashLoanDetails() {
|
71
|
+
if (!this.flRequirements) {
|
72
|
+
throw new Error("Flash loan requirements data needed");
|
73
|
+
}
|
74
|
+
const boosting = this.values.rebalanceDirection === generated_1.RebalanceDirection.Boost;
|
75
|
+
const useDebtLiquidity = this.flRequirements.liquiditySource === generated_1.TokenType.Debt;
|
76
|
+
let flashLoanToken = undefined;
|
77
|
+
if (boosting || useDebtLiquidity) {
|
78
|
+
flashLoanToken = this.client.solautoPosition.state().debt;
|
79
|
+
}
|
80
|
+
else {
|
81
|
+
flashLoanToken = this.client.solautoPosition.state().supply;
|
82
|
+
}
|
83
|
+
return {
|
84
|
+
...this.flRequirements,
|
85
|
+
baseUnitAmount: this.swapManager.flBorrowAmount,
|
86
|
+
mint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(flashLoanToken.mint),
|
87
|
+
};
|
88
|
+
}
|
89
|
+
setRebalanceType() {
|
90
|
+
if (this.flRequirements) {
|
91
|
+
const tokenBalanceChangeType = this.values.tokenBalanceChange?.changeType;
|
92
|
+
const firstRebalanceTokenChanges = tokenBalanceChangeType === generated_1.TokenBalanceChangeType.PreSwapDeposit;
|
93
|
+
const lastRebalanceTokenChanges = [
|
94
|
+
generated_1.TokenBalanceChangeType.PostSwapDeposit,
|
95
|
+
generated_1.TokenBalanceChangeType.PostRebalanceWithdrawDebtToken,
|
96
|
+
generated_1.TokenBalanceChangeType.PostRebalanceWithdrawSupplyToken,
|
97
|
+
].includes(tokenBalanceChangeType ?? generated_1.TokenBalanceChangeType.None);
|
98
|
+
const swapType = this.swapManager.swapParams.exactIn
|
99
|
+
? generated_1.SwapType.ExactIn
|
100
|
+
: generated_1.SwapType.ExactOut;
|
101
|
+
if ((firstRebalanceTokenChanges && swapType === generated_1.SwapType.ExactIn) ||
|
102
|
+
(lastRebalanceTokenChanges && swapType === generated_1.SwapType.ExactOut)) {
|
103
|
+
this.rebalanceType = generated_1.SolautoRebalanceType.DoubleRebalanceWithFL;
|
104
|
+
}
|
105
|
+
else {
|
106
|
+
this.rebalanceType =
|
107
|
+
swapType === generated_1.SwapType.ExactOut
|
108
|
+
? generated_1.SolautoRebalanceType.FLRebalanceThenSwap
|
109
|
+
: generated_1.SolautoRebalanceType.FLSwapThenRebalance;
|
110
|
+
}
|
111
|
+
}
|
112
|
+
else {
|
113
|
+
this.rebalanceType = generated_1.SolautoRebalanceType.Regular;
|
114
|
+
}
|
115
|
+
(0, utils_1.consoleLog)("Rebalance type:", this.rebalanceType);
|
116
|
+
}
|
117
|
+
async setRebalanceDetails(attemptNum) {
|
118
|
+
this.values = this.getRebalanceValues();
|
119
|
+
this.flRequirements = await this.flashLoanRequirements(attemptNum);
|
120
|
+
if (this.flRequirements?.flFeeBps) {
|
121
|
+
this.values = this.getRebalanceValues(this.flRequirements.flFeeBps);
|
122
|
+
}
|
123
|
+
(0, utils_1.consoleLog)("Rebalance values:", this.values);
|
124
|
+
this.swapManager = new rebalanceSwapManager_1.RebalanceSwapManager(this.client, this.values, this.flRequirements, this.targetLiqUtilizationRateBps);
|
125
|
+
await this.swapManager.setSwapParams(attemptNum);
|
126
|
+
this.setRebalanceType();
|
127
|
+
}
|
128
|
+
async refreshBeforeRebalance() {
|
129
|
+
if (this.client.selfManaged ||
|
130
|
+
this.client.contextUpdates.supplyAdjustment > BigInt(0) ||
|
131
|
+
this.client.contextUpdates.debtAdjustment > BigInt(0)) {
|
132
|
+
return false;
|
133
|
+
}
|
134
|
+
// Rebalance ix will already refresh internally if position is self managed
|
135
|
+
if (!this.client.solautoPosition.data().position) {
|
136
|
+
return true;
|
137
|
+
}
|
138
|
+
const utilizationRateDiff = Math.abs(await this.client.solautoPosition.utilizationRateBpsDrift());
|
139
|
+
(0, utils_1.consoleLog)("Liq utilization rate diff:", utilizationRateDiff);
|
140
|
+
if (utilizationRateDiff >= 10) {
|
141
|
+
(0, utils_1.consoleLog)("Refreshing before rebalance");
|
142
|
+
return true;
|
143
|
+
}
|
144
|
+
(0, utils_1.consoleLog)("Not refreshing before rebalance");
|
145
|
+
return false;
|
146
|
+
}
|
147
|
+
async assembleTransaction() {
|
148
|
+
const { swapQuote, lookupTableAddresses, setupInstructions, swapIx } = await this.swapManager.getSwapTxData();
|
149
|
+
const flashLoanDetails = this.flRequirements
|
150
|
+
? this.getFlashLoanDetails()
|
151
|
+
: undefined;
|
152
|
+
let tx = (0, umi_1.transactionBuilder)();
|
153
|
+
if (await this.refreshBeforeRebalance()) {
|
154
|
+
tx = tx.add(this.client.refreshIx());
|
155
|
+
}
|
156
|
+
const rebalanceDetails = {
|
157
|
+
values: this.values,
|
158
|
+
rebalanceType: this.rebalanceType,
|
159
|
+
flashLoan: flashLoanDetails,
|
160
|
+
swapQuote,
|
161
|
+
targetLiqUtilizationRateBps: this.targetLiqUtilizationRateBps,
|
162
|
+
};
|
163
|
+
const firstRebalance = this.client.rebalanceIx(generated_1.RebalanceStep.PreSwap, rebalanceDetails);
|
164
|
+
const lastRebalance = this.client.rebalanceIx(generated_1.RebalanceStep.PostSwap, rebalanceDetails);
|
165
|
+
if (!flashLoanDetails) {
|
166
|
+
tx = tx.add([setupInstructions, firstRebalance, swapIx, lastRebalance]);
|
167
|
+
}
|
168
|
+
else {
|
169
|
+
(0, utils_1.consoleLog)("Flash loan details:", flashLoanDetails);
|
170
|
+
const exactOut = swapQuote.swapMode === "ExactOut";
|
171
|
+
const addFirstRebalance = (0, utils_1.hasFirstRebalance)(this.rebalanceType);
|
172
|
+
const addLastRebalance = (0, utils_1.hasLastRebalance)(this.rebalanceType);
|
173
|
+
const flashBorrowDest = exactOut
|
174
|
+
? (0, utils_1.getTokenAccount)(this.client.solautoPosition.publicKey, new web3_js_1.PublicKey(swapQuote.outputMint))
|
175
|
+
: (0, utils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.client.signer.publicKey), new web3_js_1.PublicKey(swapQuote.inputMint));
|
176
|
+
tx = tx.add([
|
177
|
+
setupInstructions,
|
178
|
+
this.client.flProvider.flashBorrow(flashLoanDetails, flashBorrowDest),
|
179
|
+
...(addFirstRebalance ? [firstRebalance] : []),
|
180
|
+
swapIx,
|
181
|
+
...(addLastRebalance ? [lastRebalance] : []),
|
182
|
+
this.client.flProvider.flashRepay(flashLoanDetails),
|
183
|
+
]);
|
184
|
+
}
|
185
|
+
return {
|
186
|
+
tx,
|
187
|
+
lookupTableAddresses,
|
188
|
+
};
|
189
|
+
}
|
190
|
+
async buildRebalanceTx(attemptNum) {
|
191
|
+
await this.client.solautoPosition.refreshPositionState();
|
192
|
+
if (!this.shouldProceedWithRebalance()) {
|
193
|
+
this.client.log("Not eligible for a rebalance");
|
194
|
+
return undefined;
|
195
|
+
}
|
196
|
+
await this.setRebalanceDetails(attemptNum);
|
197
|
+
return await this.assembleTransaction();
|
198
|
+
}
|
199
|
+
}
|
200
|
+
exports.RebalanceTxBuilder = RebalanceTxBuilder;
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { RebalanceDirection, TokenBalanceChange } from "../../generated";
|
2
|
+
import { SolautoPositionEx } from "../../solautoPosition";
|
3
|
+
import { SolautoFeesBps } from "./solautoFees";
|
4
|
+
export interface PositionValues {
|
5
|
+
supplyUsd: number;
|
6
|
+
debtUsd: number;
|
7
|
+
}
|
8
|
+
export interface DebtAdjustment {
|
9
|
+
debtAdjustmentUsd: number;
|
10
|
+
endResult: PositionValues;
|
11
|
+
intermediaryLiqUtilizationRateBps: number;
|
12
|
+
}
|
13
|
+
export interface RebalanceFeesBps {
|
14
|
+
solauto: number;
|
15
|
+
lpBorrow: number;
|
16
|
+
flashLoan: number;
|
17
|
+
}
|
18
|
+
interface ApplyDebtAdjustmentResult {
|
19
|
+
newPos: PositionValues;
|
20
|
+
intermediaryLiqUtilizationRateBps: number;
|
21
|
+
}
|
22
|
+
export declare function applyDebtAdjustmentUsd(debtAdjustmentUsd: number, pos: PositionValues, fees: RebalanceFeesBps, liqThreshold: number): ApplyDebtAdjustmentResult;
|
23
|
+
export declare function getDebtAdjustment(liqThreshold: number, pos: PositionValues, fees: RebalanceFeesBps, targetLiqUtilizationRateBps: number): DebtAdjustment;
|
24
|
+
export interface RebalanceValues extends DebtAdjustment {
|
25
|
+
rebalanceDirection: RebalanceDirection;
|
26
|
+
tokenBalanceChange?: TokenBalanceChange;
|
27
|
+
repayingCloseToMaxLtv: boolean;
|
28
|
+
}
|
29
|
+
export declare function getRebalanceValues(solautoPosition: SolautoPositionEx, solautoFeeBps: SolautoFeesBps, flFeeBps: number, targetLiqUtilizationRateBps?: number): RebalanceValues;
|
30
|
+
export {};
|
31
|
+
//# sourceMappingURL=rebalanceValues.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"rebalanceValues.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/rebalanceValues.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAEnB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAO1D,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,SAAS,EAAE,cAAc,CAAC;IAC1B,iCAAiC,EAAE,MAAM,CAAC;CAC3C;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,UAAU,yBAAyB;IACjC,MAAM,EAAE,cAAc,CAAC;IACvB,iCAAiC,EAAE,MAAM,CAAC;CAC3C;AAED,wBAAgB,sBAAsB,CACpC,iBAAiB,EAAE,MAAM,EACzB,GAAG,EAAE,cAAc,EACnB,IAAI,EAAE,gBAAgB,EACtB,YAAY,EAAE,MAAM,GACnB,yBAAyB,CA6B3B;AAED,wBAAgB,iBAAiB,CAC/B,YAAY,EAAE,MAAM,EACpB,GAAG,EAAE,cAAc,EACnB,IAAI,EAAE,gBAAgB,EACtB,2BAA2B,EAAE,MAAM,GAClC,cAAc,CA+BhB;AAqED,MAAM,WAAW,eAAgB,SAAQ,cAAc;IACrD,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC,qBAAqB,EAAE,OAAO,CAAC;CAChC;AAED,wBAAgB,kBAAkB,CAChC,eAAe,EAAE,iBAAiB,EAClC,aAAa,EAAE,cAAc,EAC7B,QAAQ,EAAE,MAAM,EAChB,2BAA2B,CAAC,EAAE,MAAM,GACnC,eAAe,CA2CjB"}
|
@@ -0,0 +1,118 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.applyDebtAdjustmentUsd = applyDebtAdjustmentUsd;
|
4
|
+
exports.getDebtAdjustment = getDebtAdjustment;
|
5
|
+
exports.getRebalanceValues = getRebalanceValues;
|
6
|
+
const generated_1 = require("../../generated");
|
7
|
+
const utils_1 = require("../../utils");
|
8
|
+
function applyDebtAdjustmentUsd(debtAdjustmentUsd, pos, fees, liqThreshold) {
|
9
|
+
const newPos = { ...pos };
|
10
|
+
const isBoost = debtAdjustmentUsd > 0;
|
11
|
+
const daMinusSolautoFees = debtAdjustmentUsd - debtAdjustmentUsd * (0, utils_1.fromBps)(fees.solauto);
|
12
|
+
const daWithFlashLoan = debtAdjustmentUsd * (1.0 + (0, utils_1.fromBps)(fees.flashLoan));
|
13
|
+
let intermediaryLiqUtilizationRateBps = 0;
|
14
|
+
if (isBoost) {
|
15
|
+
newPos.debtUsd +=
|
16
|
+
daWithFlashLoan * (0, utils_1.fromBps)(fees.lpBorrow) + daWithFlashLoan;
|
17
|
+
intermediaryLiqUtilizationRateBps = (0, utils_1.getLiqUtilzationRateBps)(newPos.supplyUsd, newPos.debtUsd, (0, utils_1.toBps)(liqThreshold));
|
18
|
+
newPos.supplyUsd += daMinusSolautoFees;
|
19
|
+
}
|
20
|
+
else {
|
21
|
+
newPos.supplyUsd += daWithFlashLoan;
|
22
|
+
intermediaryLiqUtilizationRateBps = (0, utils_1.getLiqUtilzationRateBps)(newPos.supplyUsd, newPos.debtUsd, (0, utils_1.toBps)(liqThreshold));
|
23
|
+
newPos.debtUsd += daMinusSolautoFees;
|
24
|
+
}
|
25
|
+
return { newPos, intermediaryLiqUtilizationRateBps };
|
26
|
+
}
|
27
|
+
function getDebtAdjustment(liqThreshold, pos, fees, targetLiqUtilizationRateBps) {
|
28
|
+
const isBoost = (0, utils_1.getLiqUtilzationRateBps)(pos.supplyUsd, pos.debtUsd, (0, utils_1.toBps)(liqThreshold)) <
|
29
|
+
targetLiqUtilizationRateBps;
|
30
|
+
const targetUtilizationRate = (0, utils_1.fromBps)(targetLiqUtilizationRateBps);
|
31
|
+
const actualizedFee = 1.0 - (0, utils_1.fromBps)(fees.solauto);
|
32
|
+
const flFee = (0, utils_1.fromBps)(fees.flashLoan);
|
33
|
+
const lpBorrowFee = (0, utils_1.fromBps)(fees.lpBorrow);
|
34
|
+
const debtAdjustmentUsd = isBoost
|
35
|
+
? (targetUtilizationRate * liqThreshold * pos.supplyUsd - pos.debtUsd) /
|
36
|
+
(1.0 +
|
37
|
+
lpBorrowFee +
|
38
|
+
flFee -
|
39
|
+
targetUtilizationRate * actualizedFee * liqThreshold)
|
40
|
+
: (targetUtilizationRate * liqThreshold * pos.supplyUsd - pos.debtUsd) /
|
41
|
+
(actualizedFee - targetUtilizationRate * liqThreshold * (1.0 + flFee));
|
42
|
+
const newPos = applyDebtAdjustmentUsd(debtAdjustmentUsd, pos, fees, liqThreshold);
|
43
|
+
return {
|
44
|
+
debtAdjustmentUsd,
|
45
|
+
endResult: newPos.newPos,
|
46
|
+
intermediaryLiqUtilizationRateBps: newPos.intermediaryLiqUtilizationRateBps,
|
47
|
+
};
|
48
|
+
}
|
49
|
+
function getTokenBalanceChange() {
|
50
|
+
// TODO: DCA, limit orders, take profit, stop loss, etc.
|
51
|
+
return undefined;
|
52
|
+
}
|
53
|
+
function getTargetLiqUtilizationRateBps(solautoPosition, targetLiqUtilizationRateBps, tokenBalanceChange) {
|
54
|
+
if (targetLiqUtilizationRateBps !== undefined) {
|
55
|
+
return targetLiqUtilizationRateBps;
|
56
|
+
}
|
57
|
+
const currentRate = solautoPosition.state().liqUtilizationRateBps;
|
58
|
+
if (currentRate <= solautoPosition.boostFromBps()) {
|
59
|
+
return solautoPosition.settings().boostToBps;
|
60
|
+
}
|
61
|
+
else if (currentRate >= solautoPosition.repayFromBps()) {
|
62
|
+
return solautoPosition.settings().repayToBps;
|
63
|
+
}
|
64
|
+
// TODO: DCA, limit orders, take profit, stop loss, etc.
|
65
|
+
// else if (tokenBalanceChange !== null) {
|
66
|
+
// return currentRate;
|
67
|
+
// }
|
68
|
+
throw new Error("Invalid rebalance condition");
|
69
|
+
}
|
70
|
+
function getAdjustedPositionValues(solautoPosition, tokenBalanceChange) {
|
71
|
+
let supplyUsd = solautoPosition.supplyUsd();
|
72
|
+
const debtUsd = solautoPosition.debtUsd();
|
73
|
+
if (tokenBalanceChange) {
|
74
|
+
const tb = tokenBalanceChange;
|
75
|
+
switch (tb.changeType) {
|
76
|
+
case generated_1.TokenBalanceChangeType.PreSwapDeposit:
|
77
|
+
case generated_1.TokenBalanceChangeType.PostSwapDeposit:
|
78
|
+
supplyUsd += Number(tb.amountUsd);
|
79
|
+
break;
|
80
|
+
case generated_1.TokenBalanceChangeType.PostRebalanceWithdrawDebtToken:
|
81
|
+
case generated_1.TokenBalanceChangeType.PostRebalanceWithdrawSupplyToken:
|
82
|
+
supplyUsd -= Number(tb.amountUsd);
|
83
|
+
break;
|
84
|
+
default:
|
85
|
+
break;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
return {
|
89
|
+
supplyUsd,
|
90
|
+
debtUsd,
|
91
|
+
};
|
92
|
+
}
|
93
|
+
function getRebalanceDirection(solautoPosition, targetLtvBps) {
|
94
|
+
return solautoPosition.state().liqUtilizationRateBps < targetLtvBps
|
95
|
+
? generated_1.RebalanceDirection.Boost
|
96
|
+
: generated_1.RebalanceDirection.Repay;
|
97
|
+
}
|
98
|
+
function getRebalanceValues(solautoPosition, solautoFeeBps, flFeeBps, targetLiqUtilizationRateBps) {
|
99
|
+
const tokenBalanceChange = getTokenBalanceChange();
|
100
|
+
const targetRate = getTargetLiqUtilizationRateBps(solautoPosition, targetLiqUtilizationRateBps, tokenBalanceChange);
|
101
|
+
const rebalanceDirection = getRebalanceDirection(solautoPosition, targetRate);
|
102
|
+
const position = getAdjustedPositionValues(solautoPosition, tokenBalanceChange);
|
103
|
+
const fees = {
|
104
|
+
solauto: solautoFeeBps.getSolautoFeesBps(rebalanceDirection).total,
|
105
|
+
lpBorrow: solautoPosition.state().debt.borrowFeeBps,
|
106
|
+
flashLoan: flFeeBps,
|
107
|
+
};
|
108
|
+
const debtAdjustment = getDebtAdjustment((0, utils_1.fromBps)(solautoPosition.state().liqThresholdBps), position, fees, targetRate);
|
109
|
+
const repayingCloseToMaxLtv = rebalanceDirection === generated_1.RebalanceDirection.Repay &&
|
110
|
+
targetRate >=
|
111
|
+
(0, utils_1.maxRepayToBps)(solautoPosition.state().maxLtvBps, solautoPosition.state().liqThresholdBps);
|
112
|
+
return {
|
113
|
+
...debtAdjustment,
|
114
|
+
rebalanceDirection,
|
115
|
+
tokenBalanceChange,
|
116
|
+
repayingCloseToMaxLtv,
|
117
|
+
};
|
118
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { RebalanceDirection } from "../../generated";
|
2
|
+
export declare class SolautoFeesBps {
|
3
|
+
private isReferred;
|
4
|
+
private targetLiqUtilizationRateBps;
|
5
|
+
private positionNetWorthUsd;
|
6
|
+
constructor(isReferred: boolean, targetLiqUtilizationRateBps: number | undefined, positionNetWorthUsd: number);
|
7
|
+
getSolautoFeesBps(rebalanceDirection: RebalanceDirection): {
|
8
|
+
solauto: number;
|
9
|
+
referrer: number;
|
10
|
+
total: number;
|
11
|
+
};
|
12
|
+
}
|
13
|
+
//# sourceMappingURL=solautoFees.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"solautoFees.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/solautoFees.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAErD,qBAAa,cAAc;IAEvB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,2BAA2B;IACnC,OAAO,CAAC,mBAAmB;gBAFnB,UAAU,EAAE,OAAO,EACnB,2BAA2B,EAAE,MAAM,GAAG,SAAS,EAC/C,mBAAmB,EAAE,MAAM;IAG9B,iBAAiB,CAAC,kBAAkB,EAAE,kBAAkB;;;;;CAkDhE"}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.SolautoFeesBps = void 0;
|
4
|
+
const constants_1 = require("../../constants");
|
5
|
+
const generated_1 = require("../../generated");
|
6
|
+
class SolautoFeesBps {
|
7
|
+
constructor(isReferred, targetLiqUtilizationRateBps, positionNetWorthUsd) {
|
8
|
+
this.isReferred = isReferred;
|
9
|
+
this.targetLiqUtilizationRateBps = targetLiqUtilizationRateBps;
|
10
|
+
this.positionNetWorthUsd = positionNetWorthUsd;
|
11
|
+
}
|
12
|
+
getSolautoFeesBps(rebalanceDirection) {
|
13
|
+
const minSize = 10000; // Minimum position size
|
14
|
+
const maxSize = 250000; // Maximum position size
|
15
|
+
const maxFeeBps = 50; // Fee in basis points for minSize (0.5%)
|
16
|
+
const minFeeBps = 25; // Fee in basis points for maxSize (0.25%)
|
17
|
+
const k = 1.5;
|
18
|
+
if (this.targetLiqUtilizationRateBps !== undefined &&
|
19
|
+
this.targetLiqUtilizationRateBps === 0) {
|
20
|
+
return {
|
21
|
+
solauto: 0,
|
22
|
+
referrer: 0,
|
23
|
+
total: 0,
|
24
|
+
};
|
25
|
+
}
|
26
|
+
let feeBps = 0;
|
27
|
+
if (this.targetLiqUtilizationRateBps !== undefined ||
|
28
|
+
rebalanceDirection === generated_1.RebalanceDirection.Repay) {
|
29
|
+
feeBps = 25;
|
30
|
+
}
|
31
|
+
else if (this.positionNetWorthUsd <= minSize) {
|
32
|
+
feeBps = maxFeeBps;
|
33
|
+
}
|
34
|
+
else if (this.positionNetWorthUsd >= maxSize) {
|
35
|
+
feeBps = minFeeBps;
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
const t = (Math.log(this.positionNetWorthUsd) - Math.log(minSize)) /
|
39
|
+
(Math.log(maxSize) - Math.log(minSize));
|
40
|
+
feeBps = Math.round(minFeeBps + (maxFeeBps - minFeeBps) * (1 - Math.pow(t, k)));
|
41
|
+
}
|
42
|
+
let referrer = 0;
|
43
|
+
if (this.isReferred) {
|
44
|
+
feeBps *= 1.0 - constants_1.REFERRER_PERCENTAGE;
|
45
|
+
referrer = Math.floor(feeBps * constants_1.REFERRER_PERCENTAGE);
|
46
|
+
}
|
47
|
+
return {
|
48
|
+
solauto: feeBps - referrer,
|
49
|
+
referrer,
|
50
|
+
total: feeBps,
|
51
|
+
};
|
52
|
+
}
|
53
|
+
}
|
54
|
+
exports.SolautoFeesBps = SolautoFeesBps;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/solauto/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,aAAa,CAAC"}
|
@@ -1,11 +1,8 @@
|
|
1
1
|
import { PublicKey } from "@solana/web3.js";
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
import { ReferralState } from "../generated";
|
2
|
+
import { TransactionBuilder, Umi } from "@metaplex-foundation/umi";
|
3
|
+
import { ReferralState } from "../../generated";
|
5
4
|
import { TxHandler } from "./txHandler";
|
6
5
|
export interface ReferralStateManagerArgs {
|
7
|
-
signer?: Signer;
|
8
|
-
wallet?: WalletAdapter;
|
9
6
|
authority?: PublicKey;
|
10
7
|
referralState?: PublicKey;
|
11
8
|
referredByAuthority?: PublicKey;
|
@@ -19,7 +16,9 @@ export declare class ReferralStateManager extends TxHandler {
|
|
19
16
|
referredByState?: PublicKey;
|
20
17
|
initialize(args: ReferralStateManagerArgs): Promise<void>;
|
21
18
|
defaultLookupTables(): string[];
|
19
|
+
refetchReferralState(): Promise<void>;
|
22
20
|
setReferredBy(referredBy?: PublicKey): void;
|
21
|
+
isReferred(): boolean;
|
23
22
|
updateReferralStatesIx(destFeesMint?: PublicKey, lookupTable?: PublicKey): TransactionBuilder;
|
24
23
|
claimReferralFeesIx(): TransactionBuilder;
|
25
24
|
resetLiveTxUpdates(success?: boolean): Promise<void>;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"referralStateManager.d.ts","sourceRoot":"","sources":["../../../src/services/solauto/referralStateManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAa,kBAAkB,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAE9E,OAAO,EAEL,aAAa,EAGd,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGxC,MAAM,WAAW,wBAAwB;IACvC,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,aAAa,CAAC,EAAE,SAAS,CAAC;IAC1B,mBAAmB,CAAC,EAAE,SAAS,CAAC;CACjC;AAED,qBAAa,oBAAqB,SAAQ,SAAS;IAC1C,GAAG,EAAG,GAAG,CAAC;IAEV,aAAa,EAAG,SAAS,CAAC;IAC1B,iBAAiB,EAAG,aAAa,GAAG,IAAI,CAAC;IACzC,SAAS,EAAG,SAAS,CAAC;IAEtB,UAAU,CAAC,EAAE,SAAS,CAAC;IACvB,eAAe,CAAC,EAAE,SAAS,CAAC;IAE7B,UAAU,CAAC,IAAI,EAAE,wBAAwB;IAoB/C,mBAAmB,IAAI,MAAM,EAAE;IASzB,oBAAoB;IAQ1B,aAAa,CAAC,UAAU,CAAC,EAAE,SAAS;IAsBpC,UAAU,IAAI,OAAO;IAIrB,sBAAsB,CACpB,YAAY,CAAC,EAAE,SAAS,EACxB,WAAW,CAAC,EAAE,SAAS,GACtB,kBAAkB;IAerB,mBAAmB,IAAI,kBAAkB;IA0BnC,kBAAkB,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAC3D"}
|