@mento-protocol/mento-sdk 3.2.7 → 3.3.0-beta.0

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 (302) hide show
  1. package/dist/cache/routes.d.ts +13 -0
  2. package/dist/cache/routes.js +14588 -0
  3. package/dist/cache/tokens.d.ts +68 -0
  4. package/dist/cache/tokens.js +514 -0
  5. package/dist/core/abis/activePool.d.ts +2 -0
  6. package/dist/core/abis/activePool.js +14 -0
  7. package/dist/core/abis/addressesRegistry.d.ts +2 -0
  8. package/dist/core/abis/addressesRegistry.js +26 -0
  9. package/dist/core/abis/bipoolmanager.d.ts +34 -0
  10. package/dist/core/abis/bipoolmanager.js +72 -0
  11. package/dist/core/abis/borrowerOperations.d.ts +9 -0
  12. package/dist/core/abis/borrowerOperations.js +89 -0
  13. package/dist/core/abis/breakerbox.d.ts +13 -0
  14. package/dist/core/abis/breakerbox.js +8 -0
  15. package/dist/core/abis/broker.d.ts +2 -0
  16. package/dist/core/abis/broker.js +9 -0
  17. package/dist/core/abis/erc20.d.ts +9 -0
  18. package/dist/core/abis/erc20.js +21 -0
  19. package/dist/core/abis/fpmm.d.ts +270 -0
  20. package/dist/core/abis/fpmm.js +49 -0
  21. package/dist/core/abis/fpmmFactory.d.ts +85 -0
  22. package/dist/core/abis/fpmmFactory.js +26 -0
  23. package/dist/core/abis/hintHelpers.d.ts +2 -0
  24. package/dist/core/abis/hintHelpers.js +14 -0
  25. package/dist/core/abis/index.d.ts +22 -0
  26. package/dist/core/abis/index.js +38 -0
  27. package/dist/core/abis/liquidityStrategy.d.ts +132 -0
  28. package/dist/core/abis/liquidityStrategy.js +10 -0
  29. package/dist/core/abis/multiTroveGetter.d.ts +8 -0
  30. package/dist/core/abis/multiTroveGetter.js +15 -0
  31. package/dist/core/abis/priceFeed.d.ts +7 -0
  32. package/dist/core/abis/priceFeed.js +16 -0
  33. package/dist/core/abis/pricingmodule.d.ts +2 -0
  34. package/dist/core/abis/pricingmodule.js +6 -0
  35. package/dist/core/abis/reserve.d.ts +3 -0
  36. package/dist/core/abis/reserve.js +18 -0
  37. package/dist/core/abis/router.d.ts +521 -0
  38. package/dist/core/abis/router.js +45 -0
  39. package/dist/core/abis/sortedTroves.d.ts +2 -0
  40. package/dist/core/abis/sortedTroves.js +15 -0
  41. package/dist/core/abis/systemParams.d.ts +2 -0
  42. package/dist/core/abis/systemParams.js +14 -0
  43. package/dist/core/abis/troveManager.d.ts +2 -0
  44. package/dist/core/abis/troveManager.js +27 -0
  45. package/dist/core/abis/troveNFT.d.ts +2 -0
  46. package/dist/core/abis/troveNFT.js +9 -0
  47. package/dist/core/abis/virtualPool.d.ts +50 -0
  48. package/dist/core/abis/virtualPool.js +11 -0
  49. package/dist/core/abis/virtualPoolFactory.d.ts +59 -0
  50. package/dist/core/abis/virtualPoolFactory.js +17 -0
  51. package/dist/core/constants/addresses.d.ts +18 -0
  52. package/dist/core/constants/addresses.js +125 -0
  53. package/dist/core/constants/borrowConstants.d.ts +10 -0
  54. package/dist/core/constants/borrowConstants.js +16 -0
  55. package/dist/core/constants/borrowRegistries.d.ts +7 -0
  56. package/dist/core/constants/borrowRegistries.js +34 -0
  57. package/dist/core/constants/chainId.d.ts +8 -0
  58. package/dist/core/constants/chainId.js +12 -0
  59. package/dist/core/constants/contractNames.d.ts +21 -0
  60. package/dist/core/constants/contractNames.js +24 -0
  61. package/dist/core/constants/index.d.ts +6 -0
  62. package/dist/core/constants/index.js +22 -0
  63. package/dist/core/errors/base.d.ts +8 -0
  64. package/dist/core/errors/base.js +17 -0
  65. package/dist/core/errors/index.d.ts +4 -0
  66. package/dist/core/errors/index.js +20 -0
  67. package/dist/core/errors/oracle.d.ts +9 -0
  68. package/dist/core/errors/oracle.js +15 -0
  69. package/dist/core/errors/router.d.ts +14 -0
  70. package/dist/core/errors/router.js +24 -0
  71. package/dist/core/types/borrow.d.ts +87 -0
  72. package/dist/core/types/borrow.js +3 -0
  73. package/dist/core/types/contractAddresses.d.ts +42 -0
  74. package/dist/core/types/contractAddresses.js +3 -0
  75. package/dist/core/types/index.d.ts +10 -0
  76. package/dist/core/types/index.js +26 -0
  77. package/dist/core/types/liquidity.d.ts +194 -0
  78. package/dist/core/types/liquidity.js +3 -0
  79. package/dist/core/types/pool.d.ts +208 -0
  80. package/dist/core/types/pool.js +14 -0
  81. package/dist/core/types/provider.d.ts +45 -0
  82. package/dist/core/types/provider.js +3 -0
  83. package/dist/core/types/route.d.ts +62 -0
  84. package/dist/core/types/route.js +3 -0
  85. package/dist/core/types/token.d.ts +21 -0
  86. package/dist/core/types/token.js +3 -0
  87. package/dist/core/types/tradingLimits.d.ts +91 -0
  88. package/dist/core/types/tradingLimits.js +3 -0
  89. package/dist/core/types/tradingMode.d.ts +24 -0
  90. package/dist/core/types/tradingMode.js +31 -0
  91. package/dist/core/types/transaction.d.ts +45 -0
  92. package/dist/core/types/transaction.js +3 -0
  93. package/dist/esm/cache/routes.js +14583 -0
  94. package/dist/esm/cache/tokens.js +506 -0
  95. package/dist/esm/core/abis/activePool.js +10 -0
  96. package/dist/esm/core/abis/addressesRegistry.js +22 -0
  97. package/dist/esm/core/abis/bipoolmanager.js +68 -0
  98. package/dist/esm/core/abis/borrowerOperations.js +85 -0
  99. package/dist/esm/core/abis/breakerbox.js +4 -0
  100. package/dist/esm/core/abis/broker.js +5 -0
  101. package/dist/esm/core/abis/erc20.js +17 -0
  102. package/dist/esm/core/abis/fpmm.js +45 -0
  103. package/dist/esm/core/abis/fpmmFactory.js +22 -0
  104. package/dist/esm/core/abis/hintHelpers.js +10 -0
  105. package/dist/esm/core/abis/index.js +21 -0
  106. package/dist/esm/core/abis/liquidityStrategy.js +6 -0
  107. package/dist/esm/core/abis/multiTroveGetter.js +11 -0
  108. package/dist/esm/core/abis/priceFeed.js +12 -0
  109. package/dist/esm/core/abis/pricingmodule.js +2 -0
  110. package/dist/esm/core/abis/reserve.js +14 -0
  111. package/dist/esm/core/abis/router.js +41 -0
  112. package/dist/esm/core/abis/sortedTroves.js +11 -0
  113. package/dist/esm/core/abis/systemParams.js +10 -0
  114. package/dist/esm/core/abis/troveManager.js +23 -0
  115. package/dist/esm/core/abis/troveNFT.js +5 -0
  116. package/dist/esm/core/abis/virtualPool.js +7 -0
  117. package/dist/esm/core/abis/virtualPoolFactory.js +13 -0
  118. package/dist/esm/core/constants/addresses.js +119 -0
  119. package/dist/esm/core/constants/borrowConstants.js +12 -0
  120. package/dist/esm/core/constants/borrowRegistries.js +29 -0
  121. package/dist/esm/core/constants/chainId.js +8 -0
  122. package/dist/esm/core/constants/contractNames.js +20 -0
  123. package/dist/esm/core/constants/index.js +5 -0
  124. package/dist/esm/core/errors/base.js +12 -0
  125. package/dist/esm/core/errors/index.js +3 -0
  126. package/dist/esm/core/errors/oracle.js +10 -0
  127. package/dist/esm/core/errors/router.js +18 -0
  128. package/dist/esm/core/types/borrow.js +1 -0
  129. package/dist/esm/core/types/contractAddresses.js +1 -0
  130. package/dist/esm/core/types/index.js +9 -0
  131. package/dist/esm/core/types/liquidity.js +1 -0
  132. package/dist/esm/core/types/pool.js +10 -0
  133. package/dist/esm/core/types/provider.js +1 -0
  134. package/dist/esm/core/types/route.js +1 -0
  135. package/dist/esm/core/types/token.js +1 -0
  136. package/dist/esm/core/types/tradingLimits.js +1 -0
  137. package/dist/esm/core/types/tradingMode.js +26 -0
  138. package/dist/esm/core/types/transaction.js +1 -0
  139. package/dist/esm/index.js +139 -0
  140. package/dist/esm/package.json +1 -0
  141. package/dist/esm/services/borrow/BorrowService.js +455 -0
  142. package/dist/esm/services/borrow/borrowHelpers.js +3 -0
  143. package/dist/esm/services/borrow/borrowMath.js +127 -0
  144. package/dist/esm/services/borrow/index.js +3 -0
  145. package/dist/esm/services/borrow/internal/borrowApprovalService.js +48 -0
  146. package/dist/esm/services/borrow/internal/borrowContextStore.js +35 -0
  147. package/dist/esm/services/borrow/internal/borrowErc20.js +38 -0
  148. package/dist/esm/services/borrow/internal/borrowHints.js +27 -0
  149. package/dist/esm/services/borrow/internal/borrowPositionParser.js +82 -0
  150. package/dist/esm/services/borrow/internal/borrowReadService.js +271 -0
  151. package/dist/esm/services/borrow/internal/borrowRegistryReader.js +108 -0
  152. package/dist/esm/services/borrow/internal/borrowTransactionService.js +271 -0
  153. package/dist/esm/services/borrow/internal/borrowTypes.js +1 -0
  154. package/dist/esm/services/borrow/internal/borrowValidation.js +89 -0
  155. package/dist/esm/services/index.js +8 -0
  156. package/dist/esm/services/liquidity/LiquidityService.js +163 -0
  157. package/dist/esm/services/liquidity/basicLiquidity.js +162 -0
  158. package/dist/esm/services/liquidity/index.js +1 -0
  159. package/dist/esm/services/liquidity/liquidityHelpers.js +95 -0
  160. package/dist/esm/services/liquidity/rebalance.js +59 -0
  161. package/dist/esm/services/liquidity/zapHelpers.js +181 -0
  162. package/dist/esm/services/liquidity/zapIn.js +131 -0
  163. package/dist/esm/services/liquidity/zapOut.js +248 -0
  164. package/dist/esm/services/pools/PoolService.js +204 -0
  165. package/dist/esm/services/pools/index.js +1 -0
  166. package/dist/esm/services/pools/poolDetails.js +209 -0
  167. package/dist/esm/services/pools/poolDiscovery.js +112 -0
  168. package/dist/esm/services/pools/rebalancePreview.js +181 -0
  169. package/dist/esm/services/quotes/QuoteService.js +85 -0
  170. package/dist/esm/services/quotes/index.js +1 -0
  171. package/dist/esm/services/routes/RouteService.js +268 -0
  172. package/dist/esm/services/routes/index.js +1 -0
  173. package/dist/esm/services/swap/SwapService.js +247 -0
  174. package/dist/esm/services/swap/index.js +1 -0
  175. package/dist/esm/services/tokens/index.js +1 -0
  176. package/dist/esm/services/tokens/tokenService.js +285 -0
  177. package/dist/esm/services/trading/TradingLimitsService.js +154 -0
  178. package/dist/esm/services/trading/TradingService.js +222 -0
  179. package/dist/esm/services/trading/index.js +2 -0
  180. package/dist/esm/utils/chainConfig.js +122 -0
  181. package/dist/esm/utils/costUtils.js +56 -0
  182. package/dist/esm/utils/deadline.js +22 -0
  183. package/dist/esm/utils/index.js +9 -0
  184. package/dist/esm/utils/multicall.js +47 -0
  185. package/dist/esm/utils/pathEncoder.js +69 -0
  186. package/dist/esm/utils/rateFeed.js +23 -0
  187. package/dist/esm/utils/retry.js +24 -0
  188. package/dist/esm/utils/routeUtils.js +361 -0
  189. package/dist/esm/utils/routes.js +2 -0
  190. package/dist/esm/utils/sortUtils.js +33 -0
  191. package/dist/esm/utils/tokens.js +2 -0
  192. package/dist/esm/utils/tradingLimits.js +163 -0
  193. package/dist/esm/utils/validation.js +30 -0
  194. package/dist/index.d.ts +101 -0
  195. package/dist/index.js +158 -0
  196. package/dist/services/borrow/BorrowService.d.ts +381 -0
  197. package/dist/services/borrow/BorrowService.js +460 -0
  198. package/dist/services/borrow/borrowHelpers.d.ts +4 -0
  199. package/dist/services/borrow/borrowHelpers.js +13 -0
  200. package/dist/services/borrow/borrowMath.d.ts +21 -0
  201. package/dist/services/borrow/borrowMath.js +137 -0
  202. package/dist/services/borrow/index.d.ts +4 -0
  203. package/dist/services/borrow/index.js +20 -0
  204. package/dist/services/borrow/internal/borrowApprovalService.d.ts +14 -0
  205. package/dist/services/borrow/internal/borrowApprovalService.js +53 -0
  206. package/dist/services/borrow/internal/borrowContextStore.d.ts +11 -0
  207. package/dist/services/borrow/internal/borrowContextStore.js +40 -0
  208. package/dist/services/borrow/internal/borrowErc20.d.ts +5 -0
  209. package/dist/services/borrow/internal/borrowErc20.js +43 -0
  210. package/dist/services/borrow/internal/borrowHints.d.ts +7 -0
  211. package/dist/services/borrow/internal/borrowHints.js +31 -0
  212. package/dist/services/borrow/internal/borrowPositionParser.d.ts +4 -0
  213. package/dist/services/borrow/internal/borrowPositionParser.js +87 -0
  214. package/dist/services/borrow/internal/borrowReadService.d.ts +31 -0
  215. package/dist/services/borrow/internal/borrowReadService.js +276 -0
  216. package/dist/services/borrow/internal/borrowRegistryReader.d.ts +5 -0
  217. package/dist/services/borrow/internal/borrowRegistryReader.js +113 -0
  218. package/dist/services/borrow/internal/borrowTransactionService.d.ts +23 -0
  219. package/dist/services/borrow/internal/borrowTransactionService.js +276 -0
  220. package/dist/services/borrow/internal/borrowTypes.d.ts +15 -0
  221. package/dist/services/borrow/internal/borrowTypes.js +3 -0
  222. package/dist/services/borrow/internal/borrowValidation.d.ts +14 -0
  223. package/dist/services/borrow/internal/borrowValidation.js +104 -0
  224. package/dist/services/index.d.ts +9 -0
  225. package/dist/services/index.js +25 -0
  226. package/dist/services/liquidity/LiquidityService.d.ts +139 -0
  227. package/dist/services/liquidity/LiquidityService.js +168 -0
  228. package/dist/services/liquidity/basicLiquidity.d.ts +11 -0
  229. package/dist/services/liquidity/basicLiquidity.js +172 -0
  230. package/dist/services/liquidity/index.d.ts +2 -0
  231. package/dist/services/liquidity/index.js +18 -0
  232. package/dist/services/liquidity/liquidityHelpers.d.ts +19 -0
  233. package/dist/services/liquidity/liquidityHelpers.js +104 -0
  234. package/dist/services/liquidity/rebalance.d.ts +6 -0
  235. package/dist/services/liquidity/rebalance.js +64 -0
  236. package/dist/services/liquidity/zapHelpers.d.ts +100 -0
  237. package/dist/services/liquidity/zapHelpers.js +192 -0
  238. package/dist/services/liquidity/zapIn.d.ts +18 -0
  239. package/dist/services/liquidity/zapIn.js +138 -0
  240. package/dist/services/liquidity/zapOut.d.ts +9 -0
  241. package/dist/services/liquidity/zapOut.js +255 -0
  242. package/dist/services/pools/PoolService.d.ts +69 -0
  243. package/dist/services/pools/PoolService.js +209 -0
  244. package/dist/services/pools/index.d.ts +2 -0
  245. package/dist/services/pools/index.js +18 -0
  246. package/dist/services/pools/poolDetails.d.ts +13 -0
  247. package/dist/services/pools/poolDetails.js +216 -0
  248. package/dist/services/pools/poolDiscovery.d.ts +12 -0
  249. package/dist/services/pools/poolDiscovery.js +117 -0
  250. package/dist/services/pools/rebalancePreview.d.ts +5 -0
  251. package/dist/services/pools/rebalancePreview.js +186 -0
  252. package/dist/services/quotes/QuoteService.d.ts +51 -0
  253. package/dist/services/quotes/QuoteService.js +91 -0
  254. package/dist/services/quotes/index.d.ts +2 -0
  255. package/dist/services/quotes/index.js +18 -0
  256. package/dist/services/routes/RouteService.d.ts +117 -0
  257. package/dist/services/routes/RouteService.js +306 -0
  258. package/dist/services/routes/index.d.ts +2 -0
  259. package/dist/services/routes/index.js +18 -0
  260. package/dist/services/swap/SwapService.d.ts +198 -0
  261. package/dist/services/swap/SwapService.js +252 -0
  262. package/dist/services/swap/index.d.ts +2 -0
  263. package/dist/services/swap/index.js +18 -0
  264. package/dist/services/tokens/index.d.ts +2 -0
  265. package/dist/services/tokens/index.js +18 -0
  266. package/dist/services/tokens/tokenService.d.ts +55 -0
  267. package/dist/services/tokens/tokenService.js +290 -0
  268. package/dist/services/trading/TradingLimitsService.d.ts +38 -0
  269. package/dist/services/trading/TradingLimitsService.js +159 -0
  270. package/dist/services/trading/TradingService.d.ts +115 -0
  271. package/dist/services/trading/TradingService.js +227 -0
  272. package/dist/services/trading/index.d.ts +3 -0
  273. package/dist/services/trading/index.js +19 -0
  274. package/dist/utils/chainConfig.d.ts +16 -0
  275. package/dist/utils/chainConfig.js +127 -0
  276. package/dist/utils/costUtils.d.ts +12 -0
  277. package/dist/utils/costUtils.js +60 -0
  278. package/dist/utils/deadline.d.ts +21 -0
  279. package/dist/utils/deadline.js +26 -0
  280. package/dist/utils/index.d.ts +10 -0
  281. package/dist/utils/index.js +26 -0
  282. package/dist/utils/multicall.d.ts +30 -0
  283. package/dist/utils/multicall.js +52 -0
  284. package/dist/utils/pathEncoder.d.ts +34 -0
  285. package/dist/utils/pathEncoder.js +73 -0
  286. package/dist/utils/rateFeed.d.ts +18 -0
  287. package/dist/utils/rateFeed.js +27 -0
  288. package/dist/utils/retry.d.ts +12 -0
  289. package/dist/utils/retry.js +28 -0
  290. package/dist/utils/routeUtils.d.ts +295 -0
  291. package/dist/utils/routeUtils.js +371 -0
  292. package/dist/utils/routes.d.ts +3 -0
  293. package/dist/utils/routes.js +8 -0
  294. package/dist/utils/sortUtils.d.ts +24 -0
  295. package/dist/utils/sortUtils.js +39 -0
  296. package/dist/utils/tokens.d.ts +2 -0
  297. package/dist/utils/tokens.js +13 -0
  298. package/dist/utils/tradingLimits.d.ts +41 -0
  299. package/dist/utils/tradingLimits.js +171 -0
  300. package/dist/utils/validation.d.ts +19 -0
  301. package/dist/utils/validation.js +34 -0
  302. package/package.json +1 -1
