@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
@@ -1,42 +1,8 @@
|
|
1
|
-
import {
|
2
|
-
Signer,
|
3
|
-
TransactionBuilder,
|
4
|
-
transactionBuilder,
|
5
|
-
} from "@metaplex-foundation/umi";
|
6
1
|
import { PublicKey, TransactionInstruction } from "@solana/web3.js";
|
7
|
-
import {
|
8
|
-
import {
|
9
|
-
import {
|
10
|
-
createJupiterApiClient,
|
11
|
-
Instruction,
|
12
|
-
QuoteResponse,
|
13
|
-
} from "@jup-ag/api";
|
14
|
-
import { getTokenAccount } from "./accountUtils";
|
15
|
-
import {
|
16
|
-
consoleLog,
|
17
|
-
retryWithExponentialBackoff,
|
18
|
-
tokenInfo,
|
19
|
-
} from "./generalUtils";
|
2
|
+
import { retryWithExponentialBackoff } from "./generalUtils";
|
3
|
+
import { Instruction } from "@jup-ag/api";
|
20
4
|
|
21
|
-
|
22
|
-
|
23
|
-
export interface JupSwapInput {
|
24
|
-
inputMint: PublicKey;
|
25
|
-
outputMint: PublicKey;
|
26
|
-
amount: bigint;
|
27
|
-
exactIn?: boolean;
|
28
|
-
exactOut?: boolean;
|
29
|
-
}
|
30
|
-
|
31
|
-
export interface JupSwapDetails extends JupSwapInput {
|
32
|
-
destinationWallet: PublicKey;
|
33
|
-
slippageIncFactor?: number;
|
34
|
-
addPadding?: boolean;
|
35
|
-
jupQuote?: QuoteResponse;
|
36
|
-
wrapAndUnwrapSol?: boolean;
|
37
|
-
}
|
38
|
-
|
39
|
-
function createTransactionInstruction(
|
5
|
+
export function jupIxToSolanaIx(
|
40
6
|
instruction: Instruction
|
41
7
|
): TransactionInstruction {
|
42
8
|
return new TransactionInstruction({
|
@@ -50,133 +16,6 @@ function createTransactionInstruction(
|
|
50
16
|
});
|
51
17
|
}
|
52
18
|
|
53
|
-
export async function getJupQuote(swapDetails: JupSwapInput) {
|
54
|
-
return await retryWithExponentialBackoff(
|
55
|
-
async (attemptNum: number) =>
|
56
|
-
await jupApi.quoteGet({
|
57
|
-
amount: Number(swapDetails.amount),
|
58
|
-
inputMint: swapDetails.inputMint.toString(),
|
59
|
-
outputMint: swapDetails.outputMint.toString(),
|
60
|
-
swapMode: swapDetails.exactOut
|
61
|
-
? "ExactOut"
|
62
|
-
: swapDetails.exactIn
|
63
|
-
? "ExactIn"
|
64
|
-
: undefined,
|
65
|
-
slippageBps: 300,
|
66
|
-
maxAccounts: !swapDetails.exactOut ? 15 + attemptNum * 5 : undefined,
|
67
|
-
}),
|
68
|
-
3,
|
69
|
-
200
|
70
|
-
);
|
71
|
-
}
|
72
|
-
|
73
|
-
export interface JupSwapTransaction {
|
74
|
-
jupQuote: QuoteResponse;
|
75
|
-
priceImpactBps: number;
|
76
|
-
lookupTableAddresses: string[];
|
77
|
-
setupInstructions: TransactionBuilder;
|
78
|
-
tokenLedgerIx?: TransactionBuilder;
|
79
|
-
swapIx: TransactionBuilder;
|
80
|
-
cleanupIx: TransactionBuilder;
|
81
|
-
}
|
82
|
-
|
83
|
-
export async function getJupSwapTransaction(
|
84
|
-
signer: Signer,
|
85
|
-
swapDetails: JupSwapDetails,
|
86
|
-
attemptNum?: number
|
87
|
-
): Promise<JupSwapTransaction> {
|
88
|
-
const quoteResponse =
|
89
|
-
swapDetails.jupQuote ?? (await getJupQuote(swapDetails));
|
90
|
-
|
91
|
-
const priceImpactBps =
|
92
|
-
Math.round(toBps(parseFloat(quoteResponse.priceImpactPct))) + 1;
|
93
|
-
const finalPriceSlippageBps = Math.round(
|
94
|
-
Math.max(50, quoteResponse.slippageBps, priceImpactBps) *
|
95
|
-
(1 + (swapDetails.slippageIncFactor ?? 0))
|
96
|
-
);
|
97
|
-
quoteResponse.slippageBps = finalPriceSlippageBps;
|
98
|
-
consoleLog("Quote:", quoteResponse);
|
99
|
-
|
100
|
-
consoleLog("Getting jup instructions...");
|
101
|
-
const instructions = await retryWithExponentialBackoff(
|
102
|
-
async () => {
|
103
|
-
const res = await jupApi.swapInstructionsPost({
|
104
|
-
swapRequest: {
|
105
|
-
userPublicKey: signer.publicKey.toString(),
|
106
|
-
quoteResponse,
|
107
|
-
wrapAndUnwrapSol: swapDetails.wrapAndUnwrapSol ?? false,
|
108
|
-
useTokenLedger: !swapDetails.exactOut && !swapDetails.exactIn,
|
109
|
-
destinationTokenAccount: getTokenAccount(
|
110
|
-
swapDetails.destinationWallet,
|
111
|
-
swapDetails.outputMint
|
112
|
-
).toString(),
|
113
|
-
},
|
114
|
-
});
|
115
|
-
if (!res) {
|
116
|
-
throw new Error("No instructions retrieved");
|
117
|
-
}
|
118
|
-
return res;
|
119
|
-
},
|
120
|
-
4,
|
121
|
-
200
|
122
|
-
);
|
123
|
-
|
124
|
-
if (!instructions.swapInstruction) {
|
125
|
-
throw new Error("No swap instruction was returned by Jupiter");
|
126
|
-
}
|
127
|
-
|
128
|
-
consoleLog("Raw price impact bps:", priceImpactBps);
|
129
|
-
const finalPriceImpactBps =
|
130
|
-
priceImpactBps * (1 + (swapDetails.slippageIncFactor ?? 0));
|
131
|
-
consoleLog("Increased price impact bps:", finalPriceImpactBps);
|
132
|
-
|
133
|
-
if (swapDetails.addPadding) {
|
134
|
-
consoleLog("Raw inAmount:", quoteResponse.inAmount);
|
135
|
-
const inc = Math.max(
|
136
|
-
fromBps(finalPriceImpactBps) * 1.1,
|
137
|
-
fromBps(finalPriceSlippageBps) * 0.05
|
138
|
-
);
|
139
|
-
consoleLog("Inc:", inc);
|
140
|
-
quoteResponse.inAmount = Math.round(
|
141
|
-
parseInt(quoteResponse.inAmount) + parseInt(quoteResponse.inAmount) * inc
|
142
|
-
).toString();
|
143
|
-
consoleLog("Increased inAmount:", quoteResponse.inAmount);
|
144
|
-
}
|
145
|
-
|
146
|
-
return {
|
147
|
-
jupQuote: quoteResponse,
|
148
|
-
priceImpactBps: finalPriceImpactBps,
|
149
|
-
lookupTableAddresses: instructions.addressLookupTableAddresses,
|
150
|
-
setupInstructions: transactionBuilder().add(
|
151
|
-
(instructions.setupInstructions ?? []).map((ix) =>
|
152
|
-
getWrappedInstruction(signer, createTransactionInstruction(ix))
|
153
|
-
)
|
154
|
-
),
|
155
|
-
tokenLedgerIx: instructions.tokenLedgerInstruction
|
156
|
-
? transactionBuilder().add(
|
157
|
-
getWrappedInstruction(
|
158
|
-
signer,
|
159
|
-
createTransactionInstruction(instructions.tokenLedgerInstruction)
|
160
|
-
)
|
161
|
-
)
|
162
|
-
: undefined,
|
163
|
-
swapIx: transactionBuilder().add(
|
164
|
-
getWrappedInstruction(
|
165
|
-
signer,
|
166
|
-
createTransactionInstruction(instructions.swapInstruction)
|
167
|
-
)
|
168
|
-
),
|
169
|
-
cleanupIx: instructions.cleanupInstruction
|
170
|
-
? transactionBuilder().add(
|
171
|
-
getWrappedInstruction(
|
172
|
-
signer,
|
173
|
-
createTransactionInstruction(instructions.cleanupInstruction)
|
174
|
-
)
|
175
|
-
)
|
176
|
-
: transactionBuilder(),
|
177
|
-
};
|
178
|
-
}
|
179
|
-
|
180
19
|
export async function getJupPriceData(mints: PublicKey[]) {
|
181
20
|
const data = await retryWithExponentialBackoff(async () => {
|
182
21
|
const res = await (
|
@@ -1,8 +1,9 @@
|
|
1
|
-
import {
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
2
2
|
import { publicKey, Umi } from "@metaplex-foundation/umi";
|
3
3
|
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
4
4
|
import {
|
5
5
|
Bank,
|
6
|
+
deserializeMarginfiAccount,
|
6
7
|
getMarginfiAccountSize,
|
7
8
|
MARGINFI_PROGRAM_ID,
|
8
9
|
MarginfiAccount,
|
@@ -24,9 +25,9 @@ import {
|
|
24
25
|
MARGINFI_ACCOUNTS,
|
25
26
|
} from "../constants/marginfiAccounts";
|
26
27
|
import { MarginfiAssetAccounts } from "../types/accounts";
|
27
|
-
import { PositionState,
|
28
|
+
import { PositionState, PositionTokenState } from "../generated";
|
28
29
|
import { USD_DECIMALS } from "../constants/generalAccounts";
|
29
|
-
import { ContextUpdates } from "./
|
30
|
+
import { ContextUpdates } from "./solautoUtils";
|
30
31
|
import { ALL_SUPPORTED_TOKENS, TOKEN_INFO } from "../constants";
|
31
32
|
import { fetchTokenPrices, safeGetPrice } from "./priceUtils";
|
32
33
|
|
@@ -143,6 +144,40 @@ export async function getMarginfiMaxLtvAndLiqThreshold(
|
|
143
144
|
);
|
144
145
|
}
|
145
146
|
|
147
|
+
export async function getEmptyMarginfiAccountsByAuthority(
|
148
|
+
umi: Umi,
|
149
|
+
authority: PublicKey
|
150
|
+
): Promise<MarginfiAccount[]> {
|
151
|
+
const marginfiAccounts = await umi.rpc.getProgramAccounts(
|
152
|
+
MARGINFI_PROGRAM_ID,
|
153
|
+
{
|
154
|
+
commitment: "confirmed",
|
155
|
+
filters: [
|
156
|
+
{
|
157
|
+
dataSize: getMarginfiAccountSize(),
|
158
|
+
},
|
159
|
+
{
|
160
|
+
memcmp: {
|
161
|
+
bytes: new Uint8Array(authority.toBuffer()),
|
162
|
+
offset: 8 + 32, // Anchor account discriminator + group pubkey
|
163
|
+
},
|
164
|
+
},
|
165
|
+
{
|
166
|
+
// First balance is not active
|
167
|
+
memcmp: {
|
168
|
+
bytes: new Uint8Array([0]),
|
169
|
+
offset: 8 + 32 + 32,
|
170
|
+
},
|
171
|
+
},
|
172
|
+
],
|
173
|
+
}
|
174
|
+
);
|
175
|
+
|
176
|
+
return marginfiAccounts
|
177
|
+
.map((x) => deserializeMarginfiAccount(x))
|
178
|
+
.filter((x) => marginfiAccountEmpty(x));
|
179
|
+
}
|
180
|
+
|
146
181
|
export async function getAllMarginfiAccountsByAuthority(
|
147
182
|
umi: Umi,
|
148
183
|
authority: PublicKey,
|
@@ -239,10 +274,11 @@ async function getTokenUsage(
|
|
239
274
|
isAsset: boolean,
|
240
275
|
shares: number,
|
241
276
|
amountUsedAdjustment?: bigint
|
242
|
-
): Promise<
|
277
|
+
): Promise<PositionTokenState> {
|
243
278
|
let amountUsed = 0;
|
244
279
|
let amountCanBeUsed = BigInt(0);
|
245
280
|
let marketPrice = 0;
|
281
|
+
let originationFee = 0;
|
246
282
|
|
247
283
|
if (bank !== null) {
|
248
284
|
[marketPrice] = await fetchTokenPrices([toWeb3JsPublicKey(bank.mint)]);
|
@@ -250,6 +286,9 @@ async function getTokenUsage(
|
|
250
286
|
const shareValue = isAsset ? assetShareValue : liabilityShareValue;
|
251
287
|
amountUsed = shares * shareValue + Number(amountUsedAdjustment ?? 0);
|
252
288
|
amountCanBeUsed = getBankLiquidityAvailableBaseUnit(bank, isAsset);
|
289
|
+
originationFee = bytesToI80F48(
|
290
|
+
bank?.config.interestRateConfig.protocolOriginationFee.value
|
291
|
+
);
|
253
292
|
}
|
254
293
|
|
255
294
|
return {
|
@@ -275,8 +314,7 @@ async function getTokenUsage(
|
|
275
314
|
: BigInt(0),
|
276
315
|
},
|
277
316
|
baseAmountMarketPriceUsd: toBaseUnit(marketPrice, USD_DECIMALS),
|
278
|
-
|
279
|
-
borrowFeeBps: 0,
|
317
|
+
borrowFeeBps: isAsset ? 0 : toBps(originationFee),
|
280
318
|
padding1: [],
|
281
319
|
padding2: [],
|
282
320
|
padding: new Uint8Array([]),
|
@@ -290,9 +328,29 @@ interface BankSelection {
|
|
290
328
|
|
291
329
|
type BanksCache = { [group: string]: { [mint: string]: Bank } };
|
292
330
|
|
331
|
+
async function getBank(
|
332
|
+
umi: Umi,
|
333
|
+
data: BankSelection,
|
334
|
+
marginfiGroup?: PublicKey
|
335
|
+
) {
|
336
|
+
return data?.banksCache && data.mint && marginfiGroup
|
337
|
+
? data.banksCache[marginfiGroup!.toString()][data?.mint?.toString()]
|
338
|
+
: data?.mint && data?.mint !== PublicKey.default
|
339
|
+
? await safeFetchBank(
|
340
|
+
umi,
|
341
|
+
publicKey(
|
342
|
+
MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][
|
343
|
+
data?.mint.toString()
|
344
|
+
].bank
|
345
|
+
),
|
346
|
+
{ commitment: "confirmed" }
|
347
|
+
)
|
348
|
+
: null;
|
349
|
+
}
|
350
|
+
|
293
351
|
export async function getMarginfiAccountPositionState(
|
294
352
|
umi: Umi,
|
295
|
-
protocolAccount: { pk
|
353
|
+
protocolAccount: { pk?: PublicKey; data?: MarginfiAccount | null },
|
296
354
|
marginfiGroup?: PublicKey,
|
297
355
|
supply?: BankSelection,
|
298
356
|
debt?: BankSelection,
|
@@ -303,9 +361,11 @@ export async function getMarginfiAccountPositionState(
|
|
303
361
|
> {
|
304
362
|
let marginfiAccount =
|
305
363
|
protocolAccount.data ??
|
306
|
-
(
|
307
|
-
|
308
|
-
|
364
|
+
(protocolAccount.pk
|
365
|
+
? await safeFetchMarginfiAccount(umi, publicKey(protocolAccount.pk), {
|
366
|
+
commitment: "confirmed",
|
367
|
+
})
|
368
|
+
: null);
|
309
369
|
|
310
370
|
if (!supply) {
|
311
371
|
supply = {};
|
@@ -318,37 +378,11 @@ export async function getMarginfiAccountPositionState(
|
|
318
378
|
marginfiGroup = toWeb3JsPublicKey(marginfiAccount.group);
|
319
379
|
}
|
320
380
|
|
321
|
-
let supplyBank: Bank | null =
|
322
|
-
|
323
|
-
? supply.banksCache[marginfiGroup!.toString()][supply?.mint?.toString()]
|
324
|
-
: supply?.mint && supply?.mint !== PublicKey.default
|
325
|
-
? await safeFetchBank(
|
326
|
-
umi,
|
327
|
-
publicKey(
|
328
|
-
MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][
|
329
|
-
supply?.mint.toString()
|
330
|
-
].bank
|
331
|
-
),
|
332
|
-
{ commitment: "confirmed" }
|
333
|
-
)
|
334
|
-
: null;
|
335
|
-
let debtBank: Bank | null =
|
336
|
-
debt?.banksCache && debt.mint && marginfiGroup
|
337
|
-
? debt.banksCache[marginfiGroup!.toString()][debt?.mint?.toString()]
|
338
|
-
: debt?.mint && debt?.mint !== PublicKey.default
|
339
|
-
? await safeFetchBank(
|
340
|
-
umi,
|
341
|
-
publicKey(
|
342
|
-
MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][
|
343
|
-
debt?.mint.toString()
|
344
|
-
].bank
|
345
|
-
),
|
346
|
-
{ commitment: "confirmed" }
|
347
|
-
)
|
348
|
-
: null;
|
381
|
+
let supplyBank: Bank | null = await getBank(umi, supply, marginfiGroup);
|
382
|
+
let debtBank: Bank | null = await getBank(umi, debt, marginfiGroup);
|
349
383
|
|
350
|
-
let supplyUsage:
|
351
|
-
let debtUsage:
|
384
|
+
let supplyUsage: PositionTokenState | undefined = undefined;
|
385
|
+
let debtUsage: PositionTokenState | undefined = undefined;
|
352
386
|
|
353
387
|
if (
|
354
388
|
marginfiAccount !== null &&
|
@@ -487,7 +521,7 @@ export async function getMarginfiAccountPositionState(
|
|
487
521
|
debt: debtUsage!,
|
488
522
|
maxLtvBps: toBps(maxLtv),
|
489
523
|
liqThresholdBps: toBps(liqThreshold),
|
490
|
-
|
524
|
+
lastRefreshed: BigInt(currentUnixSeconds()),
|
491
525
|
padding1: [],
|
492
526
|
padding2: [],
|
493
527
|
padding: [],
|
@@ -591,8 +625,8 @@ export function marginfiAccountEmpty(marginfiAccount: MarginfiAccount) {
|
|
591
625
|
marginfiAccount.lendingAccount.balances.find(
|
592
626
|
(x) =>
|
593
627
|
x.bankPk.toString() !== PublicKey.default.toString() &&
|
594
|
-
(
|
595
|
-
|
628
|
+
(bytesToI80F48(x.assetShares.value) > 0.000001 ||
|
629
|
+
bytesToI80F48(x.liabilityShares.value) > 0.000001)
|
596
630
|
) === undefined
|
597
631
|
);
|
598
632
|
}
|
package/src/utils/numberUtils.ts
CHANGED
@@ -1,24 +1,19 @@
|
|
1
1
|
import { BASIS_POINTS, MIN_REPAY_GAP_BPS, USD_DECIMALS } from "../constants";
|
2
|
-
import { PositionState
|
2
|
+
import { PositionState } from "../generated";
|
3
3
|
|
4
4
|
export function calcNetWorthUsd(state?: PositionState) {
|
5
|
-
return
|
6
|
-
state?.netWorth.baseAmountUsdValue ?? BigInt(0),
|
7
|
-
USD_DECIMALS
|
8
|
-
);
|
5
|
+
return fromRoundedUsdValue(state?.netWorth.baseAmountUsdValue ?? BigInt(0));
|
9
6
|
}
|
10
7
|
|
11
8
|
export function calcSupplyUsd(state?: PositionState) {
|
12
|
-
return
|
13
|
-
state?.supply.amountUsed.baseAmountUsdValue ?? BigInt(0)
|
14
|
-
USD_DECIMALS
|
9
|
+
return fromRoundedUsdValue(
|
10
|
+
state?.supply.amountUsed.baseAmountUsdValue ?? BigInt(0)
|
15
11
|
);
|
16
12
|
}
|
17
13
|
|
18
14
|
export function calcDebtUsd(state?: PositionState) {
|
19
|
-
return
|
20
|
-
state?.debt.amountUsed.baseAmountUsdValue ?? BigInt(0)
|
21
|
-
USD_DECIMALS
|
15
|
+
return fromRoundedUsdValue(
|
16
|
+
state?.debt.amountUsed.baseAmountUsdValue ?? BigInt(0)
|
22
17
|
);
|
23
18
|
}
|
24
19
|
|
@@ -44,19 +39,25 @@ export function calcTotalDebt(state?: PositionState) {
|
|
44
39
|
}
|
45
40
|
|
46
41
|
export function debtLiquidityUsdAvailable(state?: PositionState) {
|
47
|
-
return
|
48
|
-
state?.debt.amountCanBeUsed.baseAmountUsdValue ?? BigInt(0)
|
49
|
-
USD_DECIMALS
|
42
|
+
return fromRoundedUsdValue(
|
43
|
+
state?.debt.amountCanBeUsed.baseAmountUsdValue ?? BigInt(0)
|
50
44
|
);
|
51
45
|
}
|
52
46
|
|
53
47
|
export function supplyLiquidityUsdDepositable(state?: PositionState) {
|
54
|
-
return
|
55
|
-
state?.supply.amountCanBeUsed.baseAmountUsdValue ?? BigInt(0)
|
56
|
-
USD_DECIMALS
|
48
|
+
return fromRoundedUsdValue(
|
49
|
+
state?.supply.amountCanBeUsed.baseAmountUsdValue ?? BigInt(0)
|
57
50
|
);
|
58
51
|
}
|
59
52
|
|
53
|
+
export function fromRoundedUsdValue(number: bigint) {
|
54
|
+
return fromBaseUnit(number, USD_DECIMALS);
|
55
|
+
}
|
56
|
+
|
57
|
+
export function toRoundedUsdValue(number: number) {
|
58
|
+
return toBaseUnit(number, USD_DECIMALS);
|
59
|
+
}
|
60
|
+
|
60
61
|
export function getLiqUtilzationRateBps(
|
61
62
|
supplyUsd: number,
|
62
63
|
debtUsd: number,
|
@@ -149,66 +150,6 @@ export function getDebtAdjustmentUsd(
|
|
149
150
|
return debtAdjustmentUsd;
|
150
151
|
}
|
151
152
|
|
152
|
-
export function getSolautoFeesBps(
|
153
|
-
isReferred: boolean,
|
154
|
-
targetLiqUtilizationRateBps: number | undefined,
|
155
|
-
positionNetWorthUsd: number,
|
156
|
-
rebalanceDirection: RebalanceDirection
|
157
|
-
): {
|
158
|
-
solauto: number;
|
159
|
-
referrer: number;
|
160
|
-
total: number;
|
161
|
-
} {
|
162
|
-
const minSize = 10_000; // Minimum position size
|
163
|
-
const maxSize = 250_000; // Maximum position size
|
164
|
-
const maxFeeBps = 50; // Fee in basis points for minSize (0.5%)
|
165
|
-
const minFeeBps = 25; // Fee in basis points for maxSize (0.25%)
|
166
|
-
const k = 1.5;
|
167
|
-
|
168
|
-
if (
|
169
|
-
targetLiqUtilizationRateBps !== undefined &&
|
170
|
-
targetLiqUtilizationRateBps === 0
|
171
|
-
) {
|
172
|
-
return {
|
173
|
-
solauto: 0,
|
174
|
-
referrer: 0,
|
175
|
-
total: 0,
|
176
|
-
};
|
177
|
-
}
|
178
|
-
|
179
|
-
let feeBps: number = 0;
|
180
|
-
|
181
|
-
if (
|
182
|
-
targetLiqUtilizationRateBps !== undefined ||
|
183
|
-
rebalanceDirection === RebalanceDirection.Repay
|
184
|
-
) {
|
185
|
-
feeBps = 25;
|
186
|
-
} else if (positionNetWorthUsd <= minSize) {
|
187
|
-
feeBps = maxFeeBps;
|
188
|
-
} else if (positionNetWorthUsd >= maxSize) {
|
189
|
-
feeBps = minFeeBps;
|
190
|
-
} else {
|
191
|
-
const t =
|
192
|
-
(Math.log(positionNetWorthUsd) - Math.log(minSize)) /
|
193
|
-
(Math.log(maxSize) - Math.log(minSize));
|
194
|
-
feeBps = Math.round(
|
195
|
-
minFeeBps + (maxFeeBps - minFeeBps) * (1 - Math.pow(t, k))
|
196
|
-
);
|
197
|
-
}
|
198
|
-
|
199
|
-
let referrer = 0;
|
200
|
-
if (isReferred) {
|
201
|
-
feeBps *= 0.9;
|
202
|
-
referrer = Math.floor(feeBps * 0.15);
|
203
|
-
}
|
204
|
-
|
205
|
-
return {
|
206
|
-
solauto: feeBps - referrer,
|
207
|
-
referrer,
|
208
|
-
total: feeBps,
|
209
|
-
};
|
210
|
-
}
|
211
|
-
|
212
153
|
export function getMaxLiqUtilizationRateBps(
|
213
154
|
maxLtvBps: number,
|
214
155
|
liqThresholdBps: number,
|
package/src/utils/solanaUtils.ts
CHANGED
@@ -44,8 +44,8 @@ import {
|
|
44
44
|
getLendingAccountStartFlashloanInstructionDataSerializer,
|
45
45
|
} from "../marginfi-sdk";
|
46
46
|
import { PriorityFeeSetting, TransactionRunType } from "../types";
|
47
|
-
import { createDynamicSolautoProgram } from "./solauto";
|
48
47
|
import { SOLAUTO_PROD_PROGRAM } from "../constants";
|
48
|
+
import { createDynamicSolautoProgram } from "./solautoUtils";
|
49
49
|
|
50
50
|
export function buildHeliusApiUrl(heliusApiKey: string) {
|
51
51
|
return `https://mainnet.helius-rpc.com/?api-key=${heliusApiKey}`;
|