@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.
Files changed (268) hide show
  1. package/dist/constants/solautoConstants.d.ts +1 -0
  2. package/dist/constants/solautoConstants.d.ts.map +1 -1
  3. package/dist/constants/solautoConstants.js +4 -10
  4. package/dist/constants/switchboardConstants.d.ts.map +1 -1
  5. package/dist/constants/switchboardConstants.js +5 -5
  6. package/dist/generated/accounts/solautoPosition.js +2 -2
  7. package/dist/generated/errors/solauto.d.ts +14 -2
  8. package/dist/generated/errors/solauto.d.ts.map +1 -1
  9. package/dist/generated/errors/solauto.js +27 -5
  10. package/dist/generated/instructions/claimReferralFees.d.ts +1 -1
  11. package/dist/generated/instructions/claimReferralFees.d.ts.map +1 -1
  12. package/dist/generated/instructions/closePosition.d.ts +1 -1
  13. package/dist/generated/instructions/closePosition.d.ts.map +1 -1
  14. package/dist/generated/instructions/closePosition.js +2 -2
  15. package/dist/generated/instructions/marginfiOpenPosition.d.ts +1 -3
  16. package/dist/generated/instructions/marginfiOpenPosition.d.ts.map +1 -1
  17. package/dist/generated/instructions/marginfiOpenPosition.js +0 -1
  18. package/dist/generated/instructions/marginfiRebalance.d.ts +7 -3
  19. package/dist/generated/instructions/marginfiRebalance.d.ts.map +1 -1
  20. package/dist/generated/instructions/marginfiRebalance.js +3 -1
  21. package/dist/generated/types/index.d.ts +7 -1
  22. package/dist/generated/types/index.d.ts.map +1 -1
  23. package/dist/generated/types/index.js +7 -1
  24. package/dist/generated/types/positionData.d.ts +2 -2
  25. package/dist/generated/types/positionData.d.ts.map +1 -1
  26. package/dist/generated/types/positionData.js +1 -1
  27. package/dist/generated/types/positionState.d.ts +7 -7
  28. package/dist/generated/types/positionState.d.ts.map +1 -1
  29. package/dist/generated/types/positionState.js +3 -3
  30. package/dist/generated/types/{positionTokenUsage.d.ts → positionTokenState.d.ts} +6 -8
  31. package/dist/generated/types/positionTokenState.d.ts.map +1 -0
  32. package/dist/generated/types/{positionTokenUsage.js → positionTokenState.js} +6 -7
  33. package/dist/generated/types/rebalanceData.d.ts +7 -13
  34. package/dist/generated/types/rebalanceData.d.ts.map +1 -1
  35. package/dist/generated/types/rebalanceData.js +3 -6
  36. package/dist/generated/types/rebalanceDirection.d.ts +3 -2
  37. package/dist/generated/types/rebalanceDirection.d.ts.map +1 -1
  38. package/dist/generated/types/rebalanceDirection.js +3 -2
  39. package/dist/generated/types/rebalanceInstructionData.d.ts +27 -0
  40. package/dist/generated/types/rebalanceInstructionData.d.ts.map +1 -0
  41. package/dist/generated/types/rebalanceInstructionData.js +22 -0
  42. package/dist/generated/types/rebalanceStateValues.d.ts +27 -0
  43. package/dist/generated/types/rebalanceStateValues.d.ts.map +1 -0
  44. package/dist/generated/types/rebalanceStateValues.js +22 -0
  45. package/dist/generated/types/rebalanceStep.d.ts +15 -0
  46. package/dist/generated/types/rebalanceStep.d.ts.map +1 -0
  47. package/dist/generated/types/rebalanceStep.js +22 -0
  48. package/dist/generated/types/solautoRebalanceType.d.ts +4 -5
  49. package/dist/generated/types/solautoRebalanceType.d.ts.map +1 -1
  50. package/dist/generated/types/solautoRebalanceType.js +4 -5
  51. package/dist/generated/types/solautoSettingsParameters.d.ts +2 -15
  52. package/dist/generated/types/solautoSettingsParameters.d.ts.map +1 -1
  53. package/dist/generated/types/solautoSettingsParameters.js +1 -5
  54. package/dist/generated/types/solautoSettingsParametersInp.d.ts +1 -12
  55. package/dist/generated/types/solautoSettingsParametersInp.d.ts.map +1 -1
  56. package/dist/generated/types/solautoSettingsParametersInp.js +0 -3
  57. package/dist/generated/types/swapType.d.ts +15 -0
  58. package/dist/generated/types/swapType.d.ts.map +1 -0
  59. package/dist/generated/types/swapType.js +22 -0
  60. package/dist/generated/types/tokenBalanceChange.d.ts +21 -0
  61. package/dist/generated/types/tokenBalanceChange.d.ts.map +1 -0
  62. package/dist/generated/types/tokenBalanceChange.js +19 -0
  63. package/dist/generated/types/tokenBalanceChangeType.d.ts +18 -0
  64. package/dist/generated/types/tokenBalanceChangeType.d.ts.map +1 -0
  65. package/dist/generated/types/tokenBalanceChangeType.js +25 -0
  66. package/dist/generated/types/updatePositionData.d.ts +2 -2
  67. package/dist/generated/types/updatePositionData.d.ts.map +1 -1
  68. package/dist/generated/types/updatePositionData.js +1 -1
  69. package/dist/index.d.ts +8 -6
  70. package/dist/index.d.ts.map +1 -1
  71. package/dist/index.js +5 -5
  72. package/dist/marginfi-sdk/types/interestRateConfig.d.ts +3 -1
  73. package/dist/marginfi-sdk/types/interestRateConfig.d.ts.map +1 -1
  74. package/dist/marginfi-sdk/types/interestRateConfig.js +2 -1
  75. package/dist/services/flashLoans/flProviderAggregator.d.ts +19 -0
  76. package/dist/services/flashLoans/flProviderAggregator.d.ts.map +1 -0
  77. package/dist/services/flashLoans/flProviderAggregator.js +46 -0
  78. package/dist/services/flashLoans/flProviderBase.d.ts +23 -0
  79. package/dist/services/flashLoans/flProviderBase.d.ts.map +1 -0
  80. package/dist/services/flashLoans/flProviderBase.js +37 -0
  81. package/dist/services/flashLoans/index.d.ts +4 -0
  82. package/dist/services/flashLoans/index.d.ts.map +1 -0
  83. package/dist/services/flashLoans/index.js +19 -0
  84. package/dist/services/flashLoans/marginfiFlProvider.d.ts +25 -0
  85. package/dist/services/flashLoans/marginfiFlProvider.d.ts.map +1 -0
  86. package/dist/services/flashLoans/marginfiFlProvider.js +246 -0
  87. package/dist/services/index.d.ts +6 -0
  88. package/dist/services/index.d.ts.map +1 -0
  89. package/dist/services/index.js +21 -0
  90. package/dist/services/rebalance/index.d.ts +3 -0
  91. package/dist/services/rebalance/index.d.ts.map +1 -0
  92. package/dist/services/rebalance/index.js +18 -0
  93. package/dist/services/rebalance/rebalanceSwapManager.d.ts +30 -0
  94. package/dist/services/rebalance/rebalanceSwapManager.d.ts.map +1 -0
  95. package/dist/services/rebalance/rebalanceSwapManager.js +144 -0
  96. package/dist/services/rebalance/rebalanceTxBuilder.d.ts +22 -0
  97. package/dist/services/rebalance/rebalanceTxBuilder.d.ts.map +1 -0
  98. package/dist/services/rebalance/rebalanceTxBuilder.js +200 -0
  99. package/dist/services/rebalance/rebalanceValues.d.ts +31 -0
  100. package/dist/services/rebalance/rebalanceValues.d.ts.map +1 -0
  101. package/dist/services/rebalance/rebalanceValues.js +118 -0
  102. package/dist/services/rebalance/solautoFees.d.ts +13 -0
  103. package/dist/services/rebalance/solautoFees.d.ts.map +1 -0
  104. package/dist/services/rebalance/solautoFees.js +54 -0
  105. package/dist/services/solauto/index.d.ts +5 -0
  106. package/dist/services/solauto/index.d.ts.map +1 -0
  107. package/dist/{clients → services/solauto}/referralStateManager.d.ts +4 -5
  108. package/dist/services/solauto/referralStateManager.d.ts.map +1 -0
  109. package/dist/{clients → services/solauto}/referralStateManager.js +10 -12
  110. package/dist/services/solauto/solautoClient.d.ts +59 -0
  111. package/dist/services/solauto/solautoClient.d.ts.map +1 -0
  112. package/dist/{clients → services/solauto}/solautoClient.js +78 -109
  113. package/dist/services/solauto/solautoMarginfiClient.d.ts +34 -0
  114. package/dist/services/solauto/solautoMarginfiClient.d.ts.map +1 -0
  115. package/dist/services/solauto/solautoMarginfiClient.js +322 -0
  116. package/dist/{clients → services/solauto}/txHandler.d.ts +11 -2
  117. package/dist/services/solauto/txHandler.d.ts.map +1 -0
  118. package/dist/services/solauto/txHandler.js +38 -0
  119. package/dist/services/swap/index.d.ts +2 -0
  120. package/dist/services/swap/index.d.ts.map +1 -0
  121. package/dist/{utils/solauto → services/swap}/index.js +1 -2
  122. package/dist/services/swap/jupSwapManager.d.ts +37 -0
  123. package/dist/services/swap/jupSwapManager.d.ts.map +1 -0
  124. package/dist/services/swap/jupSwapManager.js +108 -0
  125. package/dist/services/transactions/index.d.ts.map +1 -0
  126. package/dist/{transactions → services/transactions}/transactionUtils.d.ts +3 -5
  127. package/dist/services/transactions/transactionUtils.d.ts.map +1 -0
  128. package/dist/{transactions → services/transactions}/transactionUtils.js +29 -137
  129. package/dist/{transactions → services/transactions}/transactionsManager.d.ts +4 -4
  130. package/dist/services/transactions/transactionsManager.d.ts.map +1 -0
  131. package/dist/{transactions → services/transactions}/transactionsManager.js +10 -8
  132. package/dist/solautoPosition/index.d.ts +4 -0
  133. package/dist/solautoPosition/index.d.ts.map +1 -0
  134. package/dist/solautoPosition/index.js +19 -0
  135. package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts +14 -0
  136. package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts.map +1 -0
  137. package/dist/solautoPosition/marginfiSolautoPositionEx.js +73 -0
  138. package/dist/solautoPosition/solautoPositionEx.d.ts +67 -0
  139. package/dist/solautoPosition/solautoPositionEx.d.ts.map +1 -0
  140. package/dist/solautoPosition/solautoPositionEx.js +143 -0
  141. package/dist/solautoPosition/utils.d.ts +17 -0
  142. package/dist/solautoPosition/utils.d.ts.map +1 -0
  143. package/dist/solautoPosition/utils.js +115 -0
  144. package/dist/types/solauto.d.ts +19 -1
  145. package/dist/types/solauto.d.ts.map +1 -1
  146. package/dist/utils/generalUtils.d.ts +1 -0
  147. package/dist/utils/generalUtils.d.ts.map +1 -1
  148. package/dist/utils/generalUtils.js +9 -1
  149. package/dist/utils/index.d.ts +1 -1
  150. package/dist/utils/index.d.ts.map +1 -1
  151. package/dist/utils/index.js +1 -1
  152. package/dist/utils/jitoUtils.d.ts.map +1 -1
  153. package/dist/utils/jupiterUtils.d.ts +3 -28
  154. package/dist/utils/jupiterUtils.d.ts.map +1 -1
  155. package/dist/utils/jupiterUtils.js +2 -73
  156. package/dist/utils/marginfiUtils.d.ts +4 -3
  157. package/dist/utils/marginfiUtils.d.ts.map +1 -1
  158. package/dist/utils/marginfiUtils.js +47 -18
  159. package/dist/utils/numberUtils.d.ts +3 -6
  160. package/dist/utils/numberUtils.d.ts.map +1 -1
  161. package/dist/utils/numberUtils.js +13 -48
  162. package/dist/utils/priceUtils.js +1 -1
  163. package/dist/utils/solanaUtils.js +2 -2
  164. package/dist/utils/{solauto/generalUtils.d.ts → solautoUtils.d.ts} +9 -16
  165. package/dist/utils/solautoUtils.d.ts.map +1 -0
  166. package/dist/utils/{solauto/generalUtils.js → solautoUtils.js} +48 -173
  167. package/dist/utils/switchboardUtils.d.ts +1 -1
  168. package/dist/utils/switchboardUtils.d.ts.map +1 -1
  169. package/dist/utils/switchboardUtils.js +10 -9
  170. package/local/createTokenAccounts.ts +1 -5
  171. package/local/logPositions.ts +22 -68
  172. package/local/shared.ts +29 -24
  173. package/local/txSandbox.ts +27 -0
  174. package/local/updateMarginfiLUT.ts +80 -4
  175. package/package.json +4 -7
  176. package/src/constants/solautoConstants.ts +4 -9
  177. package/src/constants/switchboardConstants.ts +10 -5
  178. package/src/generated/accounts/solautoPosition.ts +2 -2
  179. package/src/generated/errors/solauto.ts +45 -4
  180. package/src/generated/instructions/claimReferralFees.ts +1 -1
  181. package/src/generated/instructions/closePosition.ts +3 -3
  182. package/src/generated/instructions/marginfiOpenPosition.ts +0 -7
  183. package/src/generated/instructions/marginfiRebalance.ts +12 -3
  184. package/src/generated/types/index.ts +7 -1
  185. package/src/generated/types/positionData.ts +3 -3
  186. package/src/generated/types/positionState.ts +12 -12
  187. package/src/generated/types/{positionTokenUsage.ts → positionTokenState.ts} +13 -16
  188. package/src/generated/types/rebalanceData.ts +16 -27
  189. package/src/generated/types/rebalanceDirection.ts +1 -0
  190. package/src/generated/types/rebalanceInstructionData.ts +62 -0
  191. package/src/generated/types/rebalanceStateValues.ts +59 -0
  192. package/src/generated/types/rebalanceStep.ts +25 -0
  193. package/src/generated/types/solautoRebalanceType.ts +0 -1
  194. package/src/generated/types/solautoSettingsParameters.ts +4 -25
  195. package/src/generated/types/solautoSettingsParametersInp.ts +2 -24
  196. package/src/generated/types/swapType.ts +22 -0
  197. package/src/generated/types/tokenBalanceChange.ts +46 -0
  198. package/src/generated/types/tokenBalanceChangeType.ts +28 -0
  199. package/src/generated/types/updatePositionData.ts +3 -3
  200. package/src/index.ts +8 -8
  201. package/src/marginfi-sdk/types/interestRateConfig.ts +6 -2
  202. package/src/services/flashLoans/flProviderAggregator.ts +74 -0
  203. package/src/services/flashLoans/flProviderBase.ts +88 -0
  204. package/src/services/flashLoans/index.ts +3 -0
  205. package/src/services/flashLoans/marginfiFlProvider.ts +392 -0
  206. package/src/services/index.ts +5 -0
  207. package/src/services/rebalance/index.ts +2 -0
  208. package/src/services/rebalance/rebalanceSwapManager.ts +218 -0
  209. package/src/services/rebalance/rebalanceTxBuilder.ts +330 -0
  210. package/src/services/rebalance/rebalanceValues.ts +232 -0
  211. package/src/services/rebalance/solautoFees.ts +61 -0
  212. package/src/services/solauto/index.ts +4 -0
  213. package/src/{clients → services/solauto}/referralStateManager.ts +17 -31
  214. package/src/{clients → services/solauto}/solautoClient.ts +142 -205
  215. package/src/services/solauto/solautoMarginfiClient.ts +472 -0
  216. package/src/services/solauto/txHandler.ts +69 -0
  217. package/src/services/swap/index.ts +1 -0
  218. package/src/services/swap/jupSwapManager.ts +189 -0
  219. package/src/{transactions → services/transactions}/transactionUtils.ts +40 -280
  220. package/src/{transactions → services/transactions}/transactionsManager.ts +15 -10
  221. package/src/solautoPosition/index.ts +3 -0
  222. package/src/solautoPosition/marginfiSolautoPositionEx.ts +111 -0
  223. package/src/solautoPosition/solautoPositionEx.ts +281 -0
  224. package/src/solautoPosition/utils.ts +188 -0
  225. package/src/types/solauto.ts +30 -2
  226. package/src/utils/generalUtils.ts +9 -1
  227. package/src/utils/index.ts +2 -2
  228. package/src/utils/jitoUtils.ts +0 -4
  229. package/src/utils/jupiterUtils.ts +3 -164
  230. package/src/utils/marginfiUtils.ts +77 -43
  231. package/src/utils/numberUtils.ts +18 -77
  232. package/src/utils/solanaUtils.ts +1 -1
  233. package/src/utils/{solauto/generalUtils.ts → solautoUtils.ts} +66 -288
  234. package/src/utils/switchboardUtils.ts +15 -17
  235. package/tests/transactions/shared.ts +135 -0
  236. package/tests/transactions/solautoMarginfi.ts +8 -198
  237. package/tests/unit/accounts.ts +3 -10
  238. package/tests/unit/lookupTables.ts +23 -2
  239. package/tests/unit/rebalanceCalculations.ts +65 -343
  240. package/dist/clients/index.d.ts +0 -5
  241. package/dist/clients/index.d.ts.map +0 -1
  242. package/dist/clients/referralStateManager.d.ts.map +0 -1
  243. package/dist/clients/solautoClient.d.ts +0 -71
  244. package/dist/clients/solautoClient.d.ts.map +0 -1
  245. package/dist/clients/solautoMarginfiClient.d.ts +0 -51
  246. package/dist/clients/solautoMarginfiClient.d.ts.map +0 -1
  247. package/dist/clients/solautoMarginfiClient.js +0 -497
  248. package/dist/clients/txHandler.d.ts.map +0 -1
  249. package/dist/clients/txHandler.js +0 -23
  250. package/dist/generated/types/positionTokenUsage.d.ts.map +0 -1
  251. package/dist/transactions/index.d.ts.map +0 -1
  252. package/dist/transactions/transactionUtils.d.ts.map +0 -1
  253. package/dist/transactions/transactionsManager.d.ts.map +0 -1
  254. package/dist/utils/solauto/generalUtils.d.ts.map +0 -1
  255. package/dist/utils/solauto/index.d.ts +0 -3
  256. package/dist/utils/solauto/index.d.ts.map +0 -1
  257. package/dist/utils/solauto/rebalanceUtils.d.ts +0 -30
  258. package/dist/utils/solauto/rebalanceUtils.d.ts.map +0 -1
  259. package/dist/utils/solauto/rebalanceUtils.js +0 -287
  260. package/src/clients/index.ts +0 -4
  261. package/src/clients/solautoMarginfiClient.ts +0 -774
  262. package/src/clients/txHandler.ts +0 -38
  263. package/src/utils/solauto/index.ts +0 -2
  264. package/src/utils/solauto/rebalanceUtils.ts +0 -562
  265. package/dist/{clients → services/solauto}/index.js +2 -2
  266. /package/dist/{transactions → services/transactions}/index.d.ts +0 -0
  267. /package/dist/{transactions → services/transactions}/index.js +0 -0
  268. /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 { getWrappedInstruction } from "./solanaUtils";
