@haven-fi/solauto-sdk 1.0.582 → 1.0.584
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/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/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} +49 -173
- package/local/createTokenAccounts.ts +1 -5
- package/local/logPositions.ts +22 -68
- package/local/shared.ts +29 -24
- package/local/txSandbox.ts +66 -0
- package/local/updateMarginfiLUT.ts +80 -4
- package/package.json +3 -6
- package/src/constants/solautoConstants.ts +4 -9
- 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} +67 -288
- 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
@@ -1,11 +1,5 @@
|
|
1
1
|
import { PublicKey } from "@solana/web3.js";
|
2
|
-
import {
|
3
|
-
isOption,
|
4
|
-
isSome,
|
5
|
-
Program,
|
6
|
-
publicKey,
|
7
|
-
Umi,
|
8
|
-
} from "@metaplex-foundation/umi";
|
2
|
+
import { Program, publicKey, Umi } from "@metaplex-foundation/umi";
|
9
3
|
import {
|
10
4
|
AutomationSettings,
|
11
5
|
DCASettings,
|
@@ -13,6 +7,7 @@ import {
|
|
13
7
|
LendingPlatform,
|
14
8
|
PositionState,
|
15
9
|
PositionType,
|
10
|
+
SolautoRebalanceType,
|
16
11
|
SolautoSettingsParameters,
|
17
12
|
SolautoSettingsParametersInpArgs,
|
18
13
|
TokenType,
|
@@ -21,32 +16,31 @@ import {
|
|
21
16
|
getSolautoErrorFromName,
|
22
17
|
getSolautoPositionAccountDataSerializer,
|
23
18
|
getSolautoPositionSize,
|
24
|
-
} from "
|
25
|
-
import {
|
19
|
+
} from "../generated";
|
20
|
+
import { getReferralState } from "./accountUtils";
|
21
|
+
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
22
|
+
import { ALL_SUPPORTED_TOKENS } from "../constants";
|
23
|
+
import {
|
24
|
+
findMarginfiAccounts,
|
25
|
+
getAllMarginfiAccountsByAuthority,
|
26
|
+
} from "./marginfiUtils";
|
27
|
+
import { SolautoPositionDetails } from "../types/solauto";
|
28
|
+
import { QuoteResponse } from "@jup-ag/api";
|
29
|
+
import { createSolautoSettings } from "../solautoPosition";
|
30
|
+
import {
|
31
|
+
SolautoClient,
|
32
|
+
SolautoMarginfiClient,
|
33
|
+
TxHandlerProps,
|
34
|
+
} from "../services";
|
26
35
|
import {
|
27
36
|
calcTotalDebt,
|
28
37
|
calcTotalSupply,
|
29
|
-
debtLiquidityUsdAvailable,
|
30
38
|
fromBaseUnit,
|
31
39
|
getLiqUtilzationRateBps,
|
32
|
-
supplyLiquidityUsdDepositable,
|
33
40
|
toBaseUnit,
|
34
|
-
|
35
|
-
|
36
|
-
import {
|
37
|
-
import {
|
38
|
-
ALL_SUPPORTED_TOKENS,
|
39
|
-
TOKEN_INFO,
|
40
|
-
USD_DECIMALS,
|
41
|
-
} from "../../constants";
|
42
|
-
import {
|
43
|
-
findMarginfiAccounts,
|
44
|
-
getAllMarginfiAccountsByAuthority,
|
45
|
-
} from "../marginfiUtils";
|
46
|
-
import { RebalanceAction, SolautoPositionDetails } from "../../types/solauto";
|
47
|
-
import { fetchTokenPrices } from "../priceUtils";
|
48
|
-
import { getRebalanceValues } from "./rebalanceUtils";
|
49
|
-
import { QuoteResponse } from "@jup-ag/api";
|
41
|
+
toRoundedUsdValue,
|
42
|
+
} from "./numberUtils";
|
43
|
+
import { fetchTokenPrices } from "./priceUtils";
|
50
44
|
|
51
45
|
export function createDynamicSolautoProgram(programId: PublicKey): Program {
|
52
46
|
return {
|
@@ -112,138 +106,6 @@ export function getUpdatedValueFromAutomation(
|
|
112
106
|
return newValue;
|
113
107
|
}
|
114
108
|
|
115
|
-
export function getAdjustedSettingsFromAutomation(
|
116
|
-
settings: SolautoSettingsParameters,
|
117
|
-
currentUnixTime: number
|
118
|
-
): SolautoSettingsParameters {
|
119
|
-
const boostToBps =
|
120
|
-
settings.automation.targetPeriods > 0 &&
|
121
|
-
eligibleForNextAutomationPeriod(settings.automation, currentUnixTime)
|
122
|
-
? getUpdatedValueFromAutomation(
|
123
|
-
settings.boostToBps,
|
124
|
-
settings.targetBoostToBps,
|
125
|
-
settings.automation,
|
126
|
-
currentUnixTime
|
127
|
-
)
|
128
|
-
: settings.boostToBps;
|
129
|
-
|
130
|
-
return {
|
131
|
-
...settings,
|
132
|
-
boostToBps,
|
133
|
-
};
|
134
|
-
}
|
135
|
-
|
136
|
-
export function sufficientLiquidityToBoost(
|
137
|
-
positionState: PositionState,
|
138
|
-
positionSettings: SolautoSettingsParameters | undefined,
|
139
|
-
positionDca: DCASettings | undefined,
|
140
|
-
supplyMintPrice: number,
|
141
|
-
debtMintPrice: number
|
142
|
-
) {
|
143
|
-
const limitsUpToDate =
|
144
|
-
positionState.supply.amountCanBeUsed.baseUnit > BigInt(0) ||
|
145
|
-
positionState.debt.amountCanBeUsed.baseUnit > BigInt(0);
|
146
|
-
|
147
|
-
if (limitsUpToDate) {
|
148
|
-
const values = getRebalanceValues(
|
149
|
-
positionState!,
|
150
|
-
positionSettings,
|
151
|
-
positionDca,
|
152
|
-
currentUnixSeconds(),
|
153
|
-
supplyMintPrice,
|
154
|
-
debtMintPrice
|
155
|
-
);
|
156
|
-
|
157
|
-
const debtAvailable = debtLiquidityUsdAvailable(positionState);
|
158
|
-
const supplyDepositable = supplyLiquidityUsdDepositable(positionState);
|
159
|
-
const sufficientLiquidity =
|
160
|
-
debtAvailable * 0.95 > values.debtAdjustmentUsd &&
|
161
|
-
supplyDepositable * 0.95 > values.debtAdjustmentUsd;
|
162
|
-
|
163
|
-
if (!sufficientLiquidity) {
|
164
|
-
consoleLog("Insufficient liquidity to further boost");
|
165
|
-
}
|
166
|
-
return sufficientLiquidity;
|
167
|
-
}
|
168
|
-
|
169
|
-
return true;
|
170
|
-
}
|
171
|
-
|
172
|
-
export function eligibleForRebalance(
|
173
|
-
positionState: PositionState,
|
174
|
-
positionSettings: SolautoSettingsParameters | undefined,
|
175
|
-
positionDca: DCASettings | undefined,
|
176
|
-
currentUnixTime: number,
|
177
|
-
supplyMintPrice: number,
|
178
|
-
debtMintPrice: number,
|
179
|
-
bpsDistanceThreshold = 0
|
180
|
-
): RebalanceAction | undefined {
|
181
|
-
if (!positionSettings) {
|
182
|
-
return undefined;
|
183
|
-
}
|
184
|
-
|
185
|
-
if (
|
186
|
-
positionDca &&
|
187
|
-
positionDca.automation.targetPeriods > 0 &&
|
188
|
-
eligibleForNextAutomationPeriod(positionDca.automation, currentUnixTime)
|
189
|
-
) {
|
190
|
-
return "dca";
|
191
|
-
}
|
192
|
-
|
193
|
-
if (positionState.supply.amountUsed.baseUnit === BigInt(0)) {
|
194
|
-
return undefined;
|
195
|
-
}
|
196
|
-
|
197
|
-
const boostToBps =
|
198
|
-
eligibleForRefresh(positionState, positionSettings, currentUnixTime) &&
|
199
|
-
positionSettings.automation.targetPeriods > 0
|
200
|
-
? getUpdatedValueFromAutomation(
|
201
|
-
positionSettings.boostToBps,
|
202
|
-
positionSettings.targetBoostToBps,
|
203
|
-
positionSettings.automation,
|
204
|
-
currentUnixTime
|
205
|
-
)
|
206
|
-
: positionSettings.boostToBps;
|
207
|
-
const repayFrom = positionSettings.repayToBps + positionSettings.repayGap;
|
208
|
-
const boostFrom = boostToBps - positionSettings.boostGap;
|
209
|
-
|
210
|
-
if (positionState.liqUtilizationRateBps - boostFrom <= bpsDistanceThreshold) {
|
211
|
-
const sufficientLiquidity = sufficientLiquidityToBoost(
|
212
|
-
positionState,
|
213
|
-
positionSettings,
|
214
|
-
positionDca,
|
215
|
-
supplyMintPrice,
|
216
|
-
debtMintPrice
|
217
|
-
);
|
218
|
-
return sufficientLiquidity ? "boost" : undefined;
|
219
|
-
} else if (
|
220
|
-
repayFrom - positionState.liqUtilizationRateBps <=
|
221
|
-
bpsDistanceThreshold
|
222
|
-
) {
|
223
|
-
return "repay";
|
224
|
-
}
|
225
|
-
|
226
|
-
return undefined;
|
227
|
-
}
|
228
|
-
|
229
|
-
export function eligibleForRefresh(
|
230
|
-
positionState: PositionState,
|
231
|
-
positionSettings: SolautoSettingsParameters,
|
232
|
-
currentUnixTime: number
|
233
|
-
): boolean {
|
234
|
-
if (positionSettings.automation.targetPeriods > 0) {
|
235
|
-
return eligibleForNextAutomationPeriod(
|
236
|
-
positionSettings.automation,
|
237
|
-
currentUnixTime
|
238
|
-
);
|
239
|
-
} else {
|
240
|
-
return (
|
241
|
-
currentUnixSeconds() - Number(positionState.lastUpdated) >
|
242
|
-
60 * 60 * 24 * 7
|
243
|
-
);
|
244
|
-
}
|
245
|
-
}
|
246
|
-
|
247
109
|
export async function getSolautoManagedPositions(
|
248
110
|
umi: Umi,
|
249
111
|
authority?: PublicKey,
|
@@ -467,161 +329,43 @@ export async function positionStateWithLatestPrices(
|
|
467
329
|
),
|
468
330
|
netWorth: {
|
469
331
|
baseUnit: toBaseUnit(
|
470
|
-
|
332
|
+
supplyUsd > 0 ? (supplyUsd - debtUsd) / supplyPrice : 0,
|
471
333
|
state.supply.decimals
|
472
334
|
),
|
473
|
-
baseAmountUsdValue:
|
335
|
+
baseAmountUsdValue: toRoundedUsdValue(supplyUsd - debtUsd),
|
474
336
|
},
|
475
337
|
supply: {
|
476
338
|
...state.supply,
|
477
339
|
amountCanBeUsed: {
|
478
340
|
...state.supply.amountCanBeUsed,
|
479
|
-
baseAmountUsdValue:
|
341
|
+
baseAmountUsdValue: toRoundedUsdValue(
|
480
342
|
fromBaseUnit(
|
481
343
|
state.supply.amountCanBeUsed.baseUnit,
|
482
344
|
state.supply.decimals
|
483
|
-
) * supplyPrice
|
484
|
-
USD_DECIMALS
|
345
|
+
) * supplyPrice
|
485
346
|
),
|
486
347
|
},
|
487
348
|
amountUsed: {
|
488
349
|
...state.supply.amountUsed,
|
489
|
-
baseAmountUsdValue:
|
350
|
+
baseAmountUsdValue: toRoundedUsdValue(supplyUsd),
|
490
351
|
},
|
491
352
|
},
|
492
353
|
debt: {
|
493
354
|
...state.debt,
|
494
355
|
amountCanBeUsed: {
|
495
356
|
...state.debt.amountCanBeUsed,
|
496
|
-
baseAmountUsdValue:
|
357
|
+
baseAmountUsdValue: toRoundedUsdValue(
|
497
358
|
fromBaseUnit(
|
498
359
|
state.debt.amountCanBeUsed.baseUnit,
|
499
360
|
state.debt.decimals
|
500
|
-
) * debtPrice
|
501
|
-
USD_DECIMALS
|
361
|
+
) * debtPrice
|
502
362
|
),
|
503
363
|
},
|
504
364
|
amountUsed: {
|
505
365
|
...state.debt.amountUsed,
|
506
|
-
baseAmountUsdValue:
|
507
|
-
},
|
508
|
-
},
|
509
|
-
};
|
510
|
-
}
|
511
|
-
|
512
|
-
interface AssetProps {
|
513
|
-
mint: PublicKey;
|
514
|
-
price?: number;
|
515
|
-
amountUsed?: number;
|
516
|
-
amountCanBeUsed?: number;
|
517
|
-
}
|
518
|
-
|
519
|
-
export function createFakePositionState(
|
520
|
-
supply: AssetProps,
|
521
|
-
debt: AssetProps,
|
522
|
-
maxLtvBps: number,
|
523
|
-
liqThresholdBps: number
|
524
|
-
): PositionState {
|
525
|
-
const supplyDecimals = TOKEN_INFO[supply.mint.toString()].decimals;
|
526
|
-
const debtDecimals = TOKEN_INFO[debt.mint.toString()].decimals;
|
527
|
-
|
528
|
-
const supplyUsd = (supply.amountUsed ?? 0) * (supply.price ?? 0);
|
529
|
-
const debtUsd = (debt.amountUsed ?? 0) * (debt.price ?? 0);
|
530
|
-
|
531
|
-
return {
|
532
|
-
liqUtilizationRateBps: getLiqUtilzationRateBps(
|
533
|
-
supplyUsd,
|
534
|
-
debtUsd,
|
535
|
-
liqThresholdBps
|
536
|
-
),
|
537
|
-
supply: {
|
538
|
-
amountUsed: {
|
539
|
-
baseUnit: toBaseUnit(supply.amountUsed ?? 0, supplyDecimals),
|
540
|
-
baseAmountUsdValue: toBaseUnit(supplyUsd, USD_DECIMALS),
|
541
|
-
},
|
542
|
-
amountCanBeUsed: {
|
543
|
-
baseUnit: toBaseUnit(supply.amountCanBeUsed ?? 0, supplyDecimals),
|
544
|
-
baseAmountUsdValue: toBaseUnit(
|
545
|
-
(supply.amountCanBeUsed ?? 0) * (supply.price ?? 0),
|
546
|
-
USD_DECIMALS
|
547
|
-
),
|
548
|
-
},
|
549
|
-
baseAmountMarketPriceUsd: toBaseUnit(supply.price ?? 0, USD_DECIMALS),
|
550
|
-
borrowFeeBps: 0,
|
551
|
-
decimals: supplyDecimals,
|
552
|
-
flashLoanFeeBps: 0,
|
553
|
-
mint: publicKey(supply.mint),
|
554
|
-
padding1: [],
|
555
|
-
padding2: [],
|
556
|
-
padding: new Uint8Array([]),
|
557
|
-
},
|
558
|
-
debt: {
|
559
|
-
amountUsed: {
|
560
|
-
baseUnit: toBaseUnit(debt.amountUsed ?? 0, debtDecimals),
|
561
|
-
baseAmountUsdValue: toBaseUnit(debtUsd, USD_DECIMALS),
|
562
|
-
},
|
563
|
-
amountCanBeUsed: {
|
564
|
-
baseUnit: toBaseUnit(debt.amountCanBeUsed ?? 0, debtDecimals),
|
565
|
-
baseAmountUsdValue: toBaseUnit(
|
566
|
-
(debt.amountCanBeUsed ?? 0) * (debt.price ?? 0),
|
567
|
-
USD_DECIMALS
|
568
|
-
),
|
366
|
+
baseAmountUsdValue: toRoundedUsdValue(debtUsd),
|
569
367
|
},
|
570
|
-
baseAmountMarketPriceUsd: toBaseUnit(debt.price ?? 0, USD_DECIMALS),
|
571
|
-
borrowFeeBps: 0,
|
572
|
-
decimals: debtDecimals,
|
573
|
-
flashLoanFeeBps: 0,
|
574
|
-
mint: publicKey(debt.mint),
|
575
|
-
padding1: [],
|
576
|
-
padding2: [],
|
577
|
-
padding: new Uint8Array([]),
|
578
368
|
},
|
579
|
-
netWorth: {
|
580
|
-
baseUnit: supply.price
|
581
|
-
? toBaseUnit((supplyUsd - debtUsd) / supply.price, supplyDecimals)
|
582
|
-
: BigInt(0),
|
583
|
-
baseAmountUsdValue: toBaseUnit(supplyUsd - debtUsd, USD_DECIMALS),
|
584
|
-
},
|
585
|
-
maxLtvBps,
|
586
|
-
liqThresholdBps,
|
587
|
-
lastUpdated: BigInt(currentUnixSeconds()),
|
588
|
-
padding1: [],
|
589
|
-
padding2: [],
|
590
|
-
padding: [],
|
591
|
-
};
|
592
|
-
}
|
593
|
-
|
594
|
-
export function createSolautoSettings(
|
595
|
-
settings: SolautoSettingsParametersInpArgs
|
596
|
-
): SolautoSettingsParameters {
|
597
|
-
return {
|
598
|
-
automation:
|
599
|
-
isOption(settings.automation) && isSome(settings.automation)
|
600
|
-
? {
|
601
|
-
...settings.automation.value,
|
602
|
-
intervalSeconds: BigInt(settings.automation.value.intervalSeconds),
|
603
|
-
unixStartDate: BigInt(settings.automation.value.unixStartDate),
|
604
|
-
padding: new Uint8Array([]),
|
605
|
-
padding1: [],
|
606
|
-
}
|
607
|
-
: {
|
608
|
-
targetPeriods: 0,
|
609
|
-
periodsPassed: 0,
|
610
|
-
intervalSeconds: BigInt(0),
|
611
|
-
unixStartDate: BigInt(0),
|
612
|
-
padding: new Uint8Array([]),
|
613
|
-
padding1: [],
|
614
|
-
},
|
615
|
-
targetBoostToBps:
|
616
|
-
isOption(settings.targetBoostToBps) && isSome(settings.targetBoostToBps)
|
617
|
-
? settings.targetBoostToBps.value
|
618
|
-
: 0,
|
619
|
-
boostGap: settings.boostGap,
|
620
|
-
boostToBps: settings.boostToBps,
|
621
|
-
repayGap: settings.repayGap,
|
622
|
-
repayToBps: settings.repayToBps,
|
623
|
-
padding: new Uint8Array([]),
|
624
|
-
padding1: [],
|
625
369
|
};
|
626
370
|
}
|
627
371
|
|
@@ -638,7 +382,7 @@ export class ContextUpdates {
|
|
638
382
|
public supplyAdjustment = BigInt(0);
|
639
383
|
public debtAdjustment = BigInt(0);
|
640
384
|
public settings: SolautoSettingsParameters | undefined = undefined;
|
641
|
-
public
|
385
|
+
public dca: DCASettings | undefined = undefined;
|
642
386
|
public dcaInBalance?: { amount: bigint; tokenType: TokenType } = undefined;
|
643
387
|
public cancellingDca: TokenType | undefined = undefined;
|
644
388
|
public jupSwap: QuoteResponse | undefined = undefined;
|
@@ -653,7 +397,7 @@ export class ContextUpdates {
|
|
653
397
|
this.settings = createSolautoSettings(settings);
|
654
398
|
} else if (update.type === "dca") {
|
655
399
|
const dca = update.value;
|
656
|
-
this.
|
400
|
+
this.dca = {
|
657
401
|
automation: {
|
658
402
|
...dca.automation,
|
659
403
|
intervalSeconds: BigInt(dca.automation.intervalSeconds),
|
@@ -678,7 +422,7 @@ export class ContextUpdates {
|
|
678
422
|
this.supplyAdjustment = BigInt(0);
|
679
423
|
this.debtAdjustment = BigInt(0);
|
680
424
|
this.settings = undefined;
|
681
|
-
this.
|
425
|
+
this.dca = undefined;
|
682
426
|
this.dcaInBalance = undefined;
|
683
427
|
this.cancellingDca = undefined;
|
684
428
|
}
|
@@ -693,3 +437,38 @@ export class ContextUpdates {
|
|
693
437
|
);
|
694
438
|
}
|
695
439
|
}
|
440
|
+
|
441
|
+
export function getClient(
|
442
|
+
lendingPlatform: LendingPlatform,
|
443
|
+
txHandlerProps: TxHandlerProps
|
444
|
+
) {
|
445
|
+
if (lendingPlatform === LendingPlatform.Marginfi) {
|
446
|
+
return new SolautoMarginfiClient(txHandlerProps);
|
447
|
+
} else {
|
448
|
+
throw new Error("Lending platform not yet supported");
|
449
|
+
// TODO: PF
|
450
|
+
}
|
451
|
+
}
|
452
|
+
|
453
|
+
export function isMarginfiClient(
|
454
|
+
client: SolautoClient
|
455
|
+
): client is SolautoMarginfiClient {
|
456
|
+
return client.lendingPlatform == LendingPlatform.Marginfi;
|
457
|
+
}
|
458
|
+
// TODO: PF
|
459
|
+
|
460
|
+
export function hasFirstRebalance(rebalanceType: SolautoRebalanceType) {
|
461
|
+
return [
|
462
|
+
SolautoRebalanceType.Regular,
|
463
|
+
SolautoRebalanceType.DoubleRebalanceWithFL,
|
464
|
+
SolautoRebalanceType.FLRebalanceThenSwap,
|
465
|
+
].includes(rebalanceType);
|
466
|
+
}
|
467
|
+
|
468
|
+
export function hasLastRebalance(rebalanceType: SolautoRebalanceType) {
|
469
|
+
return [
|
470
|
+
SolautoRebalanceType.Regular,
|
471
|
+
SolautoRebalanceType.DoubleRebalanceWithFL,
|
472
|
+
SolautoRebalanceType.FLSwapThenRebalance,
|
473
|
+
].includes(rebalanceType);
|
474
|
+
}
|
@@ -0,0 +1,135 @@
|
|
1
|
+
import { Signer } from "@metaplex-foundation/umi";
|
2
|
+
import {
|
3
|
+
buildIronforgeApiUrl,
|
4
|
+
consoleLog,
|
5
|
+
fetchTokenPrices,
|
6
|
+
getClient,
|
7
|
+
LendingPlatform,
|
8
|
+
maxBoostToBps,
|
9
|
+
maxRepayToBps,
|
10
|
+
RebalanceTxBuilder,
|
11
|
+
SOLAUTO_PROD_PROGRAM,
|
12
|
+
SOLAUTO_TEST_PROGRAM,
|
13
|
+
solautoAction,
|
14
|
+
SolautoSettingsParametersInpArgs,
|
15
|
+
toBaseUnit,
|
16
|
+
tokenInfo,
|
17
|
+
TransactionItem,
|
18
|
+
TransactionsManager,
|
19
|
+
USDC,
|
20
|
+
} from "../../src";
|
21
|
+
import { PublicKey } from "@solana/web3.js";
|
22
|
+
import { NATIVE_MINT } from "@solana/spl-token";
|
23
|
+
|
24
|
+
export async function e2eTransactionTest(
|
25
|
+
signer: Signer,
|
26
|
+
testProgram: boolean,
|
27
|
+
lendingPlatform: LendingPlatform,
|
28
|
+
withFlashLoan: boolean
|
29
|
+
) {
|
30
|
+
const client = getClient(lendingPlatform, {
|
31
|
+
signer,
|
32
|
+
rpcUrl: buildIronforgeApiUrl(process.env.IRONFORGE_API_KEY!),
|
33
|
+
programId: testProgram ? SOLAUTO_TEST_PROGRAM : SOLAUTO_PROD_PROGRAM,
|
34
|
+
});
|
35
|
+
|
36
|
+
const supplyMint = new PublicKey(NATIVE_MINT);
|
37
|
+
const debtMint = new PublicKey(USDC);
|
38
|
+
|
39
|
+
await client.initialize({
|
40
|
+
positionId: 1,
|
41
|
+
new: true,
|
42
|
+
supplyMint,
|
43
|
+
debtMint,
|
44
|
+
});
|
45
|
+
|
46
|
+
const [maxLtvBps, liqThresholdBps] =
|
47
|
+
await client.solautoPosition.maxLtvAndLiqThresholdBps();
|
48
|
+
const settings: SolautoSettingsParametersInpArgs = {
|
49
|
+
boostToBps: maxBoostToBps(maxLtvBps, liqThresholdBps) - 200,
|
50
|
+
boostGap: 50,
|
51
|
+
repayToBps: maxRepayToBps(maxLtvBps, liqThresholdBps),
|
52
|
+
repayGap: 50,
|
53
|
+
};
|
54
|
+
|
55
|
+
const transactionItems: TransactionItem[] = [];
|
56
|
+
|
57
|
+
transactionItems.push(
|
58
|
+
new TransactionItem(async () => {
|
59
|
+
return {
|
60
|
+
tx: client.openPositionIx(settings),
|
61
|
+
};
|
62
|
+
}, "open position")
|
63
|
+
);
|
64
|
+
|
65
|
+
const [supplyPrice, debtPrice] = await fetchTokenPrices([
|
66
|
+
supplyMint,
|
67
|
+
debtMint,
|
68
|
+
]);
|
69
|
+
|
70
|
+
const supplyUsd = 100;
|
71
|
+
transactionItems.push(
|
72
|
+
new TransactionItem(async () => {
|
73
|
+
return {
|
74
|
+
tx: client.protocolInteractionIx(
|
75
|
+
solautoAction("Deposit", [
|
76
|
+
toBaseUnit(supplyUsd / supplyPrice, tokenInfo(supplyMint).decimals),
|
77
|
+
])
|
78
|
+
),
|
79
|
+
};
|
80
|
+
}, "deposit")
|
81
|
+
);
|
82
|
+
|
83
|
+
const debtUsd = withFlashLoan ? 60 : 10;
|
84
|
+
transactionItems.push(
|
85
|
+
new TransactionItem(async () => {
|
86
|
+
return {
|
87
|
+
tx: client.protocolInteractionIx(
|
88
|
+
solautoAction("Borrow", [
|
89
|
+
toBaseUnit(debtUsd / debtPrice, tokenInfo(debtMint).decimals),
|
90
|
+
])
|
91
|
+
),
|
92
|
+
};
|
93
|
+
}, "borrow")
|
94
|
+
);
|
95
|
+
|
96
|
+
transactionItems.push(
|
97
|
+
new TransactionItem(async (attemptNum) => {
|
98
|
+
const rebalancer = new RebalanceTxBuilder(client, 0);
|
99
|
+
return await rebalancer.buildRebalanceTx(attemptNum);
|
100
|
+
}, "rebalance")
|
101
|
+
);
|
102
|
+
|
103
|
+
transactionItems.push(
|
104
|
+
new TransactionItem(
|
105
|
+
async () => ({
|
106
|
+
tx: client.protocolInteractionIx(
|
107
|
+
solautoAction("Withdraw", [{ __kind: "All" }])
|
108
|
+
),
|
109
|
+
}),
|
110
|
+
"withdraw"
|
111
|
+
)
|
112
|
+
);
|
113
|
+
|
114
|
+
transactionItems.push(
|
115
|
+
new TransactionItem(
|
116
|
+
async () => ({
|
117
|
+
tx: client.closePositionIx(),
|
118
|
+
}),
|
119
|
+
"close position"
|
120
|
+
)
|
121
|
+
);
|
122
|
+
|
123
|
+
const txManager = new TransactionsManager(
|
124
|
+
client,
|
125
|
+
undefined,
|
126
|
+
"only-simulate",
|
127
|
+
undefined,
|
128
|
+
true,
|
129
|
+
undefined,
|
130
|
+
{ totalRetries: 5 }
|
131
|
+
);
|
132
|
+
const statuses = await txManager.clientSend(transactionItems);
|
133
|
+
|
134
|
+
consoleLog(statuses);
|
135
|
+
}
|