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