8
- import { fromBps, toBps } from "./numberUtils";
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
- const jupApi = createJupiterApiClient();
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 { Connection, PublicKey } from "@solana/web3.js";
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, PositionTokenUsage } from "../generated";
28
+ import { PositionState, PositionTokenState } from "../generated";
28
29
  import { USD_DECIMALS } from "../constants/generalAccounts";
29
- import { ContextUpdates } from "./solauto/generalUtils";
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<PositionTokenUsage> {
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
- flashLoanFeeBps: 0,
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: PublicKey; data?: MarginfiAccount },
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
- (await safeFetchMarginfiAccount(umi, publicKey(protocolAccount.pk), {
307
- commitment: "confirmed",
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
- supply?.banksCache && supply.mint && marginfiGroup
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: PositionTokenUsage | undefined = undefined;
351
- let debtUsage: PositionTokenUsage | undefined = undefined;
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
- lastUpdated: BigInt(currentUnixSeconds()),
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
- (Math.round(bytesToI80F48(x.assetShares.value)) != 0 ||
595
- Math.round(bytesToI80F48(x.liabilityShares.value)) != 0)
628
+ (bytesToI80F48(x.assetShares.value) > 0.000001 ||
629
+ bytesToI80F48(x.liabilityShares.value) > 0.000001)
596
630
  ) === undefined
597
631
  );
598
632
  }
@@ -1,24 +1,19 @@
1
1
  import { BASIS_POINTS, MIN_REPAY_GAP_BPS, USD_DECIMALS } from "../constants";
2
- import { PositionState, RebalanceDirection } from "../generated";
2
+ import { PositionState } from "../generated";
3
3
 
4
4
  export function calcNetWorthUsd(state?: PositionState) {
5
- return fromBaseUnit(
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 fromBaseUnit(
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 fromBaseUnit(
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 fromBaseUnit(
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 fromBaseUnit(
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,
@@ -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}`;