@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
package/src/api.ts ADDED
@@ -0,0 +1,1100 @@
1
+ /* eslint-disable no-param-reassign */
2
+ /* eslint-disable class-methods-use-this */
3
+ import type { KioskClient, KioskOwnerCap } from '@mysten/kiosk'
4
+ import { KioskTransaction } from '@mysten/kiosk'
5
+ import type { SuiClient } from '@mysten/sui/client'
6
+ import { Transaction } from '@mysten/sui/transactions'
7
+ import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui/utils'
8
+
9
+ import type { Network } from './consts'
10
+ import { ALLOW_TRADE_CAN_TRADE, ALLOW_TRADE_MUST_TRADE, ALLOW_TRADE_NO_TRADE } from './consts'
11
+ import type { ICredential } from './data'
12
+ import { DataAPI } from './data'
13
+ import { joinSymbol } from './utils'
14
+
15
+ export class API extends DataAPI {
16
+ private static instance: API
17
+
18
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor
19
+ constructor(
20
+ network: Network,
21
+ provider: SuiClient | null,
22
+ apiEndpoint: string,
23
+ connectionURL: string,
24
+ ) {
25
+ super(network, provider, apiEndpoint, connectionURL)
26
+ }
27
+
28
+ public static getInstance(
29
+ network: Network,
30
+ provider: SuiClient | null,
31
+ apiEndpoint: string,
32
+ connectionURL: string,
33
+ ): API {
34
+ if (!API.instance) {
35
+ API.instance = new API(network, provider, apiEndpoint, connectionURL)
36
+ }
37
+ return API.instance
38
+ }
39
+
40
+ #processCoins = (tx: Transaction, coin: string, coinObjects: string[]) => {
41
+ if (coin === 'sui') {
42
+ return tx.gas
43
+ }
44
+ if (coinObjects.length > 1) {
45
+ tx.mergeCoins(
46
+ tx.object(coinObjects[0]),
47
+ coinObjects.slice(1).map(coinObject => tx.object(coinObject)),
48
+ )
49
+ }
50
+ return tx.object(coinObjects[0])
51
+ }
52
+
53
+ #processSlippage(indexPrice: number, long: boolean, slippage: number) {
54
+ const raw = long ? indexPrice * (1 + slippage) : indexPrice * (1 - slippage)
55
+ return BigInt(Math.round(raw * 1e18))
56
+ }
57
+
58
+ deposit = async (
59
+ coin: string,
60
+ coinObjects: string[],
61
+ amount: number,
62
+ minAmountOut = 0,
63
+ referralAddress = '',
64
+ sender = '',
65
+ ) => {
66
+ let tx = new Transaction()
67
+ tx = await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder), tx)
68
+ if (referralAddress && !(await this.hasReferral(sender || ''))) {
69
+ tx = await this.addReferral(referralAddress, tx)
70
+ }
71
+
72
+ const coinObject = this.#processCoins(tx, coin, coinObjects)
73
+ const [depositObject] = tx.splitCoins(coinObject, [tx.pure.u64(amount)])
74
+
75
+ const { vaultsValuation, symbolsValuation } = this.valuate(tx)
76
+
77
+ tx.moveCall({
78
+ target: `${this.consts.zoCore.upgradedPackage}::market::deposit`,
79
+ typeArguments: [`${this.consts.zoCore.package}::zlp::ZLP`, this.consts.coins[coin].module],
80
+ arguments: [
81
+ tx.object(this.consts.zoCore.market),
82
+ tx.object(this.consts.zoCore.rebaseFeeModel),
83
+ depositObject,
84
+ tx.pure.u64(minAmountOut),
85
+ vaultsValuation,
86
+ symbolsValuation,
87
+ ],
88
+ })
89
+ return tx
90
+ }
91
+
92
+ withdraw = async (coin: string, zlpCoinObjects: string[], amount: number, minAmountOut = 0) => {
93
+ const tx = await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder))
94
+ const zlpCoinObject = this.#processCoins(tx, 'zlp', zlpCoinObjects)
95
+ const [withdrawObject] = tx.splitCoins(zlpCoinObject, [tx.pure.u64(amount)])
96
+
97
+ const { vaultsValuation, symbolsValuation } = this.valuate(tx)
98
+
99
+ tx.moveCall({
100
+ target: `${this.consts.zoCore.upgradedPackage}::market::withdraw`,
101
+ typeArguments: [`${this.consts.zoCore.package}::zlp::ZLP`, this.consts.coins[coin].module],
102
+ arguments: [
103
+ tx.object(this.consts.zoCore.market),
104
+ tx.object(this.consts.zoCore.rebaseFeeModel),
105
+ withdrawObject,
106
+ tx.pure.u64(minAmountOut),
107
+ vaultsValuation,
108
+ symbolsValuation,
109
+ ],
110
+ })
111
+ return tx
112
+ }
113
+
114
+ openPosition = async (
115
+ collateralToken: string,
116
+ indexToken: string,
117
+ size: bigint,
118
+ collateralAmount: bigint,
119
+ coinObjects: string[],
120
+ long: boolean,
121
+ reserveAmount: bigint,
122
+ indexPrice: number,
123
+ collateralPrice: number,
124
+ isLimitOrder = false,
125
+ isIocOrder = false,
126
+ pricesSlippage = 0.003,
127
+ collateralSlippage = 0.5,
128
+ relayerFee = BigInt(0.5),
129
+ referralAddress = '',
130
+ sender = '',
131
+ ) => {
132
+ let tx = new Transaction()
133
+ if (referralAddress && !(await this.hasReferral(sender || ''))) {
134
+ tx = await this.addReferral(referralAddress, tx)
135
+ }
136
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
137
+ const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
138
+ const [depositObject, feeObject] = tx.splitCoins(coinObject, [
139
+ tx.pure.u64(collateralAmount),
140
+ tx.pure.u64(relayerFee),
141
+ ])
142
+
143
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
144
+ const adjustPrice = this.#processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage)
145
+ const adjustCollateralPrice = this.#processSlippage(collateralPrice, false, collateralSlippage)
146
+
147
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
148
+ if (isLimitOrder) {
149
+ allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
150
+ }
151
+
152
+ tx.moveCall({
153
+ target: `${this.consts.zoCore.upgradedPackage}::market::open_position`,
154
+ typeArguments: [
155
+ `${this.consts.zoCore.package}::zlp::ZLP`,
156
+ this.consts.coins[collateralToken].module,
157
+ this.consts.coins[indexToken].module,
158
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
159
+ this.consts.coins[collateralToken].module,
160
+ ],
161
+ arguments: [
162
+ tx.object(SUI_CLOCK_OBJECT_ID),
163
+ tx.object(this.consts.zoCore.market),
164
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
165
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
166
+ tx.object(this.consts.zoCore.symbols[symbol].positionConfig),
167
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
168
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
169
+ depositObject,
170
+ feeObject,
171
+ tx.pure.u8(allowTrade),
172
+ tx.pure.u64(size),
173
+ tx.pure.u64(reserveAmount),
174
+ tx.pure.u256(adjustCollateralPrice),
175
+ tx.pure.u256(adjustPrice),
176
+ ],
177
+ })
178
+ return tx
179
+ }
180
+
181
+ openPositionWithSCard = async (
182
+ collateralToken: string,
183
+ indexToken: string,
184
+ size: bigint,
185
+ collateralAmount: bigint,
186
+ coinObjects: string[],
187
+ long: boolean,
188
+ reserveAmount: bigint,
189
+ indexPrice: number,
190
+ collateralPrice: number,
191
+ kioskClient: KioskClient,
192
+ kioskCap: KioskOwnerCap,
193
+ scard: string,
194
+ isLimitOrder = false,
195
+ isIocOrder = false,
196
+ pricesSlippage = 0.003,
197
+ collateralSlippage = 0.5,
198
+ relayerFee = BigInt(0.5),
199
+ referralAddress = '',
200
+ sender = '',
201
+ ) => {
202
+ let tx = new Transaction()
203
+ if (referralAddress && !(await this.hasReferral(sender || ''))) {
204
+ tx = await this.addReferral(referralAddress, tx)
205
+ }
206
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
207
+ const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
208
+ const [depositObject, feeObject] = tx.splitCoins(coinObject, [
209
+ tx.pure.u64(collateralAmount),
210
+ tx.pure.u64(relayerFee),
211
+ ])
212
+
213
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
214
+ const adjustPrice = this.#processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage)
215
+ const adjustCollateralPrice = this.#processSlippage(collateralPrice, false, collateralSlippage)
216
+
217
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
218
+ if (isLimitOrder) {
219
+ allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
220
+ }
221
+
222
+ const kioskTx = new KioskTransaction({
223
+ transaction: tx,
224
+ kioskClient,
225
+ cap: kioskCap,
226
+ })
227
+
228
+ const [sudoCard, promise] = kioskTx.borrow({
229
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
230
+ itemId: scard,
231
+ })
232
+
233
+ tx.moveCall({
234
+ target: `${this.consts.zoCore.upgradedPackage}::market::open_position_with_scard`,
235
+ typeArguments: [
236
+ `${this.consts.zoCore.package}::zlp::ZLP`,
237
+ this.consts.coins[collateralToken].module,
238
+ this.consts.coins[indexToken].module,
239
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
240
+ this.consts.coins[collateralToken].module,
241
+ ],
242
+ arguments: [
243
+ tx.object(SUI_CLOCK_OBJECT_ID),
244
+ tx.object(this.consts.zoCore.market),
245
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
246
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
247
+ tx.object(this.consts.zoCore.symbols[symbol].positionConfig),
248
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
249
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
250
+ depositObject,
251
+ feeObject,
252
+ tx.pure.u8(allowTrade),
253
+ tx.pure.u64(size),
254
+ tx.pure.u64(reserveAmount),
255
+ tx.pure.u256(adjustCollateralPrice),
256
+ tx.pure.u256(adjustPrice),
257
+ sudoCard,
258
+ ],
259
+ })
260
+
261
+ kioskTx
262
+ .return({
263
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
264
+ item: sudoCard,
265
+ promise,
266
+ })
267
+ .finalize()
268
+ return tx
269
+ }
270
+
271
+ pledgeInPosition = async (
272
+ pcpId: string,
273
+ collateralToken: string,
274
+ indexToken: string,
275
+ amount: number,
276
+ coinObjects: string[],
277
+ long: boolean,
278
+ ) => {
279
+ const tx = await this.initOracleTxb([collateralToken, indexToken])
280
+ const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
281
+ const [depositObject] = tx.splitCoins(coinObject, [tx.pure.u64(amount)])
282
+
283
+ tx.moveCall({
284
+ target: `${this.consts.zoCore.upgradedPackage}::market::pledge_in_position`,
285
+ typeArguments: [
286
+ `${this.consts.zoCore.package}::zlp::ZLP`,
287
+ this.consts.coins[collateralToken].module,
288
+ this.consts.coins[indexToken].module,
289
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
290
+ ],
291
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(pcpId), depositObject],
292
+ })
293
+ return tx
294
+ }
295
+
296
+ redeemFromPosition = async (
297
+ pcpId: string,
298
+ collateralToken: string,
299
+ indexToken: string,
300
+ amount: number,
301
+ long: boolean,
302
+ ) => {
303
+ const tx = await this.initOracleTxb([collateralToken, indexToken])
304
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
305
+
306
+ tx.moveCall({
307
+ target: `${this.consts.zoCore.upgradedPackage}::market::redeem_from_position`,
308
+ typeArguments: [
309
+ `${this.consts.zoCore.package}::zlp::ZLP`,
310
+ this.consts.coins[collateralToken].module,
311
+ this.consts.coins[indexToken].module,
312
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
313
+ ],
314
+ arguments: [
315
+ tx.object(SUI_CLOCK_OBJECT_ID),
316
+ tx.object(this.consts.zoCore.market),
317
+ tx.object(pcpId),
318
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
319
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
320
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
321
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
322
+ tx.pure.u64(amount),
323
+ ],
324
+ })
325
+
326
+ return tx
327
+ }
328
+
329
+ decreasePosition = async (
330
+ pcpId: string,
331
+ collateralToken: string,
332
+ coinObjects: string[],
333
+ indexToken: string,
334
+ amount: bigint,
335
+ long: boolean,
336
+ indexPrice: number,
337
+ collateralPrice: number,
338
+ isTriggerOrder = false,
339
+ isTakeProfitOrder = true,
340
+ isIocOrder = false,
341
+ pricesSlippage = 0.003,
342
+ collateralSlippage = 0.5,
343
+ relayerFee = BigInt(0.5),
344
+ ) => {
345
+ const tx = await this.initOracleTxb([collateralToken, indexToken])
346
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
347
+ const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
348
+ const feeObject = tx.splitCoins(coinObject, [tx.pure.u64(relayerFee)])
349
+
350
+ const adjustPrice = this.#processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
351
+ const adjustCollateralPrice = this.#processSlippage(collateralPrice, false, collateralSlippage)
352
+
353
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
354
+ if (isTriggerOrder) {
355
+ allowTrade = isIocOrder || !isTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
356
+ }
357
+ else {
358
+ isTakeProfitOrder = true
359
+ }
360
+
361
+ tx.moveCall({
362
+ target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position`,
363
+ typeArguments: [
364
+ `${this.consts.zoCore.package}::zlp::ZLP`,
365
+ this.consts.coins[collateralToken].module,
366
+ this.consts.coins[indexToken].module,
367
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
368
+ this.consts.coins[collateralToken].module,
369
+ ],
370
+ arguments: [
371
+ tx.object(SUI_CLOCK_OBJECT_ID),
372
+ tx.object(this.consts.zoCore.market),
373
+ tx.object(pcpId),
374
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
375
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
376
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
377
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
378
+ feeObject,
379
+ tx.pure.u8(allowTrade),
380
+ tx.pure.bool(isTakeProfitOrder),
381
+ tx.pure.u64(amount),
382
+ tx.pure.u256(adjustCollateralPrice),
383
+ tx.pure.u256(adjustPrice),
384
+ ],
385
+ })
386
+
387
+ return tx
388
+ }
389
+
390
+ decreasePositionWithSCard = async (
391
+ pcpId: string,
392
+ collateralToken: string,
393
+ coinObjects: string[],
394
+ indexToken: string,
395
+ amount: bigint,
396
+ long: boolean,
397
+ indexPrice: number,
398
+ collateralPrice: number,
399
+ kioskClient: KioskClient,
400
+ kioskCap: KioskOwnerCap,
401
+ scard: string,
402
+ isTriggerOrder = false,
403
+ isTakeProfitOrder = true,
404
+ isIocOrder = false,
405
+ pricesSlippage = 0.003,
406
+ collateralSlippage = 0.5,
407
+ relayerFee = BigInt(0.5),
408
+ ) => {
409
+ const tx = await this.initOracleTxb([collateralToken, indexToken])
410
+
411
+ const kioskTx = new KioskTransaction({
412
+ transaction: tx,
413
+ kioskClient,
414
+ cap: kioskCap,
415
+ })
416
+
417
+ const [sudoCard, promise] = kioskTx.borrow({
418
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
419
+ itemId: scard,
420
+ })
421
+
422
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
423
+ const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
424
+ const feeObject = tx.splitCoins(coinObject, [tx.pure.u64(relayerFee)])
425
+
426
+ const adjustPrice = this.#processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
427
+ const adjustCollateralPrice = this.#processSlippage(collateralPrice, false, collateralSlippage)
428
+
429
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
430
+ if (isTriggerOrder) {
431
+ allowTrade = isIocOrder || !isTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
432
+ }
433
+ else {
434
+ isTakeProfitOrder = true
435
+ }
436
+
437
+ tx.moveCall({
438
+ target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_with_scard`,
439
+ typeArguments: [
440
+ `${this.consts.zoCore.package}::zlp::ZLP`,
441
+ this.consts.coins[collateralToken].module,
442
+ this.consts.coins[indexToken].module,
443
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
444
+ this.consts.coins[collateralToken].module,
445
+ ],
446
+ arguments: [
447
+ tx.object(SUI_CLOCK_OBJECT_ID),
448
+ tx.object(this.consts.zoCore.market),
449
+ tx.object(pcpId),
450
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
451
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
452
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
453
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
454
+ feeObject,
455
+ tx.pure.u8(allowTrade),
456
+ tx.pure.bool(isTakeProfitOrder),
457
+ tx.pure.u64(amount),
458
+ tx.pure.u256(adjustCollateralPrice),
459
+ tx.pure.u256(adjustPrice),
460
+ sudoCard,
461
+ ],
462
+ })
463
+
464
+ kioskTx
465
+ .return({
466
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
467
+ item: sudoCard,
468
+ promise,
469
+ })
470
+ .finalize()
471
+ return tx
472
+ }
473
+
474
+ decreaseMultiPositions = async (positions: Array<{
475
+ pcpId: string
476
+ collateralToken: string
477
+ coinObjects: string[]
478
+ indexToken: string
479
+ amount: bigint
480
+ long: boolean
481
+ indexPrice: number
482
+ collateralPrice: number
483
+ isTriggerOrder: boolean
484
+ isTakeProfitOrder: boolean
485
+ isIocOrder: boolean
486
+ slippage: number
487
+ relayerFee: bigint
488
+ }>, tx?: Transaction) => {
489
+ if (!tx) {
490
+ tx = new Transaction()
491
+ }
492
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
493
+
494
+ for (const position of positions) {
495
+ const {
496
+ pcpId,
497
+ collateralToken,
498
+ coinObjects,
499
+ indexToken,
500
+ amount,
501
+ long,
502
+ indexPrice,
503
+ collateralPrice,
504
+ isTriggerOrder,
505
+ isTakeProfitOrder,
506
+ isIocOrder,
507
+ slippage,
508
+ relayerFee,
509
+ } = position
510
+ let innerIsTakeProfitOrder = isTakeProfitOrder
511
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
512
+ const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
513
+ const feeObject = tx.splitCoins(coinObject, [tx.pure.u64(relayerFee)])
514
+
515
+ const adjustPrice = this.#processSlippage(indexPrice, !long, isTriggerOrder ? 0 : slippage)
516
+ const adjustCollateralPrice = this.#processSlippage(collateralPrice, false, 0.5)
517
+
518
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
519
+ if (isTriggerOrder) {
520
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
521
+ }
522
+ else {
523
+ innerIsTakeProfitOrder = true
524
+ }
525
+
526
+ tx.moveCall({
527
+ target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position`,
528
+ typeArguments: [
529
+ `${this.consts.zoCore.package}::zlp::ZLP`,
530
+ this.consts.coins[collateralToken].module,
531
+ this.consts.coins[indexToken].module,
532
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
533
+ this.consts.coins[collateralToken].module,
534
+ ],
535
+ arguments: [
536
+ tx.object(SUI_CLOCK_OBJECT_ID),
537
+ tx.object(this.consts.zoCore.market),
538
+ tx.object(pcpId),
539
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
540
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
541
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
542
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
543
+ feeObject,
544
+ tx.pure.u8(allowTrade),
545
+ tx.pure.bool(innerIsTakeProfitOrder),
546
+ tx.pure.u64(amount),
547
+ tx.pure.u256(adjustCollateralPrice),
548
+ tx.pure.u256(adjustPrice),
549
+ ],
550
+ })
551
+ }
552
+ return tx
553
+ }
554
+
555
+ decreaseMultiPositionsWithSCard = async (positions: Array<{
556
+ pcpId: string
557
+ collateralToken: string
558
+ coinObjects: string[]
559
+ indexToken: string
560
+ amount: bigint
561
+ long: boolean
562
+ indexPrice: number
563
+ collateralPrice: number
564
+ isTriggerOrder: boolean
565
+ isTakeProfitOrder: boolean
566
+ isIocOrder: boolean
567
+ slippage: number
568
+ relayerFee: bigint
569
+ }>, kioskClient: KioskClient, kioskCap: KioskOwnerCap, scard: string, tx?: Transaction) => {
570
+ if (!tx) {
571
+ tx = new Transaction()
572
+ }
573
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
574
+
575
+ const kioskTx = new KioskTransaction({
576
+ transaction: tx,
577
+ kioskClient,
578
+ cap: kioskCap,
579
+ })
580
+
581
+ const [sudoCard, promise] = kioskTx.borrow({
582
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
583
+ itemId: scard,
584
+ })
585
+
586
+ for (const position of positions) {
587
+ const {
588
+ pcpId,
589
+ collateralToken,
590
+ coinObjects,
591
+ indexToken,
592
+ amount,
593
+ long,
594
+ indexPrice,
595
+ collateralPrice,
596
+ isTriggerOrder,
597
+ isTakeProfitOrder,
598
+ isIocOrder,
599
+ slippage,
600
+ relayerFee,
601
+ } = position
602
+ let innerIsTakeProfitOrder = isTakeProfitOrder
603
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
604
+ const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
605
+ const feeObject = tx.splitCoins(coinObject, [tx.pure.u64(relayerFee)])
606
+
607
+ const adjustPrice = this.#processSlippage(indexPrice, !long, isTriggerOrder ? 0 : slippage)
608
+ const adjustCollateralPrice = this.#processSlippage(collateralPrice, false, 0.5)
609
+
610
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
611
+ if (isTriggerOrder) {
612
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
613
+ }
614
+ else {
615
+ innerIsTakeProfitOrder = true
616
+ }
617
+
618
+ tx.moveCall({
619
+ target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_with_scard`,
620
+ typeArguments: [
621
+ `${this.consts.zoCore.package}::zlp::ZLP`,
622
+ this.consts.coins[collateralToken].module,
623
+ this.consts.coins[indexToken].module,
624
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
625
+ this.consts.coins[collateralToken].module,
626
+ ],
627
+ arguments: [
628
+ tx.object(SUI_CLOCK_OBJECT_ID),
629
+ tx.object(this.consts.zoCore.market),
630
+ tx.object(pcpId),
631
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
632
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
633
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
634
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
635
+ feeObject,
636
+ tx.pure.u8(allowTrade),
637
+ tx.pure.bool(innerIsTakeProfitOrder),
638
+ tx.pure.u64(amount),
639
+ tx.pure.u256(adjustCollateralPrice),
640
+ tx.pure.u256(adjustPrice),
641
+ sudoCard,
642
+ ],
643
+ })
644
+ }
645
+
646
+ kioskTx
647
+ .return({
648
+ itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
649
+ item: sudoCard,
650
+ promise,
651
+ })
652
+ .finalize()
653
+ return tx
654
+ }
655
+
656
+ cancelOrder = (
657
+ orderCapId: string,
658
+ collateralToken: string,
659
+ indexToken: string,
660
+ long: boolean,
661
+ type: string,
662
+ isV11Order = false,
663
+ ) => {
664
+ const tx = new Transaction()
665
+ let functionName = ''
666
+ switch (type) {
667
+ case 'OPEN_POSITION': {
668
+ functionName = isV11Order ? 'clear_open_position_order' : 'clear_open_position_order'
669
+ break
670
+ }
671
+ case 'DECREASE_POSITION': {
672
+ functionName = isV11Order
673
+ ? 'clear_decrease_position_order'
674
+ : 'clear_decrease_position_order'
675
+ break
676
+ }
677
+ default: {
678
+ throw new Error('invalid order type')
679
+ }
680
+ }
681
+ tx.moveCall({
682
+ target: `${this.consts.zoCore.upgradedPackage}::market::${functionName}`,
683
+ typeArguments: [
684
+ `${this.consts.zoCore.package}::zlp::ZLP`,
685
+ this.consts.coins[collateralToken].module,
686
+ this.consts.coins[indexToken].module,
687
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
688
+ this.consts.coins[collateralToken].module,
689
+ ],
690
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(orderCapId)],
691
+ })
692
+ return tx
693
+ }
694
+
695
+ cancelMultiOrder = (orders: Array<{
696
+ orderCapId: string
697
+ collateralToken: string
698
+ indexToken: string
699
+ long: boolean
700
+ type: string
701
+ isV11Order: boolean
702
+ }>, tx?: Transaction) => {
703
+ if (!tx) {
704
+ tx = new Transaction()
705
+ }
706
+
707
+ for (const order of orders) {
708
+ const { orderCapId, collateralToken, indexToken, long, type, isV11Order } = order
709
+ let functionName = ''
710
+ switch (type) {
711
+ case 'OPEN_POSITION': {
712
+ functionName = isV11Order ? 'clear_open_position_order' : 'clear_open_position_order'
713
+ break
714
+ }
715
+ case 'DECREASE_POSITION': {
716
+ functionName = isV11Order
717
+ ? 'clear_decrease_position_order'
718
+ : 'clear_decrease_position_order'
719
+ break
720
+ }
721
+ default: {
722
+ throw new Error('invalid order type')
723
+ }
724
+ }
725
+
726
+ tx.moveCall({
727
+ target: `${this.consts.zoCore.upgradedPackage}::market::${functionName}`,
728
+ typeArguments: [
729
+ `${this.consts.zoCore.package}::zlp::ZLP`,
730
+ this.consts.coins[collateralToken].module,
731
+ this.consts.coins[indexToken].module,
732
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
733
+ this.consts.coins[collateralToken].module,
734
+ ],
735
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(orderCapId)],
736
+ })
737
+ }
738
+ return tx
739
+ }
740
+
741
+ swap = async (
742
+ fromToken: string,
743
+ toToken: string,
744
+ fromAmount: bigint,
745
+ fromCoinObjects: string[],
746
+ ) => {
747
+ const tx = await this.initOracleTxb(Object.keys(this.consts.zoCore.vaults))
748
+ const fromCoinObject = this.#processCoins(tx, fromToken, fromCoinObjects)
749
+ const [fromDepositObject] = tx.splitCoins(fromCoinObject, [tx.pure.u64(fromAmount)])
750
+ const vaultsValuation = this.valuateVaults(tx)
751
+
752
+ tx.moveCall({
753
+ target: `${this.consts.zoCore.upgradedPackage}::market::swap`,
754
+ typeArguments: [
755
+ `${this.consts.zoCore.package}::zlp::ZLP`,
756
+ this.consts.coins[fromToken].module,
757
+ this.consts.coins[toToken].module,
758
+ ],
759
+ arguments: [
760
+ tx.object(this.consts.zoCore.market),
761
+ tx.object(this.consts.zoCore.rebaseFeeModel),
762
+ fromDepositObject,
763
+ // FIXME: minAmountOut
764
+ tx.pure.u64(0),
765
+ vaultsValuation,
766
+ ],
767
+ })
768
+ return tx
769
+ }
770
+
771
+ clearClosedPosition = (
772
+ pcpId: string,
773
+ collateralToken: string,
774
+ indexToken: string,
775
+ long: boolean,
776
+ tx: Transaction,
777
+ ) => {
778
+ tx.moveCall({
779
+ target: `${this.consts.zoCore.upgradedPackage}::market::clear_closed_position`,
780
+ typeArguments: [
781
+ `${this.consts.zoCore.package}::zlp::ZLP`,
782
+ this.consts.coins[collateralToken].module,
783
+ this.consts.coins[indexToken].module,
784
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
785
+ ],
786
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(pcpId)],
787
+ })
788
+ }
789
+
790
+ clearOpenPositionOrder = (
791
+ orderCapId: string,
792
+ collateralToken: string,
793
+ indexToken: string,
794
+ long: boolean,
795
+ tx: Transaction,
796
+ isV11Order = false,
797
+ ) => {
798
+ const funcName = isV11Order ? 'clear_open_position_order' : 'clear_open_position_order'
799
+ tx.moveCall({
800
+ target: `${this.consts.zoCore.upgradedPackage}::market::${funcName}`,
801
+ typeArguments: [
802
+ `${this.consts.zoCore.package}::zlp::ZLP`,
803
+ this.consts.coins[collateralToken].module,
804
+ this.consts.coins[indexToken].module,
805
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
806
+ this.consts.coins[collateralToken].module,
807
+ ],
808
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(orderCapId)],
809
+ })
810
+ }
811
+
812
+ clearDecreasePositionOrder = (
813
+ orderCapId: string,
814
+ collateralToken: string,
815
+ indexToken: string,
816
+ long: boolean,
817
+ tx: Transaction,
818
+ isV11Order = false,
819
+ ) => {
820
+ const funcName = isV11Order ? 'clear_decrease_position_order' : 'clear_decrease_position_order'
821
+ tx.moveCall({
822
+ target: `${this.consts.zoCore.upgradedPackage}::market::${funcName}`,
823
+ typeArguments: [
824
+ `${this.consts.zoCore.package}::zlp::ZLP`,
825
+ this.consts.coins[collateralToken].module,
826
+ this.consts.coins[indexToken].module,
827
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
828
+ this.consts.coins[collateralToken].module,
829
+ ],
830
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(orderCapId)],
831
+ })
832
+ }
833
+
834
+ stake = (coinObjects: string[], amount: bigint, pool: string, tx?: Transaction | undefined) => {
835
+ if (!tx) {
836
+ tx = new Transaction()
837
+ }
838
+ const coinObject = this.#processCoins(tx, 'zlp', coinObjects)
839
+ const [depositObject] = tx.splitCoins(coinObject, [tx.pure.u64(amount)])
840
+
841
+ tx.moveCall({
842
+ target: `${this.consts.zoStaking.package}::pool::deposit`,
843
+ typeArguments: [`${this.consts.zoCore.package}::zlp::ZLP`, this.consts.coins.sui.module],
844
+ arguments: [tx.object(pool), tx.object(SUI_CLOCK_OBJECT_ID), depositObject],
845
+ })
846
+
847
+ return tx
848
+ }
849
+
850
+ unstake = (
851
+ credentials: ICredential[],
852
+ amount: bigint,
853
+ pool: string,
854
+ tx?: Transaction | undefined,
855
+ ) => {
856
+ if (!tx) {
857
+ tx = new Transaction()
858
+ }
859
+ for (const credential of credentials) {
860
+ // eslint-disable-next-line unicorn/prefer-math-min-max
861
+ const withdrawAmount = amount < credential.amount ? amount : credential.amount
862
+ amount -= withdrawAmount
863
+ tx.moveCall({
864
+ target: `0xdb353cb79a88d5ee83b7c0ca0249662a1facab17ba62879080ea3d61b9d21112::pool::withdraw`,
865
+ typeArguments: [`${this.consts.zoCore.package}::zlp::ZLP`, this.consts.coins.sui.module],
866
+ arguments: [
867
+ tx.object(pool),
868
+ tx.object(SUI_CLOCK_OBJECT_ID),
869
+ tx.object(credential.id),
870
+ tx.pure.u64(withdrawAmount),
871
+ ],
872
+ })
873
+ if (credential.amount === BigInt(0)) {
874
+ tx.moveCall({
875
+ target: `${this.consts.zoStaking.package}::pool::clear_empty_credential`,
876
+ typeArguments: [`${this.consts.zoCore.package}::zlp::ZLP`, this.consts.coins.sui.module],
877
+ arguments: [tx.object(credential.id)],
878
+ })
879
+ }
880
+ }
881
+
882
+ return tx
883
+ }
884
+
885
+ addReferral = (referrer: string, tx?: Transaction | undefined) => {
886
+ if (!tx) {
887
+ tx = new Transaction()
888
+ }
889
+ tx.moveCall({
890
+ target: `${this.consts.zoCore.upgradedPackage}::market::add_new_referral`,
891
+ typeArguments: [`${this.consts.zoCore.package}::zlp::ZLP`],
892
+ arguments: [tx.object(this.consts.zoCore.market), tx.object(referrer)],
893
+ })
894
+
895
+ return tx
896
+ }
897
+
898
+ // admin methods
899
+ adminUpdatePriceFeed = async (collateralToken: string, indexToken: string) => {
900
+ const tx = await this.initOracleTxb([collateralToken, indexToken])
901
+ return tx
902
+ }
903
+
904
+ adminSettlePosition = (
905
+ positionId: string,
906
+ owner: string,
907
+ collateralToken: string,
908
+ indexToken: string,
909
+ long: boolean,
910
+ ) => {
911
+ const tx = new Transaction()
912
+
913
+ tx.moveCall({
914
+ target: `${this.consts.zoCore.upgradedPackage}::market::force_settle_position`,
915
+ typeArguments: [
916
+ `${this.consts.zoCore.package}::zlp::ZLP`,
917
+ this.consts.coins[collateralToken].module,
918
+ this.consts.coins[indexToken].module,
919
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
920
+ ],
921
+ arguments: [
922
+ tx.object(this.consts.zoCore.adminCap),
923
+ tx.object(SUI_CLOCK_OBJECT_ID),
924
+ tx.object(this.consts.zoCore.market),
925
+ tx.object(owner),
926
+ tx.object(positionId),
927
+ ],
928
+ })
929
+
930
+ this.adminClearClosedPosition(positionId, owner, collateralToken, indexToken, long, tx)
931
+
932
+ return tx
933
+ }
934
+
935
+ adminDecreasePosition = (
936
+ positionId: string,
937
+ owner: string,
938
+ collateralToken: string,
939
+ indexToken: string,
940
+ positionAmount: number,
941
+ amount: bigint,
942
+ long: boolean,
943
+ ) => {
944
+ const tx = new Transaction()
945
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
946
+
947
+ tx.moveCall({
948
+ target: `${this.consts.zoCore.upgradedPackage}::market::force_close_position`,
949
+ typeArguments: [
950
+ `${this.consts.zoCore.package}::zlp::ZLP`,
951
+ this.consts.coins[collateralToken].module,
952
+ this.consts.coins[indexToken].module,
953
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
954
+ ],
955
+ arguments: [
956
+ tx.object(this.consts.zoCore.adminCap),
957
+ tx.object(SUI_CLOCK_OBJECT_ID),
958
+ tx.object(this.consts.zoCore.market),
959
+ tx.object(this.consts.zoCore.vaults[collateralToken].reservingFeeModel),
960
+ tx.object(this.consts.zoCore.symbols[symbol].fundingFeeModel),
961
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
962
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
963
+ tx.object(owner),
964
+ tx.object(positionId),
965
+ ],
966
+ })
967
+
968
+ if (amount === BigInt(positionAmount)) {
969
+ this.adminClearClosedPosition(positionId, owner, collateralToken, indexToken, long, tx)
970
+ }
971
+
972
+ return tx
973
+ }
974
+
975
+ adminClearClosedPosition = (
976
+ positionId: string,
977
+ owner: string,
978
+ collateralToken: string,
979
+ indexToken: string,
980
+ long: boolean,
981
+ tx: Transaction,
982
+ ) => {
983
+ tx.moveCall({
984
+ target: `${this.consts.zoCore.upgradedPackage}::market::force_clear_closed_position`,
985
+ typeArguments: [
986
+ `${this.consts.zoCore.package}::zlp::ZLP`,
987
+ this.consts.coins[collateralToken].module,
988
+ this.consts.coins[indexToken].module,
989
+ `${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
990
+ ],
991
+ arguments: [
992
+ tx.object(this.consts.zoCore.adminCap),
993
+ tx.object(this.consts.zoCore.market),
994
+ tx.object(positionId),
995
+ tx.object(owner),
996
+ ],
997
+ })
998
+ }
999
+
1000
+ // ZO Lootbox
1001
+ adminIssueLootbox = (
1002
+ coin: string,
1003
+ tiers: number[],
1004
+ beneficiaries: string[],
1005
+ amounts: number[],
1006
+ tx: Transaction,
1007
+ ) => {
1008
+ let module = `${this.consts.zoCore.package}::zlp::ZLP`
1009
+ if (coin !== 'zlp') {
1010
+ module = this.consts.coins[coin].module
1011
+ }
1012
+ for (const [i, beneficiary] of beneficiaries.entries()) {
1013
+ tx.moveCall({
1014
+ target: `${this.consts.zoLootbox.package}::lootbox::admin_issue_lootbox`,
1015
+ typeArguments: [module],
1016
+ arguments: [
1017
+ tx.object(this.consts.zoLootbox.adminCap),
1018
+ tx.pure.u8(tiers[i]),
1019
+ tx.pure.address(beneficiary),
1020
+ tx.pure.u64(amounts[i]),
1021
+ tx.pure.bool(amounts[i] > 0),
1022
+ tx.object(`${this.consts.zoLootbox.lootboxSettings}`),
1023
+ ],
1024
+ })
1025
+ }
1026
+ }
1027
+
1028
+ depositToPrizePool = (
1029
+ coin: string,
1030
+ coinObjects: string[],
1031
+ amount: number,
1032
+ ) => {
1033
+ const tx = new Transaction()
1034
+ const coinObject = this.#processCoins(tx, coin, coinObjects)
1035
+ const [depositObject] = tx.splitCoins(coinObject, [tx.pure.u64(amount)])
1036
+
1037
+ let module = `${this.consts.zoCore.package}::zlp::ZLP`
1038
+ if (coin !== 'zlp') {
1039
+ module = this.consts.coins[coin].module
1040
+ }
1041
+
1042
+ tx.moveCall({
1043
+ target: `${this.consts.zoLootbox.package}::lootbox::deposit`,
1044
+ typeArguments: [module],
1045
+ arguments: [
1046
+ tx.object(this.consts.zoLootbox.adminCap),
1047
+ tx.object(this.consts.zoLootbox.lootboxTreasury),
1048
+ depositObject,
1049
+ ],
1050
+ })
1051
+ return tx
1052
+ }
1053
+
1054
+ withdrawFromPricePool = (coin: string, amount: number) => {
1055
+ const tx = new Transaction()
1056
+
1057
+ let module = `${this.consts.zoCore.package}::zlp::ZLP`
1058
+ let metadata = this.consts.zoCore.zlpMetadata
1059
+ if (coin !== 'zlp') {
1060
+ module = this.consts.coins[coin].module
1061
+ metadata = this.consts.coins[coin].metadata
1062
+ }
1063
+
1064
+ tx.moveCall({
1065
+ target: `${this.consts.zoLootbox.package}::lootbox::withdraw`,
1066
+ typeArguments: [module],
1067
+ arguments: [
1068
+ tx.object(this.consts.zoLootbox.adminCap),
1069
+ tx.object(this.consts.zoLootbox.lootboxTreasury),
1070
+ tx.object(metadata),
1071
+ tx.pure.u64(amount),
1072
+ ],
1073
+ })
1074
+ return tx
1075
+ }
1076
+
1077
+ openLootbox = (coin: string, lootbox: string) => {
1078
+ const tx = new Transaction()
1079
+
1080
+ let module = `${this.consts.zoCore.package}::zlp::ZLP`
1081
+ let metadata = this.consts.zoCore.zlpMetadata
1082
+ if (coin !== 'zlp') {
1083
+ module = this.consts.coins[coin].module
1084
+ metadata = this.consts.coins[coin].metadata
1085
+ }
1086
+
1087
+ tx.moveCall({
1088
+ target: `${this.consts.zoLootbox.package}::lootbox::open_lootbox`,
1089
+ typeArguments: [module],
1090
+ arguments: [
1091
+ tx.object(`${this.consts.zoLootbox.lootboxTreasury}`),
1092
+ tx.object(lootbox),
1093
+ tx.object(metadata),
1094
+ tx.object(`${this.consts.zoLootbox.lootboxSettings}`),
1095
+ ],
1096
+ })
1097
+
1098
+ return tx
1099
+ }
1100
+ }