@zofai/zo-sdk 0.1.92

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 (275) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/.gitattributes +4 -0
  3. package/.prettierrc.js +9 -0
  4. package/README.md +28 -0
  5. package/dist/abstract/BaseAPI.cjs +206 -0
  6. package/dist/abstract/BaseAPI.cjs.map +1 -0
  7. package/dist/abstract/BaseAPI.d.cts +172 -0
  8. package/dist/abstract/BaseAPI.d.cts.map +1 -0
  9. package/dist/abstract/BaseAPI.d.mts +172 -0
  10. package/dist/abstract/BaseAPI.d.mts.map +1 -0
  11. package/dist/abstract/BaseAPI.mjs +202 -0
  12. package/dist/abstract/BaseAPI.mjs.map +1 -0
  13. package/dist/abstract/BaseDataAPI.cjs +140 -0
  14. package/dist/abstract/BaseDataAPI.cjs.map +1 -0
  15. package/dist/abstract/BaseDataAPI.d.cts +89 -0
  16. package/dist/abstract/BaseDataAPI.d.cts.map +1 -0
  17. package/dist/abstract/BaseDataAPI.d.mts +89 -0
  18. package/dist/abstract/BaseDataAPI.d.mts.map +1 -0
  19. package/dist/abstract/BaseDataAPI.mjs +136 -0
  20. package/dist/abstract/BaseDataAPI.mjs.map +1 -0
  21. package/dist/abstract/index.cjs +12 -0
  22. package/dist/abstract/index.cjs.map +1 -0
  23. package/dist/abstract/index.d.cts +7 -0
  24. package/dist/abstract/index.d.cts.map +1 -0
  25. package/dist/abstract/index.d.mts +7 -0
  26. package/dist/abstract/index.d.mts.map +1 -0
  27. package/dist/abstract/index.mjs +7 -0
  28. package/dist/abstract/index.mjs.map +1 -0
  29. package/dist/api.cjs +779 -0
  30. package/dist/api.cjs.map +1 -0
  31. package/dist/api.d.cts +75 -0
  32. package/dist/api.d.cts.map +1 -0
  33. package/dist/api.d.mts +75 -0
  34. package/dist/api.d.mts.map +1 -0
  35. package/dist/api.mjs +775 -0
  36. package/dist/api.mjs.map +1 -0
  37. package/dist/bcs.cjs +42 -0
  38. package/dist/bcs.cjs.map +1 -0
  39. package/dist/bcs.d.cts +91 -0
  40. package/dist/bcs.d.cts.map +1 -0
  41. package/dist/bcs.d.mts +91 -0
  42. package/dist/bcs.d.mts.map +1 -0
  43. package/dist/bcs.mjs +39 -0
  44. package/dist/bcs.mjs.map +1 -0
  45. package/dist/consts/deployments-shared-mainnet.json +50 -0
  46. package/dist/consts/deployments-shared-testnet.json +45 -0
  47. package/dist/consts/deployments-slp-mainnet.json +600 -0
  48. package/dist/consts/deployments-slp-testnet.json +87 -0
  49. package/dist/consts/deployments-usdz-mainnet.json +494 -0
  50. package/dist/consts/deployments-usdz-testnet.json +98 -0
  51. package/dist/consts/deployments-zbtcvc-mainnet.json +180 -0
  52. package/dist/consts/deployments-zlp-mainnet.json +791 -0
  53. package/dist/consts/deployments-zlp-testnet.json +76 -0
  54. package/dist/consts/index.cjs +200 -0
  55. package/dist/consts/index.cjs.map +1 -0
  56. package/dist/consts/index.d.cts +157 -0
  57. package/dist/consts/index.d.cts.map +1 -0
  58. package/dist/consts/index.d.mts +157 -0
  59. package/dist/consts/index.d.mts.map +1 -0
  60. package/dist/consts/index.mjs +189 -0
  61. package/dist/consts/index.mjs.map +1 -0
  62. package/dist/consts/price_id_to_object_id.mainnet.json +56 -0
  63. package/dist/consts/price_id_to_object_id.testnet.json +17 -0
  64. package/dist/data.cjs +919 -0
  65. package/dist/data.cjs.map +1 -0
  66. package/dist/data.d.cts +235 -0
  67. package/dist/data.d.cts.map +1 -0
  68. package/dist/data.d.mts +235 -0
  69. package/dist/data.d.mts.map +1 -0
  70. package/dist/data.mjs +915 -0
  71. package/dist/data.mjs.map +1 -0
  72. package/dist/factory/SDKFactory.cjs +228 -0
  73. package/dist/factory/SDKFactory.cjs.map +1 -0
  74. package/dist/factory/SDKFactory.d.cts +84 -0
  75. package/dist/factory/SDKFactory.d.cts.map +1 -0
  76. package/dist/factory/SDKFactory.d.mts +84 -0
  77. package/dist/factory/SDKFactory.d.mts.map +1 -0
  78. package/dist/factory/SDKFactory.mjs +222 -0
  79. package/dist/factory/SDKFactory.mjs.map +1 -0
  80. package/dist/implementations/SLPAPI.cjs +1794 -0
  81. package/dist/implementations/SLPAPI.cjs.map +1 -0
  82. package/dist/implementations/SLPAPI.d.cts +183 -0
  83. package/dist/implementations/SLPAPI.d.cts.map +1 -0
  84. package/dist/implementations/SLPAPI.d.mts +183 -0
  85. package/dist/implementations/SLPAPI.d.mts.map +1 -0
  86. package/dist/implementations/SLPAPI.mjs +1790 -0
  87. package/dist/implementations/SLPAPI.mjs.map +1 -0
  88. package/dist/implementations/SLPDataAPI.cjs +1384 -0
  89. package/dist/implementations/SLPDataAPI.cjs.map +1 -0
  90. package/dist/implementations/SLPDataAPI.d.cts +158 -0
  91. package/dist/implementations/SLPDataAPI.d.cts.map +1 -0
  92. package/dist/implementations/SLPDataAPI.d.mts +158 -0
  93. package/dist/implementations/SLPDataAPI.d.mts.map +1 -0
  94. package/dist/implementations/SLPDataAPI.mjs +1380 -0
  95. package/dist/implementations/SLPDataAPI.mjs.map +1 -0
  96. package/dist/implementations/USDZAPI.cjs +1676 -0
  97. package/dist/implementations/USDZAPI.cjs.map +1 -0
  98. package/dist/implementations/USDZAPI.d.cts +180 -0
  99. package/dist/implementations/USDZAPI.d.cts.map +1 -0
  100. package/dist/implementations/USDZAPI.d.mts +180 -0
  101. package/dist/implementations/USDZAPI.d.mts.map +1 -0
  102. package/dist/implementations/USDZAPI.mjs +1672 -0
  103. package/dist/implementations/USDZAPI.mjs.map +1 -0
  104. package/dist/implementations/USDZDataAPI.cjs +1209 -0
  105. package/dist/implementations/USDZDataAPI.cjs.map +1 -0
  106. package/dist/implementations/USDZDataAPI.d.cts +191 -0
  107. package/dist/implementations/USDZDataAPI.d.cts.map +1 -0
  108. package/dist/implementations/USDZDataAPI.d.mts +191 -0
  109. package/dist/implementations/USDZDataAPI.d.mts.map +1 -0
  110. package/dist/implementations/USDZDataAPI.mjs +1205 -0
  111. package/dist/implementations/USDZDataAPI.mjs.map +1 -0
  112. package/dist/implementations/ZBTCVCAPI.cjs +906 -0
  113. package/dist/implementations/ZBTCVCAPI.cjs.map +1 -0
  114. package/dist/implementations/ZBTCVCAPI.d.cts +107 -0
  115. package/dist/implementations/ZBTCVCAPI.d.cts.map +1 -0
  116. package/dist/implementations/ZBTCVCAPI.d.mts +107 -0
  117. package/dist/implementations/ZBTCVCAPI.d.mts.map +1 -0
  118. package/dist/implementations/ZBTCVCAPI.mjs +902 -0
  119. package/dist/implementations/ZBTCVCAPI.mjs.map +1 -0
  120. package/dist/implementations/ZBTCVCDataAPI.cjs +829 -0
  121. package/dist/implementations/ZBTCVCDataAPI.cjs.map +1 -0
  122. package/dist/implementations/ZBTCVCDataAPI.d.cts +94 -0
  123. package/dist/implementations/ZBTCVCDataAPI.d.cts.map +1 -0
  124. package/dist/implementations/ZBTCVCDataAPI.d.mts +94 -0
  125. package/dist/implementations/ZBTCVCDataAPI.d.mts.map +1 -0
  126. package/dist/implementations/ZBTCVCDataAPI.mjs +825 -0
  127. package/dist/implementations/ZBTCVCDataAPI.mjs.map +1 -0
  128. package/dist/implementations/ZLPAPI.cjs +1948 -0
  129. package/dist/implementations/ZLPAPI.cjs.map +1 -0
  130. package/dist/implementations/ZLPAPI.d.cts +192 -0
  131. package/dist/implementations/ZLPAPI.d.cts.map +1 -0
  132. package/dist/implementations/ZLPAPI.d.mts +192 -0
  133. package/dist/implementations/ZLPAPI.d.mts.map +1 -0
  134. package/dist/implementations/ZLPAPI.mjs +1944 -0
  135. package/dist/implementations/ZLPAPI.mjs.map +1 -0
  136. package/dist/implementations/ZLPDataAPI.cjs +1267 -0
  137. package/dist/implementations/ZLPDataAPI.cjs.map +1 -0
  138. package/dist/implementations/ZLPDataAPI.d.cts +193 -0
  139. package/dist/implementations/ZLPDataAPI.d.cts.map +1 -0
  140. package/dist/implementations/ZLPDataAPI.d.mts +193 -0
  141. package/dist/implementations/ZLPDataAPI.d.mts.map +1 -0
  142. package/dist/implementations/ZLPDataAPI.mjs +1263 -0
  143. package/dist/implementations/ZLPDataAPI.mjs.map +1 -0
  144. package/dist/implementations/index.cjs +26 -0
  145. package/dist/implementations/index.cjs.map +1 -0
  146. package/dist/implementations/index.d.cts +13 -0
  147. package/dist/implementations/index.d.cts.map +1 -0
  148. package/dist/implementations/index.d.mts +13 -0
  149. package/dist/implementations/index.d.mts.map +1 -0
  150. package/dist/implementations/index.mjs +15 -0
  151. package/dist/implementations/index.mjs.map +1 -0
  152. package/dist/index.cjs +69 -0
  153. package/dist/index.cjs.map +1 -0
  154. package/dist/index.d.cts +51 -0
  155. package/dist/index.d.cts.map +1 -0
  156. package/dist/index.d.mts +51 -0
  157. package/dist/index.d.mts.map +1 -0
  158. package/dist/index.mjs +51 -0
  159. package/dist/index.mjs.map +1 -0
  160. package/dist/interfaces/base.cjs +7 -0
  161. package/dist/interfaces/base.cjs.map +1 -0
  162. package/dist/interfaces/base.d.cts +346 -0
  163. package/dist/interfaces/base.d.cts.map +1 -0
  164. package/dist/interfaces/base.d.mts +346 -0
  165. package/dist/interfaces/base.d.mts.map +1 -0
  166. package/dist/interfaces/base.mjs +6 -0
  167. package/dist/interfaces/base.mjs.map +1 -0
  168. package/dist/interfaces/index.cjs +31 -0
  169. package/dist/interfaces/index.cjs.map +1 -0
  170. package/dist/interfaces/index.d.cts +15 -0
  171. package/dist/interfaces/index.d.cts.map +1 -0
  172. package/dist/interfaces/index.d.mts +15 -0
  173. package/dist/interfaces/index.d.mts.map +1 -0
  174. package/dist/interfaces/index.mjs +15 -0
  175. package/dist/interfaces/index.mjs.map +1 -0
  176. package/dist/interfaces/slp.cjs +7 -0
  177. package/dist/interfaces/slp.cjs.map +1 -0
  178. package/dist/interfaces/slp.d.cts +179 -0
  179. package/dist/interfaces/slp.d.cts.map +1 -0
  180. package/dist/interfaces/slp.d.mts +179 -0
  181. package/dist/interfaces/slp.d.mts.map +1 -0
  182. package/dist/interfaces/slp.mjs +6 -0
  183. package/dist/interfaces/slp.mjs.map +1 -0
  184. package/dist/interfaces/usdz.cjs +7 -0
  185. package/dist/interfaces/usdz.cjs.map +1 -0
  186. package/dist/interfaces/usdz.d.cts +104 -0
  187. package/dist/interfaces/usdz.d.cts.map +1 -0
  188. package/dist/interfaces/usdz.d.mts +104 -0
  189. package/dist/interfaces/usdz.d.mts.map +1 -0
  190. package/dist/interfaces/usdz.mjs +6 -0
  191. package/dist/interfaces/usdz.mjs.map +1 -0
  192. package/dist/interfaces/zbtcvc.cjs +7 -0
  193. package/dist/interfaces/zbtcvc.cjs.map +1 -0
  194. package/dist/interfaces/zbtcvc.d.cts +64 -0
  195. package/dist/interfaces/zbtcvc.d.cts.map +1 -0
  196. package/dist/interfaces/zbtcvc.d.mts +64 -0
  197. package/dist/interfaces/zbtcvc.d.mts.map +1 -0
  198. package/dist/interfaces/zbtcvc.mjs +6 -0
  199. package/dist/interfaces/zbtcvc.mjs.map +1 -0
  200. package/dist/interfaces/zlp.cjs +7 -0
  201. package/dist/interfaces/zlp.cjs.map +1 -0
  202. package/dist/interfaces/zlp.d.cts +114 -0
  203. package/dist/interfaces/zlp.d.cts.map +1 -0
  204. package/dist/interfaces/zlp.d.mts +114 -0
  205. package/dist/interfaces/zlp.d.mts.map +1 -0
  206. package/dist/interfaces/zlp.mjs +6 -0
  207. package/dist/interfaces/zlp.mjs.map +1 -0
  208. package/dist/oracle.cjs +118 -0
  209. package/dist/oracle.cjs.map +1 -0
  210. package/dist/oracle.d.cts +25 -0
  211. package/dist/oracle.d.cts.map +1 -0
  212. package/dist/oracle.d.mts +25 -0
  213. package/dist/oracle.d.mts.map +1 -0
  214. package/dist/oracle.mjs +114 -0
  215. package/dist/oracle.mjs.map +1 -0
  216. package/dist/utils.cjs +129 -0
  217. package/dist/utils.cjs.map +1 -0
  218. package/dist/utils.d.cts +44 -0
  219. package/dist/utils.d.cts.map +1 -0
  220. package/dist/utils.d.mts +44 -0
  221. package/dist/utils.d.mts.map +1 -0
  222. package/dist/utils.mjs +115 -0
  223. package/dist/utils.mjs.map +1 -0
  224. package/docs/SUMMARY.md +10 -0
  225. package/docs/api-reference.md +32 -0
  226. package/docs/architecture.md +14 -0
  227. package/docs/common-operations.md +52 -0
  228. package/docs/error-handling.md +17 -0
  229. package/docs/getting-started.md +60 -0
  230. package/docs/introduction.md +15 -0
  231. package/docs/lp-specific-features.md +96 -0
  232. package/docs/type-safety.md +29 -0
  233. package/eslint.config.mjs +18 -0
  234. package/package.json +42 -0
  235. package/src/abstract/BaseAPI.ts +575 -0
  236. package/src/abstract/BaseDataAPI.ts +207 -0
  237. package/src/abstract/index.ts +7 -0
  238. package/src/api.ts +1100 -0
  239. package/src/bcs.ts +45 -0
  240. package/src/consts/deployments-shared-mainnet.json +50 -0
  241. package/src/consts/deployments-shared-testnet.json +45 -0
  242. package/src/consts/deployments-slp-mainnet.json +600 -0
  243. package/src/consts/deployments-slp-testnet.json +87 -0
  244. package/src/consts/deployments-usdz-mainnet.json +494 -0
  245. package/src/consts/deployments-usdz-testnet.json +98 -0
  246. package/src/consts/deployments-zbtcvc-mainnet.json +180 -0
  247. package/src/consts/deployments-zlp-mainnet.json +791 -0
  248. package/src/consts/deployments-zlp-testnet.json +76 -0
  249. package/src/consts/index.ts +345 -0
  250. package/src/consts/price_id_to_object_id.mainnet.json +56 -0
  251. package/src/consts/price_id_to_object_id.testnet.json +17 -0
  252. package/src/data.ts +1279 -0
  253. package/src/factory/SDKFactory.ts +340 -0
  254. package/src/implementations/SLPAPI.ts +2722 -0
  255. package/src/implementations/SLPDataAPI.ts +1839 -0
  256. package/src/implementations/USDZAPI.ts +2488 -0
  257. package/src/implementations/USDZDataAPI.ts +1548 -0
  258. package/src/implementations/ZBTCVCAPI.ts +1337 -0
  259. package/src/implementations/ZBTCVCDataAPI.ts +993 -0
  260. package/src/implementations/ZLPAPI.ts +2888 -0
  261. package/src/implementations/ZLPDataAPI.ts +1603 -0
  262. package/src/implementations/index.ts +16 -0
  263. package/src/index.ts +58 -0
  264. package/src/interfaces/base.ts +838 -0
  265. package/src/interfaces/index.ts +50 -0
  266. package/src/interfaces/slp.ts +268 -0
  267. package/src/interfaces/usdz.ts +181 -0
  268. package/src/interfaces/zbtcvc.ts +116 -0
  269. package/src/interfaces/zlp.ts +244 -0
  270. package/src/oracle.ts +153 -0
  271. package/src/utils.ts +168 -0
  272. package/tests/api.test.ts +219 -0
  273. package/tests/data.test.ts +156 -0
  274. package/tests/oracle.test.ts +33 -0
  275. package/tsconfig.json +22 -0
