@haven-fi/solauto-sdk 1.0.582 → 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 (260) 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/generated/accounts/solautoPosition.js +2 -2
  5. package/dist/generated/errors/solauto.d.ts +14 -2
  6. package/dist/generated/errors/solauto.d.ts.map +1 -1
  7. package/dist/generated/errors/solauto.js +27 -5
  8. package/dist/generated/instructions/claimReferralFees.d.ts +1 -1
  9. package/dist/generated/instructions/claimReferralFees.d.ts.map +1 -1
  10. package/dist/generated/instructions/closePosition.d.ts +1 -1
  11. package/dist/generated/instructions/closePosition.d.ts.map +1 -1
  12. package/dist/generated/instructions/closePosition.js +2 -2
  13. package/dist/generated/instructions/marginfiOpenPosition.d.ts +1 -3
  14. package/dist/generated/instructions/marginfiOpenPosition.d.ts.map +1 -1
  15. package/dist/generated/instructions/marginfiOpenPosition.js +0 -1
  16. package/dist/generated/instructions/marginfiRebalance.d.ts +7 -3
  17. package/dist/generated/instructions/marginfiRebalance.d.ts.map +1 -1
  18. package/dist/generated/instructions/marginfiRebalance.js +3 -1
  19. package/dist/generated/types/index.d.ts +7 -1
  20. package/dist/generated/types/index.d.ts.map +1 -1
  21. package/dist/generated/types/index.js +7 -1
  22. package/dist/generated/types/positionData.d.ts +2 -2
  23. package/dist/generated/types/positionData.d.ts.map +1 -1
  24. package/dist/generated/types/positionData.js +1 -1
  25. package/dist/generated/types/positionState.d.ts +7 -7
  26. package/dist/generated/types/positionState.d.ts.map +1 -1
  27. package/dist/generated/types/positionState.js +3 -3
  28. package/dist/generated/types/{positionTokenUsage.d.ts → positionTokenState.d.ts} +6 -8
  29. package/dist/generated/types/positionTokenState.d.ts.map +1 -0
  30. package/dist/generated/types/{positionTokenUsage.js → positionTokenState.js} +6 -7
  31. package/dist/generated/types/rebalanceData.d.ts +7 -13
  32. package/dist/generated/types/rebalanceData.d.ts.map +1 -1
  33. package/dist/generated/types/rebalanceData.js +3 -6
  34. package/dist/generated/types/rebalanceDirection.d.ts +3 -2
  35. package/dist/generated/types/rebalanceDirection.d.ts.map +1 -1
  36. package/dist/generated/types/rebalanceDirection.js +3 -2
  37. package/dist/generated/types/rebalanceInstructionData.d.ts +27 -0
  38. package/dist/generated/types/rebalanceInstructionData.d.ts.map +1 -0
  39. package/dist/generated/types/rebalanceInstructionData.js +22 -0
  40. package/dist/generated/types/rebalanceStateValues.d.ts +27 -0
  41. package/dist/generated/types/rebalanceStateValues.d.ts.map +1 -0
  42. package/dist/generated/types/rebalanceStateValues.js +22 -0
  43. package/dist/generated/types/rebalanceStep.d.ts +15 -0
  44. package/dist/generated/types/rebalanceStep.d.ts.map +1 -0
  45. package/dist/generated/types/rebalanceStep.js +22 -0
  46. package/dist/generated/types/solautoRebalanceType.d.ts +4 -5
  47. package/dist/generated/types/solautoRebalanceType.d.ts.map +1 -1
  48. package/dist/generated/types/solautoRebalanceType.js +4 -5
  49. package/dist/generated/types/solautoSettingsParameters.d.ts +2 -15
  50. package/dist/generated/types/solautoSettingsParameters.d.ts.map +1 -1
  51. package/dist/generated/types/solautoSettingsParameters.js +1 -5
  52. package/dist/generated/types/solautoSettingsParametersInp.d.ts +1 -12
  53. package/dist/generated/types/solautoSettingsParametersInp.d.ts.map +1 -1
  54. package/dist/generated/types/solautoSettingsParametersInp.js +0 -3
  55. package/dist/generated/types/swapType.d.ts +15 -0
  56. package/dist/generated/types/swapType.d.ts.map +1 -0
  57. package/dist/generated/types/swapType.js +22 -0
  58. package/dist/generated/types/tokenBalanceChange.d.ts +21 -0
  59. package/dist/generated/types/tokenBalanceChange.d.ts.map +1 -0
  60. package/dist/generated/types/tokenBalanceChange.js +19 -0
  61. package/dist/generated/types/tokenBalanceChangeType.d.ts +18 -0
  62. package/dist/generated/types/tokenBalanceChangeType.d.ts.map +1 -0
  63. package/dist/generated/types/tokenBalanceChangeType.js +25 -0
  64. package/dist/generated/types/updatePositionData.d.ts +2 -2
  65. package/dist/generated/types/updatePositionData.d.ts.map +1 -1
  66. package/dist/generated/types/updatePositionData.js +1 -1
  67. package/dist/index.d.ts +8 -6
  68. package/dist/index.d.ts.map +1 -1
  69. package/dist/index.js +5 -5
  70. package/dist/marginfi-sdk/types/interestRateConfig.d.ts +3 -1
  71. package/dist/marginfi-sdk/types/interestRateConfig.d.ts.map +1 -1
  72. package/dist/marginfi-sdk/types/interestRateConfig.js +2 -1
  73. package/dist/services/flashLoans/flProviderAggregator.d.ts +19 -0
  74. package/dist/services/flashLoans/flProviderAggregator.d.ts.map +1 -0
  75. package/dist/services/flashLoans/flProviderAggregator.js +46 -0
  76. package/dist/services/flashLoans/flProviderBase.d.ts +23 -0
  77. package/dist/services/flashLoans/flProviderBase.d.ts.map +1 -0
  78. package/dist/services/flashLoans/flProviderBase.js +37 -0
  79. package/dist/services/flashLoans/index.d.ts +4 -0
  80. package/dist/services/flashLoans/index.d.ts.map +1 -0
  81. package/dist/services/flashLoans/index.js +19 -0
  82. package/dist/services/flashLoans/marginfiFlProvider.d.ts +25 -0
  83. package/dist/services/flashLoans/marginfiFlProvider.d.ts.map +1 -0
  84. package/dist/services/flashLoans/marginfiFlProvider.js +246 -0
  85. package/dist/services/index.d.ts +6 -0
  86. package/dist/services/index.d.ts.map +1 -0
  87. package/dist/services/index.js +21 -0
  88. package/dist/services/rebalance/index.d.ts +3 -0
  89. package/dist/services/rebalance/index.d.ts.map +1 -0
  90. package/dist/services/rebalance/index.js +18 -0
  91. package/dist/services/rebalance/rebalanceSwapManager.d.ts +30 -0
  92. package/dist/services/rebalance/rebalanceSwapManager.d.ts.map +1 -0
  93. package/dist/services/rebalance/rebalanceSwapManager.js +144 -0
  94. package/dist/services/rebalance/rebalanceTxBuilder.d.ts +22 -0
  95. package/dist/services/rebalance/rebalanceTxBuilder.d.ts.map +1 -0
  96. package/dist/services/rebalance/rebalanceTxBuilder.js +200 -0
  97. package/dist/services/rebalance/rebalanceValues.d.ts +31 -0
  98. package/dist/services/rebalance/rebalanceValues.d.ts.map +1 -0
  99. package/dist/services/rebalance/rebalanceValues.js +118 -0
  100. package/dist/services/rebalance/solautoFees.d.ts +13 -0
  101. package/dist/services/rebalance/solautoFees.d.ts.map +1 -0
  102. package/dist/services/rebalance/solautoFees.js +54 -0
  103. package/dist/services/solauto/index.d.ts +5 -0
  104. package/dist/services/solauto/index.d.ts.map +1 -0
  105. package/dist/{clients → services/solauto}/referralStateManager.d.ts +4 -5
  106. package/dist/services/solauto/referralStateManager.d.ts.map +1 -0
  107. package/dist/{clients → services/solauto}/referralStateManager.js +10 -12
  108. package/dist/services/solauto/solautoClient.d.ts +59 -0
  109. package/dist/services/solauto/solautoClient.d.ts.map +1 -0
  110. package/dist/{clients → services/solauto}/solautoClient.js +78 -109
  111. package/dist/services/solauto/solautoMarginfiClient.d.ts +34 -0
  112. package/dist/services/solauto/solautoMarginfiClient.d.ts.map +1 -0
  113. package/dist/services/solauto/solautoMarginfiClient.js +322 -0
  114. package/dist/{clients → services/solauto}/txHandler.d.ts +11 -2
  115. package/dist/services/solauto/txHandler.d.ts.map +1 -0
  116. package/dist/services/solauto/txHandler.js +38 -0
  117. package/dist/services/swap/index.d.ts +2 -0
  118. package/dist/services/swap/index.d.ts.map +1 -0
  119. package/dist/{utils/solauto → services/swap}/index.js +1 -2
  120. package/dist/services/swap/jupSwapManager.d.ts +37 -0
  121. package/dist/services/swap/jupSwapManager.d.ts.map +1 -0
  122. package/dist/services/swap/jupSwapManager.js +108 -0
  123. package/dist/services/transactions/index.d.ts.map +1 -0
  124. package/dist/{transactions → services/transactions}/transactionUtils.d.ts +3 -5
  125. package/dist/services/transactions/transactionUtils.d.ts.map +1 -0
  126. package/dist/{transactions → services/transactions}/transactionUtils.js +29 -137
  127. package/dist/{transactions → services/transactions}/transactionsManager.d.ts +4 -4
  128. package/dist/services/transactions/transactionsManager.d.ts.map +1 -0
  129. package/dist/{transactions → services/transactions}/transactionsManager.js +10 -8
  130. package/dist/solautoPosition/index.d.ts +4 -0
  131. package/dist/solautoPosition/index.d.ts.map +1 -0
  132. package/dist/solautoPosition/index.js +19 -0
  133. package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts +14 -0
  134. package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts.map +1 -0
  135. package/dist/solautoPosition/marginfiSolautoPositionEx.js +73 -0
  136. package/dist/solautoPosition/solautoPositionEx.d.ts +67 -0
  137. package/dist/solautoPosition/solautoPositionEx.d.ts.map +1 -0
  138. package/dist/solautoPosition/solautoPositionEx.js +143 -0
  139. package/dist/solautoPosition/utils.d.ts +17 -0
  140. package/dist/solautoPosition/utils.d.ts.map +1 -0
  141. package/dist/solautoPosition/utils.js +115 -0
  142. package/dist/types/solauto.d.ts +19 -1
  143. package/dist/types/solauto.d.ts.map +1 -1
  144. package/dist/utils/generalUtils.d.ts +1 -0
  145. package/dist/utils/generalUtils.d.ts.map +1 -1
  146. package/dist/utils/generalUtils.js +9 -1
  147. package/dist/utils/index.d.ts +1 -1
  148. package/dist/utils/index.d.ts.map +1 -1
  149. package/dist/utils/index.js +1 -1
  150. package/dist/utils/jitoUtils.d.ts.map +1 -1
  151. package/dist/utils/jupiterUtils.d.ts +3 -28
  152. package/dist/utils/jupiterUtils.d.ts.map +1 -1
  153. package/dist/utils/jupiterUtils.js +2 -73
  154. package/dist/utils/marginfiUtils.d.ts +4 -3
  155. package/dist/utils/marginfiUtils.d.ts.map +1 -1
  156. package/dist/utils/marginfiUtils.js +47 -18
  157. package/dist/utils/numberUtils.d.ts +3 -6
  158. package/dist/utils/numberUtils.d.ts.map +1 -1
  159. package/dist/utils/numberUtils.js +13 -48
  160. package/dist/utils/solanaUtils.js +2 -2
  161. package/dist/utils/{solauto/generalUtils.d.ts → solautoUtils.d.ts} +9 -16
  162. package/dist/utils/solautoUtils.d.ts.map +1 -0
  163. package/dist/utils/{solauto/generalUtils.js → solautoUtils.js} +48 -173
  164. package/local/createTokenAccounts.ts +1 -5
  165. package/local/logPositions.ts +22 -68
  166. package/local/shared.ts +29 -24
  167. package/local/txSandbox.ts +27 -0
  168. package/local/updateMarginfiLUT.ts +80 -4
  169. package/package.json +3 -6
  170. package/src/constants/solautoConstants.ts +4 -9
  171. package/src/generated/accounts/solautoPosition.ts +2 -2
  172. package/src/generated/errors/solauto.ts +45 -4
  173. package/src/generated/instructions/claimReferralFees.ts +1 -1
  174. package/src/generated/instructions/closePosition.ts +3 -3
  175. package/src/generated/instructions/marginfiOpenPosition.ts +0 -7
  176. package/src/generated/instructions/marginfiRebalance.ts +12 -3
  177. package/src/generated/types/index.ts +7 -1
  178. package/src/generated/types/positionData.ts +3 -3
  179. package/src/generated/types/positionState.ts +12 -12
  180. package/src/generated/types/{positionTokenUsage.ts → positionTokenState.ts} +13 -16
  181. package/src/generated/types/rebalanceData.ts +16 -27
  182. package/src/generated/types/rebalanceDirection.ts +1 -0
  183. package/src/generated/types/rebalanceInstructionData.ts +62 -0
  184. package/src/generated/types/rebalanceStateValues.ts +59 -0
  185. package/src/generated/types/rebalanceStep.ts +25 -0
  186. package/src/generated/types/solautoRebalanceType.ts +0 -1
  187. package/src/generated/types/solautoSettingsParameters.ts +4 -25
  188. package/src/generated/types/solautoSettingsParametersInp.ts +2 -24
  189. package/src/generated/types/swapType.ts +22 -0
  190. package/src/generated/types/tokenBalanceChange.ts +46 -0
  191. package/src/generated/types/tokenBalanceChangeType.ts +28 -0
  192. package/src/generated/types/updatePositionData.ts +3 -3
  193. package/src/index.ts +8 -8
  194. package/src/marginfi-sdk/types/interestRateConfig.ts +6 -2
  195. package/src/services/flashLoans/flProviderAggregator.ts +74 -0
  196. package/src/services/flashLoans/flProviderBase.ts +88 -0
  197. package/src/services/flashLoans/index.ts +3 -0
  198. package/src/services/flashLoans/marginfiFlProvider.ts +392 -0
  199. package/src/services/index.ts +5 -0
  200. package/src/services/rebalance/index.ts +2 -0
  201. package/src/services/rebalance/rebalanceSwapManager.ts +218 -0
  202. package/src/services/rebalance/rebalanceTxBuilder.ts +330 -0
  203. package/src/services/rebalance/rebalanceValues.ts +232 -0
  204. package/src/services/rebalance/solautoFees.ts +61 -0
  205. package/src/services/solauto/index.ts +4 -0
  206. package/src/{clients → services/solauto}/referralStateManager.ts +17 -31
  207. package/src/{clients → services/solauto}/solautoClient.ts +142 -205
  208. package/src/services/solauto/solautoMarginfiClient.ts +472 -0
  209. package/src/services/solauto/txHandler.ts +69 -0
  210. package/src/services/swap/index.ts +1 -0
  211. package/src/services/swap/jupSwapManager.ts +189 -0
  212. package/src/{transactions → services/transactions}/transactionUtils.ts +40 -280
  213. package/src/{transactions → services/transactions}/transactionsManager.ts +15 -10
  214. package/src/solautoPosition/index.ts +3 -0
  215. package/src/solautoPosition/marginfiSolautoPositionEx.ts +111 -0
  216. package/src/solautoPosition/solautoPositionEx.ts +281 -0
  217. package/src/solautoPosition/utils.ts +188 -0
  218. package/src/types/solauto.ts +30 -2
  219. package/src/utils/generalUtils.ts +9 -1
  220. package/src/utils/index.ts +2 -2
  221. package/src/utils/jitoUtils.ts +0 -4
  222. package/src/utils/jupiterUtils.ts +3 -164
  223. package/src/utils/marginfiUtils.ts +77 -43
  224. package/src/utils/numberUtils.ts +18 -77
  225. package/src/utils/solanaUtils.ts +1 -1
  226. package/src/utils/{solauto/generalUtils.ts → solautoUtils.ts} +66 -288
  227. package/tests/transactions/shared.ts +135 -0
  228. package/tests/transactions/solautoMarginfi.ts +8 -198
  229. package/tests/unit/accounts.ts +3 -10
  230. package/tests/unit/lookupTables.ts +23 -2
  231. package/tests/unit/rebalanceCalculations.ts +65 -343
  232. package/dist/clients/index.d.ts +0 -5
  233. package/dist/clients/index.d.ts.map +0 -1
  234. package/dist/clients/referralStateManager.d.ts.map +0 -1
  235. package/dist/clients/solautoClient.d.ts +0 -71
  236. package/dist/clients/solautoClient.d.ts.map +0 -1
  237. package/dist/clients/solautoMarginfiClient.d.ts +0 -51
  238. package/dist/clients/solautoMarginfiClient.d.ts.map +0 -1
  239. package/dist/clients/solautoMarginfiClient.js +0 -497
  240. package/dist/clients/txHandler.d.ts.map +0 -1
  241. package/dist/clients/txHandler.js +0 -23
  242. package/dist/generated/types/positionTokenUsage.d.ts.map +0 -1
  243. package/dist/transactions/index.d.ts.map +0 -1
  244. package/dist/transactions/transactionUtils.d.ts.map +0 -1
  245. package/dist/transactions/transactionsManager.d.ts.map +0 -1
  246. package/dist/utils/solauto/generalUtils.d.ts.map +0 -1
  247. package/dist/utils/solauto/index.d.ts +0 -3
  248. package/dist/utils/solauto/index.d.ts.map +0 -1
  249. package/dist/utils/solauto/rebalanceUtils.d.ts +0 -30
  250. package/dist/utils/solauto/rebalanceUtils.d.ts.map +0 -1
  251. package/dist/utils/solauto/rebalanceUtils.js +0 -287
  252. package/src/clients/index.ts +0 -4
  253. package/src/clients/solautoMarginfiClient.ts +0 -774
  254. package/src/clients/txHandler.ts +0 -38
  255. package/src/utils/solauto/index.ts +0 -2
  256. package/src/utils/solauto/rebalanceUtils.ts +0 -562
  257. package/dist/{clients → services/solauto}/index.js +2 -2
  258. /package/dist/{transactions → services/transactions}/index.d.ts +0 -0
  259. /package/dist/{transactions → services/transactions}/index.js +0 -0
  260. /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 "../../generated";