@@ -0,0 +1,271 @@
1
+ import { encodeFunctionData } from 'viem';
2
+ import { BORROWER_OPERATIONS_ABI, TROVE_MANAGER_ABI } from '../../../core/abis';
3
+ import { getTroveOperationHints } from './borrowHints';
4
+ import { optionalAddressOrZero, parseTroveId, requireAddress, requireNonNegativeBigInt, requireNonNegativeInteger, requirePositiveBigInt, requireUint128, } from './borrowValidation';
5
+ const ZERO_VALUE_HEX = '0x0';
6
+ export class BorrowTransactionService {
7
+ constructor(publicClient) {
8
+ this.publicClient = publicClient;
9
+ }
10
+ async buildOpenTroveTransaction(ctx, params) {
11
+ const owner = requireAddress(params.owner, 'owner');
12
+ const ownerIndex = requireNonNegativeInteger(params.ownerIndex, 'ownerIndex');
13
+ const collAmount = requirePositiveBigInt(params.collAmount, 'collAmount');
14
+ const boldAmount = requirePositiveBigInt(params.boldAmount, 'boldAmount');
15
+ const annualInterestRate = requireNonNegativeBigInt(params.annualInterestRate, 'annualInterestRate');
16
+ const maxUpfrontFee = requireNonNegativeBigInt(params.maxUpfrontFee, 'maxUpfrontFee');
17
+ const addManager = optionalAddressOrZero(params.addManager, 'addManager');
18
+ const removeManager = optionalAddressOrZero(params.removeManager, 'removeManager');
19
+ const receiver = optionalAddressOrZero(params.receiver, 'receiver');
20
+ const hints = await getTroveOperationHints(this.publicClient, ctx, annualInterestRate);
21
+ const data = params.interestBatchManager
22
+ ? encodeFunctionData({
23
+ abi: BORROWER_OPERATIONS_ABI,
24
+ functionName: 'openTroveAndJoinInterestBatchManager',
25
+ args: [
26
+ {
27
+ owner,
28
+ ownerIndex: BigInt(ownerIndex),
29
+ collAmount,
30
+ boldAmount,
31
+ upperHint: hints.upper,
32
+ lowerHint: hints.lower,
33
+ interestBatchManager: requireAddress(params.interestBatchManager, 'interestBatchManager'),
34
+ maxUpfrontFee,
35
+ addManager,
36
+ removeManager,
37
+ receiver,
38
+ },
39
+ ],
40
+ })
41
+ : encodeFunctionData({
42
+ abi: BORROWER_OPERATIONS_ABI,
43
+ functionName: 'openTrove',
44
+ args: [
45
+ owner,
46
+ BigInt(ownerIndex),
47
+ collAmount,
48
+ boldAmount,
49
+ hints.upper,
50
+ hints.lower,
51
+ annualInterestRate,
52
+ maxUpfrontFee,
53
+ addManager,
54
+ removeManager,
55
+ receiver,
56
+ ],
57
+ });
58
+ return {
59
+ to: ctx.addresses.borrowerOperations,
60
+ data,
61
+ value: ZERO_VALUE_HEX,
62
+ };
63
+ }
64
+ buildAdjustTroveTransaction(ctx, params) {
65
+ const troveId = parseTroveId(params.troveId);
66
+ const collChange = requireNonNegativeBigInt(params.collChange, 'collChange');
67
+ const debtChange = requireNonNegativeBigInt(params.debtChange, 'debtChange');
68
+ const maxUpfrontFee = requireNonNegativeBigInt(params.maxUpfrontFee, 'maxUpfrontFee');
69
+ const data = encodeFunctionData({
70
+ abi: BORROWER_OPERATIONS_ABI,
71
+ functionName: 'adjustTrove',
72
+ args: [troveId, collChange, params.isCollIncrease, debtChange, params.isDebtIncrease, maxUpfrontFee],
73
+ });
74
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
75
+ }
76
+ async buildAdjustZombieTroveTransaction(ctx, params) {
77
+ const troveId = parseTroveId(params.troveId);
78
+ const collChange = requireNonNegativeBigInt(params.collChange, 'collChange');
79
+ const debtChange = requireNonNegativeBigInt(params.debtChange, 'debtChange');
80
+ const maxUpfrontFee = requireNonNegativeBigInt(params.maxUpfrontFee, 'maxUpfrontFee');
81
+ const currentRate = (await this.publicClient.readContract({
82
+ address: ctx.addresses.troveManager,
83
+ abi: TROVE_MANAGER_ABI,
84
+ functionName: 'getTroveAnnualInterestRate',
85
+ args: [troveId],
86
+ }));
87
+ const hints = await getTroveOperationHints(this.publicClient, ctx, currentRate);
88
+ const data = encodeFunctionData({
89
+ abi: BORROWER_OPERATIONS_ABI,
90
+ functionName: 'adjustZombieTrove',
91
+ args: [
92
+ troveId,
93
+ collChange,
94
+ params.isCollIncrease,
95
+ debtChange,
96
+ params.isDebtIncrease,
97
+ hints.upper,
98
+ hints.lower,
99
+ maxUpfrontFee,
100
+ ],
101
+ });
102
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
103
+ }
104
+ buildCloseTroveTransaction(ctx, troveId) {
105
+ const parsedTroveId = parseTroveId(troveId);
106
+ const data = encodeFunctionData({
107
+ abi: BORROWER_OPERATIONS_ABI,
108
+ functionName: 'closeTrove',
109
+ args: [parsedTroveId],
110
+ });
111
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
112
+ }
113
+ buildAddCollTransaction(ctx, troveId, amount) {
114
+ const parsedTroveId = parseTroveId(troveId);
115
+ const collAmount = requirePositiveBigInt(amount, 'amount');
116
+ const data = encodeFunctionData({
117
+ abi: BORROWER_OPERATIONS_ABI,
118
+ functionName: 'addColl',
119
+ args: [parsedTroveId, collAmount],
120
+ });
121
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
122
+ }
123
+ buildWithdrawCollTransaction(ctx, troveId, amount) {
124
+ const parsedTroveId = parseTroveId(troveId);
125
+ const collAmount = requirePositiveBigInt(amount, 'amount');
126
+ const data = encodeFunctionData({
127
+ abi: BORROWER_OPERATIONS_ABI,
128
+ functionName: 'withdrawColl',
129
+ args: [parsedTroveId, collAmount],
130
+ });
131
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
132
+ }
133
+ buildBorrowMoreTransaction(ctx, troveId, amount, maxFee) {
134
+ const parsedTroveId = parseTroveId(troveId);
135
+ const borrowAmount = requirePositiveBigInt(amount, 'amount');
136
+ const maxUpfrontFee = requireNonNegativeBigInt(maxFee, 'maxFee');
137
+ const data = encodeFunctionData({
138
+ abi: BORROWER_OPERATIONS_ABI,
139
+ functionName: 'withdrawBold',
140
+ args: [parsedTroveId, borrowAmount, maxUpfrontFee],
141
+ });
142
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
143
+ }
144
+ buildRepayDebtTransaction(ctx, troveId, amount) {
145
+ const parsedTroveId = parseTroveId(troveId);
146
+ const repayAmount = requirePositiveBigInt(amount, 'amount');
147
+ const data = encodeFunctionData({
148
+ abi: BORROWER_OPERATIONS_ABI,
149
+ functionName: 'repayBold',
150
+ args: [parsedTroveId, repayAmount],
151
+ });
152
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
153
+ }
154
+ async buildAdjustInterestRateTransaction(ctx, troveId, newRate, maxFee) {
155
+ const parsedTroveId = parseTroveId(troveId);
156
+ const newAnnualInterestRate = requireNonNegativeBigInt(newRate, 'newRate');
157
+ const maxUpfrontFee = requireNonNegativeBigInt(maxFee, 'maxFee');
158
+ const hints = await getTroveOperationHints(this.publicClient, ctx, newAnnualInterestRate);
159
+ const data = encodeFunctionData({
160
+ abi: BORROWER_OPERATIONS_ABI,
161
+ functionName: 'adjustTroveInterestRate',
162
+ args: [parsedTroveId, newAnnualInterestRate, hints.upper, hints.lower, maxUpfrontFee],
163
+ });
164
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
165
+ }
166
+ buildClaimCollateralTransaction(ctx) {
167
+ const data = encodeFunctionData({
168
+ abi: BORROWER_OPERATIONS_ABI,
169
+ functionName: 'claimCollateral',
170
+ args: [],
171
+ });
172
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
173
+ }
174
+ async buildSetBatchManagerTransaction(ctx, troveId, manager, maxFee) {
175
+ const parsedTroveId = parseTroveId(troveId);
176
+ const newBatchManager = requireAddress(manager, 'manager');
177
+ const maxUpfrontFee = requireNonNegativeBigInt(maxFee, 'maxFee');
178
+ const currentRate = (await this.publicClient.readContract({
179
+ address: ctx.addresses.troveManager,
180
+ abi: TROVE_MANAGER_ABI,
181
+ functionName: 'getTroveAnnualInterestRate',
182
+ args: [parsedTroveId],
183
+ }));
184
+ const hints = await getTroveOperationHints(this.publicClient, ctx, currentRate);
185
+ const data = encodeFunctionData({
186
+ abi: BORROWER_OPERATIONS_ABI,
187
+ functionName: 'setInterestBatchManager',
188
+ args: [parsedTroveId, newBatchManager, hints.upper, hints.lower, maxUpfrontFee],
189
+ });
190
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
191
+ }
192
+ async buildRemoveFromBatchTransaction(ctx, troveId, newRate, maxFee) {
193
+ const parsedTroveId = parseTroveId(troveId);
194
+ const newAnnualInterestRate = requireNonNegativeBigInt(newRate, 'newRate');
195
+ const maxUpfrontFee = requireNonNegativeBigInt(maxFee, 'maxFee');
196
+ const hints = await getTroveOperationHints(this.publicClient, ctx, newAnnualInterestRate);
197
+ const data = encodeFunctionData({
198
+ abi: BORROWER_OPERATIONS_ABI,
199
+ functionName: 'removeFromBatch',
200
+ args: [parsedTroveId, newAnnualInterestRate, hints.upper, hints.lower, maxUpfrontFee],
201
+ });
202
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
203
+ }
204
+ async buildSwitchBatchManagerTransaction(ctx, troveId, newManager, maxFee) {
205
+ const parsedTroveId = parseTroveId(troveId);
206
+ const managerAddress = requireAddress(newManager, 'newManager');
207
+ const maxUpfrontFee = requireNonNegativeBigInt(maxFee, 'maxFee');
208
+ const currentRate = (await this.publicClient.readContract({
209
+ address: ctx.addresses.troveManager,
210
+ abi: TROVE_MANAGER_ABI,
211
+ functionName: 'getTroveAnnualInterestRate',
212
+ args: [parsedTroveId],
213
+ }));
214
+ const [removeHints, addHints] = await Promise.all([
215
+ getTroveOperationHints(this.publicClient, ctx, currentRate),
216
+ getTroveOperationHints(this.publicClient, ctx, currentRate),
217
+ ]);
218
+ const data = encodeFunctionData({
219
+ abi: BORROWER_OPERATIONS_ABI,
220
+ functionName: 'switchBatchManager',
221
+ args: [
222
+ parsedTroveId,
223
+ removeHints.upper,
224
+ removeHints.lower,
225
+ managerAddress,
226
+ addHints.upper,
227
+ addHints.lower,
228
+ maxUpfrontFee,
229
+ ],
230
+ });
231
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
232
+ }
233
+ async buildSetInterestDelegateTransaction(ctx, troveId, delegate, minRate, maxRate, newRate, maxFee, minChangePeriod) {
234
+ const parsedTroveId = parseTroveId(troveId);
235
+ const delegateAddress = requireAddress(delegate, 'delegate');
236
+ const minInterestRate = requireUint128(minRate, 'minRate');
237
+ const maxInterestRate = requireUint128(maxRate, 'maxRate');
238
+ const newAnnualInterestRate = requireNonNegativeBigInt(newRate, 'newRate');
239
+ const maxUpfrontFee = requireNonNegativeBigInt(maxFee, 'maxFee');
240
+ const minRateChangePeriod = requireNonNegativeBigInt(minChangePeriod, 'minChangePeriod');
241
+ if (minInterestRate > maxInterestRate) {
242
+ throw new Error('minRate cannot be greater than maxRate');
243
+ }
244
+ const hints = await getTroveOperationHints(this.publicClient, ctx, newAnnualInterestRate);
245
+ const data = encodeFunctionData({
246
+ abi: BORROWER_OPERATIONS_ABI,
247
+ functionName: 'setInterestIndividualDelegate',
248
+ args: [
249
+ parsedTroveId,
250
+ delegateAddress,
251
+ minInterestRate,
252
+ maxInterestRate,
253
+ newAnnualInterestRate,
254
+ hints.upper,
255
+ hints.lower,
256
+ maxUpfrontFee,
257
+ minRateChangePeriod,
258
+ ],
259
+ });
260
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
261
+ }
262
+ buildRemoveInterestDelegateTransaction(ctx, troveId) {
263
+ const parsedTroveId = parseTroveId(troveId);
264
+ const data = encodeFunctionData({
265
+ abi: BORROWER_OPERATIONS_ABI,
266
+ functionName: 'removeInterestIndividualDelegate',
267
+ args: [parsedTroveId],
268
+ });
269
+ return { to: ctx.addresses.borrowerOperations, data, value: ZERO_VALUE_HEX };
270
+ }
271
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,89 @@
1
+ import { encodeAbiParameters, getAddress, keccak256, parseAbiParameters, zeroAddress } from 'viem';
2
+ import { validateAddress } from '../../../utils/validation';
3
+ const UINT128_MAX = (1n << 128n) - 1n;
4
+ export const MAX_SAFE_INTEGER_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
5
+ const TROVE_ID_PARAMETERS = parseAbiParameters('address opener, address owner, uint256 ownerIndex');
6
+ export function requireDebtTokenSymbol(symbol) {
7
+ if (typeof symbol !== 'string' || symbol.trim().length === 0) {
8
+ throw new Error('debtTokenSymbol must be a non-empty string');
9
+ }
10
+ return symbol.trim();
11
+ }
12
+ export function requireAddress(address, fieldName) {
13
+ validateAddress(address, fieldName);
14
+ return address;
15
+ }
16
+ export function optionalAddressOrZero(value, fieldName) {
17
+ if (!value)
18
+ return zeroAddress;
19
+ return requireAddress(value, fieldName);
20
+ }
21
+ export function parseTroveId(troveId) {
22
+ if (typeof troveId !== 'string' || troveId.trim().length === 0) {
23
+ throw new Error('troveId must be a non-empty string');
24
+ }
25
+ let parsed;
26
+ try {
27
+ parsed = BigInt(troveId);
28
+ }
29
+ catch {
30
+ throw new Error(`Invalid troveId: ${troveId}`);
31
+ }
32
+ if (parsed < 0n) {
33
+ throw new Error('troveId cannot be negative');
34
+ }
35
+ return parsed;
36
+ }
37
+ export function formatTroveId(troveId) {
38
+ return `0x${troveId.toString(16)}`;
39
+ }
40
+ export function deriveTroveId(opener, owner, ownerIndex) {
41
+ const openerAddress = getAddress(requireAddress(opener, 'opener'));
42
+ const ownerAddress = getAddress(requireAddress(owner, 'owner'));
43
+ const validatedOwnerIndex = requireNonNegativeInteger(ownerIndex, 'ownerIndex');
44
+ return BigInt(keccak256(encodeAbiParameters(TROVE_ID_PARAMETERS, [openerAddress, ownerAddress, BigInt(validatedOwnerIndex)])));
45
+ }
46
+ export function requireNonNegativeInteger(value, fieldName) {
47
+ if (!Number.isSafeInteger(value) || value < 0) {
48
+ throw new Error(`${fieldName} must be a non-negative safe integer`);
49
+ }
50
+ return value;
51
+ }
52
+ export function requireNonNegativeBigInt(value, fieldName) {
53
+ if (typeof value !== 'bigint' || value < 0n) {
54
+ throw new Error(`${fieldName} must be a non-negative bigint`);
55
+ }
56
+ return value;
57
+ }
58
+ export function requirePositiveBigInt(value, fieldName) {
59
+ if (typeof value !== 'bigint' || value <= 0n) {
60
+ throw new Error(`${fieldName} must be a positive bigint`);
61
+ }
62
+ return value;
63
+ }
64
+ export function requireUint128(value, fieldName) {
65
+ const parsed = requireNonNegativeBigInt(value, fieldName);
66
+ if (parsed > UINT128_MAX) {
67
+ throw new Error(`${fieldName} exceeds uint128 max value`);
68
+ }
69
+ return parsed;
70
+ }
71
+ function integerSqrt(value) {
72
+ if (value < 0n) {
73
+ throw new Error('Cannot compute square root of a negative bigint');
74
+ }
75
+ if (value < 2n) {
76
+ return value;
77
+ }
78
+ let x0 = value;
79
+ let x1 = (x0 + 1n) >> 1n;
80
+ while (x1 < x0) {
81
+ x0 = x1;
82
+ x1 = (x1 + value / x1) >> 1n;
83
+ }
84
+ return x0;
85
+ }
86
+ export function ceilSqrt(value) {
87
+ const floor = integerSqrt(value);
88
+ return floor * floor === value ? floor : floor + 1n;
89
+ }
@@ -0,0 +1,8 @@
1
+ export * from './pools';
2
+ export * from './routes';
3
+ export * from './quotes';
4
+ export * from './swap';
5
+ export * from './trading';
6
+ export * from './borrow';
7
+ export * from './tokens';
8
+ export * from './liquidity';
@@ -0,0 +1,163 @@
1
+ import { buildAddLiquidityTransactionInternal, buildAddLiquidityParamsInternal, buildRemoveLiquidityTransactionInternal, buildRemoveLiquidityParamsInternal, quoteAddLiquidityInternal, quoteRemoveLiquidityInternal, getLPTokenBalanceInternal, } from './basicLiquidity';
2
+ import { buildZapInTransactionInternal, buildZapInParamsInternal, prepareZapInInternal, quoteZapInInternal, } from './zapIn';
3
+ import { buildZapOutTransactionInternal, buildZapOutParamsInternal, prepareZapOutInternal, quoteZapOutInternal, } from './zapOut';
4
+ import { buildRebalanceParamsInternal, buildRebalanceTransactionInternal, } from './rebalance';
5
+ export class LiquidityService {
6
+ constructor(publicClient, chainId, poolService, routeService) {
7
+ this.publicClient = publicClient;
8
+ this.chainId = chainId;
9
+ this.poolService = poolService;
10
+ this.routeService = routeService;
11
+ }
12
+ /**
13
+ * Builds add liquidity transaction with token approvals if needed.
14
+ * Provide two tokens in any order - the Router handles token ordering automatically.
15
+ * @param input - Add liquidity parameters including owner for allowance checks
16
+ * @returns Transaction with approvals (if needed) and add liquidity call
17
+ */
18
+ async buildAddLiquidityTransaction(input) {
19
+ return buildAddLiquidityTransactionInternal(this.publicClient, this.chainId, this.poolService, input.poolAddress, input.tokenA, input.amountA, input.tokenB, input.amountB, input.recipient, input.owner, input.options);
20
+ }
21
+ /**
22
+ * Builds add liquidity transaction parameters without checking token approvals.
23
+ * Use buildAddLiquidityTransaction if you need approval handling.
24
+ * @param input - Add liquidity parameters
25
+ * @returns Transaction details with encoded call data
26
+ */
27
+ async buildAddLiquidityParams(input) {
28
+ return buildAddLiquidityParamsInternal(this.publicClient, this.chainId, this.poolService, input.poolAddress, input.tokenA, input.amountA, input.tokenB, input.amountB, input.recipient, input.options);
29
+ }
30
+ /**
31
+ * Builds remove liquidity transaction with LP token approval if needed.
32
+ * @param input - Remove liquidity parameters including owner for allowance check
33
+ * @returns Transaction with approval (if needed) and remove liquidity call
34
+ */
35
+ async buildRemoveLiquidityTransaction(input) {
36
+ return buildRemoveLiquidityTransactionInternal(this.publicClient, this.chainId, this.poolService, input.poolAddress, input.liquidity, input.recipient, input.owner, input.options);
37
+ }
38
+ /**
39
+ * Builds remove liquidity transaction parameters without checking LP token approval.
40
+ * Use buildRemoveLiquidityTransaction if you need approval handling.
41
+ * @param input - Remove liquidity parameters
42
+ * @returns Transaction details with encoded call data
43
+ */
44
+ async buildRemoveLiquidityParams(input) {
45
+ return buildRemoveLiquidityParamsInternal(this.publicClient, this.chainId, this.poolService, input.poolAddress, input.liquidity, input.recipient, input.options);
46
+ }
47
+ /**
48
+ * Quotes an add liquidity operation (read-only call).
49
+ * Returns expected amounts and LP tokens based on current pool reserves.
50
+ * @param poolAddress - FPMM pool address
51
+ * @param tokenA - First token address
52
+ * @param amountA - Amount of first token
53
+ * @param tokenB - Second token address
54
+ * @param amountB - Amount of second token
55
+ * @returns Expected amounts and LP tokens to be minted
56
+ */
57
+ async quoteAddLiquidity(poolAddress, tokenA, amountA, tokenB, amountB) {
58
+ return quoteAddLiquidityInternal(this.publicClient, this.chainId, this.poolService, poolAddress, tokenA, amountA, tokenB, amountB);
59
+ }
60
+ /**
61
+ * Quotes a remove liquidity operation (read-only call).
62
+ * Returns expected token amounts based on current pool reserves.
63
+ * @param poolAddress - FPMM pool address
64
+ * @param liquidity - Amount of LP tokens to burn
65
+ * @returns Expected amounts of token0 and token1
66
+ */
67
+ async quoteRemoveLiquidity(poolAddress, liquidity) {
68
+ return quoteRemoveLiquidityInternal(this.publicClient, this.chainId, this.poolService, poolAddress, liquidity);
69
+ }
70
+ /**
71
+ * Gets LP token balance and pool share percentage for an address.
72
+ * @param poolAddress - FPMM pool address (also the LP token address)
73
+ * @param owner - Address to check balance for
74
+ * @returns Balance, total supply, and share percentage
75
+ */
76
+ async getLPTokenBalance(poolAddress, owner) {
77
+ return getLPTokenBalanceInternal(this.publicClient, this.poolService, poolAddress, owner);
78
+ }
79
+ /**
80
+ * Builds zap in transaction with approval if needed.
81
+ * Adds liquidity using a single input token - the Router swaps it to both pool tokens automatically.
82
+ * @param input - Zap in parameters including owner for allowance check
83
+ * @returns Transaction with approval (if needed) and zap in call
84
+ */
85
+ async buildZapInTransaction(input) {
86
+ return buildZapInTransactionInternal(this.publicClient, this.chainId, this.poolService, this.routeService, input.poolAddress, input.tokenIn, input.amountIn, input.amountInSplit, input.recipient, input.owner, input.options);
87
+ }
88
+ /**
89
+ * Builds zap in transaction parameters without checking approval.
90
+ * Use buildZapInTransaction if you need approval handling.
91
+ * @param input - Zap in parameters
92
+ * @returns Transaction details with encoded call data and routing information
93
+ */
94
+ async buildZapInParams(input) {
95
+ return buildZapInParamsInternal(this.publicClient, this.chainId, this.poolService, this.routeService, input.poolAddress, input.tokenIn, input.amountIn, input.amountInSplit, input.recipient, input.options);
96
+ }
97
+ async prepareZapIn(input) {
98
+ return prepareZapInInternal(this.publicClient, this.chainId, this.poolService, this.routeService, input.poolAddress, input.tokenIn, input.amountIn, input.amountInSplit, input.recipient, input.owner, input.options);
99
+ }
100
+ /**
101
+ * Builds zap out transaction with approval if needed.
102
+ * Removes liquidity and swaps both tokens to a single output token.
103
+ * @param input - Zap out parameters including owner for allowance check
104
+ * @returns Transaction with approval (if needed) and zap out call
105
+ */
106
+ async buildZapOutTransaction(input) {
107
+ return buildZapOutTransactionInternal(this.publicClient, this.chainId, this.poolService, this.routeService, input.poolAddress, input.tokenOut, input.liquidity, input.recipient, input.owner, input.options);
108
+ }
109
+ /**
110
+ * Builds zap out transaction parameters without checking approval.
111
+ * Use buildZapOutTransaction if you need approval handling.
112
+ * @param input - Zap out parameters
113
+ * @returns Transaction details with encoded call data and routing information
114
+ */
115
+ async buildZapOutParams(input) {
116
+ return buildZapOutParamsInternal(this.publicClient, this.chainId, this.poolService, this.routeService, input.poolAddress, input.tokenOut, input.liquidity, input.recipient, input.options);
117
+ }
118
+ async prepareZapOut(input) {
119
+ return prepareZapOutInternal(this.publicClient, this.chainId, this.poolService, this.routeService, input.poolAddress, input.tokenOut, input.liquidity, input.recipient, input.owner, input.options);
120
+ }
121
+ /**
122
+ * Quotes a zap in operation (read-only call).
123
+ * Estimates expected LP tokens and minimum amounts after swaps and slippage.
124
+ * @param poolAddress - FPMM pool address
125
+ * @param tokenIn - Input token address
126
+ * @param amountIn - Total input amount
127
+ * @param amountInSplit - Split ratio (0-1)
128
+ * @param options - Slippage tolerance and deadline
129
+ * @returns Expected LP tokens and minimum amounts for both pool tokens
130
+ */
131
+ async quoteZapIn(poolAddress, tokenIn, amountIn, amountInSplit, options) {
132
+ return quoteZapInInternal(this.publicClient, this.chainId, this.poolService, this.routeService, poolAddress, tokenIn, amountIn, amountInSplit, options);
133
+ }
134
+ /**
135
+ * Quotes a zap out operation (read-only call).
136
+ * Estimates expected output tokens after removing liquidity and swapping.
137
+ * @param poolAddress - FPMM pool address
138
+ * @param tokenOut - Output token address
139
+ * @param liquidity - Amount of LP tokens to burn
140
+ * @param options - Slippage tolerance and deadline
141
+ * @returns Expected output amount and minimum amounts after slippage
142
+ */
143
+ async quoteZapOut(poolAddress, tokenOut, liquidity, options) {
144
+ return quoteZapOutInternal(this.publicClient, this.chainId, this.poolService, this.routeService, poolAddress, tokenOut, liquidity, options);
145
+ }
146
+ /**
147
+ * Builds rebalance transaction parameters without checking approval.
148
+ * Use buildRebalanceTransaction if you need approval handling.
149
+ * @param input - Rebalance parameters
150
+ * @returns Transaction details with encoded call data
151
+ */
152
+ async buildRebalanceParams(input) {
153
+ return buildRebalanceParamsInternal(this.publicClient, this.chainId, this.poolService, input.poolAddress);
154
+ }
155
+ /**
156
+ * Builds a rebalance transaction with ERC20 approval if needed.
157
+ * @param input - Rebalance parameters including owner for allowance checks
158
+ * @returns Transaction with approval (if needed) and rebalance call
159
+ */
160
+ async buildRebalanceTransaction(input) {
161
+ return buildRebalanceTransactionInternal(this.publicClient, this.chainId, this.poolService, input.poolAddress, input.owner);
162
+ }
163
+ }