@@ -0,0 +1,1794 @@
1
+ "use strict";
2
+ /**
3
+ * SLP API implementation
4
+ * Implements SLP-specific trading and transaction methods for Sudo SDK
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.SLPAPI = void 0;
8
+ const kiosk_1 = require("@mysten/kiosk");
9
+ const transactions_1 = require("@mysten/sui/transactions");
10
+ const utils_1 = require("@mysten/sui/utils");
11
+ const abstract_1 = require("../abstract/index.cjs");
12
+ const consts_1 = require("../consts/index.cjs");
13
+ const utils_2 = require("../utils.cjs");
14
+ const SLPDataAPI_1 = require("./SLPDataAPI.cjs");
15
+ class SLPAPI extends abstract_1.BaseAPI {
16
+ constructor(network, provider, apiEndpoint, connectionURL) {
17
+ super(network, provider, apiEndpoint, connectionURL, consts_1.LPToken.SLP);
18
+ this.dataAPI = new SLPDataAPI_1.SLPDataAPI(network, provider, apiEndpoint, connectionURL);
19
+ }
20
+ calcPositionReserveFeeAmount(_position) {
21
+ throw new Error(`Method not implemented in ${this.constructor.name}.`);
22
+ }
23
+ calcPositionFundingFeeValue(_position) {
24
+ throw new Error(`Method not implemented in ${this.constructor.name}.`);
25
+ }
26
+ /**
27
+ * Deposits collateral into SLP vault using Sudo SDK approach
28
+ */
29
+ async deposit(coin, coinObjects, amount, minAmountOut = 0, referralAddress, sender, sponsoredTx, suiCoinObjectsForPythUpdate) {
30
+ let tx = new transactions_1.Transaction();
31
+ // Add referral if needed
32
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
33
+ tx = await this.addReferral(referralAddress, tx);
34
+ }
35
+ // Initialize oracle transaction
36
+ const pythFeederKeys = Object.keys(this.consts.pythFeeder.feeder);
37
+ // Handle sponsored transaction case
38
+ if (sponsoredTx) {
39
+ const suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
40
+ tx = await this.initOracleTxb(pythFeederKeys, tx, true, suiCoinObject);
41
+ // Process deposit coins
42
+ const depositObject = coin === 'sui'
43
+ ? tx.splitCoins(suiCoinObject, [tx.pure.u64(amount)])[0]
44
+ : tx.splitCoins(this.processCoins(tx, coin, coinObjects, true), [tx.pure.u64(amount)])[0];
45
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
46
+ tx.moveCall({
47
+ target: `${this.consts.sudoCore.upgradedPackage}::market::deposit`,
48
+ typeArguments: [
49
+ `${this.consts.sudoCore.package}::slp::SLP`,
50
+ this.consts.coins[coin].module,
51
+ ],
52
+ arguments: [
53
+ tx.object(this.consts.sudoCore.market),
54
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
55
+ depositObject,
56
+ tx.pure.u64(minAmountOut),
57
+ vaultsValuation,
58
+ symbolsValuation,
59
+ ],
60
+ });
61
+ return tx;
62
+ }
63
+ // Handle non-sponsored transaction case
64
+ tx = await this.initOracleTxb(pythFeederKeys, tx);
65
+ const depositObject = tx.splitCoins(this.processCoins(tx, coin, coinObjects, false), [tx.pure.u64(amount)])[0];
66
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
67
+ tx.moveCall({
68
+ target: `${this.consts.sudoCore.upgradedPackage}::market::deposit`,
69
+ typeArguments: [
70
+ `${this.consts.sudoCore.package}::slp::SLP`,
71
+ this.consts.coins[coin].module,
72
+ ],
73
+ arguments: [
74
+ tx.object(this.consts.sudoCore.market),
75
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
76
+ depositObject,
77
+ tx.pure.u64(minAmountOut),
78
+ vaultsValuation,
79
+ symbolsValuation,
80
+ ],
81
+ });
82
+ return tx;
83
+ }
84
+ /**
85
+ * Deposits collateral into SLP vault and returns a coin
86
+ */
87
+ async depositPtb(coin, coinObjects, amount, minAmountOut = 0, referralAddress, sender, tx, sponsoredTx, suiCoinObjectsForPythUpdate) {
88
+ if (!tx) {
89
+ tx = new transactions_1.Transaction();
90
+ }
91
+ // Add referral if needed
92
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
93
+ tx = await this.addReferral(referralAddress, tx);
94
+ }
95
+ // Initialize oracle transaction
96
+ const pythFeederKeys = Object.keys(this.consts.pythFeeder.feeder);
97
+ // Handle sponsored transaction case
98
+ if (sponsoredTx) {
99
+ const suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
100
+ tx = await this.initOracleTxb(pythFeederKeys, tx, true, suiCoinObject);
101
+ // Process deposit coins
102
+ const depositObject = coin === 'sui'
103
+ ? tx.splitCoins(suiCoinObject, [tx.pure.u64(amount)])[0]
104
+ : tx.splitCoins(this.processCoins(tx, coin, coinObjects, true), [tx.pure.u64(amount)])[0];
105
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
106
+ const [mintedCoin] = tx.moveCall({
107
+ target: `${this.consts.sudoCore.upgradedPackage}::market::deposit_ptb`,
108
+ typeArguments: [`${this.consts.sudoCore.package}::slp::SLP`, this.consts.coins[coin].module],
109
+ arguments: [
110
+ tx.object(this.consts.sudoCore.market),
111
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
112
+ depositObject,
113
+ tx.pure.u64(minAmountOut),
114
+ vaultsValuation,
115
+ symbolsValuation,
116
+ ],
117
+ });
118
+ return mintedCoin;
119
+ }
120
+ // Handle non-sponsored transaction case
121
+ tx = await this.initOracleTxb(pythFeederKeys, tx);
122
+ const depositObject = tx.splitCoins(this.processCoins(tx, coin, coinObjects, false), [tx.pure.u64(amount)])[0];
123
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
124
+ const [mintedCoin] = tx.moveCall({
125
+ target: `${this.consts.sudoCore.upgradedPackage}::market::deposit_ptb`,
126
+ typeArguments: [
127
+ `${this.consts.sudoCore.package}::slp::SLP`,
128
+ this.consts.coins[coin].module,
129
+ ],
130
+ arguments: [
131
+ tx.object(this.consts.sudoCore.market),
132
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
133
+ depositObject,
134
+ tx.pure.u64(minAmountOut),
135
+ vaultsValuation,
136
+ symbolsValuation,
137
+ ],
138
+ });
139
+ return mintedCoin;
140
+ }
141
+ /**
142
+ * Deposits collateral pool with PTB deposit object
143
+ */
144
+ async depositWithPtb(coin, depositObject, minAmountOut = 0, referralAddress, sender, tx, sponsoredTx, suiCoinObjectsForPythUpdate) {
145
+ if (!tx) {
146
+ tx = new transactions_1.Transaction();
147
+ }
148
+ // Add referral if needed
149
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
150
+ tx = await this.addReferral(referralAddress, tx);
151
+ }
152
+ // Initialize oracle transaction
153
+ const pythFeederKeys = Object.keys(this.consts.pythFeeder.feeder);
154
+ // Handle sponsored transaction case
155
+ if (sponsoredTx) {
156
+ const suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
157
+ tx = await this.initOracleTxb(pythFeederKeys, tx, true, suiCoinObject);
158
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
159
+ tx.moveCall({
160
+ target: `${this.consts.sudoCore.upgradedPackage}::market::deposit`,
161
+ typeArguments: [
162
+ `${this.consts.sudoCore.package}::slp::SLP`,
163
+ this.consts.coins[coin].module,
164
+ ],
165
+ arguments: [
166
+ tx.object(this.consts.sudoCore.market),
167
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
168
+ depositObject,
169
+ tx.pure.u64(minAmountOut),
170
+ vaultsValuation,
171
+ symbolsValuation,
172
+ ],
173
+ });
174
+ return tx;
175
+ }
176
+ // Handle non-sponsored transaction case
177
+ tx = await this.initOracleTxb(pythFeederKeys, tx);
178
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
179
+ tx.moveCall({
180
+ target: `${this.consts.sudoCore.upgradedPackage}::market::deposit`,
181
+ typeArguments: [
182
+ `${this.consts.sudoCore.package}::slp::SLP`,
183
+ this.consts.coins[coin].module,
184
+ ],
185
+ arguments: [
186
+ tx.object(this.consts.sudoCore.market),
187
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
188
+ depositObject,
189
+ tx.pure.u64(minAmountOut),
190
+ vaultsValuation,
191
+ symbolsValuation,
192
+ ],
193
+ });
194
+ return tx;
195
+ }
196
+ /**
197
+ * Withdraws collateral from SLP vault using Sudo SDK approach
198
+ */
199
+ async withdraw(coin, lpCoinObjects, amount, minAmountOut = 0, sponsoredTx, suiCoinObjectsForPythUpdate) {
200
+ let tx = new transactions_1.Transaction();
201
+ // Initialize oracle transaction
202
+ const pythFeederKeys = Object.keys(this.consts.pythFeeder.feeder);
203
+ if (sponsoredTx) {
204
+ const suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
205
+ tx = await this.initOracleTxb(pythFeederKeys, tx, true, suiCoinObject);
206
+ }
207
+ else {
208
+ tx = await this.initOracleTxb(pythFeederKeys, tx);
209
+ }
210
+ const slpCoinObject = this.processCoins(tx, 'slp', lpCoinObjects, false);
211
+ const [withdrawObject] = tx.splitCoins(slpCoinObject, [tx.pure.u64(amount)]);
212
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
213
+ tx.moveCall({
214
+ target: `${this.consts.sudoCore.upgradedPackage}::market::withdraw`,
215
+ typeArguments: [
216
+ `${this.consts.sudoCore.package}::slp::SLP`,
217
+ this.consts.coins[coin].module,
218
+ ],
219
+ arguments: [
220
+ tx.object(this.consts.sudoCore.market),
221
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
222
+ withdrawObject,
223
+ tx.pure.u64(minAmountOut),
224
+ vaultsValuation,
225
+ symbolsValuation,
226
+ ],
227
+ });
228
+ return tx;
229
+ }
230
+ /**
231
+ * Withdraws LP coin object from SLP Vault
232
+ */
233
+ async withdrawLpCoinObject(coin, lpCoinObject, minAmountOut = 0, tx) {
234
+ if (!tx) {
235
+ tx = new transactions_1.Transaction();
236
+ }
237
+ // Initialize oracle transaction
238
+ const pythFeederKeys = Object.keys(this.consts.pythFeeder.feeder);
239
+ tx = await this.initOracleTxb(pythFeederKeys, tx);
240
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
241
+ tx.moveCall({
242
+ target: `${this.consts.sudoCore.upgradedPackage}::market::withdraw`,
243
+ typeArguments: [
244
+ `${this.consts.sudoCore.package}::slp::SLP`,
245
+ this.consts.coins[coin].module,
246
+ ],
247
+ arguments: [
248
+ tx.object(this.consts.sudoCore.market),
249
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
250
+ lpCoinObject,
251
+ tx.pure.u64(minAmountOut),
252
+ vaultsValuation,
253
+ symbolsValuation,
254
+ ],
255
+ });
256
+ return tx;
257
+ }
258
+ async withdrawPtb(coin, lpCoinObjects, amount, minAmountOut = 0, tx, sponsoredTx, suiCoinObjectsForPythUpdate) {
259
+ if (!tx) {
260
+ tx = new transactions_1.Transaction();
261
+ }
262
+ // Initialize oracle transaction
263
+ const pythFeederKeys = Object.keys(this.consts.pythFeeder.feeder);
264
+ if (sponsoredTx) {
265
+ const suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
266
+ tx = await this.initOracleTxb(pythFeederKeys, tx, true, suiCoinObject);
267
+ }
268
+ else {
269
+ tx = await this.initOracleTxb(pythFeederKeys, tx);
270
+ }
271
+ const slpCoinObject = this.processCoins(tx, 'slp', lpCoinObjects, false);
272
+ const [withdrawObject] = tx.splitCoins(slpCoinObject, [tx.pure.u64(amount)]);
273
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
274
+ const [withdrawCoin] = tx.moveCall({
275
+ target: `${this.consts.sudoCore.upgradedPackage}::market::withdraw_ptb`,
276
+ typeArguments: [
277
+ `${this.consts.sudoCore.package}::slp::SLP`,
278
+ this.consts.coins[coin].module,
279
+ ],
280
+ arguments: [
281
+ tx.object(this.consts.sudoCore.market),
282
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
283
+ withdrawObject,
284
+ tx.pure.u64(minAmountOut),
285
+ vaultsValuation,
286
+ symbolsValuation,
287
+ ],
288
+ });
289
+ return withdrawCoin;
290
+ }
291
+ async swap(fromToken, toToken, fromAmount, fromCoinObjects, minAmountOut) {
292
+ const tx = await this.initOracleTxb(Object.keys(this.consts.sudoCore.vaults));
293
+ const fromCoinObject = this.processCoins(tx, fromToken, fromCoinObjects);
294
+ const [fromDepositObject] = tx.splitCoins(fromCoinObject, [
295
+ tx.pure.u64(fromAmount),
296
+ ]);
297
+ const vaultsValuation = this.dataAPI.valuateVaults(tx);
298
+ tx.moveCall({
299
+ target: `${this.consts.sudoCore.upgradedPackage}::market::swap_v2`,
300
+ typeArguments: [
301
+ `${this.consts.sudoCore.package}::slp::SLP`,
302
+ this.consts.coins[fromToken].module,
303
+ this.consts.coins[toToken].module,
304
+ ],
305
+ arguments: [
306
+ tx.object(this.consts.sudoCore.market),
307
+ tx.object(this.consts.sudoCore.rebaseFeeModel),
308
+ fromDepositObject,
309
+ tx.pure.u64(minAmountOut || 0),
310
+ vaultsValuation,
311
+ tx.object(this.consts.pythFeeder.feeder[fromToken]),
312
+ tx.object(this.consts.pythFeeder.feeder[toToken]),
313
+ ],
314
+ });
315
+ return tx;
316
+ }
317
+ /**
318
+ * Stakes SLP tokens in ZO staking pools
319
+ */
320
+ stake(lpCoinObjects, amount, pool, tx) {
321
+ if (!tx) {
322
+ tx = new transactions_1.Transaction();
323
+ }
324
+ const coinObject = this.processCoins(tx, 'slp', lpCoinObjects);
325
+ const [depositObject] = tx.splitCoins(coinObject, [tx.pure.u64(amount)]);
326
+ tx.moveCall({
327
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::deposit`,
328
+ typeArguments: [
329
+ `${this.consts.sudoCore.package}::slp::SLP`,
330
+ `${this.consts.sudoCore.package}::slp::SLP`,
331
+ ],
332
+ arguments: [
333
+ tx.object(pool),
334
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
335
+ depositObject,
336
+ ],
337
+ });
338
+ return tx;
339
+ }
340
+ /**
341
+ * Stakes SLP tokens using coin object only (PTB)
342
+ */
343
+ stakeCoinObject(coinObject, pool, tx) {
344
+ if (!tx) {
345
+ tx = new transactions_1.Transaction();
346
+ }
347
+ tx.moveCall({
348
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::deposit`,
349
+ typeArguments: [
350
+ `${this.consts.sudoCore.package}::slp::SLP`,
351
+ `${this.consts.sudoCore.package}::slp::SLP`,
352
+ ],
353
+ arguments: [
354
+ tx.object(pool),
355
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
356
+ coinObject,
357
+ ],
358
+ });
359
+ return tx;
360
+ }
361
+ /**
362
+ * Unstakes SLP tokens from ZO staking pools
363
+ */
364
+ unstake(credentials, amount, pool, tx) {
365
+ let unstakeAmount = amount;
366
+ if (!tx) {
367
+ tx = new transactions_1.Transaction();
368
+ }
369
+ for (const credential of credentials) {
370
+ const withdrawAmount = (() => {
371
+ const min = (a, b) => a < b ? a : b;
372
+ return min(unstakeAmount, credential.amount);
373
+ })();
374
+ unstakeAmount -= withdrawAmount;
375
+ tx.moveCall({
376
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::withdraw`,
377
+ typeArguments: [
378
+ `${this.consts.sudoCore.package}::slp::SLP`,
379
+ `${this.consts.sudoCore.package}::slp::SLP`,
380
+ ],
381
+ arguments: [
382
+ tx.object(pool),
383
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
384
+ tx.object(credential.id),
385
+ tx.pure.u64(withdrawAmount),
386
+ ],
387
+ });
388
+ if (credential.amount === BigInt(0)) {
389
+ tx.moveCall({
390
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::clear_empty_credential`,
391
+ typeArguments: [
392
+ `${this.consts.sudoCore.package}::slp::SLP`,
393
+ `${this.consts.sudoCore.package}::slp::SLP`,
394
+ ],
395
+ arguments: [tx.object(credential.id)],
396
+ });
397
+ }
398
+ }
399
+ return tx;
400
+ }
401
+ /**
402
+ * Unstakes SLP tokens from ZO staking pools return Coins
403
+ */
404
+ unstakeToCoins(credentials, amount, pool, tx) {
405
+ let unstakeAmount = amount;
406
+ const withdrawnCoinObjects = [];
407
+ for (const credential of credentials) {
408
+ const withdrawAmount = (() => {
409
+ const min = (a, b) => a < b ? a : b;
410
+ return min(unstakeAmount, credential.amount);
411
+ })();
412
+ unstakeAmount -= withdrawAmount;
413
+ const [withdrawnCoin] = tx.moveCall({
414
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::withdraw_ptb`,
415
+ typeArguments: [
416
+ `${this.consts.sudoCore.package}::slp::SLP`,
417
+ `${this.consts.sudoCore.package}::slp::SLP`,
418
+ ],
419
+ arguments: [
420
+ tx.object(pool),
421
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
422
+ tx.object(credential.id),
423
+ tx.pure.u64(withdrawAmount),
424
+ ],
425
+ });
426
+ withdrawnCoinObjects.push(withdrawnCoin);
427
+ if (credential.amount === BigInt(0)) {
428
+ tx.moveCall({
429
+ target: `${this.sharedConfig.zoStaking.package}::pool::clear_empty_credential`,
430
+ typeArguments: [
431
+ `${this.consts.sudoCore.package}::slp::SLP`,
432
+ `${this.consts.sudoCore.package}::slp::SLP`,
433
+ ],
434
+ arguments: [tx.object(credential.id)],
435
+ });
436
+ }
437
+ }
438
+ return withdrawnCoinObjects;
439
+ }
440
+ /**
441
+ * Withdraws SLP tokens from a staking pool credential (PTB)
442
+ * Returns the withdrawn coin object for use in programmable transaction blocks
443
+ */
444
+ withdrawFromVaultPtb(pool, credential, withdrawAmount, tx) {
445
+ const [withdrawnCoin] = tx.moveCall({
446
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::withdraw_ptb`,
447
+ typeArguments: [
448
+ `${this.consts.sudoCore.package}::slp::SLP`,
449
+ `${this.consts.sudoCore.package}::slp::SLP`,
450
+ ],
451
+ arguments: [
452
+ tx.object(pool),
453
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
454
+ tx.object(credential),
455
+ tx.pure.u64(withdrawAmount),
456
+ ],
457
+ });
458
+ return withdrawnCoin;
459
+ }
460
+ /**
461
+ * Claims rewards from a staking pool credential (PTB)
462
+ * Returns the reward coin object for use in programmable transaction blocks
463
+ */
464
+ claimRewardsPtb(pool, credential, tx) {
465
+ const [rewardCoin] = tx.moveCall({
466
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::claim_rewards_ptb`,
467
+ typeArguments: [
468
+ `${this.consts.sudoCore.package}::slp::SLP`,
469
+ `${this.consts.sudoCore.package}::slp::SLP`,
470
+ ],
471
+ arguments: [
472
+ tx.object(pool),
473
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
474
+ tx.object(credential),
475
+ ],
476
+ });
477
+ return rewardCoin;
478
+ }
479
+ /**
480
+ * Opens a position using Sudo SDK approach
481
+ */
482
+ async openPosition(collateralToken, indexToken, size, collateralAmount, coinObjects, long, reserveAmount, indexPrice, collateralPrice, isLimitOrder = false, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, sponsoredTx, suiCoinObjectsForPythUpdate) {
483
+ let tx = new transactions_1.Transaction();
484
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
485
+ tx = await this.addReferral(referralAddress, tx);
486
+ }
487
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
488
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage);
489
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
490
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
491
+ if (isLimitOrder) {
492
+ allowTrade = isIocOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
493
+ }
494
+ // Handle oracle initialization and coin processing
495
+ let suiCoinObject;
496
+ if (sponsoredTx) {
497
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
498
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
499
+ }
500
+ else {
501
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
502
+ }
503
+ // Process coin splitting
504
+ const [depositObject, feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
505
+ tx.moveCall({
506
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_v2_1`,
507
+ typeArguments: [
508
+ `${this.consts.sudoCore.package}::slp::SLP`,
509
+ this.consts.coins[collateralToken].module,
510
+ this.consts.coins[indexToken].module,
511
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
512
+ this.consts.coins[collateralToken].module,
513
+ ],
514
+ arguments: [
515
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
516
+ tx.object(this.consts.sudoCore.market),
517
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
518
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
519
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
520
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
521
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
522
+ depositObject,
523
+ feeObject,
524
+ tx.pure.u8(allowTrade),
525
+ tx.pure.u64(size),
526
+ tx.pure.u64(reserveAmount),
527
+ tx.pure.u256(adjustCollateralPrice),
528
+ tx.pure.u256(adjustPrice),
529
+ ],
530
+ });
531
+ return tx;
532
+ }
533
+ /**
534
+ * Opens a position using Sudo SDK approach
535
+ */
536
+ async openPositionWithCoin(collateralToken, indexToken, size, coinObj, long, reserveAmount, indexPrice, collateralPrice, isLimitOrder = false, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, tx, sponsoredTx, suiCoinObjectsForPythUpdate) {
537
+ if (!tx) {
538
+ tx = new transactions_1.Transaction();
539
+ }
540
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
541
+ tx = await this.addReferral(referralAddress, tx);
542
+ }
543
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
544
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage);
545
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
546
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
547
+ if (isLimitOrder) {
548
+ allowTrade = isIocOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
549
+ }
550
+ // Handle oracle initialization and coin processing
551
+ let suiCoinObject;
552
+ if (sponsoredTx) {
553
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
554
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
555
+ }
556
+ else {
557
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
558
+ }
559
+ // Process coin splitting
560
+ const [feeObject] = tx.splitCoins(coinObj, [tx.pure.u64(relayerFee)]);
561
+ tx.moveCall({
562
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_v2_1`,
563
+ typeArguments: [
564
+ `${this.consts.sudoCore.package}::slp::SLP`,
565
+ this.consts.coins[collateralToken].module,
566
+ this.consts.coins[indexToken].module,
567
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
568
+ this.consts.coins[collateralToken].module,
569
+ ],
570
+ arguments: [
571
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
572
+ tx.object(this.consts.sudoCore.market),
573
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
574
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
575
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
576
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
577
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
578
+ coinObj,
579
+ feeObject,
580
+ tx.pure.u8(allowTrade),
581
+ tx.pure.u64(size),
582
+ tx.pure.u64(reserveAmount),
583
+ tx.pure.u256(adjustCollateralPrice),
584
+ tx.pure.u256(adjustPrice),
585
+ ],
586
+ });
587
+ return tx;
588
+ }
589
+ /**
590
+ * Opens a new position in SLP (V2 - uses v3 contract)
591
+ */
592
+ async openPositionV2(collateralToken, indexToken, size, collateralAmount, coinObjects, long, reserveAmount, indexPrice, collateralPrice, isLimitOrder, isIocOrder, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, sponsoredTx, suiCoinObjectsForPythUpdate) {
593
+ let tx = new transactions_1.Transaction();
594
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
595
+ tx = await this.addReferral(referralAddress, tx);
596
+ }
597
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
598
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage);
599
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
600
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
601
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
602
+ if (isLimitOrder) {
603
+ allowTrade = isIocOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
604
+ }
605
+ // Handle oracle initialization and coin processing
606
+ let suiCoinObject;
607
+ if (sponsoredTx) {
608
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
609
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
610
+ }
611
+ else {
612
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
613
+ }
614
+ // Process coin splitting
615
+ const [depositObject, feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
616
+ tx.moveCall({
617
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_v3`,
618
+ typeArguments: [
619
+ `${this.consts.sudoCore.package}::slp::SLP`,
620
+ this.consts.coins[collateralToken].module,
621
+ this.consts.coins[indexToken].module,
622
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
623
+ this.consts.coins[collateralToken].module,
624
+ ],
625
+ arguments: [
626
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
627
+ tx.object(this.consts.sudoCore.market),
628
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
629
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
630
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
631
+ depositObject,
632
+ feeObject,
633
+ tx.pure.u8(allowTrade),
634
+ tx.pure.u64(size),
635
+ tx.pure.u64(reserveAmount),
636
+ tx.pure.u256(adjustCollateralPrice),
637
+ tx.pure.u256(adjustPrice),
638
+ tx.pure.u256(indexPriceThreshold),
639
+ ],
640
+ });
641
+ return tx;
642
+ }
643
+ /**
644
+ * Opens a new position with Coin in SLP (V2 - uses v3 contract)
645
+ */
646
+ async openPositionWithCoinV2(collateralToken, indexToken, size, coinObj, long, reserveAmount, indexPrice, collateralPrice, isLimitOrder, isIocOrder, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, tx, sponsoredTx, suiCoinObjectsForPythUpdate) {
647
+ if (!tx) {
648
+ tx = new transactions_1.Transaction();
649
+ }
650
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
651
+ tx = await this.addReferral(referralAddress, tx);
652
+ }
653
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
654
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage);
655
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
656
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
657
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
658
+ if (isLimitOrder) {
659
+ allowTrade = isIocOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
660
+ }
661
+ // Handle oracle initialization and coin processing
662
+ let suiCoinObject;
663
+ if (sponsoredTx) {
664
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
665
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
666
+ }
667
+ else {
668
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
669
+ }
670
+ // Process coin splitting
671
+ const [feeObject] = tx.splitCoins(coinObj, [tx.pure.u64(relayerFee)]);
672
+ tx.moveCall({
673
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_v3`,
674
+ typeArguments: [
675
+ `${this.consts.sudoCore.package}::slp::SLP`,
676
+ this.consts.coins[collateralToken].module,
677
+ this.consts.coins[indexToken].module,
678
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
679
+ this.consts.coins[collateralToken].module,
680
+ ],
681
+ arguments: [
682
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
683
+ tx.object(this.consts.sudoCore.market),
684
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
685
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
686
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
687
+ coinObj,
688
+ feeObject,
689
+ tx.pure.u8(allowTrade),
690
+ tx.pure.u64(size),
691
+ tx.pure.u64(reserveAmount),
692
+ tx.pure.u256(adjustCollateralPrice),
693
+ tx.pure.u256(adjustPrice),
694
+ tx.pure.u256(indexPriceThreshold),
695
+ ],
696
+ });
697
+ return tx;
698
+ }
699
+ /**
700
+ * Decreases an existing position in SLP using Sudo SDK approach
701
+ */
702
+ async decreasePosition(pcpId, collateralToken, indexToken, amount, long, indexPrice, collateralPrice, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), coinObjects, sponsoredTx, suiCoinObjectsForPythUpdate) {
703
+ if (!coinObjects) {
704
+ throw new Error(`${this.constructor.name}: coinObjects is required`);
705
+ }
706
+ let tx = new transactions_1.Transaction();
707
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
708
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
709
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
710
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
711
+ if (isTriggerOrder) {
712
+ allowTrade = isIocOrder || !isTakeProfitOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
713
+ }
714
+ else {
715
+ isTakeProfitOrder = true;
716
+ }
717
+ let suiCoinObject;
718
+ if (sponsoredTx) {
719
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
720
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
721
+ }
722
+ else {
723
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
724
+ }
725
+ // Handle oracle initialization and coin processing
726
+ const [feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
727
+ tx.moveCall({
728
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_v2_1`,
729
+ typeArguments: [
730
+ `${this.consts.sudoCore.package}::slp::SLP`,
731
+ this.consts.coins[collateralToken].module,
732
+ this.consts.coins[indexToken].module,
733
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
734
+ this.consts.coins[collateralToken].module,
735
+ ],
736
+ arguments: [
737
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
738
+ tx.object(this.consts.sudoCore.market),
739
+ tx.object(pcpId),
740
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
741
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
742
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
743
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
744
+ feeObject,
745
+ tx.pure.u8(allowTrade),
746
+ tx.pure.bool(isTakeProfitOrder),
747
+ tx.pure.u64(amount),
748
+ tx.pure.u256(adjustCollateralPrice),
749
+ tx.pure.u256(adjustPrice),
750
+ ],
751
+ });
752
+ return tx;
753
+ }
754
+ /**
755
+ * Decreases an existing position in SLP (V2 - uses v3 contract)
756
+ */
757
+ async decreasePositionV2(pcpId, collateralToken, indexToken, amount, long, indexPrice, collateralPrice, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), coinObjects, sponsoredTx, suiCoinObjectsForPythUpdate) {
758
+ if (!coinObjects) {
759
+ throw new Error(`${this.constructor.name}: coinObjects is required`);
760
+ }
761
+ let tx = new transactions_1.Transaction();
762
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
763
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
764
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
765
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
766
+ if (isTriggerOrder) {
767
+ allowTrade = isIocOrder || !isTakeProfitOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
768
+ }
769
+ else {
770
+ isTakeProfitOrder = true;
771
+ }
772
+ // Handle oracle initialization and coin processing
773
+ let suiCoinObject;
774
+ if (sponsoredTx) {
775
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
776
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
777
+ }
778
+ else {
779
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
780
+ }
781
+ // Process coin splitting
782
+ const [feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
783
+ tx.moveCall({
784
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_v3`,
785
+ typeArguments: [
786
+ `${this.consts.sudoCore.package}::slp::SLP`,
787
+ this.consts.coins[collateralToken].module,
788
+ this.consts.coins[indexToken].module,
789
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
790
+ this.consts.coins[collateralToken].module,
791
+ ],
792
+ arguments: [
793
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
794
+ tx.object(this.consts.sudoCore.market),
795
+ tx.object(pcpId),
796
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
797
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
798
+ feeObject,
799
+ tx.pure.u8(allowTrade),
800
+ tx.pure.bool(isTakeProfitOrder),
801
+ tx.pure.u64(amount),
802
+ tx.pure.u256(adjustCollateralPrice),
803
+ tx.pure.u256(adjustPrice),
804
+ tx.pure.u256(indexPriceThreshold),
805
+ ],
806
+ });
807
+ return tx;
808
+ }
809
+ async decreaseMultiPositions(positions, tx, sponsoredTx, suiCoinObjectsForPythUpdate) {
810
+ if (!tx) {
811
+ tx = new transactions_1.Transaction();
812
+ }
813
+ // Handle oracle initialization and coin processing
814
+ let suiCoinObject;
815
+ if (sponsoredTx) {
816
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
817
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx, true, suiCoinObject);
818
+ }
819
+ else {
820
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx);
821
+ }
822
+ for (const position of positions) {
823
+ const { pcpId, collateralToken, coinObjects = [], indexToken, amount, long, indexPrice, collateralPrice, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), } = position;
824
+ let innerIsTakeProfitOrder = isTakeProfitOrder;
825
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
826
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
827
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
828
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
829
+ if (isTriggerOrder) {
830
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
831
+ }
832
+ else {
833
+ innerIsTakeProfitOrder = true;
834
+ }
835
+ // Process coin splitting
836
+ const [feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
837
+ tx.moveCall({
838
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_v2_1`,
839
+ typeArguments: [
840
+ `${this.consts.sudoCore.package}::slp::SLP`,
841
+ this.consts.coins[collateralToken].module,
842
+ this.consts.coins[indexToken].module,
843
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
844
+ this.consts.coins[collateralToken].module,
845
+ ],
846
+ arguments: [
847
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
848
+ tx.object(this.consts.sudoCore.market),
849
+ tx.object(pcpId),
850
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
851
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
852
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
853
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
854
+ feeObject,
855
+ tx.pure.u8(allowTrade),
856
+ tx.pure.bool(innerIsTakeProfitOrder),
857
+ tx.pure.u64(amount),
858
+ tx.pure.u256(adjustCollateralPrice),
859
+ tx.pure.u256(adjustPrice),
860
+ ],
861
+ });
862
+ }
863
+ return tx;
864
+ }
865
+ async decreaseMultiPositionsV2(positions, tx, sponsoredTx, suiCoinObjectsForPythUpdate, feeObjects) {
866
+ const transaction = tx ?? new transactions_1.Transaction();
867
+ // Handle oracle initialization and coin processing
868
+ const tokens = positions.flatMap(position => [position.collateralToken, position.indexToken]);
869
+ const suiCoinObject = sponsoredTx
870
+ ? this.processCoins(transaction, 'sui', suiCoinObjectsForPythUpdate || [], true)
871
+ : undefined;
872
+ await this.initOracleTxb(tokens, transaction, !!sponsoredTx, suiCoinObject);
873
+ if (!feeObjects || feeObjects.length !== positions.length) {
874
+ throw new Error('feeObjects must be provided (pre-processed via buildFeeCoinObjects) and match positions length');
875
+ }
876
+ for (const [i, position] of positions.entries()) {
877
+ const feeObject = feeObjects[i];
878
+ const { pcpId, collateralToken, indexToken, amount, long, indexPrice, collateralPrice, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, } = position;
879
+ let innerIsTakeProfitOrder = isTakeProfitOrder;
880
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
881
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
882
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
883
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
884
+ if (isTriggerOrder) {
885
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
886
+ }
887
+ else {
888
+ innerIsTakeProfitOrder = true;
889
+ }
890
+ transaction.moveCall({
891
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_v3`,
892
+ typeArguments: [
893
+ `${this.consts.sudoCore.package}::slp::SLP`,
894
+ this.consts.coins[collateralToken].module,
895
+ this.consts.coins[indexToken].module,
896
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
897
+ this.consts.coins[collateralToken].module,
898
+ ],
899
+ arguments: [
900
+ transaction.object(utils_1.SUI_CLOCK_OBJECT_ID),
901
+ transaction.object(this.consts.sudoCore.market),
902
+ transaction.object(pcpId),
903
+ transaction.object(this.consts.pythFeeder.feeder[collateralToken]),
904
+ transaction.object(this.consts.pythFeeder.feeder[indexToken]),
905
+ feeObject,
906
+ transaction.pure.u8(allowTrade),
907
+ transaction.pure.bool(innerIsTakeProfitOrder),
908
+ transaction.pure.u64(amount),
909
+ transaction.pure.u256(adjustCollateralPrice),
910
+ transaction.pure.u256(adjustPrice),
911
+ transaction.pure.u256(indexPriceThreshold),
912
+ ],
913
+ });
914
+ }
915
+ return transaction;
916
+ }
917
+ async pledgeInPosition(pcpId, collateralToken, indexToken, amount, coinObjects, long, sponsoredTx, suiCoinObjectsForPythUpdate) {
918
+ let tx = new transactions_1.Transaction();
919
+ // Handle oracle initialization and coin processing
920
+ let suiCoinObject;
921
+ if (sponsoredTx) {
922
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
923
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
924
+ }
925
+ else {
926
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
927
+ }
928
+ // Process coin splitting
929
+ const [depositObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(amount)], sponsoredTx, suiCoinObject);
930
+ tx.moveCall({
931
+ target: `${this.consts.sudoCore.upgradedPackage}::market::pledge_in_position`,
932
+ typeArguments: [
933
+ `${this.consts.sudoCore.package}::slp::SLP`,
934
+ this.consts.coins[collateralToken].module,
935
+ this.consts.coins[indexToken].module,
936
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
937
+ ],
938
+ arguments: [
939
+ tx.object(this.consts.sudoCore.market),
940
+ tx.object(pcpId),
941
+ depositObject,
942
+ ],
943
+ });
944
+ return tx;
945
+ }
946
+ async redeemFromPosition(pcpId, collateralToken, indexToken, amount, long, sponsoredTx, suiCoinObjectsForPythUpdate) {
947
+ let tx = new transactions_1.Transaction();
948
+ // Handle oracle initialization and coin processing
949
+ let suiCoinObject;
950
+ if (sponsoredTx) {
951
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
952
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
953
+ }
954
+ else {
955
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
956
+ }
957
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
958
+ tx.moveCall({
959
+ target: `${this.consts.sudoCore.upgradedPackage}::market::redeem_from_position_v1_1`,
960
+ typeArguments: [
961
+ `${this.consts.sudoCore.package}::slp::SLP`,
962
+ this.consts.coins[collateralToken].module,
963
+ this.consts.coins[indexToken].module,
964
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
965
+ ],
966
+ arguments: [
967
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
968
+ tx.object(this.consts.sudoCore.market),
969
+ tx.object(pcpId),
970
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
971
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
972
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
973
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
974
+ tx.pure.u64(amount),
975
+ ],
976
+ });
977
+ return tx;
978
+ }
979
+ cancelOrder(orderCapId, collateralToken, indexToken, long, type, isV11Order) {
980
+ const tx = new transactions_1.Transaction();
981
+ let functionName = '';
982
+ switch (type) {
983
+ case 'OPEN_POSITION': {
984
+ functionName = 'clear_open_position_order_unified';
985
+ break;
986
+ }
987
+ case 'OPEN_MARKET': {
988
+ functionName = 'clear_open_market_order';
989
+ break;
990
+ }
991
+ case 'DECREASE_POSITION': {
992
+ functionName = 'clear_decrease_position_order_unified';
993
+ break;
994
+ }
995
+ case 'DECREASE_MARKET': {
996
+ functionName = 'clear_decrease_market_order';
997
+ break;
998
+ }
999
+ default: {
1000
+ throw new Error('invalid order type');
1001
+ }
1002
+ }
1003
+ tx.moveCall({
1004
+ target: `${this.consts.sudoCore.upgradedPackage}::market::${functionName}`,
1005
+ typeArguments: [
1006
+ `${this.consts.sudoCore.package}::slp::SLP`,
1007
+ this.consts.coins[collateralToken].module,
1008
+ this.consts.coins[indexToken].module,
1009
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1010
+ isV11Order ? this.consts.coins.sui.module : this.consts.coins[collateralToken].module,
1011
+ ],
1012
+ arguments: [
1013
+ tx.object(this.consts.sudoCore.market),
1014
+ tx.object(orderCapId),
1015
+ ],
1016
+ });
1017
+ return tx;
1018
+ }
1019
+ cancelMultiOrders(orders, tx) {
1020
+ if (!tx) {
1021
+ tx = new transactions_1.Transaction();
1022
+ }
1023
+ for (const order of orders) {
1024
+ const { orderCapId, collateralToken, indexToken, long, type } = order;
1025
+ let functionName = '';
1026
+ switch (type) {
1027
+ case 'OPEN_POSITION': {
1028
+ functionName = 'clear_open_position_order_unified';
1029
+ break;
1030
+ }
1031
+ case 'OPEN_MARKET': {
1032
+ functionName = 'clear_open_market_order';
1033
+ break;
1034
+ }
1035
+ case 'DECREASE_POSITION': {
1036
+ functionName = 'clear_decrease_position_order_unified';
1037
+ break;
1038
+ }
1039
+ case 'DECREASE_MARKET': {
1040
+ functionName = 'clear_decrease_market_order';
1041
+ break;
1042
+ }
1043
+ default: {
1044
+ throw new Error('invalid order type');
1045
+ }
1046
+ }
1047
+ tx.moveCall({
1048
+ target: `${this.consts.sudoCore.upgradedPackage}::market::${functionName}`,
1049
+ typeArguments: [
1050
+ `${this.consts.sudoCore.package}::slp::SLP`,
1051
+ this.consts.coins[collateralToken].module,
1052
+ this.consts.coins[indexToken].module,
1053
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1054
+ this.consts.coins[collateralToken].module,
1055
+ ],
1056
+ arguments: [tx.object(this.consts.sudoCore.market), tx.object(orderCapId)],
1057
+ });
1058
+ }
1059
+ return tx;
1060
+ }
1061
+ clearClosedPosition(pcpId, collateralToken, indexToken, long, tx) {
1062
+ tx.moveCall({
1063
+ target: `${this.consts.sudoCore.upgradedPackage}::market::clear_closed_position`,
1064
+ typeArguments: [
1065
+ `${this.consts.sudoCore.package}::slp::SLP`,
1066
+ this.consts.coins[collateralToken].module,
1067
+ this.consts.coins[indexToken].module,
1068
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1069
+ ],
1070
+ arguments: [tx.object(this.consts.sudoCore.market), tx.object(pcpId)],
1071
+ });
1072
+ }
1073
+ clearOpenPositionOrder(orderCapId, collateralToken, indexToken, long, tx, isV11Order = true) {
1074
+ tx.moveCall({
1075
+ target: `${this.consts.sudoCore.upgradedPackage}::market::clear_open_position_order_unified`,
1076
+ typeArguments: [
1077
+ `${this.consts.sudoCore.package}::slp::SLP`,
1078
+ this.consts.coins[collateralToken].module,
1079
+ this.consts.coins[indexToken].module,
1080
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1081
+ isV11Order ? this.consts.coins.sui.module : this.consts.coins[collateralToken].module,
1082
+ ],
1083
+ arguments: [
1084
+ tx.object(this.consts.sudoCore.market),
1085
+ tx.object(orderCapId),
1086
+ ],
1087
+ });
1088
+ }
1089
+ clearDecreasePositionOrder(orderCapId, collateralToken, indexToken, long, tx, isV11Order = true) {
1090
+ tx.moveCall({
1091
+ target: `${this.consts.sudoCore.upgradedPackage}::market::clear_decrease_position_order_unified`,
1092
+ typeArguments: [
1093
+ `${this.consts.sudoCore.package}::slp::SLP`,
1094
+ this.consts.coins[collateralToken].module,
1095
+ this.consts.coins[indexToken].module,
1096
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1097
+ isV11Order ? this.consts.coins.sui.module : this.consts.coins[collateralToken].module,
1098
+ ],
1099
+ arguments: [
1100
+ tx.object(this.consts.sudoCore.market),
1101
+ tx.object(orderCapId),
1102
+ ],
1103
+ });
1104
+ }
1105
+ clearOpenMarketOrder(orderCapId, collateralToken, indexToken, long, tx, _isV11Order) {
1106
+ const funcName = 'clear_open_market_order';
1107
+ tx.moveCall({
1108
+ target: `${this.consts.sudoCore.upgradedPackage}::market::${funcName}`,
1109
+ typeArguments: [
1110
+ `${this.consts.sudoCore.package}::slp::SLP`,
1111
+ this.consts.coins[collateralToken].module,
1112
+ this.consts.coins[indexToken].module,
1113
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1114
+ this.consts.coins[collateralToken].module,
1115
+ ],
1116
+ arguments: [tx.object(this.consts.sudoCore.market), tx.object(orderCapId)],
1117
+ });
1118
+ }
1119
+ clearDecreaseMarketOrder(orderCapId, collateralToken, indexToken, long, tx, _isV11Order) {
1120
+ const funcName = 'clear_decrease_market_order';
1121
+ tx.moveCall({
1122
+ target: `${this.consts.sudoCore.upgradedPackage}::market::${funcName}`,
1123
+ typeArguments: [
1124
+ `${this.consts.sudoCore.package}::slp::SLP`,
1125
+ this.consts.coins[collateralToken].module,
1126
+ this.consts.coins[indexToken].module,
1127
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1128
+ this.consts.coins[collateralToken].module,
1129
+ ],
1130
+ arguments: [tx.object(this.consts.sudoCore.market), tx.object(orderCapId)],
1131
+ });
1132
+ }
1133
+ async openPositionWithSCard(collateralToken, indexToken, size, collateralAmount, coinObjects, long, reserveAmount, indexPrice, collateralPrice, kioskClient, kioskCap, scard, isLimitOrder = false, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, sponsoredTx, suiCoinObjectsForPythUpdate) {
1134
+ let tx = new transactions_1.Transaction();
1135
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
1136
+ tx = await this.addReferral(referralAddress, tx);
1137
+ }
1138
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
1139
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage || 0.003);
1140
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage || 0.5);
1141
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
1142
+ if (isLimitOrder) {
1143
+ allowTrade = isIocOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
1144
+ }
1145
+ const kioskTx = new kiosk_1.KioskTransaction({
1146
+ transaction: tx,
1147
+ kioskClient,
1148
+ cap: kioskCap,
1149
+ });
1150
+ const [sudoCard, promise] = kioskTx.borrow({
1151
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1152
+ itemId: scard,
1153
+ });
1154
+ // Handle oracle initialization and coin processing
1155
+ let suiCoinObject;
1156
+ if (sponsoredTx) {
1157
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
1158
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
1159
+ }
1160
+ else {
1161
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
1162
+ }
1163
+ // Process coin splitting
1164
+ const [depositObject, feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
1165
+ tx.moveCall({
1166
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v1`,
1167
+ typeArguments: [
1168
+ `${this.consts.sudoCore.package}::slp::SLP`,
1169
+ this.consts.coins[collateralToken].module,
1170
+ this.consts.coins[indexToken].module,
1171
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1172
+ this.consts.coins[collateralToken].module,
1173
+ ],
1174
+ arguments: [
1175
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
1176
+ tx.object(this.consts.sudoCore.market),
1177
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
1178
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1179
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
1180
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1181
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1182
+ depositObject,
1183
+ feeObject,
1184
+ tx.pure.u8(allowTrade),
1185
+ tx.pure.u64(size),
1186
+ tx.pure.u64(reserveAmount),
1187
+ tx.pure.u256(adjustCollateralPrice),
1188
+ tx.pure.u256(adjustPrice),
1189
+ sudoCard,
1190
+ ],
1191
+ });
1192
+ kioskTx
1193
+ .return({
1194
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1195
+ item: sudoCard,
1196
+ promise,
1197
+ })
1198
+ .finalize();
1199
+ return tx;
1200
+ }
1201
+ async openPositionWithCoinAndSCard(collateralToken, indexToken, size, coinObj, long, reserveAmount, indexPrice, collateralPrice, kioskClient, kioskCap, scard, isLimitOrder = false, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, tx, sponsoredTx, suiCoinObjectsForPythUpdate) {
1202
+ if (!tx) {
1203
+ tx = new transactions_1.Transaction();
1204
+ }
1205
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
1206
+ tx = await this.addReferral(referralAddress, tx);
1207
+ }
1208
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
1209
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage || 0.003);
1210
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage || 0.5);
1211
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
1212
+ if (isLimitOrder) {
1213
+ allowTrade = isIocOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
1214
+ }
1215
+ const kioskTx = new kiosk_1.KioskTransaction({
1216
+ transaction: tx,
1217
+ kioskClient,
1218
+ cap: kioskCap,
1219
+ });
1220
+ const [sudoCard, promise] = kioskTx.borrow({
1221
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1222
+ itemId: scard,
1223
+ });
1224
+ // Handle oracle initialization and coin processing
1225
+ let suiCoinObject;
1226
+ if (sponsoredTx) {
1227
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
1228
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
1229
+ }
1230
+ else {
1231
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
1232
+ }
1233
+ // Process coin splitting
1234
+ const [feeObject] = tx.splitCoins(coinObj, [tx.pure.u64(relayerFee)]);
1235
+ tx.moveCall({
1236
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v1`,
1237
+ typeArguments: [
1238
+ `${this.consts.sudoCore.package}::slp::SLP`,
1239
+ this.consts.coins[collateralToken].module,
1240
+ this.consts.coins[indexToken].module,
1241
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1242
+ this.consts.coins[collateralToken].module,
1243
+ ],
1244
+ arguments: [
1245
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
1246
+ tx.object(this.consts.sudoCore.market),
1247
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
1248
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1249
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
1250
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1251
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1252
+ coinObj,
1253
+ feeObject,
1254
+ tx.pure.u8(allowTrade),
1255
+ tx.pure.u64(size),
1256
+ tx.pure.u64(reserveAmount),
1257
+ tx.pure.u256(adjustCollateralPrice),
1258
+ tx.pure.u256(adjustPrice),
1259
+ sudoCard,
1260
+ ],
1261
+ });
1262
+ kioskTx
1263
+ .return({
1264
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1265
+ item: sudoCard,
1266
+ promise,
1267
+ })
1268
+ .finalize();
1269
+ return tx;
1270
+ }
1271
+ async openPositionWithSCardV2(collateralToken, indexToken, size, collateralAmount, coinObjects, long, reserveAmount, indexPrice, collateralPrice, kioskClient, kioskCap, scard, isLimitOrder, isIocOrder, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, sponsoredTx, suiCoinObjectsForPythUpdate) {
1272
+ let tx = new transactions_1.Transaction();
1273
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
1274
+ tx = await this.addReferral(referralAddress, tx);
1275
+ }
1276
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
1277
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage);
1278
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
1279
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
1280
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
1281
+ if (isLimitOrder) {
1282
+ allowTrade = isIocOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
1283
+ }
1284
+ const kioskTx = new kiosk_1.KioskTransaction({
1285
+ transaction: tx,
1286
+ kioskClient,
1287
+ cap: kioskCap,
1288
+ });
1289
+ const [sudoCard, promise] = kioskTx.borrow({
1290
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1291
+ itemId: scard,
1292
+ });
1293
+ // Handle oracle initialization and coin processing
1294
+ let suiCoinObject;
1295
+ if (sponsoredTx) {
1296
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
1297
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
1298
+ }
1299
+ else {
1300
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
1301
+ }
1302
+ // Process coin splitting
1303
+ const [depositObject, feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
1304
+ tx.moveCall({
1305
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v2`,
1306
+ typeArguments: [
1307
+ `${this.consts.sudoCore.package}::slp::SLP`,
1308
+ this.consts.coins[collateralToken].module,
1309
+ this.consts.coins[indexToken].module,
1310
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1311
+ this.consts.coins[collateralToken].module,
1312
+ ],
1313
+ arguments: [
1314
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
1315
+ tx.object(this.consts.sudoCore.market),
1316
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
1317
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1318
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1319
+ depositObject,
1320
+ feeObject,
1321
+ tx.pure.u8(allowTrade),
1322
+ tx.pure.u64(size),
1323
+ tx.pure.u64(reserveAmount),
1324
+ tx.pure.u256(adjustCollateralPrice),
1325
+ tx.pure.u256(adjustPrice),
1326
+ tx.pure.u256(indexPriceThreshold),
1327
+ sudoCard,
1328
+ ],
1329
+ });
1330
+ kioskTx
1331
+ .return({
1332
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1333
+ item: sudoCard,
1334
+ promise,
1335
+ })
1336
+ .finalize();
1337
+ return tx;
1338
+ }
1339
+ // S Card operations
1340
+ async openPositionWithCoinAndSCardV2(collateralToken, indexToken, size, coinObj, long, reserveAmount, indexPrice, collateralPrice, kioskClient, kioskCap, scard, isLimitOrder, isIocOrder, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, tx, sponsoredTx, suiCoinObjectsForPythUpdate) {
1341
+ if (!tx) {
1342
+ tx = new transactions_1.Transaction();
1343
+ }
1344
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
1345
+ tx = await this.addReferral(referralAddress, tx);
1346
+ }
1347
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
1348
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage);
1349
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
1350
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
1351
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
1352
+ if (isLimitOrder) {
1353
+ allowTrade = isIocOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
1354
+ }
1355
+ const kioskTx = new kiosk_1.KioskTransaction({
1356
+ transaction: tx,
1357
+ kioskClient,
1358
+ cap: kioskCap,
1359
+ });
1360
+ const [sudoCard, promise] = kioskTx.borrow({
1361
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1362
+ itemId: scard,
1363
+ });
1364
+ // Handle oracle initialization and coin processing
1365
+ let suiCoinObject;
1366
+ if (sponsoredTx) {
1367
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
1368
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
1369
+ }
1370
+ else {
1371
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
1372
+ }
1373
+ // Process coin splitting
1374
+ const [feeObject] = tx.splitCoins(coinObj, [tx.pure.u64(relayerFee)]);
1375
+ tx.moveCall({
1376
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v2`,
1377
+ typeArguments: [
1378
+ `${this.consts.sudoCore.package}::slp::SLP`,
1379
+ this.consts.coins[collateralToken].module,
1380
+ this.consts.coins[indexToken].module,
1381
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1382
+ this.consts.coins[collateralToken].module,
1383
+ ],
1384
+ arguments: [
1385
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
1386
+ tx.object(this.consts.sudoCore.market),
1387
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
1388
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1389
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1390
+ coinObj,
1391
+ feeObject,
1392
+ tx.pure.u8(allowTrade),
1393
+ tx.pure.u64(size),
1394
+ tx.pure.u64(reserveAmount),
1395
+ tx.pure.u256(adjustCollateralPrice),
1396
+ tx.pure.u256(adjustPrice),
1397
+ tx.pure.u256(indexPriceThreshold),
1398
+ sudoCard,
1399
+ ],
1400
+ });
1401
+ kioskTx
1402
+ .return({
1403
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1404
+ item: sudoCard,
1405
+ promise,
1406
+ })
1407
+ .finalize();
1408
+ return tx;
1409
+ }
1410
+ async decreasePositionWithSCard(pcpId, collateralToken, indexToken, amount, long, indexPrice, collateralPrice, kioskClient, kioskCap, scard, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), coinObjects, sponsoredTx, suiCoinObjectsForPythUpdate) {
1411
+ if (!coinObjects) {
1412
+ throw new Error(`${this.constructor.name}: coinObjects is required`);
1413
+ }
1414
+ let tx = new transactions_1.Transaction();
1415
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
1416
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
1417
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
1418
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
1419
+ if (isTriggerOrder) {
1420
+ allowTrade = isIocOrder || !isTakeProfitOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
1421
+ }
1422
+ else {
1423
+ isTakeProfitOrder = true;
1424
+ }
1425
+ const kioskTx = new kiosk_1.KioskTransaction({
1426
+ transaction: tx,
1427
+ kioskClient,
1428
+ cap: kioskCap,
1429
+ });
1430
+ const [sudoCard, promise] = kioskTx.borrow({
1431
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1432
+ itemId: scard,
1433
+ });
1434
+ // Handle oracle initialization and coin processing
1435
+ let suiCoinObject;
1436
+ if (sponsoredTx) {
1437
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
1438
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
1439
+ }
1440
+ else {
1441
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
1442
+ }
1443
+ // Process coin splitting
1444
+ const [feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
1445
+ tx.moveCall({
1446
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_with_scard_v1`,
1447
+ typeArguments: [
1448
+ `${this.consts.sudoCore.package}::slp::SLP`,
1449
+ this.consts.coins[collateralToken].module,
1450
+ this.consts.coins[indexToken].module,
1451
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1452
+ this.consts.coins[collateralToken].module,
1453
+ ],
1454
+ arguments: [
1455
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
1456
+ tx.object(this.consts.sudoCore.market),
1457
+ tx.object(pcpId),
1458
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
1459
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1460
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1461
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1462
+ feeObject,
1463
+ tx.pure.u8(allowTrade),
1464
+ tx.pure.bool(isTakeProfitOrder),
1465
+ tx.pure.u64(amount),
1466
+ tx.pure.u256(adjustCollateralPrice),
1467
+ tx.pure.u256(adjustPrice),
1468
+ sudoCard,
1469
+ ],
1470
+ });
1471
+ kioskTx
1472
+ .return({
1473
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1474
+ item: sudoCard,
1475
+ promise,
1476
+ })
1477
+ .finalize();
1478
+ return tx;
1479
+ }
1480
+ async decreasePositionWithSCardV2(pcpId, collateralToken, indexToken, amount, long, indexPrice, collateralPrice, kioskClient, kioskCap, scard, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), coinObjects, sponsoredTx, suiCoinObjectsForPythUpdate) {
1481
+ if (!coinObjects) {
1482
+ throw new Error(`${this.constructor.name}: coinObjects is required`);
1483
+ }
1484
+ let tx = new transactions_1.Transaction();
1485
+ const kioskTx = new kiosk_1.KioskTransaction({
1486
+ transaction: tx,
1487
+ kioskClient,
1488
+ cap: kioskCap,
1489
+ });
1490
+ const [sudoCard, promise] = kioskTx.borrow({
1491
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1492
+ itemId: scard,
1493
+ });
1494
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
1495
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
1496
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
1497
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
1498
+ if (isTriggerOrder) {
1499
+ allowTrade = isIocOrder || !isTakeProfitOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
1500
+ }
1501
+ else {
1502
+ isTakeProfitOrder = true;
1503
+ }
1504
+ // Handle oracle initialization and coin processing
1505
+ let suiCoinObject;
1506
+ if (sponsoredTx) {
1507
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
1508
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject);
1509
+ }
1510
+ else {
1511
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx);
1512
+ }
1513
+ // Process coin splitting
1514
+ const [feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
1515
+ tx.moveCall({
1516
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_with_scard_v2`,
1517
+ typeArguments: [
1518
+ `${this.consts.sudoCore.package}::slp::SLP`,
1519
+ this.consts.coins[collateralToken].module,
1520
+ this.consts.coins[indexToken].module,
1521
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1522
+ this.consts.coins[collateralToken].module,
1523
+ ],
1524
+ arguments: [
1525
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
1526
+ tx.object(this.consts.sudoCore.market),
1527
+ tx.object(pcpId),
1528
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1529
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1530
+ feeObject,
1531
+ tx.pure.u8(allowTrade),
1532
+ tx.pure.bool(isTakeProfitOrder),
1533
+ tx.pure.u64(amount),
1534
+ tx.pure.u256(adjustCollateralPrice),
1535
+ tx.pure.u256(adjustPrice),
1536
+ tx.pure.u256(indexPriceThreshold),
1537
+ sudoCard,
1538
+ ],
1539
+ });
1540
+ kioskTx
1541
+ .return({
1542
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1543
+ item: sudoCard,
1544
+ promise,
1545
+ })
1546
+ .finalize();
1547
+ return tx;
1548
+ }
1549
+ async decreaseMultiPositionsWithSCard(positions, kioskClient, kioskCap, scard, tx, sponsoredTx, suiCoinObjectsForPythUpdate) {
1550
+ if (!tx) {
1551
+ tx = new transactions_1.Transaction();
1552
+ }
1553
+ // Handle oracle initialization and coin processing
1554
+ let suiCoinObject;
1555
+ if (sponsoredTx) {
1556
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true);
1557
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx, true, suiCoinObject);
1558
+ }
1559
+ else {
1560
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx);
1561
+ }
1562
+ const kioskTx = new kiosk_1.KioskTransaction({
1563
+ transaction: tx,
1564
+ kioskClient,
1565
+ cap: kioskCap,
1566
+ });
1567
+ const [sudoCard, promise] = kioskTx.borrow({
1568
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1569
+ itemId: scard,
1570
+ });
1571
+ for (const position of positions) {
1572
+ const { pcpId, collateralToken, indexToken, amount, long, indexPrice, collateralPrice, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), coinObjects = [], } = position;
1573
+ let innerIsTakeProfitOrder = isTakeProfitOrder;
1574
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
1575
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
1576
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
1577
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
1578
+ if (isTriggerOrder) {
1579
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
1580
+ }
1581
+ else {
1582
+ innerIsTakeProfitOrder = true;
1583
+ }
1584
+ // Process coin splitting
1585
+ const [feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
1586
+ tx.moveCall({
1587
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_with_scard_v1`,
1588
+ typeArguments: [
1589
+ `${this.consts.sudoCore.package}::slp::SLP`,
1590
+ this.consts.coins[collateralToken].module,
1591
+ this.consts.coins[indexToken].module,
1592
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1593
+ this.consts.coins[collateralToken].module,
1594
+ ],
1595
+ arguments: [
1596
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
1597
+ tx.object(this.consts.sudoCore.market),
1598
+ tx.object(pcpId),
1599
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
1600
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1601
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1602
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1603
+ feeObject,
1604
+ tx.pure.u8(allowTrade),
1605
+ tx.pure.bool(isTakeProfitOrder),
1606
+ tx.pure.u64(amount),
1607
+ tx.pure.u256(adjustCollateralPrice),
1608
+ tx.pure.u256(adjustPrice),
1609
+ sudoCard,
1610
+ ],
1611
+ });
1612
+ }
1613
+ kioskTx
1614
+ .return({
1615
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1616
+ item: sudoCard,
1617
+ promise,
1618
+ })
1619
+ .finalize();
1620
+ return tx;
1621
+ }
1622
+ async decreaseMultiPositionsWithSCardV2(positions, kioskClient, kioskCap, scard, tx, sponsoredTx, suiCoinObjectsForPythUpdate, feeObjects) {
1623
+ const transaction = tx ?? new transactions_1.Transaction();
1624
+ // Handle oracle initialization and coin processing
1625
+ const tokens = positions.flatMap(position => [position.collateralToken, position.indexToken]);
1626
+ const suiCoinObject = sponsoredTx
1627
+ ? this.processCoins(transaction, 'sui', suiCoinObjectsForPythUpdate || [], true)
1628
+ : undefined;
1629
+ await this.initOracleTxb(tokens, transaction, !!sponsoredTx, suiCoinObject);
1630
+ if (!feeObjects || feeObjects.length !== positions.length) {
1631
+ throw new Error('feeObjects must be provided (pre-processed via buildFeeCoinObjects) and match positions length');
1632
+ }
1633
+ const kioskTx = new kiosk_1.KioskTransaction({
1634
+ transaction,
1635
+ kioskClient,
1636
+ cap: kioskCap,
1637
+ });
1638
+ const [sudoCard, promise] = kioskTx.borrow({
1639
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1640
+ itemId: scard,
1641
+ });
1642
+ for (const [i, position] of positions.entries()) {
1643
+ const feeObject = feeObjects[i];
1644
+ const { pcpId, collateralToken, indexToken, amount, long, indexPrice, collateralPrice, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, } = position;
1645
+ let innerIsTakeProfitOrder = isTakeProfitOrder;
1646
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
1647
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
1648
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
1649
+ let allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
1650
+ if (isTriggerOrder) {
1651
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? consts_1.ALLOW_TRADE_NO_TRADE : consts_1.ALLOW_TRADE_CAN_TRADE;
1652
+ }
1653
+ else {
1654
+ innerIsTakeProfitOrder = true;
1655
+ }
1656
+ transaction.moveCall({
1657
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_with_scard_v2`,
1658
+ typeArguments: [
1659
+ `${this.consts.sudoCore.package}::slp::SLP`,
1660
+ this.consts.coins[collateralToken].module,
1661
+ this.consts.coins[indexToken].module,
1662
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1663
+ this.consts.coins[collateralToken].module,
1664
+ ],
1665
+ arguments: [
1666
+ transaction.object(utils_1.SUI_CLOCK_OBJECT_ID),
1667
+ transaction.object(this.consts.sudoCore.market),
1668
+ transaction.object(pcpId),
1669
+ transaction.object(this.consts.pythFeeder.feeder[collateralToken]),
1670
+ transaction.object(this.consts.pythFeeder.feeder[indexToken]),
1671
+ feeObject,
1672
+ transaction.pure.u8(allowTrade),
1673
+ transaction.pure.bool(innerIsTakeProfitOrder),
1674
+ transaction.pure.u64(amount),
1675
+ transaction.pure.u256(adjustCollateralPrice),
1676
+ transaction.pure.u256(adjustPrice),
1677
+ transaction.pure.u256(indexPriceThreshold),
1678
+ sudoCard,
1679
+ ],
1680
+ });
1681
+ }
1682
+ kioskTx
1683
+ .return({
1684
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1685
+ item: sudoCard,
1686
+ promise,
1687
+ })
1688
+ .finalize();
1689
+ return transaction;
1690
+ }
1691
+ addReferral(referrer, tx) {
1692
+ if (!tx) {
1693
+ tx = new transactions_1.Transaction();
1694
+ }
1695
+ tx.moveCall({
1696
+ target: `${this.consts.sudoCore.upgradedPackage}::market::add_new_referral`,
1697
+ typeArguments: [`${this.consts.sudoCore.package}::slp::SLP`],
1698
+ arguments: [tx.object(this.consts.sudoCore.market), tx.pure.address(referrer)],
1699
+ });
1700
+ return tx;
1701
+ }
1702
+ async adminUpdatePriceFeed(collateralToken, indexToken) {
1703
+ const tx = await this.initOracleTxb([collateralToken, indexToken]);
1704
+ return tx;
1705
+ }
1706
+ adminSettlePosition(_positionId, _owner, _collateralToken, _indexToken, _long) {
1707
+ // SLP does not support this operation
1708
+ throw new Error(`Method not implemented for ${this.constructor.name}`);
1709
+ }
1710
+ adminDecreasePosition(positionId, owner, collateralToken, indexToken, positionAmount, amount, long, collateralPrice, collateralSlippage, relayerFee) {
1711
+ const tx = new transactions_1.Transaction();
1712
+ const symbol = (0, utils_2.joinSymbol)(long ? 'long' : 'short', indexToken);
1713
+ const feeObject = tx.splitCoins(tx.gas, [tx.pure.u64(relayerFee || BigInt(1))]);
1714
+ const adjustCollateralPrice = this.processSlippage(collateralPrice || 0, false, collateralSlippage || 0.5);
1715
+ const allowTrade = consts_1.ALLOW_TRADE_MUST_TRADE;
1716
+ tx.moveCall({
1717
+ target: `${this.consts.sudoCore.upgradedPackage}::market::admin_decrease_position_v1_3`,
1718
+ typeArguments: [
1719
+ `${this.consts.sudoCore.package}::slp::SLP`,
1720
+ this.consts.coins[collateralToken].module,
1721
+ this.consts.coins[indexToken].module,
1722
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1723
+ this.consts.coins.sui.module,
1724
+ ],
1725
+ arguments: [
1726
+ tx.object(this.consts.sudoCore.adminCap),
1727
+ tx.object(utils_1.SUI_CLOCK_OBJECT_ID),
1728
+ tx.object(this.consts.sudoCore.market),
1729
+ tx.object(positionId),
1730
+ tx.object(this.consts.sudoCore.vaults[collateralToken].reservingFeeModel),
1731
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1732
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1733
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1734
+ feeObject,
1735
+ tx.pure.u8(allowTrade),
1736
+ tx.pure.u64(amount),
1737
+ tx.pure.u256(adjustCollateralPrice),
1738
+ tx.object(owner),
1739
+ ],
1740
+ });
1741
+ if (amount === BigInt(positionAmount)) {
1742
+ this.adminClearClosedPosition(positionId, owner, collateralToken, indexToken, long, tx);
1743
+ }
1744
+ return tx;
1745
+ }
1746
+ adminClearClosedPosition(positionId, owner, collateralToken, indexToken, long, tx) {
1747
+ tx.moveCall({
1748
+ target: `${this.consts.sudoCore.upgradedPackage}::market::admin_clear_closed_position_v1_1`,
1749
+ typeArguments: [
1750
+ `${this.consts.sudoCore.package}::slp::SLP`,
1751
+ this.consts.coins[collateralToken].module,
1752
+ this.consts.coins[indexToken].module,
1753
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1754
+ ],
1755
+ arguments: [
1756
+ tx.object(this.consts.sudoCore.adminCap),
1757
+ tx.object(owner),
1758
+ tx.object(this.consts.sudoCore.market),
1759
+ tx.object(positionId),
1760
+ ],
1761
+ });
1762
+ }
1763
+ // SLP Specific APIs
1764
+ claimTokenFromSCard(token, coinObjects, kioskClient, kioskCap, scard) {
1765
+ const tx = new transactions_1.Transaction();
1766
+ const kioskTx = new kiosk_1.KioskTransaction({
1767
+ transaction: tx,
1768
+ kioskClient,
1769
+ cap: kioskCap,
1770
+ });
1771
+ const [sudoCard, promise] = kioskTx.borrow({
1772
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1773
+ itemId: scard,
1774
+ });
1775
+ // claim tokens
1776
+ coinObjects.forEach((coinObject) => {
1777
+ return tx.moveCall({
1778
+ target: `${this.consts.sudoNft.upgradedPackage}::card::claim_token`,
1779
+ typeArguments: [`${this.consts.coins[token].module}`],
1780
+ arguments: [sudoCard, tx.object(coinObject)],
1781
+ });
1782
+ });
1783
+ kioskTx
1784
+ .return({
1785
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1786
+ item: sudoCard,
1787
+ promise,
1788
+ })
1789
+ .finalize();
1790
+ return tx;
1791
+ }
1792
+ }
1793
+ exports.SLPAPI = SLPAPI;
1794
+ //# sourceMappingURL=SLPAPI.cjs.map