@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,1337 @@
1
+ /**
2
+ * ZBTCVC API implementation
3
+ * Implements ZBTCVC-specific trading and transaction methods
4
+ */
5
+
6
+ import type { KioskClient, KioskOwnerCap } from '@mysten/kiosk'
7
+ import { KioskTransaction } from '@mysten/kiosk'
8
+ import type { SuiClient } from '@mysten/sui/client'
9
+ import { Transaction } from '@mysten/sui/transactions'
10
+ import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui/utils'
11
+
12
+ import { BaseAPI } from '../abstract'
13
+ import type { Network } from '../consts'
14
+ import { ALLOW_TRADE_CAN_TRADE, ALLOW_TRADE_MUST_TRADE, ALLOW_TRADE_NO_TRADE, LPToken } from '../consts'
15
+ import type {
16
+ IBasePositionInfo,
17
+ IZBTCVCAPI,
18
+ IZBTCVCCredential,
19
+ } from '../interfaces'
20
+ import { joinSymbol } from '../utils'
21
+ import { ZBTCVCDataAPI } from './ZBTCVCDataAPI'
22
+
23
+ export class ZBTCVCAPI extends BaseAPI implements IZBTCVCAPI {
24
+ public dataAPI: ZBTCVCDataAPI
25
+
26
+ constructor(
27
+ network: Network,
28
+ provider: SuiClient,
29
+ apiEndpoint: string,
30
+ connectionURL: string,
31
+ ) {
32
+ super(network, provider, apiEndpoint, connectionURL, LPToken.ZBTCVC)
33
+ this.dataAPI = new ZBTCVCDataAPI(network, provider, apiEndpoint, connectionURL)
34
+ }
35
+
36
+ public calcPositionReserveFeeAmount(_position: IBasePositionInfo): Promise<number> {
37
+ throw new Error(`Method not implemented in ${this.constructor.name}.`)
38
+ }
39
+
40
+ public calcPositionFundingFeeValue(_position: IBasePositionInfo): Promise<number> {
41
+ throw new Error(`Method not implemented in ${this.constructor.name}.`)
42
+ }
43
+
44
+ public clearClosedPosition(pcpId: string, collateralToken: string, indexToken: string, long: boolean, tx: Transaction): void {
45
+ tx.moveCall({
46
+ target: `${this.consts.zoCore.upgradedPackage}::market::clear_closed_position`,
47
+ typeArguments: [
48
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
49
+ this.consts.coins[collateralToken].module,
50
+ this.consts.coins[indexToken].module,
51
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
52
+ ],
53
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(pcpId)],
54
+ })
55
+ }
56
+
57
+ public clearOpenPositionOrder(orderCapId: string, collateralToken: string, indexToken: string, long: boolean, tx: Transaction, isV11Order?: boolean): void {
58
+ const funcName = isV11Order ? 'clear_open_position_order' : 'clear_open_position_order'
59
+ tx.moveCall({
60
+ target: `${this.consts.zoCore.upgradedPackage}::market::${funcName}`,
61
+ typeArguments: [
62
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
63
+ this.consts.coins[collateralToken].module,
64
+ this.consts.coins[indexToken].module,
65
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
66
+ this.consts.coins[collateralToken].module,
67
+ ],
68
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(orderCapId)],
69
+ })
70
+ }
71
+
72
+ public clearDecreasePositionOrder(orderCapId: string, collateralToken: string, indexToken: string, long: boolean, tx: Transaction, isV11Order?: boolean): void {
73
+ const funcName = isV11Order ? 'clear_decrease_position_order' : 'clear_decrease_position_order'
74
+ tx.moveCall({
75
+ target: `${this.consts.zoCore.upgradedPackage}::market::${funcName}`,
76
+ typeArguments: [
77
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
78
+ this.consts.coins[collateralToken].module,
79
+ this.consts.coins[indexToken].module,
80
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
81
+ this.consts.coins[collateralToken].module,
82
+ ],
83
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(orderCapId)],
84
+ })
85
+ }
86
+
87
+ public async openPositionWithSCard(
88
+ collateralToken: string,
89
+ indexToken: string,
90
+ size: bigint,
91
+ collateralAmount: bigint,
92
+ coinObjects: string[],
93
+ long: boolean,
94
+ reserveAmount: bigint,
95
+ indexPrice: number,
96
+ collateralPrice: number,
97
+ kioskClient: KioskClient,
98
+ kioskCap: KioskOwnerCap,
99
+ scard: string,
100
+ isLimitOrder?: boolean,
101
+ isIocOrder?: boolean,
102
+ pricesSlippage = 0.003,
103
+ collateralSlippage = 0.5,
104
+ relayerFee = BigInt(0.5),
105
+ referralAddress?: string,
106
+ sender?: string,
107
+ sponsoredTx?: boolean,
108
+ suiCoinObjectsForPythUpdate?: string[],
109
+ ): Promise<Transaction> {
110
+ let tx = new Transaction()
111
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
112
+ tx = await this.addReferral(referralAddress, tx)
113
+ }
114
+
115
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
116
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage)
117
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
118
+
119
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
120
+ if (isLimitOrder) {
121
+ allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
122
+ }
123
+
124
+ const kioskTx = new KioskTransaction({
125
+ transaction: tx,
126
+ kioskClient,
127
+ cap: kioskCap,
128
+ })
129
+
130
+ const [sudoCard, promise] = kioskTx.borrow({
131
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
132
+ itemId: scard,
133
+ })
134
+
135
+ // Handle oracle initialization and coin processing
136
+ let suiCoinObject
137
+ if (sponsoredTx) {
138
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
139
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
140
+ }
141
+ else {
142
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
143
+ }
144
+
145
+ // Process coin splitting
146
+ const [depositObject, feeObject] = this.processCoinSplitting(
147
+ tx,
148
+ collateralToken,
149
+ coinObjects,
150
+ [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)],
151
+ sponsoredTx,
152
+ suiCoinObject,
153
+ )
154
+
155
+ tx.moveCall({
156
+ target: `${this.consts.zoCore.upgradedPackage}::market::open_position_with_scard`,
157
+ typeArguments: [
158
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
159
+ this.consts.coins[collateralToken].module,
160
+ this.consts.coins[indexToken].module,
161
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
162
+ this.consts.coins[collateralToken].module,
163
+ ],
164
+ arguments: [
165
+ tx.object(SUI_CLOCK_OBJECT_ID),
166
+ tx.object(this.consts.zoCore.market),
167
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
168
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
169
+ tx.object(this.consts.zoCore.symbols[symbol].positionConfig),
170
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
171
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
172
+ depositObject,
173
+ feeObject,
174
+ tx.pure.u8(allowTrade),
175
+ tx.pure.u64(size),
176
+ tx.pure.u64(reserveAmount),
177
+ tx.pure.u256(adjustCollateralPrice),
178
+ tx.pure.u256(adjustPrice),
179
+ sudoCard,
180
+ ],
181
+ })
182
+
183
+ kioskTx
184
+ .return({
185
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
186
+ item: sudoCard,
187
+ promise,
188
+ })
189
+ .finalize()
190
+ return tx
191
+ }
192
+
193
+ public async decreasePositionWithSCard(
194
+ pcpId: string,
195
+ collateralToken: string,
196
+ indexToken: string,
197
+ amount: bigint,
198
+ long: boolean,
199
+ indexPrice: number,
200
+ collateralPrice: number,
201
+ kioskClient: KioskClient,
202
+ kioskCap: KioskOwnerCap,
203
+ scard: string,
204
+ isTriggerOrder = false,
205
+ isTakeProfitOrder = true,
206
+ isIocOrder = false,
207
+ pricesSlippage = 0.003,
208
+ collateralSlippage = 0.5,
209
+ relayerFee = BigInt(0.5),
210
+ coinObjects?: string[],
211
+ sponsoredTx?: boolean,
212
+ suiCoinObjectsForPythUpdate?: string[],
213
+ ): Promise<Transaction> {
214
+ if (!coinObjects) {
215
+ throw new Error(`${this.constructor.name}: coinObjects is required`)
216
+ }
217
+
218
+ let tx = new Transaction()
219
+
220
+ const kioskTx = new KioskTransaction({
221
+ transaction: tx,
222
+ kioskClient,
223
+ cap: kioskCap,
224
+ })
225
+
226
+ const [sudoCard, promise] = kioskTx.borrow({
227
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
228
+ itemId: scard,
229
+ })
230
+
231
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
232
+
233
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
234
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
235
+
236
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
237
+ if (isTriggerOrder) {
238
+ allowTrade = isIocOrder || !isTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
239
+ }
240
+ else {
241
+ isTakeProfitOrder = true
242
+ }
243
+
244
+ // Handle oracle initialization and coin processing
245
+ let suiCoinObject
246
+ if (sponsoredTx) {
247
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
248
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
249
+ }
250
+ else {
251
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
252
+ }
253
+
254
+ // Process coin splitting
255
+ const [feeObject] = this.processCoinSplitting(
256
+ tx,
257
+ collateralToken,
258
+ coinObjects,
259
+ [tx.pure.u64(relayerFee)],
260
+ sponsoredTx,
261
+ suiCoinObject,
262
+ )
263
+
264
+ tx.moveCall({
265
+ target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_with_scard`,
266
+ typeArguments: [
267
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
268
+ this.consts.coins[collateralToken].module,
269
+ this.consts.coins[indexToken].module,
270
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
271
+ this.consts.coins[collateralToken].module,
272
+ ],
273
+ arguments: [
274
+ tx.object(SUI_CLOCK_OBJECT_ID),
275
+ tx.object(this.consts.zoCore.market),
276
+ tx.object(pcpId),
277
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
278
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
279
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
280
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
281
+ feeObject,
282
+ tx.pure.u8(allowTrade),
283
+ tx.pure.bool(isTakeProfitOrder),
284
+ tx.pure.u64(amount),
285
+ tx.pure.u256(adjustCollateralPrice),
286
+ tx.pure.u256(adjustPrice),
287
+ sudoCard,
288
+ ],
289
+ })
290
+
291
+ kioskTx
292
+ .return({
293
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
294
+ item: sudoCard,
295
+ promise,
296
+ })
297
+ .finalize()
298
+ return tx
299
+ }
300
+
301
+ public async decreaseMultiPositionsWithSCard(
302
+ positions: Array<{
303
+ pcpId: string
304
+ collateralToken: string
305
+ indexToken: string
306
+ amount: bigint
307
+ long: boolean
308
+ indexPrice: number
309
+ collateralPrice: number
310
+ isTriggerOrder?: boolean
311
+ isTakeProfitOrder?: boolean
312
+ isIocOrder?: boolean
313
+ pricesSlippage?: number
314
+ collateralSlippage?: number
315
+ relayerFee?: bigint
316
+ coinObjects?: string[]
317
+ }>,
318
+ kioskClient: KioskClient,
319
+ kioskCap: KioskOwnerCap,
320
+ scard: string,
321
+ tx?: Transaction,
322
+ sponsoredTx?: boolean,
323
+ suiCoinObjectsForPythUpdate?: string[],
324
+ ): Promise<Transaction> {
325
+ if (!tx) {
326
+ tx = new Transaction()
327
+ }
328
+
329
+ // Handle oracle initialization and coin processing
330
+ let suiCoinObject
331
+ if (sponsoredTx) {
332
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
333
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx, true, suiCoinObject)
334
+ }
335
+ else {
336
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
337
+ }
338
+
339
+ const kioskTx = new KioskTransaction({
340
+ transaction: tx,
341
+ kioskClient,
342
+ cap: kioskCap,
343
+ })
344
+
345
+ const [sudoCard, promise] = kioskTx.borrow({
346
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
347
+ itemId: scard,
348
+ })
349
+
350
+ for (const position of positions) {
351
+ const {
352
+ pcpId,
353
+ collateralToken,
354
+ coinObjects = [],
355
+ indexToken,
356
+ amount,
357
+ long,
358
+ indexPrice,
359
+ collateralPrice,
360
+ isTriggerOrder = false,
361
+ isTakeProfitOrder = true,
362
+ isIocOrder = false,
363
+ pricesSlippage = 0.003,
364
+ collateralSlippage = 0.5,
365
+ relayerFee = BigInt(0.5),
366
+ } = position
367
+ let innerIsTakeProfitOrder = isTakeProfitOrder
368
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
369
+
370
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
371
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
372
+
373
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
374
+ if (isTriggerOrder) {
375
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
376
+ }
377
+ else {
378
+ innerIsTakeProfitOrder = true
379
+ }
380
+
381
+ // Process coin splitting
382
+ const [feeObject] = this.processCoinSplitting(
383
+ tx,
384
+ collateralToken,
385
+ coinObjects,
386
+ [tx.pure.u64(relayerFee)],
387
+ sponsoredTx,
388
+ suiCoinObject,
389
+ )
390
+
391
+ tx.moveCall({
392
+ target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_with_scard`,
393
+ typeArguments: [
394
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
395
+ this.consts.coins[collateralToken].module,
396
+ this.consts.coins[indexToken].module,
397
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
398
+ this.consts.coins[collateralToken].module,
399
+ ],
400
+ arguments: [
401
+ tx.object(SUI_CLOCK_OBJECT_ID),
402
+ tx.object(this.consts.zoCore.market),
403
+ tx.object(pcpId),
404
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
405
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
406
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
407
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
408
+ feeObject,
409
+ tx.pure.u8(allowTrade),
410
+ tx.pure.bool(innerIsTakeProfitOrder),
411
+ tx.pure.u64(amount),
412
+ tx.pure.u256(adjustCollateralPrice),
413
+ tx.pure.u256(adjustPrice),
414
+ sudoCard,
415
+ ],
416
+ })
417
+ }
418
+
419
+ kioskTx
420
+ .return({
421
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
422
+ item: sudoCard,
423
+ promise,
424
+ })
425
+ .finalize()
426
+ return tx
427
+ }
428
+
429
+ /**
430
+ * Deposits collateral into ZBTCVC vault
431
+ */
432
+ public async deposit(
433
+ coin: string,
434
+ coinObjects: string[],
435
+ amount: number,
436
+ minAmountOut = 0,
437
+ referralAddress?: string,
438
+ sender?: string,
439
+ sponsoredTx?: boolean,
440
+ suiCoinObjectsForPythUpdate?: string[],
441
+ ): Promise<Transaction> {
442
+ let tx = new Transaction()
443
+
444
+ // Add referral if needed
445
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
446
+ tx = await this.addReferral(referralAddress, tx)
447
+ }
448
+
449
+ // Initialize oracle transaction
450
+ const pythFeederKeys = Object.keys(this.consts.pythFeeder.feeder)
451
+
452
+ // Handle sponsored transaction case
453
+ if (sponsoredTx) {
454
+ const suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
455
+ tx = await this.initOracleTxb(pythFeederKeys, tx, true, suiCoinObject)
456
+
457
+ // Process deposit coins
458
+ const depositObject = coin === 'sui'
459
+ ? tx.splitCoins(suiCoinObject, [tx.pure.u64(amount)])[0]
460
+ : tx.splitCoins(this.processCoins(tx, coin, coinObjects, true), [tx.pure.u64(amount)])[0]
461
+
462
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx)
463
+
464
+ tx.moveCall({
465
+ target: `${this.consts.zoCore.upgradedPackage}::market::deposit`,
466
+ typeArguments: [`${this.consts.zoCore.package}::zbtcvc::ZBTCVC`, this.consts.coins[coin].module],
467
+ arguments: [
468
+ tx.object(this.consts.zoCore.market),
469
+ tx.object(this.consts.zoCore.rebaseFeeModel),
470
+ depositObject,
471
+ tx.pure.u64(minAmountOut),
472
+ vaultsValuation,
473
+ symbolsValuation,
474
+ ],
475
+ })
476
+ return tx
477
+ }
478
+
479
+ // Handle non-sponsored transaction case
480
+ tx = await this.initOracleTxb(pythFeederKeys, tx)
481
+ const depositObject = tx.splitCoins(
482
+ this.processCoins(tx, coin, coinObjects, false),
483
+ [tx.pure.u64(amount)],
484
+ )[0]
485
+
486
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx)
487
+
488
+ tx.moveCall({
489
+ target: `${this.consts.zoCore.upgradedPackage}::market::deposit`,
490
+ typeArguments: [
491
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
492
+ this.consts.coins[coin].module,
493
+ ],
494
+ arguments: [
495
+ tx.object(this.consts.zoCore.market),
496
+ tx.object(this.consts.zoCore.rebaseFeeModel),
497
+ depositObject,
498
+ tx.pure.u64(minAmountOut),
499
+ vaultsValuation,
500
+ symbolsValuation,
501
+ ],
502
+ })
503
+ return tx
504
+ }
505
+
506
+ /**
507
+ * Deposits collateral into ZBTCVC vault
508
+ */
509
+ public async depositPtb(
510
+ coin: string,
511
+ coinObjects: string[],
512
+ amount: number,
513
+ minAmountOut = 0,
514
+ referralAddress?: string,
515
+ sender?: string,
516
+ tx?: Transaction,
517
+ sponsoredTx?: boolean,
518
+ suiCoinObjectsForPythUpdate?: string[],
519
+ ): Promise<any> {
520
+ if (!tx) {
521
+ tx = new Transaction()
522
+ }
523
+
524
+ // Add referral if needed
525
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
526
+ tx = await this.addReferral(referralAddress, tx)
527
+ }
528
+
529
+ // Initialize oracle transaction
530
+ const pythFeederKeys = Object.keys(this.consts.pythFeeder.feeder)
531
+
532
+ // Handle sponsored transaction case
533
+ if (sponsoredTx) {
534
+ const suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
535
+ tx = await this.initOracleTxb(pythFeederKeys, tx, true, suiCoinObject)
536
+
537
+ // Process deposit coins
538
+ const depositObject = coin === 'sui'
539
+ ? tx.splitCoins(suiCoinObject, [tx.pure.u64(amount)])[0]
540
+ : tx.splitCoins(this.processCoins(tx, coin, coinObjects, true), [tx.pure.u64(amount)])[0]
541
+
542
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx)
543
+
544
+ const [mintedCoin] = tx.moveCall({
545
+ target: `${this.consts.zoCore.upgradedPackage}::market::deposit_ptb`,
546
+ typeArguments: [`${this.consts.zoCore.package}::zbtcvc::ZBTCVC`, this.consts.coins[coin].module],
547
+ arguments: [
548
+ tx.object(this.consts.zoCore.market),
549
+ tx.object(this.consts.zoCore.rebaseFeeModel),
550
+ depositObject,
551
+ tx.pure.u64(minAmountOut),
552
+ vaultsValuation,
553
+ symbolsValuation,
554
+ ],
555
+ })
556
+ return mintedCoin
557
+ }
558
+
559
+ // Handle non-sponsored transaction case
560
+ tx = await this.initOracleTxb(pythFeederKeys, tx)
561
+ const depositObject = tx.splitCoins(
562
+ this.processCoins(tx, coin, coinObjects, false),
563
+ [tx.pure.u64(amount)],
564
+ )[0]
565
+
566
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx)
567
+
568
+ const [mintedCoin] = tx.moveCall({
569
+ target: `${this.consts.zoCore.upgradedPackage}::market::deposit_ptb`,
570
+ typeArguments: [
571
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
572
+ this.consts.coins[coin].module,
573
+ ],
574
+ arguments: [
575
+ tx.object(this.consts.zoCore.market),
576
+ tx.object(this.consts.zoCore.rebaseFeeModel),
577
+ depositObject,
578
+ tx.pure.u64(minAmountOut),
579
+ vaultsValuation,
580
+ symbolsValuation,
581
+ ],
582
+ })
583
+
584
+ return mintedCoin
585
+ }
586
+
587
+ /**
588
+ * Withdraws collateral from ZBTCVC vault
589
+ */
590
+ public async withdraw(
591
+ coin: string,
592
+ lpCoinObjects: string[],
593
+ amount: number,
594
+ minAmountOut = 0,
595
+ sponsoredTx?: boolean,
596
+ suiCoinObjectsForPythUpdate?: string[],
597
+ ): Promise<Transaction> {
598
+ let tx = new Transaction()
599
+
600
+ // Initialize oracle transaction
601
+ const pythFeederKeys = Object.keys(this.consts.pythFeeder.feeder)
602
+
603
+ if (sponsoredTx) {
604
+ const suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
605
+ tx = await this.initOracleTxb(pythFeederKeys, tx, true, suiCoinObject)
606
+ }
607
+ else {
608
+ tx = await this.initOracleTxb(pythFeederKeys, tx)
609
+ }
610
+ const zbtcvcCoinObject = this.processCoins(tx, 'zbtcvc', lpCoinObjects, false)
611
+ const [withdrawObject] = tx.splitCoins(zbtcvcCoinObject, [tx.pure.u64(amount)])
612
+ const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx)
613
+
614
+ tx.moveCall({
615
+ target: `${this.consts.zoCore.upgradedPackage}::market::withdraw`,
616
+ typeArguments: [
617
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
618
+ this.consts.coins[coin].module,
619
+ ],
620
+ arguments: [
621
+ tx.object(this.consts.zoCore.market),
622
+ tx.object(this.consts.zoCore.rebaseFeeModel),
623
+ withdrawObject,
624
+ tx.pure.u64(minAmountOut),
625
+ vaultsValuation,
626
+ symbolsValuation,
627
+ ],
628
+ })
629
+ return tx
630
+ }
631
+
632
+ /**
633
+ * Stakes ZBTCVC tokens in ZO staking pools
634
+ */
635
+ public stake(
636
+ lpCoinObjects: string[],
637
+ amount: bigint,
638
+ pool: string,
639
+ tx?: Transaction,
640
+ ): Transaction {
641
+ if (!tx) {
642
+ tx = new Transaction()
643
+ }
644
+
645
+ const coinObject = this.processCoins(tx, 'zbtcvc', lpCoinObjects)
646
+ const [depositObject] = tx.splitCoins(coinObject, [tx.pure.u64(amount)])
647
+
648
+ tx.moveCall({
649
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::deposit`,
650
+ typeArguments: [
651
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
652
+ `${this.consts.coins.sui.module}`,
653
+ ],
654
+ arguments: [
655
+ tx.object(pool),
656
+ tx.object(SUI_CLOCK_OBJECT_ID),
657
+ depositObject,
658
+ ],
659
+ })
660
+ return tx
661
+ }
662
+
663
+ /**
664
+ * Stakes ZBTCVC tokens using coin object only (PTB)
665
+ */
666
+ public stakeCoinObject(
667
+ coinObject: any,
668
+ pool: string,
669
+ tx?: Transaction,
670
+ ): Transaction {
671
+ if (!tx) {
672
+ tx = new Transaction()
673
+ }
674
+
675
+ tx.moveCall({
676
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::deposit`,
677
+ typeArguments: [
678
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
679
+ `${this.consts.coins.sui.module}`,
680
+ ],
681
+ arguments: [
682
+ tx.object(pool),
683
+ tx.object(SUI_CLOCK_OBJECT_ID),
684
+ coinObject,
685
+ ],
686
+ })
687
+ return tx
688
+ }
689
+
690
+ /**
691
+ * Unstakes ZBTCVC tokens from ZO staking pools
692
+ */
693
+ public unstake(
694
+ credentials: IZBTCVCCredential[],
695
+ amount: bigint,
696
+ pool: string,
697
+ tx?: Transaction,
698
+ ): Transaction {
699
+ let unstakeAmount = amount
700
+ if (!tx) {
701
+ tx = new Transaction()
702
+ }
703
+ for (const credential of credentials) {
704
+ const withdrawAmount = (() => {
705
+ const min = (a: bigint, b: bigint) => a < b ? a : b
706
+ return min(unstakeAmount, credential.amount)
707
+ })()
708
+ unstakeAmount -= withdrawAmount
709
+ tx.moveCall({
710
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::withdraw`,
711
+ typeArguments: [
712
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
713
+ `${this.consts.coins.sui.module}`,
714
+ ],
715
+ arguments: [
716
+ tx.object(pool),
717
+ tx.object(SUI_CLOCK_OBJECT_ID),
718
+ tx.object(credential.id),
719
+ tx.pure.u64(withdrawAmount),
720
+ ],
721
+ })
722
+ if (credential.amount === BigInt(0)) {
723
+ tx.moveCall({
724
+ target: `${this.sharedConfig.zoStaking.upgradedPackage}::pool::clear_empty_credential`,
725
+ typeArguments: [
726
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
727
+ `${this.consts.coins.sui.module}`,
728
+ ],
729
+ arguments: [tx.object(credential.id)],
730
+ })
731
+ }
732
+ }
733
+ return tx
734
+ }
735
+
736
+ public async swap(
737
+ fromToken: string,
738
+ toToken: string,
739
+ fromAmount: bigint,
740
+ fromCoinObjects: string[],
741
+ minAmountOut?: number,
742
+ ): Promise<Transaction> {
743
+ const tx = await this.initOracleTxb(Object.keys(this.consts.zoCore.vaults))
744
+ const fromCoinObject = this.processCoins(tx, fromToken, fromCoinObjects)
745
+ const [fromDepositObject] = tx.splitCoins(fromCoinObject, [tx.pure.u64(fromAmount)])
746
+ const vaultsValuation = this.dataAPI.valuateVaults(tx)
747
+
748
+ tx.moveCall({
749
+ target: `${this.consts.zoCore.upgradedPackage}::market::swap`,
750
+ typeArguments: [
751
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
752
+ this.consts.coins[fromToken].module,
753
+ this.consts.coins[toToken].module,
754
+ ],
755
+ arguments: [
756
+ tx.object(this.consts.zoCore.market),
757
+ tx.object(this.consts.zoCore.rebaseFeeModel),
758
+ fromDepositObject,
759
+ tx.pure.u64(minAmountOut || 0),
760
+ vaultsValuation,
761
+ ],
762
+ })
763
+ return tx
764
+ }
765
+
766
+ /**
767
+ * Opens a new position in ZBTCVC
768
+ */
769
+ public async openPosition(
770
+ collateralToken: string,
771
+ indexToken: string,
772
+ size: bigint,
773
+ collateralAmount: bigint,
774
+ coinObjects: string[],
775
+ long: boolean,
776
+ reserveAmount: bigint,
777
+ indexPrice: number,
778
+ collateralPrice: number,
779
+ isLimitOrder?: boolean,
780
+ isIocOrder?: boolean,
781
+ pricesSlippage = 0.003,
782
+ collateralSlippage = 0.5,
783
+ relayerFee = BigInt(0.5),
784
+ referralAddress?: string,
785
+ sender?: string,
786
+ sponsoredTx?: boolean,
787
+ suiCoinObjectsForPythUpdate?: string[],
788
+ ): Promise<Transaction> {
789
+ let tx = new Transaction()
790
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
791
+ tx = await this.addReferral(referralAddress, tx)
792
+ }
793
+
794
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
795
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage)
796
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
797
+
798
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
799
+ if (isLimitOrder) {
800
+ allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
801
+ }
802
+
803
+ // Handle oracle initialization and coin processing
804
+ let suiCoinObject
805
+ if (sponsoredTx) {
806
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
807
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
808
+ }
809
+ else {
810
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
811
+ }
812
+
813
+ // Process coin splitting
814
+ const [depositObject, feeObject] = this.processCoinSplitting(
815
+ tx,
816
+ collateralToken,
817
+ coinObjects,
818
+ [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)],
819
+ sponsoredTx,
820
+ suiCoinObject,
821
+ )
822
+
823
+ tx.moveCall({
824
+ target: `${this.consts.zoCore.upgradedPackage}::market::open_position`,
825
+ typeArguments: [
826
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
827
+ this.consts.coins[collateralToken].module,
828
+ this.consts.coins[indexToken].module,
829
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
830
+ this.consts.coins[collateralToken].module,
831
+ ],
832
+ arguments: [
833
+ tx.object(SUI_CLOCK_OBJECT_ID),
834
+ tx.object(this.consts.zoCore.market),
835
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
836
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
837
+ tx.object(this.consts.zoCore.symbols[symbol].positionConfig),
838
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
839
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
840
+ depositObject,
841
+ feeObject,
842
+ tx.pure.u8(allowTrade),
843
+ tx.pure.u64(size),
844
+ tx.pure.u64(reserveAmount),
845
+ tx.pure.u256(adjustCollateralPrice),
846
+ tx.pure.u256(adjustPrice),
847
+ ],
848
+ })
849
+ return tx
850
+ }
851
+
852
+ /**
853
+ * Decreases an existing position in ZBTCVC
854
+ */
855
+ public async decreasePosition(
856
+ pcpId: string,
857
+ collateralToken: string,
858
+ indexToken: string,
859
+ amount: bigint,
860
+ long: boolean,
861
+ indexPrice: number,
862
+ collateralPrice: number,
863
+ isTriggerOrder = false,
864
+ isTakeProfitOrder = true,
865
+ isIocOrder = false,
866
+ pricesSlippage = 0.003,
867
+ collateralSlippage = 0.5,
868
+ relayerFee = BigInt(0.5),
869
+ coinObjects?: string[],
870
+ sponsoredTx?: boolean,
871
+ suiCoinObjectsForPythUpdate?: string[],
872
+ ): Promise<Transaction> {
873
+ if (!coinObjects) {
874
+ throw new Error(`${this.constructor.name}: coinObjects is required`)
875
+ }
876
+ let tx = new Transaction()
877
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
878
+
879
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
880
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
881
+
882
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
883
+ if (isTriggerOrder) {
884
+ allowTrade = isIocOrder || !isTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
885
+ }
886
+ else {
887
+ isTakeProfitOrder = true
888
+ }
889
+
890
+ // Handle oracle initialization and coin processing
891
+ let suiCoinObject
892
+ if (sponsoredTx) {
893
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
894
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
895
+ }
896
+ else {
897
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
898
+ }
899
+
900
+ // Process coin splitting
901
+ const [feeObject] = this.processCoinSplitting(
902
+ tx,
903
+ collateralToken,
904
+ coinObjects,
905
+ [tx.pure.u64(relayerFee)],
906
+ sponsoredTx,
907
+ suiCoinObject,
908
+ )
909
+
910
+ tx.moveCall({
911
+ target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position`,
912
+ typeArguments: [
913
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
914
+ this.consts.coins[collateralToken].module,
915
+ this.consts.coins[indexToken].module,
916
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
917
+ this.consts.coins[collateralToken].module,
918
+ ],
919
+ arguments: [
920
+ tx.object(SUI_CLOCK_OBJECT_ID),
921
+ tx.object(this.consts.zoCore.market),
922
+ tx.object(pcpId),
923
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
924
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
925
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
926
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
927
+ feeObject,
928
+ tx.pure.u8(allowTrade),
929
+ tx.pure.bool(isTakeProfitOrder),
930
+ tx.pure.u64(amount),
931
+ tx.pure.u256(adjustCollateralPrice),
932
+ tx.pure.u256(adjustPrice),
933
+ ],
934
+ })
935
+
936
+ return tx
937
+ }
938
+
939
+ public async decreaseMultiPositions(positions: Array<{
940
+ pcpId: string
941
+ collateralToken: string
942
+ indexToken: string
943
+ amount: bigint
944
+ long: boolean
945
+ indexPrice: number
946
+ collateralPrice: number
947
+ isTriggerOrder?: boolean
948
+ isTakeProfitOrder?: boolean
949
+ isIocOrder?: boolean
950
+ pricesSlippage?: number
951
+ collateralSlippage?: number
952
+ relayerFee?: bigint
953
+ coinObjects?: string[]
954
+ }>, tx?: Transaction, sponsoredTx?: boolean, suiCoinObjectsForPythUpdate?: string[]): Promise<Transaction> {
955
+ if (!tx) {
956
+ tx = new Transaction()
957
+ }
958
+
959
+ // Handle oracle initialization and coin processing
960
+ let suiCoinObject
961
+ if (sponsoredTx) {
962
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
963
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx, true, suiCoinObject)
964
+ }
965
+ else {
966
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
967
+ }
968
+
969
+ for (const position of positions) {
970
+ const {
971
+ pcpId,
972
+ collateralToken,
973
+ coinObjects = [],
974
+ indexToken,
975
+ amount,
976
+ long,
977
+ indexPrice,
978
+ collateralPrice,
979
+ isTriggerOrder = false,
980
+ isTakeProfitOrder = true,
981
+ isIocOrder = false,
982
+ pricesSlippage = 0.003,
983
+ collateralSlippage = 0.5,
984
+ relayerFee = BigInt(0.5),
985
+ } = position
986
+ let innerIsTakeProfitOrder = isTakeProfitOrder
987
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
988
+
989
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
990
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
991
+
992
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
993
+ if (isTriggerOrder) {
994
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
995
+ }
996
+ else {
997
+ innerIsTakeProfitOrder = true
998
+ }
999
+
1000
+ // Process coin splitting
1001
+ const [feeObject] = this.processCoinSplitting(
1002
+ tx,
1003
+ collateralToken,
1004
+ coinObjects,
1005
+ [tx.pure.u64(relayerFee)],
1006
+ sponsoredTx,
1007
+ suiCoinObject,
1008
+ )
1009
+
1010
+ tx.moveCall({
1011
+ target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position`,
1012
+ typeArguments: [
1013
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
1014
+ this.consts.coins[collateralToken].module,
1015
+ this.consts.coins[indexToken].module,
1016
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1017
+ this.consts.coins[collateralToken].module,
1018
+ ],
1019
+ arguments: [
1020
+ tx.object(SUI_CLOCK_OBJECT_ID),
1021
+ tx.object(this.consts.zoCore.market),
1022
+ tx.object(pcpId),
1023
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
1024
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
1025
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1026
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1027
+ feeObject,
1028
+ tx.pure.u8(allowTrade),
1029
+ tx.pure.bool(innerIsTakeProfitOrder),
1030
+ tx.pure.u64(amount),
1031
+ tx.pure.u256(adjustCollateralPrice),
1032
+ tx.pure.u256(adjustPrice),
1033
+ ],
1034
+ })
1035
+ }
1036
+ return tx
1037
+ }
1038
+
1039
+ /**
1040
+ * Pledges in position (ZBTCVC-specific functionality)
1041
+ */
1042
+ public async pledgeInPosition(
1043
+ pcpId: string,
1044
+ collateralToken: string,
1045
+ indexToken: string,
1046
+ amount: number,
1047
+ coinObjects: string[],
1048
+ long: boolean,
1049
+ sponsoredTx?: boolean,
1050
+ suiCoinObjectsForPythUpdate?: string[],
1051
+ ): Promise<Transaction> {
1052
+ let tx = new Transaction()
1053
+ // Handle oracle initialization and coin processing
1054
+ let suiCoinObject
1055
+ if (sponsoredTx) {
1056
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
1057
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
1058
+ }
1059
+ else {
1060
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
1061
+ }
1062
+
1063
+ // Process coin splitting
1064
+ const [depositObject] = this.processCoinSplitting(
1065
+ tx,
1066
+ collateralToken,
1067
+ coinObjects,
1068
+ [tx.pure.u64(amount)],
1069
+ sponsoredTx,
1070
+ suiCoinObject,
1071
+ )
1072
+
1073
+ tx.moveCall({
1074
+ target: `${this.consts.zoCore.upgradedPackage}::market::pledge_in_position`,
1075
+ typeArguments: [
1076
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
1077
+ this.consts.coins[collateralToken].module,
1078
+ this.consts.coins[indexToken].module,
1079
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1080
+ ],
1081
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(pcpId), depositObject],
1082
+ })
1083
+ return tx
1084
+ }
1085
+
1086
+ public async redeemFromPosition(
1087
+ pcpId: string,
1088
+ collateralToken: string,
1089
+ indexToken: string,
1090
+ amount: number,
1091
+ long: boolean,
1092
+ sponsoredTx?: boolean,
1093
+ suiCoinObjectsForPythUpdate?: string[],
1094
+ ): Promise<Transaction> {
1095
+ let tx = new Transaction()
1096
+ // Handle oracle initialization and coin processing
1097
+ let suiCoinObject
1098
+ if (sponsoredTx) {
1099
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
1100
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
1101
+ }
1102
+ else {
1103
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
1104
+ }
1105
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1106
+
1107
+ tx.moveCall({
1108
+ target: `${this.consts.zoCore.upgradedPackage}::market::redeem_from_position`,
1109
+ typeArguments: [
1110
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
1111
+ this.consts.coins[collateralToken].module,
1112
+ this.consts.coins[indexToken].module,
1113
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1114
+ ],
1115
+ arguments: [
1116
+ tx.object(SUI_CLOCK_OBJECT_ID),
1117
+ tx.object(this.consts.zoCore.market),
1118
+ tx.object(pcpId),
1119
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
1120
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
1121
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1122
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1123
+ tx.pure.u64(amount),
1124
+ ],
1125
+ })
1126
+
1127
+ return tx
1128
+ }
1129
+
1130
+ public cancelOrder(
1131
+ orderCapId: string,
1132
+ collateralToken: string,
1133
+ indexToken: string,
1134
+ long: boolean,
1135
+ type: string,
1136
+ isV11Order?: boolean,
1137
+ ): Transaction {
1138
+ const tx = new Transaction()
1139
+ let functionName = ''
1140
+ switch (type) {
1141
+ case 'OPEN_POSITION': {
1142
+ functionName = isV11Order ? 'clear_open_position_order' : 'clear_open_position_order'
1143
+ break
1144
+ }
1145
+ case 'DECREASE_POSITION': {
1146
+ functionName = isV11Order
1147
+ ? 'clear_decrease_position_order'
1148
+ : 'clear_decrease_position_order'
1149
+ break
1150
+ }
1151
+ default: {
1152
+ throw new Error('invalid order type')
1153
+ }
1154
+ }
1155
+ tx.moveCall({
1156
+ target: `${this.consts.zoCore.upgradedPackage}::market::${functionName}`,
1157
+ typeArguments: [
1158
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
1159
+ this.consts.coins[collateralToken].module,
1160
+ this.consts.coins[indexToken].module,
1161
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1162
+ this.consts.coins[collateralToken].module,
1163
+ ],
1164
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(orderCapId)],
1165
+ })
1166
+ return tx
1167
+ }
1168
+
1169
+ public cancelMultiOrders(
1170
+ orders: Array<{
1171
+ orderCapId: string
1172
+ collateralToken: string
1173
+ indexToken: string
1174
+ long: boolean
1175
+ type: string
1176
+ isV11Order?: boolean
1177
+ }>,
1178
+ tx?: Transaction,
1179
+ ): Transaction {
1180
+ if (!tx) {
1181
+ tx = new Transaction()
1182
+ }
1183
+
1184
+ for (const order of orders) {
1185
+ const { orderCapId, collateralToken, indexToken, long, type, isV11Order } = order
1186
+ let functionName = ''
1187
+ switch (type) {
1188
+ case 'OPEN_POSITION': {
1189
+ functionName = isV11Order ? 'clear_open_position_order' : 'clear_open_position_order'
1190
+ break
1191
+ }
1192
+ case 'DECREASE_POSITION': {
1193
+ functionName = isV11Order
1194
+ ? 'clear_decrease_position_order'
1195
+ : 'clear_decrease_position_order'
1196
+ break
1197
+ }
1198
+ default: {
1199
+ throw new Error('invalid order type')
1200
+ }
1201
+ }
1202
+
1203
+ tx.moveCall({
1204
+ target: `${this.consts.zoCore.upgradedPackage}::market::${functionName}`,
1205
+ typeArguments: [
1206
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
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: [tx.object(this.consts.zoCore.market), tx.object(orderCapId)],
1213
+ })
1214
+ }
1215
+ return tx
1216
+ }
1217
+
1218
+ public addReferral(
1219
+ referralAddress: string,
1220
+ tx?: Transaction,
1221
+ ): Transaction {
1222
+ if (!tx) {
1223
+ tx = new Transaction()
1224
+ }
1225
+ tx.moveCall({
1226
+ target: `${this.consts.zoCore.upgradedPackage}::market::add_new_referral`,
1227
+ typeArguments: [`${this.consts.zoCore.package}::zbtcvc::ZBTCVC`],
1228
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(referralAddress)],
1229
+ })
1230
+
1231
+ return tx
1232
+ }
1233
+
1234
+ public async adminUpdatePriceFeed(
1235
+ collateralToken: string,
1236
+ indexToken: string,
1237
+ ): Promise<Transaction> {
1238
+ const tx = await this.initOracleTxb([collateralToken, indexToken])
1239
+ return tx
1240
+ }
1241
+
1242
+ public adminSettlePosition(
1243
+ positionId: string,
1244
+ owner: string,
1245
+ collateralToken: string,
1246
+ indexToken: string,
1247
+ long: boolean,
1248
+ ): Transaction {
1249
+ const tx = new Transaction()
1250
+
1251
+ tx.moveCall({
1252
+ target: `${this.consts.zoCore.upgradedPackage}::market::force_settle_position`,
1253
+ typeArguments: [
1254
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
1255
+ this.consts.coins[collateralToken].module,
1256
+ this.consts.coins[indexToken].module,
1257
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1258
+ ],
1259
+ arguments: [
1260
+ tx.object(this.consts.zoCore.adminCap),
1261
+ tx.object(SUI_CLOCK_OBJECT_ID),
1262
+ tx.object(this.consts.zoCore.market),
1263
+ tx.object(owner),
1264
+ tx.object(positionId),
1265
+ ],
1266
+ })
1267
+
1268
+ this.adminClearClosedPosition(positionId, owner, collateralToken, indexToken, long, tx)
1269
+
1270
+ return tx
1271
+ }
1272
+
1273
+ public adminDecreasePosition(
1274
+ positionId: string,
1275
+ owner: string,
1276
+ collateralToken: string,
1277
+ indexToken: string,
1278
+ positionAmount: number,
1279
+ amount: bigint,
1280
+ long: boolean,
1281
+ ): Transaction {
1282
+ const tx = new Transaction()
1283
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1284
+
1285
+ tx.moveCall({
1286
+ target: `${this.consts.zoCore.upgradedPackage}::market::force_close_position`,
1287
+ typeArguments: [
1288
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
1289
+ this.consts.coins[collateralToken].module,
1290
+ this.consts.coins[indexToken].module,
1291
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1292
+ ],
1293
+ arguments: [
1294
+ tx.object(this.consts.zoCore.adminCap),
1295
+ tx.object(SUI_CLOCK_OBJECT_ID),
1296
+ tx.object(this.consts.zoCore.market),
1297
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
1298
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
1299
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1300
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1301
+ tx.object(owner),
1302
+ tx.object(positionId),
1303
+ ],
1304
+ })
1305
+
1306
+ if (amount === BigInt(positionAmount)) {
1307
+ this.adminClearClosedPosition(positionId, owner, collateralToken, indexToken, long, tx)
1308
+ }
1309
+
1310
+ return tx
1311
+ }
1312
+
1313
+ public adminClearClosedPosition(
1314
+ positionId: string,
1315
+ owner: string,
1316
+ collateralToken: string,
1317
+ indexToken: string,
1318
+ long: boolean,
1319
+ tx: Transaction,
1320
+ ): void {
1321
+ tx.moveCall({
1322
+ target: `${this.consts.zoCore.upgradedPackage}::market::force_clear_closed_position`,
1323
+ typeArguments: [
1324
+ `${this.consts.zoCore.package}::zbtcvc::ZBTCVC`,
1325
+ this.consts.coins[collateralToken].module,
1326
+ this.consts.coins[indexToken].module,
1327
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1328
+ ],
1329
+ arguments: [
1330
+ tx.object(this.consts.zoCore.adminCap),
1331
+ tx.object(this.consts.zoCore.market),
1332
+ tx.object(positionId),
1333
+ tx.object(owner),
1334
+ ],
1335
+ })
1336
+ }
1337
+ }