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