@sodax/sdk 0.0.1-rc.7 → 0.0.1-rc.8

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 (295) hide show
  1. package/README.md +3 -1
  2. package/dist/index.cjs +11147 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +6831 -0
  5. package/dist/index.d.ts +6830 -9
  6. package/dist/index.mjs +10991 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +19 -5
  9. package/dist/abis/asset-manager.abi.d.ts +0 -418
  10. package/dist/abis/asset-manager.abi.d.ts.map +0 -1
  11. package/dist/abis/asset-manager.abi.js +0 -542
  12. package/dist/abis/asset-manager.abi.js.map +0 -1
  13. package/dist/abis/connection.abi.d.ts +0 -72
  14. package/dist/abis/connection.abi.d.ts.map +0 -1
  15. package/dist/abis/connection.abi.js +0 -92
  16. package/dist/abis/connection.abi.js.map +0 -1
  17. package/dist/abis/erc20.abi.d.ts +0 -236
  18. package/dist/abis/erc20.abi.d.ts.map +0 -1
  19. package/dist/abis/erc20.abi.js +0 -311
  20. package/dist/abis/erc20.abi.js.map +0 -1
  21. package/dist/abis/index.d.ts +0 -10
  22. package/dist/abis/index.d.ts.map +0 -1
  23. package/dist/abis/index.js +0 -10
  24. package/dist/abis/index.js.map +0 -1
  25. package/dist/abis/intents.abi.d.ts +0 -901
  26. package/dist/abis/intents.abi.d.ts.map +0 -1
  27. package/dist/abis/intents.abi.js +0 -1158
  28. package/dist/abis/intents.abi.js.map +0 -1
  29. package/dist/abis/pool.abi.d.ts +0 -626
  30. package/dist/abis/pool.abi.d.ts.map +0 -1
  31. package/dist/abis/pool.abi.js +0 -812
  32. package/dist/abis/pool.abi.js.map +0 -1
  33. package/dist/abis/spokeAssetManager.abi.d.ts +0 -169
  34. package/dist/abis/spokeAssetManager.abi.d.ts.map +0 -1
  35. package/dist/abis/spokeAssetManager.abi.js +0 -217
  36. package/dist/abis/spokeAssetManager.abi.js.map +0 -1
  37. package/dist/abis/uiPoolData.abi.d.ts +0 -369
  38. package/dist/abis/uiPoolData.abi.d.ts.map +0 -1
  39. package/dist/abis/uiPoolData.abi.js +0 -476
  40. package/dist/abis/uiPoolData.abi.js.map +0 -1
  41. package/dist/abis/vaultToken.abi.d.ts +0 -816
  42. package/dist/abis/vaultToken.abi.d.ts.map +0 -1
  43. package/dist/abis/vaultToken.abi.js +0 -1062
  44. package/dist/abis/vaultToken.abi.js.map +0 -1
  45. package/dist/abis/walletFactory.abi.d.ts +0 -20
  46. package/dist/abis/walletFactory.abi.d.ts.map +0 -1
  47. package/dist/abis/walletFactory.abi.js +0 -27
  48. package/dist/abis/walletFactory.abi.js.map +0 -1
  49. package/dist/constants.d.ts +0 -550
  50. package/dist/constants.d.ts.map +0 -1
  51. package/dist/constants.js +0 -1189
  52. package/dist/constants.js.map +0 -1
  53. package/dist/constants.test.d.ts +0 -2
  54. package/dist/constants.test.d.ts.map +0 -1
  55. package/dist/constants.test.js +0 -129
  56. package/dist/constants.test.js.map +0 -1
  57. package/dist/entities/Providers.d.ts +0 -52
  58. package/dist/entities/Providers.d.ts.map +0 -1
  59. package/dist/entities/Providers.js +0 -49
  60. package/dist/entities/Providers.js.map +0 -1
  61. package/dist/entities/Sodax.d.ts +0 -23
  62. package/dist/entities/Sodax.d.ts.map +0 -1
  63. package/dist/entities/Sodax.js +0 -29
  64. package/dist/entities/Sodax.js.map +0 -1
  65. package/dist/entities/Sodax.test.d.ts +0 -2
  66. package/dist/entities/Sodax.test.d.ts.map +0 -1
  67. package/dist/entities/Sodax.test.js +0 -392
  68. package/dist/entities/Sodax.test.js.map +0 -1
  69. package/dist/entities/cosmos/CW20Token.d.ts +0 -39
  70. package/dist/entities/cosmos/CW20Token.d.ts.map +0 -1
  71. package/dist/entities/cosmos/CW20Token.js +0 -65
  72. package/dist/entities/cosmos/CW20Token.js.map +0 -1
  73. package/dist/entities/cosmos/CWSpokeProvider.d.ts +0 -78
  74. package/dist/entities/cosmos/CWSpokeProvider.d.ts.map +0 -1
  75. package/dist/entities/cosmos/CWSpokeProvider.js +0 -127
  76. package/dist/entities/cosmos/CWSpokeProvider.js.map +0 -1
  77. package/dist/entities/icon/HanaWalletConnector.d.ts +0 -36
  78. package/dist/entities/icon/HanaWalletConnector.d.ts.map +0 -1
  79. package/dist/entities/icon/HanaWalletConnector.js +0 -92
  80. package/dist/entities/icon/HanaWalletConnector.js.map +0 -1
  81. package/dist/entities/icon/IconSpokeProvider.d.ts +0 -10
  82. package/dist/entities/icon/IconSpokeProvider.d.ts.map +0 -1
  83. package/dist/entities/icon/IconSpokeProvider.js +0 -12
  84. package/dist/entities/icon/IconSpokeProvider.js.map +0 -1
  85. package/dist/entities/icon/index.d.ts +0 -4
  86. package/dist/entities/icon/index.d.ts.map +0 -1
  87. package/dist/entities/icon/index.js +0 -4
  88. package/dist/entities/icon/index.js.map +0 -1
  89. package/dist/entities/icon/utils.d.ts +0 -3
  90. package/dist/entities/icon/utils.d.ts.map +0 -1
  91. package/dist/entities/icon/utils.js +0 -4
  92. package/dist/entities/icon/utils.js.map +0 -1
  93. package/dist/entities/index.d.ts +0 -7
  94. package/dist/entities/index.d.ts.map +0 -1
  95. package/dist/entities/index.js +0 -7
  96. package/dist/entities/index.js.map +0 -1
  97. package/dist/entities/solana/Configs.d.ts +0 -10
  98. package/dist/entities/solana/Configs.d.ts.map +0 -1
  99. package/dist/entities/solana/Configs.js +0 -50
  100. package/dist/entities/solana/Configs.js.map +0 -1
  101. package/dist/entities/solana/SolanaSpokeProvider.d.ts +0 -9
  102. package/dist/entities/solana/SolanaSpokeProvider.d.ts.map +0 -1
  103. package/dist/entities/solana/SolanaSpokeProvider.js +0 -9
  104. package/dist/entities/solana/SolanaSpokeProvider.js.map +0 -1
  105. package/dist/entities/solana/SolanaWalletProvider.d.ts +0 -20
  106. package/dist/entities/solana/SolanaWalletProvider.d.ts.map +0 -1
  107. package/dist/entities/solana/SolanaWalletProvider.js +0 -53
  108. package/dist/entities/solana/SolanaWalletProvider.js.map +0 -1
  109. package/dist/entities/solana/index.d.ts +0 -4
  110. package/dist/entities/solana/index.d.ts.map +0 -1
  111. package/dist/entities/solana/index.js +0 -4
  112. package/dist/entities/solana/index.js.map +0 -1
  113. package/dist/entities/solana/pda/pda.d.ts +0 -45
  114. package/dist/entities/solana/pda/pda.d.ts.map +0 -1
  115. package/dist/entities/solana/pda/pda.js +0 -44
  116. package/dist/entities/solana/pda/pda.js.map +0 -1
  117. package/dist/entities/solana/types/asset_manager.d.ts +0 -919
  118. package/dist/entities/solana/types/asset_manager.d.ts.map +0 -1
  119. package/dist/entities/solana/types/asset_manager.js +0 -2
  120. package/dist/entities/solana/types/asset_manager.js.map +0 -1
  121. package/dist/entities/solana/types/connection.d.ts +0 -446
  122. package/dist/entities/solana/types/connection.d.ts.map +0 -1
  123. package/dist/entities/solana/types/connection.js +0 -2
  124. package/dist/entities/solana/types/connection.js.map +0 -1
  125. package/dist/entities/solana/types/rate_limit_contract.d.ts +0 -348
  126. package/dist/entities/solana/types/rate_limit_contract.d.ts.map +0 -1
  127. package/dist/entities/solana/types/rate_limit_contract.js +0 -2
  128. package/dist/entities/solana/types/rate_limit_contract.js.map +0 -1
  129. package/dist/entities/solana/utils/utils.d.ts +0 -6
  130. package/dist/entities/solana/utils/utils.d.ts.map +0 -1
  131. package/dist/entities/solana/utils/utils.js +0 -18
  132. package/dist/entities/solana/utils/utils.js.map +0 -1
  133. package/dist/entities/stellar/StellarSpokeProvider.d.ts +0 -17
  134. package/dist/entities/stellar/StellarSpokeProvider.d.ts.map +0 -1
  135. package/dist/entities/stellar/StellarSpokeProvider.js +0 -125
  136. package/dist/entities/stellar/StellarSpokeProvider.js.map +0 -1
  137. package/dist/entities/sui/SuiSpokeProvider.d.ts +0 -35
  138. package/dist/entities/sui/SuiSpokeProvider.d.ts.map +0 -1
  139. package/dist/entities/sui/SuiSpokeProvider.js +0 -145
  140. package/dist/entities/sui/SuiSpokeProvider.js.map +0 -1
  141. package/dist/errors.d.ts +0 -2
  142. package/dist/errors.d.ts.map +0 -1
  143. package/dist/errors.js +0 -2
  144. package/dist/errors.js.map +0 -1
  145. package/dist/guards.d.ts +0 -25
  146. package/dist/guards.d.ts.map +0 -1
  147. package/dist/guards.js +0 -110
  148. package/dist/guards.js.map +0 -1
  149. package/dist/index.d.ts.map +0 -1
  150. package/dist/index.js +0 -10
  151. package/dist/index.js.map +0 -1
  152. package/dist/services/hub/EvmAssetManagerService.d.ts +0 -61
  153. package/dist/services/hub/EvmAssetManagerService.d.ts.map +0 -1
  154. package/dist/services/hub/EvmAssetManagerService.js +0 -103
  155. package/dist/services/hub/EvmAssetManagerService.js.map +0 -1
  156. package/dist/services/hub/EvmVaultTokenService.d.ts +0 -81
  157. package/dist/services/hub/EvmVaultTokenService.d.ts.map +0 -1
  158. package/dist/services/hub/EvmVaultTokenService.js +0 -161
  159. package/dist/services/hub/EvmVaultTokenService.js.map +0 -1
  160. package/dist/services/hub/EvmWalletAbstraction.d.ts +0 -9
  161. package/dist/services/hub/EvmWalletAbstraction.d.ts.map +0 -1
  162. package/dist/services/hub/EvmWalletAbstraction.js +0 -14
  163. package/dist/services/hub/EvmWalletAbstraction.js.map +0 -1
  164. package/dist/services/hub/index.d.ts +0 -4
  165. package/dist/services/hub/index.d.ts.map +0 -1
  166. package/dist/services/hub/index.js +0 -4
  167. package/dist/services/hub/index.js.map +0 -1
  168. package/dist/services/index.d.ts +0 -7
  169. package/dist/services/index.d.ts.map +0 -1
  170. package/dist/services/index.js +0 -7
  171. package/dist/services/index.js.map +0 -1
  172. package/dist/services/intentRelay/IntentRelayApiService.d.ts +0 -112
  173. package/dist/services/intentRelay/IntentRelayApiService.d.ts.map +0 -1
  174. package/dist/services/intentRelay/IntentRelayApiService.js +0 -160
  175. package/dist/services/intentRelay/IntentRelayApiService.js.map +0 -1
  176. package/dist/services/intentRelay/IntentRelayApiService.test.d.ts +0 -2
  177. package/dist/services/intentRelay/IntentRelayApiService.test.d.ts.map +0 -1
  178. package/dist/services/intentRelay/IntentRelayApiService.test.js +0 -134
  179. package/dist/services/intentRelay/IntentRelayApiService.test.js.map +0 -1
  180. package/dist/services/intentRelay/index.d.ts +0 -2
  181. package/dist/services/intentRelay/index.d.ts.map +0 -1
  182. package/dist/services/intentRelay/index.js +0 -2
  183. package/dist/services/intentRelay/index.js.map +0 -1
  184. package/dist/services/moneyMarket/MoneyMarketService.d.ts +0 -525
  185. package/dist/services/moneyMarket/MoneyMarketService.d.ts.map +0 -1
  186. package/dist/services/moneyMarket/MoneyMarketService.js +0 -814
  187. package/dist/services/moneyMarket/MoneyMarketService.js.map +0 -1
  188. package/dist/services/moneyMarket/MoneyMarketService.test.d.ts +0 -2
  189. package/dist/services/moneyMarket/MoneyMarketService.test.d.ts.map +0 -1
  190. package/dist/services/moneyMarket/MoneyMarketService.test.js +0 -262
  191. package/dist/services/moneyMarket/MoneyMarketService.test.js.map +0 -1
  192. package/dist/services/moneyMarket/index.d.ts +0 -2
  193. package/dist/services/moneyMarket/index.d.ts.map +0 -1
  194. package/dist/services/moneyMarket/index.js +0 -2
  195. package/dist/services/moneyMarket/index.js.map +0 -1
  196. package/dist/services/shared/Erc20Service.d.ts +0 -42
  197. package/dist/services/shared/Erc20Service.d.ts.map +0 -1
  198. package/dist/services/shared/Erc20Service.js +0 -108
  199. package/dist/services/shared/Erc20Service.js.map +0 -1
  200. package/dist/services/shared/index.d.ts +0 -2
  201. package/dist/services/shared/index.d.ts.map +0 -1
  202. package/dist/services/shared/index.js +0 -2
  203. package/dist/services/shared/index.js.map +0 -1
  204. package/dist/services/solver/EvmSolverService.d.ts +0 -125
  205. package/dist/services/solver/EvmSolverService.d.ts.map +0 -1
  206. package/dist/services/solver/EvmSolverService.js +0 -166
  207. package/dist/services/solver/EvmSolverService.js.map +0 -1
  208. package/dist/services/solver/SolverApiService.d.ts +0 -40
  209. package/dist/services/solver/SolverApiService.d.ts.map +0 -1
  210. package/dist/services/solver/SolverApiService.js +0 -157
  211. package/dist/services/solver/SolverApiService.js.map +0 -1
  212. package/dist/services/solver/SolverApiService.test.d.ts +0 -2
  213. package/dist/services/solver/SolverApiService.test.d.ts.map +0 -1
  214. package/dist/services/solver/SolverApiService.test.js +0 -181
  215. package/dist/services/solver/SolverApiService.test.js.map +0 -1
  216. package/dist/services/solver/SolverService.d.ts +0 -295
  217. package/dist/services/solver/SolverService.d.ts.map +0 -1
  218. package/dist/services/solver/SolverService.js +0 -429
  219. package/dist/services/solver/SolverService.js.map +0 -1
  220. package/dist/services/solver/SolverService.test.d.ts +0 -2
  221. package/dist/services/solver/SolverService.test.d.ts.map +0 -1
  222. package/dist/services/solver/SolverService.test.js +0 -439
  223. package/dist/services/solver/SolverService.test.js.map +0 -1
  224. package/dist/services/solver/index.d.ts +0 -3
  225. package/dist/services/solver/index.d.ts.map +0 -1
  226. package/dist/services/solver/index.js +0 -3
  227. package/dist/services/solver/index.js.map +0 -1
  228. package/dist/services/spoke/CWSpokeService.d.ts +0 -67
  229. package/dist/services/spoke/CWSpokeService.d.ts.map +0 -1
  230. package/dist/services/spoke/CWSpokeService.js +0 -75
  231. package/dist/services/spoke/CWSpokeService.js.map +0 -1
  232. package/dist/services/spoke/EvmSpokeService.d.ts +0 -62
  233. package/dist/services/spoke/EvmSpokeService.d.ts.map +0 -1
  234. package/dist/services/spoke/EvmSpokeService.js +0 -120
  235. package/dist/services/spoke/EvmSpokeService.js.map +0 -1
  236. package/dist/services/spoke/IconSpokeService.d.ts +0 -56
  237. package/dist/services/spoke/IconSpokeService.d.ts.map +0 -1
  238. package/dist/services/spoke/IconSpokeService.js +0 -127
  239. package/dist/services/spoke/IconSpokeService.js.map +0 -1
  240. package/dist/services/spoke/SolanaSpokeService.d.ts +0 -46
  241. package/dist/services/spoke/SolanaSpokeService.d.ts.map +0 -1
  242. package/dist/services/spoke/SolanaSpokeService.js +0 -164
  243. package/dist/services/spoke/SolanaSpokeService.js.map +0 -1
  244. package/dist/services/spoke/SpokeService.d.ts +0 -34
  245. package/dist/services/spoke/SpokeService.d.ts.map +0 -1
  246. package/dist/services/spoke/SpokeService.js +0 -101
  247. package/dist/services/spoke/SpokeService.js.map +0 -1
  248. package/dist/services/spoke/StellarSpokeService.d.ts +0 -35
  249. package/dist/services/spoke/StellarSpokeService.d.ts.map +0 -1
  250. package/dist/services/spoke/StellarSpokeService.js +0 -39
  251. package/dist/services/spoke/StellarSpokeService.js.map +0 -1
  252. package/dist/services/spoke/SuiSpokeService.d.ts +0 -69
  253. package/dist/services/spoke/SuiSpokeService.d.ts.map +0 -1
  254. package/dist/services/spoke/SuiSpokeService.js +0 -73
  255. package/dist/services/spoke/SuiSpokeService.js.map +0 -1
  256. package/dist/services/spoke/index.d.ts +0 -3
  257. package/dist/services/spoke/index.d.ts.map +0 -1
  258. package/dist/services/spoke/index.js +0 -3
  259. package/dist/services/spoke/index.js.map +0 -1
  260. package/dist/tests/services/hub/EvmAssetManagerService.test.d.ts +0 -2
  261. package/dist/tests/services/hub/EvmAssetManagerService.test.d.ts.map +0 -1
  262. package/dist/tests/services/hub/EvmAssetManagerService.test.js +0 -138
  263. package/dist/tests/services/hub/EvmAssetManagerService.test.js.map +0 -1
  264. package/dist/tests/services/hub/EvmMoneyMarketService.test.d.ts +0 -2
  265. package/dist/tests/services/hub/EvmMoneyMarketService.test.d.ts.map +0 -1
  266. package/dist/tests/services/hub/EvmMoneyMarketService.test.js +0 -121
  267. package/dist/tests/services/hub/EvmMoneyMarketService.test.js.map +0 -1
  268. package/dist/tests/services/hub/EvmVaultTokenService.test.d.ts +0 -2
  269. package/dist/tests/services/hub/EvmVaultTokenService.test.d.ts.map +0 -1
  270. package/dist/tests/services/hub/EvmVaultTokenService.test.js +0 -98
  271. package/dist/tests/services/hub/EvmVaultTokenService.test.js.map +0 -1
  272. package/dist/tests/services/spoke/EvmSpokeService.test.d.ts +0 -2
  273. package/dist/tests/services/spoke/EvmSpokeService.test.d.ts.map +0 -1
  274. package/dist/tests/services/spoke/EvmSpokeService.test.js +0 -198
  275. package/dist/tests/services/spoke/EvmSpokeService.test.js.map +0 -1
  276. package/dist/types.d.ts +0 -345
  277. package/dist/types.d.ts.map +0 -1
  278. package/dist/types.js +0 -30
  279. package/dist/types.js.map +0 -1
  280. package/dist/utils/evm-utils.d.ts +0 -10
  281. package/dist/utils/evm-utils.d.ts.map +0 -1
  282. package/dist/utils/evm-utils.js +0 -15
  283. package/dist/utils/evm-utils.js.map +0 -1
  284. package/dist/utils/index.d.ts +0 -3
  285. package/dist/utils/index.d.ts.map +0 -1
  286. package/dist/utils/index.js +0 -3
  287. package/dist/utils/index.js.map +0 -1
  288. package/dist/utils/shared-utils.d.ts +0 -20
  289. package/dist/utils/shared-utils.d.ts.map +0 -1
  290. package/dist/utils/shared-utils.js +0 -69
  291. package/dist/utils/shared-utils.js.map +0 -1
  292. package/dist/utils/shared-utils.test.d.ts +0 -2
  293. package/dist/utils/shared-utils.test.d.ts.map +0 -1
  294. package/dist/utils/shared-utils.test.js +0 -63
  295. package/dist/utils/shared-utils.test.js.map +0 -1