25
- import { consoleLog, currentUnixSeconds } from "../generalUtils";
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
- } from "../numberUtils";
35
- import { getReferralState } from "../accountUtils";
36
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
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
- supplyPrice > 0 ? (supplyUsd - debtUsd) / supplyPrice : 0,
332
+ supplyUsd > 0 ? (supplyUsd - debtUsd) / supplyPrice : 0,
471
333
  state.supply.decimals
472
334
  ),
473
- baseAmountUsdValue: toBaseUnit(supplyUsd - debtUsd, USD_DECIMALS),
335
+ baseAmountUsdValue: toRoundedUsdValue(supplyUsd - debtUsd),
474
336
  },
475
337
  supply: {
476
338
  ...state.supply,
477
339
  amountCanBeUsed: {
478
340
  ...state.supply.amountCanBeUsed,
479
- baseAmountUsdValue: toBaseUnit(
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: toBaseUnit(supplyUsd, USD_DECIMALS),
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: toBaseUnit(
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: toBaseUnit(debtUsd, USD_DECIMALS),
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 activeDca: DCASettings | undefined = undefined;
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.activeDca = {
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.activeDca = undefined;
425
+ this.dca = undefined;
682
426
  this.dcaInBalance = undefined;
683
427
  this.cancellingDca = undefined;
684
428
  }
@@ -693,3 +437,37 @@ 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
+ // TODO: PF
449
+ }
450
+ }
451
+
452
+ export function isMarginfiClient(
453
+ client: SolautoClient
454
+ ): client is SolautoMarginfiClient {
455
+ return client.lendingPlatform == LendingPlatform.Marginfi;
456
+ }
457
+ // TODO: PF
458
+
459
+ export function hasFirstRebalance(rebalanceType: SolautoRebalanceType) {
460
+ return [
461
+ SolautoRebalanceType.Regular,
462
+ SolautoRebalanceType.DoubleRebalanceWithFL,
463
+ SolautoRebalanceType.FLRebalanceThenSwap,
464
+ ].includes(rebalanceType);
465
+ }
466
+
467
+ export function hasLastRebalance(rebalanceType: SolautoRebalanceType) {
468
+ return [
469
+ SolautoRebalanceType.Regular,
470
+ SolautoRebalanceType.DoubleRebalanceWithFL,
471
+ SolautoRebalanceType.FLSwapThenRebalance,
472
+ ].includes(rebalanceType);
473
+ }
@@ -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: 100,
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
+ }