@scallop-io/sui-scallop-sdk 3.0.2 → 4.1.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 (283) hide show
  1. package/dist/Logger-Cg2iFdpH.d.ts +8 -0
  2. package/dist/builder.cjs +22 -0
  3. package/dist/builder.d.cts +28 -0
  4. package/dist/builder.d.ts +28 -0
  5. package/dist/builder.js +4 -0
  6. package/dist/chunk-33AK5RWM.js +15 -0
  7. package/dist/chunk-6UQ4NWEU.js +3 -0
  8. package/dist/chunk-DIC6P5HB.js +11 -0
  9. package/dist/chunk-S34VOEZN.js +3 -0
  10. package/dist/chunk-VTGQACWU.js +6 -0
  11. package/dist/client.cjs +21 -0
  12. package/dist/client.d.cts +401 -0
  13. package/dist/client.d.ts +401 -0
  14. package/dist/client.js +5 -0
  15. package/dist/errors.cjs +10 -0
  16. package/dist/errors.d.cts +32 -0
  17. package/dist/errors.d.ts +32 -0
  18. package/dist/errors.js +1 -0
  19. package/dist/index.cjs +51 -34
  20. package/dist/index.d.cts +130 -3145
  21. package/dist/index.d.ts +130 -3145
  22. package/dist/index.js +8 -12
  23. package/dist/logger.cjs +6 -0
  24. package/dist/logger.d.cts +7 -0
  25. package/dist/logger.d.ts +7 -0
  26. package/dist/logger.js +1 -0
  27. package/dist/query-DkitepDi.d.ts +3305 -0
  28. package/dist/query.cjs +19 -0
  29. package/dist/query.d.cts +9 -0
  30. package/dist/query.d.ts +9 -0
  31. package/dist/query.js +3 -0
  32. package/dist/types.cjs +2 -0
  33. package/dist/types.d.cts +90 -0
  34. package/dist/types.d.ts +90 -0
  35. package/dist/types.js +1 -0
  36. package/package.json +98 -56
  37. package/src/constants/api.ts +2 -0
  38. package/src/constants/coinType.ts +2 -0
  39. package/src/constants/common.ts +0 -7
  40. package/src/constants/index.ts +2 -0
  41. package/src/constants/queryKeys.ts +14 -7
  42. package/src/constants/testAddress.ts +1 -0
  43. package/src/datasources/api.ts +34 -0
  44. package/src/datasources/indexer.ts +8 -0
  45. package/src/datasources/onchain.ts +121 -0
  46. package/src/entries/builder.ts +5 -0
  47. package/src/entries/client.ts +3 -0
  48. package/src/entries/errors.ts +2 -0
  49. package/src/entries/index.ts +17 -0
  50. package/src/entries/logger.ts +2 -0
  51. package/src/entries/query.ts +6 -0
  52. package/src/entries/types.ts +2 -0
  53. package/src/errors/ScallopConfigError.ts +8 -0
  54. package/src/errors/ScallopError.ts +30 -0
  55. package/src/errors/ScallopIndexerError.ts +8 -0
  56. package/src/errors/ScallopParseError.ts +8 -0
  57. package/src/errors/ScallopRpcError.ts +8 -0
  58. package/src/errors/ScallopTransactionBuildError.ts +8 -0
  59. package/src/errors/index.ts +6 -0
  60. package/src/logger/Logger.ts +6 -0
  61. package/src/logger/consoleLogger.ts +13 -0
  62. package/src/logger/index.ts +3 -0
  63. package/src/logger/noopLogger.ts +8 -0
  64. package/src/mappers/index.ts +1 -0
  65. package/src/mappers/moveTypeMapper.ts +48 -0
  66. package/src/models/index.ts +8 -10
  67. package/src/models/interface.ts +9 -9
  68. package/src/models/scallop.ts +9 -16
  69. package/src/models/scallopAddress/const.ts +416 -0
  70. package/src/models/scallopAddress/index.ts +188 -0
  71. package/src/models/scallopAddress/types.ts +212 -0
  72. package/src/models/{scallopBuilder.ts → scallopBuilder/index.ts} +65 -29
  73. package/src/models/scallopBuilder/types.ts +37 -0
  74. package/src/models/{scallopClient.ts → scallopClient/index.ts} +157 -598
  75. package/src/models/scallopClient/types.ts +25 -0
  76. package/src/models/scallopConstants/config/AddressConfigSource.ts +13 -0
  77. package/src/models/scallopConstants/config/ConfigValidator.ts +70 -0
  78. package/src/models/scallopConstants/config/PoolAddressConfigSource.ts +12 -0
  79. package/src/models/scallopConstants/config/ScallopConfig.ts +80 -0
  80. package/src/models/scallopConstants/config/ScallopConfigSnapshot.ts +46 -0
  81. package/src/models/scallopConstants/config/WhitelistConfigSource.ts +16 -0
  82. package/src/models/scallopConstants/config/index.ts +6 -0
  83. package/src/models/scallopConstants/const.ts +19 -0
  84. package/src/models/scallopConstants/constantsSource.ts +98 -0
  85. package/src/models/scallopConstants/deriveConstants.ts +150 -0
  86. package/src/models/scallopConstants/index.ts +257 -0
  87. package/src/models/scallopConstants/loadConstantsState.ts +135 -0
  88. package/src/models/scallopConstants/types.ts +77 -0
  89. package/src/models/scallopConstants/utils.ts +69 -0
  90. package/src/models/scallopQuery/index.ts +1279 -0
  91. package/src/models/scallopQuery/types.ts +9 -0
  92. package/src/models/scallopQuery/utils.ts +13 -0
  93. package/src/models/scallopUtils/index.ts +505 -0
  94. package/src/models/scallopUtils/types.ts +30 -0
  95. package/src/models/transactionExecutor.ts +122 -0
  96. package/src/repositories/addressApi/helpers.ts +14 -0
  97. package/src/repositories/addressApi/index.ts +24 -0
  98. package/src/repositories/addressApi/schema.ts +0 -0
  99. package/src/repositories/addressApi/types.ts +177 -0
  100. package/src/repositories/base.ts +38 -0
  101. package/src/repositories/borrowIncentive/bcs.ts +11 -0
  102. package/src/repositories/borrowIncentive/helpers.ts +396 -0
  103. package/src/repositories/borrowIncentive/index.ts +52 -0
  104. package/src/{types/query/borrowIncentive.ts → repositories/borrowIncentive/types.ts} +69 -23
  105. package/src/repositories/borrowIncentive/utils.ts +234 -0
  106. package/src/repositories/cache.ts +4 -0
  107. package/src/repositories/coinBalance/helpers.ts +394 -0
  108. package/src/repositories/coinBalance/index.ts +69 -0
  109. package/src/repositories/coinBalance/types.ts +33 -0
  110. package/src/repositories/flashloan/const.ts +3 -0
  111. package/src/repositories/flashloan/helpers.ts +107 -0
  112. package/src/repositories/flashloan/index.ts +34 -0
  113. package/src/repositories/flashloan/types.ts +16 -0
  114. package/src/repositories/isolatedAssets/bcs.ts +4 -0
  115. package/src/repositories/isolatedAssets/const.ts +2 -0
  116. package/src/repositories/isolatedAssets/helpers.ts +91 -0
  117. package/src/repositories/isolatedAssets/index.ts +42 -0
  118. package/src/repositories/isolatedAssets/types.ts +35 -0
  119. package/src/repositories/loyaltyProgram/bcs.ts +7 -0
  120. package/src/repositories/loyaltyProgram/helpers.ts +117 -0
  121. package/src/repositories/loyaltyProgram/index.ts +28 -0
  122. package/src/repositories/loyaltyProgram/schema.ts +18 -0
  123. package/src/repositories/loyaltyProgram/types.ts +37 -0
  124. package/src/repositories/market/const.ts +6 -0
  125. package/src/repositories/market/helpers.ts +740 -0
  126. package/src/repositories/market/index.ts +137 -0
  127. package/src/repositories/market/mapper.ts +30 -0
  128. package/src/repositories/market/types.ts +443 -0
  129. package/src/repositories/market/utils.ts +285 -0
  130. package/src/repositories/obligation/helpers.ts +228 -0
  131. package/src/repositories/obligation/index.ts +51 -0
  132. package/src/repositories/obligation/types.ts +71 -0
  133. package/src/repositories/obligation/utils.ts +57 -0
  134. package/src/repositories/poolAddresses/const.ts +5 -0
  135. package/src/repositories/poolAddresses/helpers.ts +422 -0
  136. package/src/repositories/poolAddresses/index.ts +56 -0
  137. package/src/repositories/poolAddresses/schema.ts +34 -0
  138. package/src/repositories/poolAddresses/types.ts +69 -0
  139. package/src/repositories/price/const.ts +1 -0
  140. package/src/repositories/price/helpers.ts +237 -0
  141. package/src/repositories/price/index.ts +91 -0
  142. package/src/repositories/price/schema.ts +24 -0
  143. package/src/repositories/price/types.ts +56 -0
  144. package/src/repositories/price/utils.ts +21 -0
  145. package/src/repositories/referral/bcs.ts +3 -0
  146. package/src/repositories/referral/helper.ts +24 -0
  147. package/src/repositories/referral/index.ts +31 -0
  148. package/src/repositories/referral/types.ts +33 -0
  149. package/src/repositories/spool/const.ts +1 -0
  150. package/src/repositories/spool/helpers.ts +471 -0
  151. package/src/repositories/spool/index.ts +135 -0
  152. package/src/{types/query/spool.ts → repositories/spool/types.ts} +67 -2
  153. package/src/repositories/spool/utils.ts +215 -0
  154. package/src/repositories/types.ts +39 -0
  155. package/src/repositories/utils.ts +137 -0
  156. package/src/repositories/veSca/bcs.ts +6 -0
  157. package/src/repositories/veSca/helpers.ts +341 -0
  158. package/src/repositories/veSca/index.ts +57 -0
  159. package/src/repositories/veSca/types.ts +79 -0
  160. package/src/repositories/veScaLoyaltyProgram/bcs.ts +3 -0
  161. package/src/repositories/veScaLoyaltyProgram/helpers.ts +135 -0
  162. package/src/repositories/veScaLoyaltyProgram/index.ts +28 -0
  163. package/src/repositories/veScaLoyaltyProgram/schema.ts +15 -0
  164. package/src/repositories/veScaLoyaltyProgram/types.ts +47 -0
  165. package/src/repositories/wiring/datasources.ts +42 -0
  166. package/src/repositories/wiring/metadata.ts +292 -0
  167. package/src/repositories/wiring/registry.ts +194 -0
  168. package/src/repositories/wiring/source.ts +63 -0
  169. package/src/repositories/xOracle/bcs.ts +7 -0
  170. package/src/repositories/xOracle/const.ts +1 -0
  171. package/src/repositories/xOracle/helpers.ts +304 -0
  172. package/src/repositories/xOracle/index.ts +45 -0
  173. package/src/repositories/xOracle/types.ts +96 -0
  174. package/src/repositories/xOracle/utils.ts +3 -0
  175. package/src/services/client/BorrowService.ts +229 -0
  176. package/src/services/client/CollateralService.ts +76 -0
  177. package/src/services/client/LendingService.ts +75 -0
  178. package/src/services/client/ReferralService.ts +71 -0
  179. package/src/services/client/SpoolService.ts +170 -0
  180. package/src/services/client/VeScaService.ts +142 -0
  181. package/src/services/client/types.ts +44 -0
  182. package/src/services/index.ts +8 -0
  183. package/src/services/query/portfolioCalculations.ts +1171 -0
  184. package/src/txBuilders/borrowIncentive/index.ts +80 -0
  185. package/src/txBuilders/borrowIncentive/moveCalls.ts +124 -0
  186. package/src/txBuilders/borrowIncentive/quick.ts +207 -0
  187. package/src/txBuilders/context.ts +62 -0
  188. package/src/txBuilders/core/index.ts +95 -0
  189. package/src/txBuilders/core/moveCalls.ts +241 -0
  190. package/src/txBuilders/core/quick.ts +306 -0
  191. package/src/{builders → txBuilders}/index.ts +35 -9
  192. package/src/txBuilders/loyaltyProgram/index.ts +78 -0
  193. package/src/txBuilders/loyaltyProgram/moveCalls.ts +44 -0
  194. package/src/txBuilders/loyaltyProgram/quick.ts +58 -0
  195. package/src/txBuilders/manifest.ts +160 -0
  196. package/src/txBuilders/modules.ts +70 -0
  197. package/src/{builders → txBuilders}/oracles/index.ts +3 -2
  198. package/src/{builders → txBuilders}/oracles/pyth.ts +4 -3
  199. package/src/txBuilders/referral/index.ts +72 -0
  200. package/src/txBuilders/referral/moveCalls.ts +93 -0
  201. package/src/txBuilders/referral/quick.ts +57 -0
  202. package/src/txBuilders/sCoin/index.ts +72 -0
  203. package/src/txBuilders/sCoin/moveCalls.ts +36 -0
  204. package/src/txBuilders/sCoin/quick.ts +34 -0
  205. package/src/txBuilders/spool/index.ts +82 -0
  206. package/src/txBuilders/spool/moveCalls.ts +79 -0
  207. package/src/{builders/spoolBuilder.ts → txBuilders/spool/quick.ts} +22 -162
  208. package/src/txBuilders/utils.ts +36 -0
  209. package/src/txBuilders/verify.ts +45 -0
  210. package/src/txBuilders/vesca/index.ts +75 -0
  211. package/src/txBuilders/vesca/moveCalls.ts +147 -0
  212. package/src/{builders/vescaBuilder.ts → txBuilders/vesca/quick.ts} +85 -274
  213. package/src/types/address.ts +4 -178
  214. package/src/types/builder/borrowIncentive.ts +4 -3
  215. package/src/types/builder/core.ts +6 -33
  216. package/src/types/builder/index.ts +5 -1
  217. package/src/types/builder/loyaltyProgram.ts +4 -3
  218. package/src/types/builder/modules.ts +49 -0
  219. package/src/types/builder/referral.ts +5 -4
  220. package/src/types/builder/sCoin.ts +20 -2
  221. package/src/types/builder/spool.ts +22 -2
  222. package/src/types/builder/vesca.ts +4 -12
  223. package/src/types/constant/index.ts +3 -1
  224. package/src/types/constant/queryKeys.ts +15 -8
  225. package/src/types/index.ts +5 -6
  226. package/src/types/internal/dto.ts +46 -0
  227. package/src/types/internal/index.ts +2 -0
  228. package/src/types/internal/move.ts +13 -0
  229. package/src/types/public/index.ts +21 -0
  230. package/src/types/query/core.ts +0 -317
  231. package/src/types/query/index.ts +3 -4
  232. package/src/types/query/portfolio.ts +2 -15
  233. package/src/types/repositories/borrowIncentive.ts +12 -0
  234. package/src/types/repositories/index.ts +9 -0
  235. package/src/types/repositories/loyaltyProgram.ts +2 -0
  236. package/src/types/repositories/market.ts +13 -0
  237. package/src/types/repositories/spool.ts +21 -0
  238. package/src/types/repositories/veSca.ts +5 -0
  239. package/src/types/repositories/veScaLoyaltyProgram.ts +2 -0
  240. package/src/types/sui.ts +0 -1
  241. package/src/utils/array.ts +7 -0
  242. package/src/utils/cache.ts +17 -0
  243. package/src/utils/object.ts +30 -12
  244. package/src/utils/query.ts +9 -190
  245. package/src/utils/vesca.ts +0 -8
  246. package/src/builders/borrowIncentiveBuilder.ts +0 -372
  247. package/src/builders/coreBuilder.ts +0 -636
  248. package/src/builders/loyaltyProgramBuilder.ts +0 -142
  249. package/src/builders/referralBuilder.ts +0 -192
  250. package/src/builders/sCoinBuilder.ts +0 -124
  251. package/src/index.ts +0 -13
  252. package/src/models/scallopAddress.ts +0 -841
  253. package/src/models/scallopAxios.ts +0 -185
  254. package/src/models/scallopConstants.ts +0 -380
  255. package/src/models/scallopIndexer.ts +0 -226
  256. package/src/models/scallopQuery.ts +0 -974
  257. package/src/models/scallopQueryClient.ts +0 -29
  258. package/src/models/scallopSuiKit.ts +0 -480
  259. package/src/models/scallopUtils.ts +0 -734
  260. package/src/queries/borrowIncentiveQuery.ts +0 -361
  261. package/src/queries/borrowLimitQuery.ts +0 -33
  262. package/src/queries/coreQuery.ts +0 -1173
  263. package/src/queries/flashloanFeeQuery.ts +0 -92
  264. package/src/queries/index.ts +0 -15
  265. package/src/queries/isolatedAssetQuery.ts +0 -103
  266. package/src/queries/loyaltyProgramQuery.ts +0 -178
  267. package/src/queries/ownerQuery.ts +0 -32
  268. package/src/queries/poolAddressesQuery.ts +0 -340
  269. package/src/queries/portfolioQuery.ts +0 -1149
  270. package/src/queries/priceQuery.ts +0 -37
  271. package/src/queries/referralQuery.ts +0 -42
  272. package/src/queries/sCoinQuery.ts +0 -195
  273. package/src/queries/spoolQuery.ts +0 -542
  274. package/src/queries/supplyLimitQuery.ts +0 -32
  275. package/src/queries/switchboardQuery.ts +0 -65
  276. package/src/queries/vescaQuery.ts +0 -312
  277. package/src/queries/xOracleQuery.ts +0 -149
  278. package/src/types/constant/common.ts +0 -55
  279. package/src/types/query/loyaltyProgram.ts +0 -12
  280. package/src/types/query/vesca.ts +0 -27
  281. package/src/utils/index.ts +0 -7
  282. package/src/utils/indexer.ts +0 -47
  283. /package/src/{models → datasources}/rateLimiter.ts +0 -0