@@ -1,814 +0,0 @@
1
- import { encodeFunctionData } from 'viem';
2
- import { poolAbi } from '../../abis/pool.abi.js';
3
- import { DEFAULT_RELAYER_API_ENDPOINT, getHubAssetInfo, getMoneyMarketConfig, getSupportedMoneyMarketTokens, isConfiguredMoneyMarketConfig, isValidOriginalAssetAddress, isValidSpokeChainId, moneyMarketReserveAssets, SpokeService, relayTxAndWaitPacket, uiPoolDataAbi, DEFAULT_RELAY_TX_TIMEOUT, EvmSpokeProvider, isMoneyMarketSupportedToken, } from '../../index.js';
4
- import { calculateFeeAmount, encodeContractCalls } from '../../utils/index.js';
5
- import { EvmAssetManagerService, EvmVaultTokenService, EvmWalletAbstraction } from '../hub/index.js';
6
- import { Erc20Service } from '../shared/index.js';
7
- import invariant from 'tiny-invariant';
8
- import { SONIC_MAINNET_CHAIN_ID } from '@sodax/types';
9
- export class MoneyMarketService {
10
- config;
11
- hubProvider;
12
- constructor(config, hubProvider, relayerApiEndpoint) {
13
- if (!config) {
14
- this.config = {
15
- ...getMoneyMarketConfig(SONIC_MAINNET_CHAIN_ID), // default to mainnet config
16
- partnerFee: undefined,
17
- relayerApiEndpoint: relayerApiEndpoint ?? DEFAULT_RELAYER_API_ENDPOINT,
18
- };
19
- }
20
- else if (isConfiguredMoneyMarketConfig(config)) {
21
- this.config = {
22
- ...config,
23
- partnerFee: config.partnerFee,
24
- relayerApiEndpoint: relayerApiEndpoint ?? DEFAULT_RELAYER_API_ENDPOINT,
25
- };
26
- }
27
- else {
28
- this.config = {
29
- ...getMoneyMarketConfig(hubProvider.chainConfig.chain.id), // default to mainnet config
30
- partnerFee: config.partnerFee,
31
- relayerApiEndpoint: relayerApiEndpoint ?? DEFAULT_RELAYER_API_ENDPOINT,
32
- };
33
- }
34
- this.hubProvider = hubProvider;
35
- }
36
- /**
37
- * Check if allowance is sufficient for supply or repay operations (currently required for EVM only)
38
- * @param {MoneyMarketRepayParams | MoneyMarketSupplyParams} params - Money market params containing token address and amount
39
- * @param {SpokeProvider} spokeProvider - The spoke provider instance
40
- * @return {Promise<Result<boolean>>} - Returns true if allowance is sufficient, false otherwise
41
- *
42
- * @example
43
- * const allowanceValid = await isAllowanceValid({
44
- * token: '0x...', // Address of the token (spoke chain) to supply
45
- * amount: 1000n, // Amount to supply (in token decimals)
46
- * }, spokeProvider);
47
- *
48
- * if (!allowanceValid.ok) {
49
- * // Handle error
50
- * }
51
- *
52
- * if (!allowanceValid.value) {
53
- * // Need to approve
54
- * }
55
- */
56
- async isAllowanceValid(params, spokeProvider) {
57
- try {
58
- if (spokeProvider instanceof EvmSpokeProvider) {
59
- const walletAddress = (await spokeProvider.walletProvider.getWalletAddress());
60
- return Erc20Service.isAllowanceValid(params.token, params.amount, walletAddress, spokeProvider.chainConfig.addresses.assetManager, spokeProvider);
61
- }
62
- return {
63
- ok: true,
64
- value: true,
65
- };
66
- }
67
- catch (error) {
68
- return {
69
- ok: false,
70
- error: error,
71
- };
72
- }
73
- }
74
- /**
75
- * Approve amount spending (currently required for EVM only)
76
- * @param token - ERC20 token address
77
- * @param amount - Amount to approve
78
- * @param spender - Spender address
79
- * @param spokeProvider - Spoke provider
80
- * @returns {Promise<Result<EvmRawTransactionReceipt>>} - Returns the transaction receipt
81
- *
82
- * @example
83
- * const approveResult = await approve(
84
- * '0x...', // ERC20 token address
85
- * 1000n, // Amount to approve (in token decimals)
86
- * '0x...', // Spender address (usually the asset manager contract: spokeProvider.chainConfig.addresses.assetManager)
87
- * spokeProvider
88
- * );
89
- *
90
- * if (!approveResult.ok) {
91
- * // Handle error
92
- * }
93
- *
94
- * const txReceipt = approveResult.value;
95
- */
96
- async approve(token, amount, spender, spokeProvider) {
97
- try {
98
- if (spokeProvider instanceof EvmSpokeProvider) {
99
- return Erc20Service.approve(token, amount, spender, spokeProvider);
100
- }
101
- return {
102
- ok: false,
103
- error: new Error('Approve only supported for EVM spoke chains'),
104
- };
105
- }
106
- catch (error) {
107
- return {
108
- ok: false,
109
- error: error,
110
- };
111
- }
112
- }
113
- /**
114
- * Supply tokens to the money market pool, relay the transaction to the hub and submit the intent to the Solver API
115
- * @param params - The parameters for the supply transaction.
116
- * @param spokeProvider - The spoke provider.
117
- * @param timeout - The timeout in milliseconds for the transaction. Default is 60 seconds.
118
- * @returns {Promise<Result<[Hex, Hex], MoneyMarketError>>} - Returns the transaction result and the hub transaction hash or error
119
- *
120
- * @example
121
- * const result = await moneyMarketService.supplyAndSubmit(
122
- * {
123
- * token: '0x...', // Address of the token (spoke chain address) to supply
124
- * amount: 1000n, // Amount to supply (in token decimals)
125
- * },
126
- * spokeProvider,
127
- * 30000 // Optional timeout in milliseconds (default: 60000, i.e. 60 seconds)
128
- * );
129
- *
130
- * if (!result.ok) {
131
- * // Handle error
132
- * }
133
- *
134
- * const [
135
- * spokeTxHash, // transaction hash on the spoke chain
136
- * hubTxHash, // transaction hash on the hub chain (i.e. the transaction that was relayed to the hub)
137
- * ] = result.value;
138
- * console.log('Supply transaction hashes:', { spokeTxHash, hubTxHash });
139
- */
140
- async supplyAndSubmit(params, spokeProvider, timeout = DEFAULT_RELAY_TX_TIMEOUT) {
141
- try {
142
- const txResult = await this.supply(params, spokeProvider);
143
- if (!txResult.ok) {
144
- return {
145
- ok: false,
146
- error: {
147
- code: 'SUPPLY_FAILED',
148
- error: txResult.error,
149
- },
150
- };
151
- }
152
- const packetResult = await relayTxAndWaitPacket(txResult.value, spokeProvider, this.config.relayerApiEndpoint, timeout);
153
- if (!packetResult.ok) {
154
- return packetResult;
155
- }
156
- return { ok: true, value: [txResult.value, packetResult.value.dst_tx_hash] };
157
- }
158
- catch (error) {
159
- return {
160
- ok: false,
161
- error: {
162
- code: 'UNKNOWN',
163
- error: error,
164
- },
165
- };
166
- }
167
- }
168
- /**
169
- * Supply tokens to the money market pool without submitting the intent to the Solver API
170
- * NOTE: This method does not submit the intent to the Solver API, it only executes the transaction on the spoke chain
171
- * In order to successfully supply tokens, you need to:
172
- * 1. Check if the allowance is sufficient
173
- * 2. Approve the asset manager contract to spend the tokens
174
- * 3. Supply the tokens
175
- * 4. Submit the intent to the Solver API and await it using relayTxAndWaitPacket method
176
- *
177
- * @param params - The parameters for the supply transaction.
178
- * @param spokeProvider - The spoke provider.
179
- * @param raw - Whether to return the raw transaction data.
180
- * @returns {Promise<Result<TxReturnType<S, R>, MoneyMarketErrorCode>>} - Returns the transaction result.
181
- *
182
- * @example
183
- * const moneyMarketService = new MoneyMarketService(config);
184
- * const result = await moneyMarketService.supply(
185
- * {
186
- * token: "0x123...", // token address
187
- * amount: 1000000000000000000n // 1 token in wei
188
- * },
189
- * spokeProvider,
190
- * raw // Optional: true = return the raw transaction data, false = exeute and return the transaction hash (default: false)
191
- * );
192
- *
193
- * if (result.ok) {
194
- * const txHash = result.value;
195
- * console.log('Supply transaction hash:', txHash);
196
- * } else {
197
- * console.error('Supply failed:', result.error);
198
- * }
199
- */
200
- async supply(params, spokeProvider, raw) {
201
- try {
202
- invariant(params.token.length > 0, 'Token is required');
203
- invariant(params.amount > 0n, 'Amount must be greater than 0');
204
- invariant(isMoneyMarketSupportedToken(spokeProvider.chainConfig.chain.id, params.token), `Unsupported spoke chain (${spokeProvider.chainConfig.chain.id}) token: ${params.token}`);
205
- const walletAddressBytes = await spokeProvider.walletProvider.getWalletAddressBytes();
206
- const hubWallet = await EvmWalletAbstraction.getUserHubWalletAddress(spokeProvider.chainConfig.chain.id, walletAddressBytes, this.hubProvider);
207
- const data = this.supplyData(params.token, hubWallet, params.amount, spokeProvider.chainConfig.chain.id);
208
- const walletAddress = (await spokeProvider.walletProvider.getWalletAddress());
209
- const txResult = await SpokeService.deposit({
210
- from: walletAddress,
211
- to: hubWallet,
212
- token: params.token,
213
- amount: params.amount,
214
- data,
215
- }, spokeProvider, this.hubProvider, raw);
216
- return {
217
- ok: true,
218
- value: txResult,
219
- };
220
- }
221
- catch (error) {
222
- return {
223
- ok: false,
224
- error: {
225
- code: 'UNKNOWN',
226
- error,
227
- },
228
- };
229
- }
230
- }
231
- /**
232
- * Borrow tokens from the money market pool, relay the transaction to the hub and submit the intent to the Solver API
233
- * @param params - The parameters for the borrow transaction.
234
- * @param spokeProvider - The spoke provider.
235
- * @param timeout - The timeout in milliseconds for the transaction. Default is 60 seconds.
236
- * @returns {Promise<Result<[Hex, Hex], MoneyMarketError>>} - Returns the transaction result and the hub transaction hash or error
237
- *
238
- * @example
239
- * const result = await moneyMarketService.borrowAndSubmit(
240
- * {
241
- * token: '0x...', // Address of the token (spoke chain address) to borrow
242
- * amount: 1000n, // Amount to borrow (in token decimals)
243
- * },
244
- * spokeProvider,
245
- * 30000 // Optional timeout in milliseconds (default: 60000, i.e. 60 seconds)
246
- * );
247
- *
248
- * if (!result.ok) {
249
- * // Handle error
250
- * }
251
- *
252
- * const [
253
- * spokeTxHash, // transaction hash on the spoke chain
254
- * hubTxHash, // transaction hash on the hub chain (i.e. the transaction that was relayed to the hub)
255
- * ] = result.value;
256
- * console.log('Borrow transaction hashes:', { spokeTxHash, hubTxHash });
257
- */
258
- async borrowAndSubmit(params, spokeProvider, timeout = DEFAULT_RELAY_TX_TIMEOUT) {
259
- try {
260
- const txResult = await this.borrow(params, spokeProvider);
261
- if (!txResult.ok) {
262
- return {
263
- ok: false,
264
- error: {
265
- code: 'BORROW_FAILED',
266
- error: txResult.error,
267
- },
268
- };
269
- }
270
- const packetResult = await relayTxAndWaitPacket(txResult.value, spokeProvider, this.config.relayerApiEndpoint, timeout);
271
- if (!packetResult.ok) {
272
- return packetResult;
273
- }
274
- return { ok: true, value: [txResult.value, packetResult.value.dst_tx_hash] };
275
- }
276
- catch (error) {
277
- return {
278
- ok: false,
279
- error: {
280
- code: 'UNKNOWN',
281
- error: error,
282
- },
283
- };
284
- }
285
- }
286
- /**
287
- * Borrow tokens from the money market pool without submitting the intent to the Solver API
288
- * NOTE: This method does not submit the intent to the Solver API, it only executes the transaction on the spoke chain
289
- * In order to successfully borrow tokens, you need to:
290
- * 1. Execute the borrow transaction on the spoke chain
291
- * 2. Submit the intent to the Solver API and await it using relayTxAndWaitPacket method
292
- *
293
- * @param params - The parameters for the borrow transaction.
294
- * @param spokeProvider - The spoke provider.
295
- * @param raw - Whether to return the raw transaction data.
296
- * @returns {Promise<Result<TxReturnType<S, R>, MoneyMarketErrorCode>>} - Returns the transaction result (raw transaction data or transaction hash).
297
- *
298
- * @example
299
- * const moneyMarketService = new MoneyMarketService(config);
300
- * const result = await moneyMarketService.borrow(
301
- * {
302
- * token: "0x123...", // token address
303
- * amount: 1000000000000000000n // 1 token in wei
304
- * },
305
- * spokeProvider,
306
- * raw // Optional: true = return the raw transaction data, false = exeute and return the transaction hash (default: false)
307
- * );
308
- *
309
- * if (result.ok) {
310
- * const txHash = result.value;
311
- * console.log('Borrow transaction hash:', txHash);
312
- * } else {
313
- * console.error('Borrow failed:', result.error);
314
- * }
315
- */
316
- async borrow(params, spokeProvider, raw) {
317
- invariant(params.token.length > 0, 'Token is required');
318
- invariant(params.amount > 0n, 'Amount must be greater than 0');
319
- invariant(isMoneyMarketSupportedToken(spokeProvider.chainConfig.chain.id, params.token), `Unsupported spoke chain (${spokeProvider.chainConfig.chain.id}) token: ${params.token}`);
320
- const walletAddressBytes = await spokeProvider.walletProvider.getWalletAddressBytes();
321
- const hubWallet = await EvmWalletAbstraction.getUserHubWalletAddress(spokeProvider.chainConfig.chain.id, walletAddressBytes, this.hubProvider);
322
- const data = this.borrowData(hubWallet, walletAddressBytes, params.token, params.amount, spokeProvider.chainConfig.chain.id);
323
- const txResult = await SpokeService.callWallet(hubWallet, data, spokeProvider, this.hubProvider, raw);
324
- return { ok: true, value: txResult };
325
- }
326
- /**
327
- * Withdraw tokens from the money market pool, relay the transaction to the hub and submit the intent to the Solver API
328
- *
329
- * @param params - The parameters for the withdraw transaction.
330
- * @param spokeProvider - The spoke provider.
331
- * @param timeout - The timeout in milliseconds for the transaction. Default is 60 seconds.
332
- * @returns {Promise<Result<[Hex, Hex], MoneyMarketError>>} - Returns the transaction result and the hub transaction hash or error
333
- *
334
- * @example
335
- * const result = await moneyMarketService.withdrawAndSubmit(
336
- * {
337
- * token: '0x...', // Address of the token (spoke chain address) to withdraw
338
- * amount: 1000n, // Amount to withdraw (in token decimals)
339
- * },
340
- * spokeProvider,
341
- * 30000 // Optional timeout in milliseconds (default: 60000, i.e. 60 seconds)
342
- * );
343
- *
344
- * if (!result.ok) {
345
- * // Handle error
346
- * }
347
- *
348
- * const [
349
- * spokeTxHash, // transaction hash on the spoke chain
350
- * hubTxHash, // transaction hash on the hub chain (i.e. the transaction that was relayed to the hub)
351
- * ] = result.value;
352
- * console.log('Withdraw transaction hashes:', { spokeTxHash, hubTxHash });
353
- */
354
- async withdrawAndSubmit(params, spokeProvider, timeout = DEFAULT_RELAY_TX_TIMEOUT) {
355
- try {
356
- const txResult = await this.withdraw(params, spokeProvider);
357
- if (!txResult.ok) {
358
- return {
359
- ok: false,
360
- error: {
361
- code: 'WITHDRAW_FAILED',
362
- error: txResult.error,
363
- },
364
- };
365
- }
366
- const packetResult = await relayTxAndWaitPacket(txResult.value, spokeProvider, this.config.relayerApiEndpoint, timeout);
367
- if (!packetResult.ok) {
368
- return packetResult;
369
- }
370
- return { ok: true, value: [txResult.value, packetResult.value.dst_tx_hash] };
371
- }
372
- catch (error) {
373
- return {
374
- ok: false,
375
- error: {
376
- code: 'UNKNOWN',
377
- error: error,
378
- },
379
- };
380
- }
381
- }
382
- /**
383
- * Withdraw tokens from the money market pool without submitting the intent to the Solver API
384
- * NOTE: This method does not submit the intent to the Solver API, it only executes the transaction on the spoke chain
385
- * In order to successfully withdraw tokens, you need to:
386
- * 1. Execute the withdraw transaction on the spoke chain
387
- * 2. Submit the intent to the Solver API and await it using relayTxAndWaitPacket method
388
- *
389
- * @param params - The parameters for the withdraw transaction.
390
- * @param spokeProvider - The spoke provider.
391
- * @param raw - Whether to return the raw transaction data.
392
- * @returns {Promise<Result<TxReturnType<S, R>, MoneyMarketErrorCode>>} - Returns the transaction result (raw transaction data or transaction hash).
393
- *
394
- * @example
395
- * const moneyMarketService = new MoneyMarketService(config);
396
- * const result = await moneyMarketService.withdraw(
397
- * {
398
- * token: "0x123...", // token address
399
- * amount: 1000000000000000000n // 1 token in wei
400
- * },
401
- * spokeProvider,
402
- * raw // Optional: true = return the raw transaction data, false = exeute and return the transaction hash (default: false)
403
- * );
404
- *
405
- * if (result.ok) {
406
- * const txHash = result.value;
407
- * console.log('Withdraw transaction hash:', txHash);
408
- * } else {
409
- * console.error('Withdraw failed:', result.error);
410
- * }
411
- */
412
- async withdraw(params, spokeProvider, raw) {
413
- invariant(params.token.length > 0, 'Token is required');
414
- invariant(params.amount > 0n, 'Amount must be greater than 0');
415
- invariant(isMoneyMarketSupportedToken(spokeProvider.chainConfig.chain.id, params.token), `Unsupported spoke chain (${spokeProvider.chainConfig.chain.id}) token: ${params.token}`);
416
- const walletAddressBytes = await spokeProvider.walletProvider.getWalletAddressBytes();
417
- const hubWallet = await EvmWalletAbstraction.getUserHubWalletAddress(spokeProvider.chainConfig.chain.id, walletAddressBytes, this.hubProvider);
418
- const data = this.withdrawData(hubWallet, walletAddressBytes, params.token, params.amount, spokeProvider.chainConfig.chain.id);
419
- const txResult = await SpokeService.callWallet(hubWallet, data, spokeProvider, this.hubProvider, raw);
420
- return { ok: true, value: txResult };
421
- }
422
- /**
423
- * Repay tokens to the money market pool, relay the transaction to the hub and submit the intent to the Solver API
424
- *
425
- * @param params - The parameters for the repay transaction.
426
- * @param spokeProvider - The spoke provider.
427
- * @param timeout - The timeout in milliseconds for the transaction. Default is 60 seconds.
428
- * @returns {Promise<Result<[Hex, Hex], MoneyMarketError>>} - Returns the transaction result and the hub transaction hash or error
429
- *
430
- * @example
431
- * const result = await moneyMarketService.repayAndSubmit(
432
- * {
433
- * token: '0x...', // Address of the token (spoke chain address) to repay
434
- * amount: 1000n, // Amount to repay (in token decimals)
435
- * },
436
- * spokeProvider,
437
- * 30000 // Optional timeout in milliseconds (default: 60000, i.e. 60 seconds)
438
- * );
439
- *
440
- * if (!result.ok) {
441
- * // Handle error
442
- * }
443
- *
444
- * const [
445
- * spokeTxHash, // transaction hash on the spoke chain
446
- * hubTxHash, // transaction hash on the hub chain (i.e. the transaction that was relayed to the hub)
447
- * ] = result.value;
448
- * console.log('Repay transaction hashes:', { spokeTxHash, hubTxHash });
449
- */
450
- async repayAndSubmit(params, spokeProvider, timeout = DEFAULT_RELAY_TX_TIMEOUT) {
451
- try {
452
- const txResult = await this.repay(params, spokeProvider);
453
- if (!txResult.ok) {
454
- return {
455
- ok: false,
456
- error: {
457
- code: 'REPAY_FAILED',
458
- error: txResult.error,
459
- },
460
- };
461
- }
462
- const packetResult = await relayTxAndWaitPacket(txResult.value, spokeProvider, this.config.relayerApiEndpoint, timeout);
463
- if (!packetResult.ok) {
464
- return packetResult;
465
- }
466
- return { ok: true, value: [txResult.value, packetResult.value.dst_tx_hash] };
467
- }
468
- catch (error) {
469
- return {
470
- ok: false,
471
- error: {
472
- code: 'UNKNOWN',
473
- error: error,
474
- },
475
- };
476
- }
477
- }
478
- /**
479
- * Repay tokens to the money market pool without submitting the intent to the Solver API
480
- * NOTE: This method does not submit the intent to the Solver API, it only executes the transaction on the spoke chain
481
- * In order to successfully repay tokens, you need to:
482
- * 1. Check if the allowance is sufficient
483
- * 2. Approve the asset manager contract to spend the tokens
484
- * 3. Execute the repay transaction on the spoke chain
485
- * 4. Submit the intent to the Solver API and await it using relayTxAndWaitPacket method
486
- *
487
- * @param params - The parameters for the repay transaction.
488
- * @param spokeProvider - The spoke provider.
489
- * @param raw - Whether to return the raw transaction data.
490
- * @returns {Promise<Result<TxReturnType<S, R>, MoneyMarketErrorCode>>} The transaction result (raw transaction data or transaction hash) or error.
491
- *
492
- * @example
493
- * const moneyMarketService = new MoneyMarketService(config);
494
- * const result = await moneyMarketService.repay(
495
- * {
496
- * token: "0x123...", // token address
497
- * amount: 1000000000000000000n // 1 token in wei
498
- * },
499
- * spokeProvider,
500
- * raw // Optional: true = return the raw transaction data, false = exeute and return the transaction hash (default: false)
501
- * );
502
- *
503
- * if (result.ok) {
504
- * const txHash = result.value;
505
- * console.log('Repay transaction hash:', txHash);
506
- * } else {
507
- * console.error('Repay failed:', result.error);
508
- * }
509
- */
510
- async repay(params, spokeProvider, raw) {
511
- invariant(params.token.length > 0, 'Token is required');
512
- invariant(params.amount > 0n, 'Amount must be greater than 0');
513
- invariant(isMoneyMarketSupportedToken(spokeProvider.chainConfig.chain.id, params.token), `Unsupported spoke chain (${spokeProvider.chainConfig.chain.id}) token: ${params.token}`);
514
- const walletAddressBytes = await spokeProvider.walletProvider.getWalletAddressBytes();
515
- const hubWallet = await EvmWalletAbstraction.getUserHubWalletAddress(spokeProvider.chainConfig.chain.id, walletAddressBytes, this.hubProvider);
516
- const data = this.repayData(params.token, hubWallet, params.amount, spokeProvider.chainConfig.chain.id);
517
- const walletAddress = (await spokeProvider.walletProvider.getWalletAddress());
518
- const txResult = await SpokeService.deposit({
519
- from: walletAddress,
520
- to: hubWallet,
521
- token: params.token,
522
- amount: params.amount,
523
- data,
524
- }, spokeProvider, this.hubProvider, raw);
525
- return { ok: true, value: txResult };
526
- }
527
- /**
528
- * Build transaction data for supplying to the money market pool
529
- * @param token - The address of the token on spoke chain
530
- * @param to - The user wallet address on the hub chain
531
- * @param amount - The amount to deposit
532
- * @param spokeChainId - The chain ID of the spoke chain
533
- * @returns {Hex} The transaction data.
534
- */
535
- supplyData(token, to, amount, spokeChainId) {
536
- const calls = [];
537
- const assetConfig = getHubAssetInfo(spokeChainId, token);
538
- invariant(assetConfig, `hub asset not found for spoke chain token (token): ${token}`);
539
- const assetAddress = assetConfig.asset;
540
- const vaultAddress = assetConfig.vault;
541
- const lendingPool = this.config.lendingPool;
542
- calls.push(Erc20Service.encodeApprove(assetAddress, vaultAddress, amount));
543
- calls.push(EvmVaultTokenService.encodeDeposit(vaultAddress, assetAddress, amount));
544
- const translatedAmount = EvmVaultTokenService.translateIncomingDecimals(assetConfig.decimal, amount);
545
- calls.push(Erc20Service.encodeApprove(vaultAddress, lendingPool, translatedAmount));
546
- calls.push(MoneyMarketService.encodeSupply({ asset: vaultAddress, amount: translatedAmount, onBehalfOf: to, referralCode: 0 }, lendingPool));
547
- return encodeContractCalls(calls);
548
- }
549
- /**
550
- * Build transaction data for borrowing from the money market pool
551
- * @param from - The user wallet address on the hub chain
552
- * @param to - The user wallet address on the spoke chain
553
- * @param token - The address of the token to borrow
554
- * @param amount - The amount to borrow in hub chain decimals
555
- * @param spokeChainId - The chain ID of the spoke chain
556
- * @returns {Hex} The transaction data.
557
- */
558
- borrowData(from, to, token, amount, spokeChainId) {
559
- invariant(isValidSpokeChainId(spokeChainId), `Invalid spokeChainId: ${spokeChainId}`);
560
- invariant(isValidOriginalAssetAddress(spokeChainId, token), `Unsupported spoke chain (${spokeChainId}) token: ${token}`);
561
- const assetConfig = getHubAssetInfo(spokeChainId, token);
562
- invariant(assetConfig, `hub asset not found for spoke chain token (token): ${token}`);
563
- const assetAddress = assetConfig.asset;
564
- const vaultAddress = assetConfig.vault;
565
- const bnUSDVault = this.config.bnUSDVault;
566
- const bnUSD = this.config.bnUSD;
567
- const feeAmount = calculateFeeAmount(amount, this.config.partnerFee);
568
- const calls = [];
569
- if (bnUSDVault && bnUSD && bnUSDVault.toLowerCase() === vaultAddress.toLowerCase()) {
570
- calls.push(MoneyMarketService.encodeBorrow({ asset: bnUSD, amount: amount, interestRateMode: 2n, referralCode: 0, onBehalfOf: from }, this.config.lendingPool));
571
- calls.push(Erc20Service.encodeApprove(bnUSD, bnUSDVault, amount));
572
- calls.push(EvmVaultTokenService.encodeDeposit(bnUSDVault, bnUSD, amount));
573
- if (this.config.partnerFee && feeAmount) {
574
- calls.push(Erc20Service.encodeTansfer(bnUSDVault, this.config.partnerFee.address, feeAmount));
575
- }
576
- }
577
- else {
578
- calls.push(MoneyMarketService.encodeBorrow({ asset: vaultAddress, amount: amount, interestRateMode: 2n, referralCode: 0, onBehalfOf: from }, this.config.lendingPool));
579
- if (this.config.partnerFee && feeAmount) {
580
- calls.push(Erc20Service.encodeTansfer(vaultAddress, this.config.partnerFee.address, feeAmount));
581
- }
582
- }
583
- calls.push(EvmVaultTokenService.encodeWithdraw(vaultAddress, assetAddress, amount - feeAmount));
584
- const translatedAmountOut = EvmVaultTokenService.translateOutgoingDecimals(assetConfig.decimal, amount - feeAmount);
585
- calls.push(EvmAssetManagerService.encodeTransfer(assetAddress, to, translatedAmountOut, this.hubProvider.chainConfig.addresses.assetManager));
586
- return encodeContractCalls(calls);
587
- }
588
- /**
589
- * Build transaction data for withdrawing from the money market pool
590
- * @param from - The user wallet address on the hub chain
591
- * @param to - The user wallet address on the spoke chain
592
- * @param token - The address of the token to borrow
593
- * @param amount - The amount to borrow in hub chain decimals
594
- * @param spokeChainId - The chain ID of the spoke chain
595
- * @returns {Hex} The transaction data.
596
- */
597
- withdrawData(from, to, token, amount, spokeChainId) {
598
- const calls = [];
599
- const assetConfig = getHubAssetInfo(spokeChainId, token);
600
- if (!assetConfig) {
601
- throw new Error('[withdrawData] Hub asset not found');
602
- }
603
- const assetAddress = assetConfig.asset;
604
- const vaultAddress = assetConfig.vault;
605
- if (!assetAddress || !vaultAddress) {
606
- throw new Error('Address not found');
607
- }
608
- calls.push(MoneyMarketService.encodeWithdraw({ asset: vaultAddress, amount: amount, to: from }, this.config.lendingPool));
609
- calls.push(EvmVaultTokenService.encodeWithdraw(vaultAddress, assetAddress, amount));
610
- const translatedAmountOut = EvmVaultTokenService.translateOutgoingDecimals(assetConfig.decimal, amount);
611
- calls.push(EvmAssetManagerService.encodeTransfer(assetAddress, to, translatedAmountOut, this.hubProvider.chainConfig.addresses.assetManager));
612
- return encodeContractCalls(calls);
613
- }
614
- /**
615
- * Build transaction data for repaying to the money market pool
616
- * @param token - The address of the token to repay
617
- * @param to - The user wallet address on the hub chain
618
- * @param amount - The amount to repay
619
- * @param spokeChainId - The chain ID of the spoke chain
620
- * @returns {Hex} The transaction data.
621
- */
622
- repayData(token, to, amount, spokeChainId) {
623
- const calls = [];
624
- const assetConfig = getHubAssetInfo(spokeChainId, token);
625
- if (!assetConfig) {
626
- throw new Error('[repayData] Hub asset not found');
627
- }
628
- const assetAddress = assetConfig.asset;
629
- const vaultAddress = assetConfig.vault;
630
- const bnUSDVault = this.config.bnUSDVault;
631
- const bnUSD = this.config.bnUSD;
632
- calls.push(Erc20Service.encodeApprove(assetAddress, vaultAddress, amount));
633
- calls.push(EvmVaultTokenService.encodeDeposit(vaultAddress, assetAddress, amount));
634
- const translatedAmount = EvmVaultTokenService.translateIncomingDecimals(assetConfig.decimal, amount);
635
- let repayToken = vaultAddress;
636
- if (bnUSDVault && bnUSD && bnUSDVault.toLowerCase() === vaultAddress.toLowerCase()) {
637
- repayToken = bnUSD;
638
- calls.push(EvmVaultTokenService.encodeWithdraw(bnUSDVault, bnUSD, translatedAmount));
639
- }
640
- calls.push(Erc20Service.encodeApprove(repayToken, this.config.lendingPool, translatedAmount));
641
- calls.push(MoneyMarketService.encodeRepay({ asset: repayToken, amount: translatedAmount, interestRateMode: 2n, onBehalfOf: to }, this.config.lendingPool));
642
- return encodeContractCalls(calls);
643
- }
644
- /**
645
- * Get the list of all reserves in the pool
646
- * @param uiPoolDataProvider - The address of the UI Pool Data Provider
647
- * @param poolAddressesProvider - The address of the Pool Addresses Provider
648
- * @returns {Promise<readonly Address[]>} - Array of reserve addresses
649
- */
650
- async getReservesList(uiPoolDataProvider, poolAddressesProvider) {
651
- return this.hubProvider.publicClient.readContract({
652
- address: uiPoolDataProvider,
653
- abi: uiPoolDataAbi,
654
- functionName: 'getReservesList',
655
- args: [poolAddressesProvider],
656
- });
657
- }
658
- /**
659
- * Get detailed data for all reserves in the pool
660
- * @param uiPoolDataProvider - The address of the UI Pool Data Provider
661
- * @param poolAddressesProvider - The address of the Pool Addresses Provider
662
- * @returns {Promise<readonly [readonly AggregatedReserveData[], BaseCurrencyInfo]>} - Tuple containing array of reserve data and base currency info
663
- */
664
- async getReservesData(uiPoolDataProvider, poolAddressesProvider) {
665
- return this.hubProvider.publicClient.readContract({
666
- address: uiPoolDataProvider,
667
- abi: uiPoolDataAbi,
668
- functionName: 'getReservesData',
669
- args: [poolAddressesProvider],
670
- });
671
- }
672
- /**
673
- * Get user-specific reserve data
674
- * @param userAddress Address of the user
675
- * @param uiPoolDataProvider - The address of the UI Pool Data Provider
676
- * @param poolAddressesProvider - The address of the Pool Addresses Provider
677
- * @returns {Promise<readonly [readonly UserReserveData[], number]>} - Tuple containing array of user reserve data and eMode category ID
678
- */
679
- async getUserReservesData(userAddress, uiPoolDataProvider, poolAddressesProvider) {
680
- return this.hubProvider.publicClient.readContract({
681
- address: uiPoolDataProvider,
682
- abi: uiPoolDataAbi,
683
- functionName: 'getUserReservesData',
684
- args: [poolAddressesProvider, userAddress],
685
- });
686
- }
687
- /**
688
- * Encodes a supply transaction for a money market pool.
689
- * @param {MoneyMarketEncodeWithdrawParams} params - The parameters for the supply transaction.
690
- * @param {Address} lendingPool - The address of the lending pool contract.
691
- * @returns {EvmContractCall} The encoded contract call.
692
- */
693
- static encodeSupply(params, lendingPool) {
694
- return {
695
- address: lendingPool,
696
- value: 0n,
697
- data: encodeFunctionData({
698
- abi: poolAbi,
699
- functionName: 'supply',
700
- args: [params.asset, params.amount, params.onBehalfOf, params.referralCode],
701
- }),
702
- };
703
- }
704
- /**
705
- * Encodes a withdraw transaction from a pool.
706
- * @param {MoneyMarketEncodeWithdrawParams} params - The parameters for the withdraw transaction.
707
- * @param {Address} params.asset - The address of the asset to withdraw.
708
- * @param {bigint} params.amount - The amount of the asset to withdraw.
709
- * @param {Address} params.to - The address that will receive the withdrawn assets.
710
- * @param {Address} lendingPool - The address of the lending pool contract.
711
- * @returns {EvmContractCall} The encoded contract call.
712
- */
713
- static encodeWithdraw(params, lendingPool) {
714
- return {
715
- address: lendingPool,
716
- value: 0n,
717
- data: encodeFunctionData({
718
- abi: poolAbi,
719
- functionName: 'withdraw',
720
- args: [params.asset, params.amount, params.to],
721
- }),
722
- };
723
- }
724
- /**
725
- * Encodes a borrow transaction from a pool.
726
- * @param {MoneyMarketEncodeBorrowParams} params - The parameters for the borrow transaction.
727
- * @param {Address} lendingPool - The address of the lending pool contract.
728
- * @returns {EvmContractCall} The encoded contract call.
729
- */
730
- static encodeBorrow(params, lendingPool) {
731
- return {
732
- address: lendingPool,
733
- value: 0n,
734
- data: encodeFunctionData({
735
- abi: poolAbi,
736
- functionName: 'borrow',
737
- args: [params.asset, params.amount, params.interestRateMode, params.referralCode, params.onBehalfOf],
738
- }),
739
- };
740
- }
741
- /**
742
- * Encodes a repay transaction for a pool.
743
- * @param {MoneyMarketEncodeRepayParams} params - The parameters for the repay transaction.
744
- * @param {Address} params.asset - The address of the borrowed asset to repay.
745
- * @param {bigint} params.amount - The amount to repay. Use type(uint256).max to repay the entire debt.
746
- * @param {number} params.interestRateMode - The interest rate mode (2 for Variable).
747
- * @param {Address} params.onBehalfOf - The address of the user who will get their debt reduced/removed.
748
- * @param {Address} lendingPool - The address of the lending pool contract.
749
- * @returns {EvmContractCall} The encoded contract call.
750
- */
751
- static encodeRepay(params, lendingPool) {
752
- return {
753
- address: lendingPool,
754
- value: 0n,
755
- data: encodeFunctionData({
756
- abi: poolAbi,
757
- functionName: 'repay',
758
- args: [params.asset, params.amount, params.interestRateMode, params.onBehalfOf],
759
- }),
760
- };
761
- }
762
- /**
763
- * Encodes a repayWithATokens transaction for a pool.
764
- * @param {MoneyMarketEncodeRepayWithATokensParams} params - The parameters for the repayWithATokens transaction.
765
- * @param {Address} lendingPool - The address of the lending pool contract.
766
- * @returns {EvmContractCall} The encoded contract call.
767
- */
768
- static encodeRepayWithATokens(params, lendingPool) {
769
- return {
770
- address: lendingPool,
771
- value: 0n,
772
- data: encodeFunctionData({
773
- abi: poolAbi,
774
- functionName: 'repayWithATokens',
775
- args: [params.asset, params.amount, params.interestRateMode],
776
- }),
777
- };
778
- }
779
- /**
780
- * Encodes a setUserUseReserveAsCollateral transaction.
781
- * @param asset - The address of the underlying asset to be used as collateral.
782
- * @param useAsCollateral - True to enable the asset as collateral, false to disable.
783
- * @param lendingPool - The address of lending pool contract
784
- * @returns The encoded contract call.
785
- */
786
- static encodeSetUserUseReserveAsCollateral(asset, useAsCollateral, lendingPool) {
787
- return {
788
- address: lendingPool,
789
- value: 0n,
790
- data: encodeFunctionData({
791
- abi: poolAbi,
792
- functionName: 'setUserUseReserveAsCollateral',
793
- args: [asset, useAsCollateral],
794
- }),
795
- };
796
- }
797
- /**
798
- * Get the list of all supported money market tokens (supply / borrow tokens) for a given spoke chain ID
799
- * @param chainId The chain ID
800
- * @returns {readonly Token[]} - Array of supported tokens
801
- */
802
- getSupportedTokens(chainId) {
803
- return getSupportedMoneyMarketTokens(chainId);
804
- }
805
- /**
806
- * Get the list of all supported money market reserves (supply / borrow reserves)
807
- * NOTE: reserve addresses are on the hub chain and can be of type vault, erc20, etc.
808
- * @returns {readonly Address[]} - Array of supported reserves
809
- */
810
- getSupportedReserves() {
811
- return [...moneyMarketReserveAssets];
812
- }
813
- }
814
- //# sourceMappingURL=MoneyMarketService.js.map