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