@@ -1,636 +0,0 @@
1
- import { Transaction } from '@mysten/sui/transactions';
2
- import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui/utils';
3
- import { SuiTxBlock as SuiKitTxBlock } from '@scallop-io/sui-kit';
4
- import { getObligations } from '../queries/index.js';
5
- import { updateOracles } from './oracles/index.js';
6
- import { requireSender } from '../utils/index.js';
7
- import type { SuiObjectArg } from '@scallop-io/sui-kit';
8
- import type { ScallopBuilder } from 'src/models/index.js';
9
- import type {
10
- CoreIds,
11
- GenerateCoreNormalMethod,
12
- GenerateCoreQuickMethod,
13
- SuiTxBlockWithCoreNormalMethods,
14
- CoreTxBlock,
15
- ScallopTxBlock,
16
- NestedResult,
17
- SuiTxBlockWithSpool,
18
- CoreNormalMethods,
19
- CoreQuickMethods,
20
- } from 'src/types/index.js';
21
-
22
- /**
23
- * Check and get Obligation information from transaction block.
24
- *
25
- * @description
26
- * If the obligation id is provided, directly return it.
27
- * If both obligation id and key is provided, directly return them.
28
- * Otherwise, automatically get obligation id and key from the sender.
29
- *
30
- * @param builder - Scallop builder instance.
31
- * @param txBlock - TxBlock created by SuiKit.
32
- * @param obligationId - Obligation id.
33
- * @param obligationKey - Obligation key.
34
- * @return Obligation id and key.
35
- */
36
- const requireObligationInfo = async (
37
- ...params: [
38
- builder: ScallopBuilder,
39
- txBlock: SuiKitTxBlock,
40
- obligationId?: SuiObjectArg,
41
- obligationKey?: SuiObjectArg,
42
- ]
43
- ) => {
44
- const [builder, txBlock, obligationId, obligationKey] = params;
45
- if (params.length === 3 && obligationId) return { obligationId };
46
- if (params.length === 4 && obligationId && obligationKey)
47
- return { obligationId, obligationKey };
48
- const sender = requireSender(txBlock);
49
- const obligations = await getObligations(builder, sender);
50
- if (obligations.length === 0) {
51
- throw new Error(`No obligation found for sender ${sender}`);
52
- }
53
- return {
54
- obligationId: obligations[0].id,
55
- obligationKey: obligations[0].keyId,
56
- };
57
- };
58
-
59
- /**
60
- * Generate core normal methods.
61
- *
62
- * @param builder - Scallop builder instance.
63
- * @param txBlock - TxBlock created by SuiKit.
64
- * @return Core normal methods.
65
- */
66
- const generateCoreNormalMethod: GenerateCoreNormalMethod = ({
67
- builder,
68
- txBlock,
69
- }) => {
70
- const coreIds: CoreIds = {
71
- protocolPkg: builder.address.get('core.packages.protocol.id'),
72
- market: builder.address.get('core.market'),
73
- version: builder.address.get('core.version'),
74
- coinDecimalsRegistry: builder.address.get('core.coinDecimalsRegistry'),
75
- xOracle: builder.address.get('core.oracles.xOracle'),
76
- };
77
-
78
- const referralPkgId = builder.address.get('referral.id');
79
- const referralWitnessType = `${referralPkgId}::scallop_referral_program::REFERRAL_WITNESS`;
80
- const clockObjectRef = txBlock.sharedObjectRef({
81
- objectId: SUI_CLOCK_OBJECT_ID,
82
- mutable: false,
83
- initialSharedVersion: '1',
84
- });
85
-
86
- const depositCollateral: CoreNormalMethods['depositCollateral'] = (
87
- obligation,
88
- coin,
89
- collateralCoinName
90
- ) => {
91
- const coinType = builder.utils.parseCoinType(collateralCoinName);
92
- builder.moveCall(
93
- txBlock,
94
- `${coreIds.protocolPkg}::deposit_collateral::deposit_collateral`,
95
- [coreIds.version, obligation, coreIds.market, coin],
96
- [coinType]
97
- );
98
- };
99
-
100
- const supply: CoreNormalMethods['supply'] = (coin, poolCoinName) => {
101
- const coinType = builder.utils.parseCoinType(poolCoinName);
102
- return builder.moveCall(
103
- txBlock,
104
- `${coreIds.protocolPkg}::mint::mint`,
105
- [coreIds.version, coreIds.market, coin, clockObjectRef],
106
- [coinType]
107
- );
108
- };
109
-
110
- return {
111
- openObligation: () => {
112
- const [obligation, obligationKey, obligationHotPotato] = builder.moveCall(
113
- txBlock,
114
- `${coreIds.protocolPkg}::open_obligation::open_obligation`,
115
- [coreIds.version]
116
- );
117
- return [obligation, obligationKey, obligationHotPotato] as [
118
- NestedResult,
119
- NestedResult,
120
- NestedResult,
121
- ];
122
- },
123
- returnObligation: (obligation, obligationHotPotato) => {
124
- builder.moveCall(
125
- txBlock,
126
- `${coreIds.protocolPkg}::open_obligation::return_obligation`,
127
- [coreIds.version, obligation, obligationHotPotato]
128
- );
129
- },
130
- openObligationEntry: () => {
131
- builder.moveCall(
132
- txBlock,
133
- `${coreIds.protocolPkg}::open_obligation::open_obligation_entry`,
134
- [coreIds.version]
135
- );
136
- },
137
- /**
138
- * @deprecated Use {@link depositCollateral} instead.
139
- */
140
- addCollateral: depositCollateral,
141
- depositCollateral,
142
- takeCollateral: (obligation, obligationKey, amount, collateralCoinName) => {
143
- const coinType = builder.utils.parseCoinType(collateralCoinName);
144
- // return await builder.moveCall(
145
- return builder.moveCall(
146
- txBlock,
147
- `${coreIds.protocolPkg}::withdraw_collateral::withdraw_collateral`,
148
- [
149
- coreIds.version,
150
- obligation,
151
- obligationKey,
152
- coreIds.market,
153
- coreIds.coinDecimalsRegistry,
154
- txBlock.pure.u64(amount),
155
- coreIds.xOracle,
156
- clockObjectRef,
157
- ],
158
- [coinType]
159
- );
160
- },
161
- /**
162
- * @deprecated Use {@link supply} instead.
163
- */
164
- deposit: supply,
165
- supply,
166
- depositEntry: (coin, poolCoinName) => {
167
- const coinType = builder.utils.parseCoinType(poolCoinName);
168
- // return await builder.moveCall(
169
- return builder.moveCall(
170
- txBlock,
171
- `${coreIds.protocolPkg}::mint::mint_entry`,
172
- [coreIds.version, coreIds.market, coin, clockObjectRef],
173
- [coinType]
174
- );
175
- },
176
- withdraw: (marketCoin, poolCoinName) => {
177
- const coinType = builder.utils.parseCoinType(poolCoinName);
178
- // return await builder.moveCall(
179
- return builder.moveCall(
180
- txBlock,
181
- `${coreIds.protocolPkg}::redeem::redeem`,
182
- [coreIds.version, coreIds.market, marketCoin, clockObjectRef],
183
- [coinType]
184
- );
185
- },
186
- withdrawEntry: (marketCoin, poolCoinName) => {
187
- const coinType = builder.utils.parseCoinType(poolCoinName);
188
- // return await builder.moveCall(
189
- return builder.moveCall(
190
- txBlock,
191
- `${coreIds.protocolPkg}::redeem::redeem_entry`,
192
- [coreIds.version, coreIds.market, marketCoin, clockObjectRef],
193
- [coinType]
194
- );
195
- },
196
- borrow: (obligation, obligationKey, amount, poolCoinName) => {
197
- const coinType = builder.utils.parseCoinType(poolCoinName);
198
- // return await builder.moveCall(
199
- return builder.moveCall(
200
- txBlock,
201
- `${coreIds.protocolPkg}::borrow::borrow`,
202
- [
203
- coreIds.version,
204
- obligation,
205
- obligationKey,
206
- coreIds.market,
207
- coreIds.coinDecimalsRegistry,
208
- amount,
209
- coreIds.xOracle,
210
- clockObjectRef,
211
- ],
212
- [coinType]
213
- );
214
- },
215
- borrowWithReferral: (
216
- obligation,
217
- obligationKey,
218
- borrowReferral,
219
- amount,
220
- poolCoinName
221
- ) => {
222
- const coinType = builder.utils.parseCoinType(poolCoinName);
223
- // return await builder.moveCall(
224
- return builder.moveCall(
225
- txBlock,
226
- `${coreIds.protocolPkg}::borrow::borrow_with_referral`,
227
- [
228
- coreIds.version,
229
- obligation,
230
- obligationKey,
231
- coreIds.market,
232
- coreIds.coinDecimalsRegistry,
233
- borrowReferral,
234
- typeof amount === 'number' ? txBlock.pure.u64(amount) : amount,
235
- coreIds.xOracle,
236
- clockObjectRef,
237
- ],
238
- [coinType, referralWitnessType]
239
- );
240
- },
241
- borrowEntry: (obligation, obligationKey, amount, poolCoinName) => {
242
- const coinType = builder.utils.parseCoinType(poolCoinName);
243
- // return await builder.moveCall(
244
- return builder.moveCall(
245
- txBlock,
246
- `${coreIds.protocolPkg}::borrow::borrow_entry`,
247
- [
248
- coreIds.version,
249
- obligation,
250
- obligationKey,
251
- coreIds.market,
252
- coreIds.coinDecimalsRegistry,
253
- txBlock.pure.u64(amount),
254
- coreIds.xOracle,
255
- clockObjectRef,
256
- ],
257
- [coinType]
258
- );
259
- },
260
- repay: (obligation, coin, poolCoinName) => {
261
- const coinType = builder.utils.parseCoinType(poolCoinName);
262
- builder.moveCall(
263
- txBlock,
264
- `${coreIds.protocolPkg}::repay::repay`,
265
- [coreIds.version, obligation, coreIds.market, coin, clockObjectRef],
266
- [coinType]
267
- );
268
- },
269
- borrowFlashLoan: (amount, poolCoinName) => {
270
- const coinType = builder.utils.parseCoinType(poolCoinName);
271
- return builder.moveCall(
272
- txBlock,
273
- `${coreIds.protocolPkg}::flash_loan::borrow_flash_loan`,
274
- [coreIds.version, coreIds.market, amount],
275
- [coinType]
276
- );
277
- },
278
- repayFlashLoan: (coin, loan, poolCoinName) => {
279
- const coinType = builder.utils.parseCoinType(poolCoinName);
280
- builder.moveCall(
281
- txBlock,
282
- `${coreIds.protocolPkg}::flash_loan::repay_flash_loan`,
283
- [coreIds.version, coreIds.market, coin, loan],
284
- [coinType]
285
- );
286
- },
287
- liquidate: (obligation, coin, debtCoinName, collateralCoinName) => {
288
- const debtCoinType = builder.utils.parseCoinType(debtCoinName);
289
- const collateralCoinType =
290
- builder.utils.parseCoinType(collateralCoinName);
291
- const [debtCoin, collateralCoin] = builder.moveCall(
292
- txBlock,
293
- `${coreIds.protocolPkg}::liquidate::liquidate`,
294
- [
295
- coreIds.version,
296
- obligation,
297
- coreIds.market,
298
- coin,
299
- coreIds.coinDecimalsRegistry,
300
- coreIds.xOracle,
301
- clockObjectRef,
302
- ],
303
- [debtCoinType, collateralCoinType]
304
- );
305
-
306
- return [debtCoin, collateralCoin] as [NestedResult, NestedResult];
307
- },
308
- };
309
- };
310
-
311
- /**
312
- * Generate core quick methods.
313
- *
314
- * @description
315
- * The quick methods are the same as the normal methods, but they will automatically
316
- * help users organize transaction blocks, include query obligation info, and transfer
317
- * coins to the sender. So, they are all asynchronous methods.
318
- *
319
- * @param builder - Scallop builder instance.
320
- * @param txBlock - TxBlock created by SuiKit.
321
- * @return Core quick methods.
322
- */
323
- const generateCoreQuickMethod: GenerateCoreQuickMethod = ({
324
- builder,
325
- txBlock,
326
- }) => {
327
- const supplyQuick: CoreQuickMethods['supplyQuick'] = async (
328
- amount,
329
- poolCoinName,
330
- returnSCoin = true,
331
- isSponsoredTx = false
332
- ) => {
333
- const sender = requireSender(txBlock);
334
- const { leftCoin, takeCoin } = await builder.selectCoin(
335
- txBlock,
336
- poolCoinName,
337
- amount,
338
- sender,
339
- isSponsoredTx
340
- );
341
- if (leftCoin) {
342
- txBlock.transferObjects([leftCoin], sender);
343
- }
344
- const marketCoinDeposit = txBlock.supply(takeCoin, poolCoinName);
345
-
346
- // convert to sCoin
347
- return returnSCoin
348
- ? txBlock.mintSCoin(
349
- builder.utils.parseMarketCoinName(poolCoinName),
350
- marketCoinDeposit
351
- )
352
- : marketCoinDeposit;
353
- };
354
- const depositCollateralQuick: CoreQuickMethods['depositCollateralQuick'] =
355
- async (amount, collateralCoinName, obligationId, isSponsoredTx = false) => {
356
- const sender = requireSender(txBlock);
357
- const { obligationId: obligationArg } = await requireObligationInfo(
358
- builder,
359
- txBlock,
360
- obligationId
361
- );
362
-
363
- const { takeCoin, leftCoin } = await builder.selectCoin(
364
- txBlock,
365
- collateralCoinName,
366
- amount,
367
- sender,
368
- isSponsoredTx
369
- );
370
-
371
- if (leftCoin) {
372
- txBlock.transferObjects([leftCoin], sender);
373
- }
374
- txBlock.depositCollateral(obligationArg, takeCoin, collateralCoinName);
375
- };
376
-
377
- return {
378
- /**
379
- * @deprecated Use {@link depositCollateralQuick} instead.
380
- */
381
- addCollateralQuick: depositCollateralQuick,
382
- depositCollateralQuick,
383
- takeCollateralQuick: async (
384
- amount,
385
- collateralCoinName,
386
- obligationId,
387
- obligationKey,
388
- updateOracleOptions
389
- ) => {
390
- const obligationInfo = await requireObligationInfo(
391
- builder,
392
- txBlock,
393
- obligationId,
394
- obligationKey
395
- );
396
- const updateCoinNames = await builder.utils.getObligationCoinNames(
397
- obligationInfo.obligationId
398
- );
399
- await updateOracles(
400
- builder,
401
- txBlock,
402
- updateCoinNames,
403
- updateOracleOptions
404
- );
405
- return txBlock.takeCollateral(
406
- obligationInfo.obligationId,
407
- obligationInfo.obligationKey as SuiObjectArg,
408
- amount,
409
- collateralCoinName
410
- );
411
- },
412
- /**
413
- * @deprecated Use {@link supplyQuick} instead.
414
- */
415
- depositQuick: supplyQuick,
416
- supplyQuick,
417
- withdrawQuick: async (amount, poolCoinName) => {
418
- const sender = requireSender(txBlock);
419
- const sCoinName = builder.utils.parseSCoinName(poolCoinName);
420
- if (!sCoinName) throw new Error(`No sCoin for ${poolCoinName}`);
421
-
422
- // eslint-disable-next-line prefer-const
423
- let { sCoin, marketCoin } = await builder.selectSCoinOrMarketCoin(
424
- txBlock,
425
- sCoinName,
426
- amount,
427
- sender
428
- );
429
-
430
- if (sCoin) {
431
- const newMarketCoin = txBlock.burnSCoin(sCoinName, sCoin);
432
- if (marketCoin) {
433
- txBlock.mergeCoins(marketCoin, [newMarketCoin]);
434
- } else {
435
- marketCoin = newMarketCoin;
436
- }
437
- }
438
-
439
- if (!marketCoin) throw new Error(`No market coin for ${poolCoinName}`);
440
- return txBlock.withdraw(marketCoin, poolCoinName);
441
- },
442
- borrowQuick: async (
443
- amount,
444
- poolCoinName,
445
- obligationId,
446
- obligationKey,
447
- updateOracleOptions
448
- ) => {
449
- const obligationInfo = await requireObligationInfo(
450
- builder,
451
- txBlock,
452
- obligationId,
453
- obligationKey
454
- );
455
- const obligationCoinNames =
456
- (await builder.utils.getObligationCoinNames(
457
- obligationInfo.obligationId
458
- )) ?? [];
459
- const updateCoinNames = [...obligationCoinNames, poolCoinName];
460
- await updateOracles(
461
- builder,
462
- txBlock,
463
- updateCoinNames,
464
- updateOracleOptions
465
- );
466
- return txBlock.borrow(
467
- obligationInfo.obligationId,
468
- obligationInfo.obligationKey as SuiObjectArg,
469
- amount,
470
- poolCoinName
471
- );
472
- },
473
- borrowWithReferralQuick: async (
474
- amount,
475
- poolCoinName,
476
- borrowReferral,
477
- obligationId,
478
- obligationKey,
479
- updateOracleOptions
480
- ) => {
481
- const obligationInfo = await requireObligationInfo(
482
- builder,
483
- txBlock,
484
- obligationId,
485
- obligationKey
486
- );
487
- const obligationCoinNames =
488
- (await builder.utils.getObligationCoinNames(
489
- obligationInfo.obligationId
490
- )) ?? [];
491
- const updateCoinNames = [...obligationCoinNames, poolCoinName];
492
- await updateOracles(
493
- builder,
494
- txBlock,
495
- updateCoinNames,
496
- updateOracleOptions
497
- );
498
- return txBlock.borrowWithReferral(
499
- obligationInfo.obligationId,
500
- obligationInfo.obligationKey as SuiObjectArg,
501
- borrowReferral,
502
- amount,
503
- poolCoinName
504
- );
505
- },
506
- repayQuick: async (
507
- amount,
508
- poolCoinName,
509
- obligationId,
510
- isSponsoredTx = false
511
- ) => {
512
- const sender = requireSender(txBlock);
513
- const obligationInfo = await requireObligationInfo(
514
- builder,
515
- txBlock,
516
- obligationId
517
- );
518
-
519
- const { leftCoin, takeCoin } = await builder.selectCoin(
520
- txBlock,
521
- poolCoinName,
522
- amount,
523
- sender,
524
- isSponsoredTx
525
- );
526
- if (leftCoin) txBlock.transferObjects([leftCoin], sender);
527
- return txBlock.repay(obligationInfo.obligationId, takeCoin, poolCoinName);
528
- },
529
- updateAssetPricesQuick: async (assetCoinNames, updateOracleOptions) => {
530
- return await updateOracles(
531
- builder,
532
- txBlock,
533
- assetCoinNames,
534
- updateOracleOptions
535
- );
536
- },
537
- liquidateQuick: async (
538
- amount,
539
- debtCoinName,
540
- collateralCoinName,
541
- obligationId,
542
- updateOracleOptions
543
- ) => {
544
- const sender = requireSender(txBlock);
545
-
546
- // Update oracle prices for debt and collateral coins
547
- const updateCoinNames =
548
- await builder.utils.getObligationCoinNames(obligationId);
549
-
550
- await updateOracles(
551
- builder,
552
- txBlock,
553
- updateCoinNames,
554
- updateOracleOptions
555
- );
556
-
557
- // Select coins for liquidation
558
- const { takeCoin, leftCoin } = await builder.selectCoin(
559
- txBlock,
560
- debtCoinName,
561
- amount,
562
- sender,
563
- updateOracleOptions?.isSponsoredTx ?? false
564
- );
565
-
566
- if (leftCoin) {
567
- txBlock.transferObjects([leftCoin], sender);
568
- }
569
-
570
- // Convert obligation to SharedObjectRef format
571
- const obligationSharedObject =
572
- typeof obligationId === 'string'
573
- ? txBlock.object(obligationId)
574
- : obligationId;
575
-
576
- // Execute liquidation
577
- return txBlock.liquidate(
578
- obligationSharedObject,
579
- takeCoin,
580
- debtCoinName,
581
- collateralCoinName
582
- );
583
- },
584
- };
585
- };
586
-
587
- /**
588
- * Create an enhanced transaction block instance for interaction with core modules of the Scallop contract.
589
- *
590
- * @param builder - Scallop builder instance.
591
- * @param initTxBlock - Scallop txBlock, txBlock created by SuiKit, or original transaction block.
592
- * @return Scallop core txBlock.
593
- */
594
- export const newCoreTxBlock = (
595
- builder: ScallopBuilder,
596
- initTxBlock?:
597
- | ScallopTxBlock
598
- | SuiKitTxBlock
599
- | Transaction
600
- | SuiTxBlockWithSpool
601
- ) => {
602
- const txBlock =
603
- initTxBlock instanceof Transaction
604
- ? new SuiKitTxBlock(initTxBlock)
605
- : initTxBlock
606
- ? initTxBlock
607
- : new SuiKitTxBlock();
608
-
609
- const normalMethod = generateCoreNormalMethod({
610
- builder,
611
- txBlock,
612
- });
613
-
614
- const normalTxBlock = new Proxy(txBlock, {
615
- get: (target, prop) => {
616
- if (prop in normalMethod) {
617
- return Reflect.get(normalMethod, prop);
618
- }
619
- return Reflect.get(target, prop);
620
- },
621
- }) as SuiTxBlockWithCoreNormalMethods;
622
-
623
- const quickMethod = generateCoreQuickMethod({
624
- builder,
625
- txBlock: normalTxBlock,
626
- });
627
-
628
- return new Proxy(normalTxBlock, {
629
- get: (target, prop) => {
630
- if (prop in quickMethod) {
631
- return Reflect.get(quickMethod, prop);
632
- }
633
- return Reflect.get(target, prop);
634
- },
635
- }) as CoreTxBlock;
636
- };