zo-sdk 0.0.50 → 0.1.0

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