zo-sdk 0.1.62 → 0.1.64

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 (46) hide show
  1. package/dist/consts/deployments-usdz-mainnet.json +37 -5
  2. package/dist/consts/price_id_to_object_id.mainnet.json +3 -1
  3. package/dist/implementations/SLPAPI.cjs +544 -0
  4. package/dist/implementations/SLPAPI.cjs.map +1 -1
  5. package/dist/implementations/SLPAPI.d.cts +49 -0
  6. package/dist/implementations/SLPAPI.d.cts.map +1 -1
  7. package/dist/implementations/SLPAPI.d.mts +49 -0
  8. package/dist/implementations/SLPAPI.d.mts.map +1 -1
  9. package/dist/implementations/SLPAPI.mjs +544 -0
  10. package/dist/implementations/SLPAPI.mjs.map +1 -1
  11. package/dist/implementations/SLPDataAPI.cjs +48 -9
  12. package/dist/implementations/SLPDataAPI.cjs.map +1 -1
  13. package/dist/implementations/SLPDataAPI.d.cts.map +1 -1
  14. package/dist/implementations/SLPDataAPI.d.mts.map +1 -1
  15. package/dist/implementations/SLPDataAPI.mjs +49 -10
  16. package/dist/implementations/SLPDataAPI.mjs.map +1 -1
  17. package/dist/implementations/USDZAPI.cjs +544 -0
  18. package/dist/implementations/USDZAPI.cjs.map +1 -1
  19. package/dist/implementations/USDZAPI.d.cts +49 -0
  20. package/dist/implementations/USDZAPI.d.cts.map +1 -1
  21. package/dist/implementations/USDZAPI.d.mts +49 -0
  22. package/dist/implementations/USDZAPI.d.mts.map +1 -1
  23. package/dist/implementations/USDZAPI.mjs +544 -0
  24. package/dist/implementations/USDZAPI.mjs.map +1 -1
  25. package/dist/implementations/USDZDataAPI.cjs +47 -7
  26. package/dist/implementations/USDZDataAPI.cjs.map +1 -1
  27. package/dist/implementations/USDZDataAPI.d.cts.map +1 -1
  28. package/dist/implementations/USDZDataAPI.d.mts.map +1 -1
  29. package/dist/implementations/USDZDataAPI.mjs +47 -7
  30. package/dist/implementations/USDZDataAPI.mjs.map +1 -1
  31. package/dist/implementations/ZLPAPI.cjs +2 -6
  32. package/dist/implementations/ZLPAPI.cjs.map +1 -1
  33. package/dist/implementations/ZLPAPI.d.cts +2 -2
  34. package/dist/implementations/ZLPAPI.d.cts.map +1 -1
  35. package/dist/implementations/ZLPAPI.d.mts +2 -2
  36. package/dist/implementations/ZLPAPI.d.mts.map +1 -1
  37. package/dist/implementations/ZLPAPI.mjs +2 -6
  38. package/dist/implementations/ZLPAPI.mjs.map +1 -1
  39. package/package.json +1 -1
  40. package/src/consts/deployments-usdz-mainnet.json +37 -5
  41. package/src/consts/price_id_to_object_id.mainnet.json +3 -1
  42. package/src/implementations/SLPAPI.ts +884 -26
  43. package/src/implementations/SLPDataAPI.ts +52 -10
  44. package/src/implementations/USDZAPI.ts +913 -55
  45. package/src/implementations/USDZDataAPI.ts +50 -7
  46. package/src/implementations/ZLPAPI.ts +2 -7
@@ -907,6 +907,175 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
907
907
  return tx
908
908
  }
909
909
 
910
+ /**
911
+ * Opens a new position in SLP (V2 - uses v3 contract)
912
+ */
913
+ public async openPositionV2(
914
+ collateralToken: string,
915
+ indexToken: string,
916
+ size: bigint,
917
+ collateralAmount: bigint,
918
+ coinObjects: string[],
919
+ long: boolean,
920
+ reserveAmount: bigint,
921
+ indexPrice: number,
922
+ collateralPrice: number,
923
+ isLimitOrder?: boolean,
924
+ isIocOrder?: boolean,
925
+ pricesSlippage = 0.003,
926
+ collateralSlippage = 0.5,
927
+ relayerFee = BigInt(0.5),
928
+ referralAddress?: string,
929
+ sender?: string,
930
+ sponsoredTx?: boolean,
931
+ suiCoinObjectsForPythUpdate?: string[],
932
+ ): Promise<Transaction> {
933
+ let tx = new Transaction()
934
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
935
+ tx = await this.addReferral(referralAddress, tx)
936
+ }
937
+
938
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
939
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage)
940
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
941
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage)
942
+
943
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
944
+ if (isLimitOrder) {
945
+ allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
946
+ }
947
+
948
+ // Handle oracle initialization and coin processing
949
+ let suiCoinObject
950
+ if (sponsoredTx) {
951
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
952
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
953
+ }
954
+ else {
955
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
956
+ }
957
+
958
+ // Process coin splitting
959
+ const [depositObject, feeObject] = this.processCoinSplitting(
960
+ tx,
961
+ collateralToken,
962
+ coinObjects,
963
+ [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)],
964
+ sponsoredTx,
965
+ suiCoinObject,
966
+ )
967
+
968
+ tx.moveCall({
969
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_v3`,
970
+ typeArguments: [
971
+ `${this.consts.sudoCore.package}::slp::SLP`,
972
+ this.consts.coins[collateralToken].module,
973
+ this.consts.coins[indexToken].module,
974
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
975
+ this.consts.coins[collateralToken].module,
976
+ ],
977
+ arguments: [
978
+ tx.object(SUI_CLOCK_OBJECT_ID),
979
+ tx.object(this.consts.sudoCore.market),
980
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
981
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
982
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
983
+ depositObject,
984
+ feeObject,
985
+ tx.pure.u8(allowTrade),
986
+ tx.pure.u64(size),
987
+ tx.pure.u64(reserveAmount),
988
+ tx.pure.u256(adjustCollateralPrice),
989
+ tx.pure.u256(adjustPrice),
990
+ tx.pure.u256(indexPriceThreshold),
991
+ ],
992
+ })
993
+
994
+ return tx
995
+ }
996
+
997
+ /**
998
+ * Opens a new position with Coin in SLP (V2 - uses v3 contract)
999
+ */
1000
+ public async openPositionWithCoinV2(
1001
+ collateralToken: string,
1002
+ indexToken: string,
1003
+ size: bigint,
1004
+ coinObj: TransactionObjectArgument,
1005
+ long: boolean,
1006
+ reserveAmount: bigint,
1007
+ indexPrice: number,
1008
+ collateralPrice: number,
1009
+ isLimitOrder?: boolean,
1010
+ isIocOrder?: boolean,
1011
+ pricesSlippage = 0.003,
1012
+ collateralSlippage = 0.5,
1013
+ relayerFee = BigInt(0.5),
1014
+ referralAddress?: string,
1015
+ sender?: string,
1016
+ tx?: Transaction,
1017
+ sponsoredTx?: boolean,
1018
+ suiCoinObjectsForPythUpdate?: string[],
1019
+ ): Promise<Transaction> {
1020
+ if (!tx) {
1021
+ tx = new Transaction()
1022
+ }
1023
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
1024
+ tx = await this.addReferral(referralAddress, tx)
1025
+ }
1026
+
1027
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1028
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage)
1029
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
1030
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage)
1031
+
1032
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
1033
+ if (isLimitOrder) {
1034
+ allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
1035
+ }
1036
+
1037
+ // Handle oracle initialization and coin processing
1038
+ let suiCoinObject
1039
+ if (sponsoredTx) {
1040
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
1041
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
1042
+ }
1043
+ else {
1044
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
1045
+ }
1046
+
1047
+ // Process coin splitting
1048
+ const [feeObject] = tx.splitCoins(coinObj, [tx.pure.u64(relayerFee)])
1049
+
1050
+ tx.moveCall({
1051
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_v3`,
1052
+ typeArguments: [
1053
+ `${this.consts.sudoCore.package}::slp::SLP`,
1054
+ this.consts.coins[collateralToken].module,
1055
+ this.consts.coins[indexToken].module,
1056
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1057
+ this.consts.coins[collateralToken].module,
1058
+ ],
1059
+ arguments: [
1060
+ tx.object(SUI_CLOCK_OBJECT_ID),
1061
+ tx.object(this.consts.sudoCore.market),
1062
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
1063
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1064
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1065
+ coinObj,
1066
+ feeObject,
1067
+ tx.pure.u8(allowTrade),
1068
+ tx.pure.u64(size),
1069
+ tx.pure.u64(reserveAmount),
1070
+ tx.pure.u256(adjustCollateralPrice),
1071
+ tx.pure.u256(adjustPrice),
1072
+ tx.pure.u256(indexPriceThreshold),
1073
+ ],
1074
+ })
1075
+
1076
+ return tx
1077
+ }
1078
+
910
1079
  /**
911
1080
  * Decreases an existing position in SLP using Sudo SDK approach
912
1081
  */
@@ -1004,6 +1173,93 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1004
1173
  return tx
1005
1174
  }
1006
1175
 
1176
+ /**
1177
+ * Decreases an existing position in SLP (V2 - uses v3 contract)
1178
+ */
1179
+ public async decreasePositionV2(
1180
+ pcpId: string,
1181
+ collateralToken: string,
1182
+ indexToken: string,
1183
+ amount: bigint,
1184
+ long: boolean,
1185
+ indexPrice: number,
1186
+ collateralPrice: number,
1187
+ isTriggerOrder = false,
1188
+ isTakeProfitOrder = true,
1189
+ isIocOrder = false,
1190
+ pricesSlippage = 0.003,
1191
+ collateralSlippage = 0.5,
1192
+ relayerFee = BigInt(0.5),
1193
+ coinObjects?: string[],
1194
+ sponsoredTx?: boolean,
1195
+ suiCoinObjectsForPythUpdate?: string[],
1196
+ ): Promise<Transaction> {
1197
+ if (!coinObjects) {
1198
+ throw new Error(`${this.constructor.name}: coinObjects is required`)
1199
+ }
1200
+
1201
+ let tx = new Transaction()
1202
+
1203
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
1204
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
1205
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage)
1206
+
1207
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
1208
+ if (isTriggerOrder) {
1209
+ allowTrade = isIocOrder || !isTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
1210
+ }
1211
+ else {
1212
+ isTakeProfitOrder = true
1213
+ }
1214
+
1215
+ // Handle oracle initialization and coin processing
1216
+ let suiCoinObject
1217
+ if (sponsoredTx) {
1218
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
1219
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
1220
+ }
1221
+ else {
1222
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
1223
+ }
1224
+
1225
+ // Process coin splitting
1226
+ const [feeObject] = this.processCoinSplitting(
1227
+ tx,
1228
+ collateralToken,
1229
+ coinObjects,
1230
+ [tx.pure.u64(relayerFee)],
1231
+ sponsoredTx,
1232
+ suiCoinObject,
1233
+ )
1234
+
1235
+ tx.moveCall({
1236
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_v3`,
1237
+ typeArguments: [
1238
+ `${this.consts.sudoCore.package}::slp::SLP`,
1239
+ this.consts.coins[collateralToken].module,
1240
+ this.consts.coins[indexToken].module,
1241
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1242
+ this.consts.coins[collateralToken].module,
1243
+ ],
1244
+ arguments: [
1245
+ tx.object(SUI_CLOCK_OBJECT_ID),
1246
+ tx.object(this.consts.sudoCore.market),
1247
+ tx.object(pcpId),
1248
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1249
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1250
+ feeObject,
1251
+ tx.pure.u8(allowTrade),
1252
+ tx.pure.bool(isTakeProfitOrder),
1253
+ tx.pure.u64(amount),
1254
+ tx.pure.u256(adjustCollateralPrice),
1255
+ tx.pure.u256(adjustPrice),
1256
+ tx.pure.u256(indexPriceThreshold),
1257
+ ],
1258
+ })
1259
+
1260
+ return tx
1261
+ }
1262
+
1007
1263
  public async decreaseMultiPositions(
1008
1264
  positions: Array<{
1009
1265
  pcpId: string
@@ -1057,10 +1313,112 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1057
1313
  relayerFee = BigInt(0.5),
1058
1314
  } = position
1059
1315
  let innerIsTakeProfitOrder = isTakeProfitOrder
1060
- const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1316
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1317
+
1318
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
1319
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
1320
+
1321
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
1322
+ if (isTriggerOrder) {
1323
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
1324
+ }
1325
+ else {
1326
+ innerIsTakeProfitOrder = true
1327
+ }
1328
+
1329
+ // Process coin splitting
1330
+ const [feeObject] = this.processCoinSplitting(
1331
+ tx,
1332
+ collateralToken,
1333
+ coinObjects,
1334
+ [tx.pure.u64(relayerFee)],
1335
+ sponsoredTx,
1336
+ suiCoinObject,
1337
+ )
1338
+
1339
+ tx.moveCall({
1340
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_v2_1`,
1341
+ typeArguments: [
1342
+ `${this.consts.sudoCore.package}::slp::SLP`,
1343
+ this.consts.coins[collateralToken].module,
1344
+ this.consts.coins[indexToken].module,
1345
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1346
+ this.consts.coins[collateralToken].module,
1347
+ ],
1348
+ arguments: [
1349
+ tx.object(SUI_CLOCK_OBJECT_ID),
1350
+ tx.object(this.consts.sudoCore.market),
1351
+ tx.object(pcpId),
1352
+ tx.object(
1353
+ this.consts.sudoCore.vaults[collateralToken].reservingFeeModel,
1354
+ ),
1355
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1356
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1357
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1358
+ feeObject,
1359
+ tx.pure.u8(allowTrade),
1360
+ tx.pure.bool(innerIsTakeProfitOrder),
1361
+ tx.pure.u64(amount),
1362
+ tx.pure.u256(adjustCollateralPrice),
1363
+ tx.pure.u256(adjustPrice),
1364
+ ],
1365
+ })
1366
+ }
1367
+ return tx
1368
+ }
1369
+
1370
+ public async decreaseMultiPositionsV2(positions: Array<{
1371
+ pcpId: string
1372
+ collateralToken: string
1373
+ indexToken: string
1374
+ amount: bigint
1375
+ long: boolean
1376
+ indexPrice: number
1377
+ collateralPrice: number
1378
+ isTriggerOrder?: boolean
1379
+ isTakeProfitOrder?: boolean
1380
+ isIocOrder?: boolean
1381
+ pricesSlippage?: number
1382
+ collateralSlippage?: number
1383
+ relayerFee?: bigint
1384
+ coinObjects?: string[]
1385
+ }>, tx?: Transaction, sponsoredTx?: boolean, suiCoinObjectsForPythUpdate?: string[]): Promise<Transaction> {
1386
+ if (!tx) {
1387
+ tx = new Transaction()
1388
+ }
1389
+
1390
+ // Handle oracle initialization and coin processing
1391
+ let suiCoinObject
1392
+ if (sponsoredTx) {
1393
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
1394
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx, true, suiCoinObject)
1395
+ }
1396
+ else {
1397
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
1398
+ }
1399
+
1400
+ for (const position of positions) {
1401
+ const {
1402
+ pcpId,
1403
+ collateralToken,
1404
+ coinObjects = [],
1405
+ indexToken,
1406
+ amount,
1407
+ long,
1408
+ indexPrice,
1409
+ collateralPrice,
1410
+ isTriggerOrder = false,
1411
+ isTakeProfitOrder = true,
1412
+ isIocOrder = false,
1413
+ pricesSlippage = 0.003,
1414
+ collateralSlippage = 0.5,
1415
+ relayerFee = BigInt(0.5),
1416
+ } = position
1417
+ let innerIsTakeProfitOrder = isTakeProfitOrder
1061
1418
 
1062
1419
  const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
1063
1420
  const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
1421
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage)
1064
1422
 
1065
1423
  let allowTrade = ALLOW_TRADE_MUST_TRADE
1066
1424
  if (isTriggerOrder) {
@@ -1081,7 +1439,7 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1081
1439
  )
1082
1440
 
1083
1441
  tx.moveCall({
1084
- target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_v2_1`,
1442
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_v3`,
1085
1443
  typeArguments: [
1086
1444
  `${this.consts.sudoCore.package}::slp::SLP`,
1087
1445
  this.consts.coins[collateralToken].module,
@@ -1093,10 +1451,6 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1093
1451
  tx.object(SUI_CLOCK_OBJECT_ID),
1094
1452
  tx.object(this.consts.sudoCore.market),
1095
1453
  tx.object(pcpId),
1096
- tx.object(
1097
- this.consts.sudoCore.vaults[collateralToken].reservingFeeModel,
1098
- ),
1099
- tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1100
1454
  tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1101
1455
  tx.object(this.consts.pythFeeder.feeder[indexToken]),
1102
1456
  feeObject,
@@ -1105,6 +1459,7 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1105
1459
  tx.pure.u64(amount),
1106
1460
  tx.pure.u256(adjustCollateralPrice),
1107
1461
  tx.pure.u256(adjustPrice),
1462
+ tx.pure.u256(indexPriceThreshold),
1108
1463
  ],
1109
1464
  })
1110
1465
  }
@@ -1221,10 +1576,18 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1221
1576
  functionName = 'clear_open_position_order_unified'
1222
1577
  break
1223
1578
  }
1579
+ case 'OPEN_MARKET': {
1580
+ functionName = 'clear_open_market_order'
1581
+ break
1582
+ }
1224
1583
  case 'DECREASE_POSITION': {
1225
1584
  functionName = 'clear_decrease_position_order_unified'
1226
1585
  break
1227
1586
  }
1587
+ case 'DECREASE_MARKET': {
1588
+ functionName = 'clear_decrease_market_order'
1589
+ break
1590
+ }
1228
1591
  default: {
1229
1592
  throw new Error('invalid order type')
1230
1593
  }
@@ -1269,10 +1632,18 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1269
1632
  functionName = 'clear_open_position_order_unified'
1270
1633
  break
1271
1634
  }
1635
+ case 'OPEN_MARKET': {
1636
+ functionName = 'clear_open_market_order'
1637
+ break
1638
+ }
1272
1639
  case 'DECREASE_POSITION': {
1273
1640
  functionName = 'clear_decrease_position_order_unified'
1274
1641
  break
1275
1642
  }
1643
+ case 'DECREASE_MARKET': {
1644
+ functionName = 'clear_decrease_market_order'
1645
+ break
1646
+ }
1276
1647
  default: {
1277
1648
  throw new Error('invalid order type')
1278
1649
  }
@@ -1360,7 +1731,262 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1360
1731
  })
1361
1732
  }
1362
1733
 
1363
- public async openPositionWithSCard(
1734
+ public clearOpenMarketOrder(
1735
+ orderCapId: string,
1736
+ collateralToken: string,
1737
+ indexToken: string,
1738
+ long: boolean,
1739
+ tx: Transaction,
1740
+ _isV11Order?: boolean,
1741
+ ): void {
1742
+ const funcName = 'clear_open_market_order'
1743
+ tx.moveCall({
1744
+ target: `${this.consts.sudoCore.upgradedPackage}::market::${funcName}`,
1745
+ typeArguments: [
1746
+ `${this.consts.sudoCore.package}::slp::SLP`,
1747
+ this.consts.coins[collateralToken].module,
1748
+ this.consts.coins[indexToken].module,
1749
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1750
+ this.consts.coins[collateralToken].module,
1751
+ ],
1752
+ arguments: [tx.object(this.consts.sudoCore.market), tx.object(orderCapId)],
1753
+ })
1754
+ }
1755
+
1756
+ public clearDecreaseMarketOrder(
1757
+ orderCapId: string,
1758
+ collateralToken: string,
1759
+ indexToken: string,
1760
+ long: boolean,
1761
+ tx: Transaction,
1762
+ _isV11Order?: boolean,
1763
+ ): void {
1764
+ const funcName = 'clear_decrease_market_order'
1765
+ tx.moveCall({
1766
+ target: `${this.consts.sudoCore.upgradedPackage}::market::${funcName}`,
1767
+ typeArguments: [
1768
+ `${this.consts.sudoCore.package}::slp::SLP`,
1769
+ this.consts.coins[collateralToken].module,
1770
+ this.consts.coins[indexToken].module,
1771
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1772
+ this.consts.coins[collateralToken].module,
1773
+ ],
1774
+ arguments: [tx.object(this.consts.sudoCore.market), tx.object(orderCapId)],
1775
+ })
1776
+ }
1777
+
1778
+ public async openPositionWithSCard(
1779
+ collateralToken: string,
1780
+ indexToken: string,
1781
+ size: bigint,
1782
+ collateralAmount: bigint,
1783
+ coinObjects: string[],
1784
+ long: boolean,
1785
+ reserveAmount: bigint,
1786
+ indexPrice: number,
1787
+ collateralPrice: number,
1788
+ kioskClient: KioskClient,
1789
+ kioskCap: KioskOwnerCap,
1790
+ scard: string,
1791
+ isLimitOrder = false,
1792
+ isIocOrder = false,
1793
+ pricesSlippage = 0.003,
1794
+ collateralSlippage = 0.5,
1795
+ relayerFee = BigInt(0.5),
1796
+ referralAddress?: string,
1797
+ sender?: string,
1798
+ sponsoredTx?: boolean,
1799
+ suiCoinObjectsForPythUpdate?: string[],
1800
+ ): Promise<Transaction> {
1801
+ let tx = new Transaction()
1802
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
1803
+ tx = await this.addReferral(referralAddress, tx)
1804
+ }
1805
+
1806
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1807
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage || 0.003)
1808
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage || 0.5)
1809
+
1810
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
1811
+ if (isLimitOrder) {
1812
+ allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
1813
+ }
1814
+
1815
+ const kioskTx = new KioskTransaction({
1816
+ transaction: tx,
1817
+ kioskClient,
1818
+ cap: kioskCap,
1819
+ })
1820
+
1821
+ const [sudoCard, promise] = kioskTx.borrow({
1822
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1823
+ itemId: scard,
1824
+ })
1825
+
1826
+ // Handle oracle initialization and coin processing
1827
+ let suiCoinObject
1828
+ if (sponsoredTx) {
1829
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
1830
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
1831
+ }
1832
+ else {
1833
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
1834
+ }
1835
+
1836
+ // Process coin splitting
1837
+ const [depositObject, feeObject] = this.processCoinSplitting(
1838
+ tx,
1839
+ collateralToken,
1840
+ coinObjects,
1841
+ [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)],
1842
+ sponsoredTx,
1843
+ suiCoinObject,
1844
+ )
1845
+
1846
+ tx.moveCall({
1847
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v1`,
1848
+ typeArguments: [
1849
+ `${this.consts.sudoCore.package}::slp::SLP`,
1850
+ this.consts.coins[collateralToken].module,
1851
+ this.consts.coins[indexToken].module,
1852
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1853
+ this.consts.coins[collateralToken].module,
1854
+ ],
1855
+ arguments: [
1856
+ tx.object(SUI_CLOCK_OBJECT_ID),
1857
+ tx.object(this.consts.sudoCore.market),
1858
+ tx.object(
1859
+ this.consts.sudoCore.vaults[collateralToken].reservingFeeModel,
1860
+ ),
1861
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1862
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
1863
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1864
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1865
+ depositObject,
1866
+ feeObject,
1867
+ tx.pure.u8(allowTrade),
1868
+ tx.pure.u64(size),
1869
+ tx.pure.u64(reserveAmount),
1870
+ tx.pure.u256(adjustCollateralPrice),
1871
+ tx.pure.u256(adjustPrice),
1872
+ sudoCard,
1873
+ ],
1874
+ })
1875
+
1876
+ kioskTx
1877
+ .return({
1878
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1879
+ item: sudoCard,
1880
+ promise,
1881
+ })
1882
+ .finalize()
1883
+ return tx
1884
+ }
1885
+
1886
+ public async openPositionWithCoinAndSCard(
1887
+ collateralToken: string,
1888
+ indexToken: string,
1889
+ size: bigint,
1890
+ coinObj: TransactionObjectArgument,
1891
+ long: boolean,
1892
+ reserveAmount: bigint,
1893
+ indexPrice: number,
1894
+ collateralPrice: number,
1895
+ kioskClient: KioskClient,
1896
+ kioskCap: KioskOwnerCap,
1897
+ scard: string,
1898
+ isLimitOrder = false,
1899
+ isIocOrder = false,
1900
+ pricesSlippage = 0.003,
1901
+ collateralSlippage = 0.5,
1902
+ relayerFee = BigInt(0.5),
1903
+ referralAddress?: string,
1904
+ sender?: string,
1905
+ tx?: Transaction,
1906
+ sponsoredTx?: boolean,
1907
+ suiCoinObjectsForPythUpdate?: string[],
1908
+ ): Promise<Transaction> {
1909
+ if (!tx) {
1910
+ tx = new Transaction()
1911
+ }
1912
+ if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
1913
+ tx = await this.addReferral(referralAddress, tx)
1914
+ }
1915
+
1916
+ const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1917
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage || 0.003)
1918
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage || 0.5)
1919
+
1920
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
1921
+ if (isLimitOrder) {
1922
+ allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
1923
+ }
1924
+
1925
+ const kioskTx = new KioskTransaction({
1926
+ transaction: tx,
1927
+ kioskClient,
1928
+ cap: kioskCap,
1929
+ })
1930
+
1931
+ const [sudoCard, promise] = kioskTx.borrow({
1932
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1933
+ itemId: scard,
1934
+ })
1935
+
1936
+ // Handle oracle initialization and coin processing
1937
+ let suiCoinObject
1938
+ if (sponsoredTx) {
1939
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
1940
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
1941
+ }
1942
+ else {
1943
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
1944
+ }
1945
+
1946
+ // Process coin splitting
1947
+ const [feeObject] = tx.splitCoins(coinObj, [tx.pure.u64(relayerFee)])
1948
+
1949
+ tx.moveCall({
1950
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v1`,
1951
+ typeArguments: [
1952
+ `${this.consts.sudoCore.package}::slp::SLP`,
1953
+ this.consts.coins[collateralToken].module,
1954
+ this.consts.coins[indexToken].module,
1955
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
1956
+ this.consts.coins[collateralToken].module,
1957
+ ],
1958
+ arguments: [
1959
+ tx.object(SUI_CLOCK_OBJECT_ID),
1960
+ tx.object(this.consts.sudoCore.market),
1961
+ tx.object(
1962
+ this.consts.sudoCore.vaults[collateralToken].reservingFeeModel,
1963
+ ),
1964
+ tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1965
+ tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
1966
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1967
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
1968
+ coinObj,
1969
+ feeObject,
1970
+ tx.pure.u8(allowTrade),
1971
+ tx.pure.u64(size),
1972
+ tx.pure.u64(reserveAmount),
1973
+ tx.pure.u256(adjustCollateralPrice),
1974
+ tx.pure.u256(adjustPrice),
1975
+ sudoCard,
1976
+ ],
1977
+ })
1978
+
1979
+ kioskTx
1980
+ .return({
1981
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
1982
+ item: sudoCard,
1983
+ promise,
1984
+ })
1985
+ .finalize()
1986
+ return tx
1987
+ }
1988
+
1989
+ public async openPositionWithSCardV2(
1364
1990
  collateralToken: string,
1365
1991
  indexToken: string,
1366
1992
  size: bigint,
@@ -1373,8 +1999,8 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1373
1999
  kioskClient: KioskClient,
1374
2000
  kioskCap: KioskOwnerCap,
1375
2001
  scard: string,
1376
- isLimitOrder = false,
1377
- isIocOrder = false,
2002
+ isLimitOrder?: boolean,
2003
+ isIocOrder?: boolean,
1378
2004
  pricesSlippage = 0.003,
1379
2005
  collateralSlippage = 0.5,
1380
2006
  relayerFee = BigInt(0.5),
@@ -1389,8 +2015,9 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1389
2015
  }
1390
2016
 
1391
2017
  const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1392
- const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage || 0.003)
1393
- const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage || 0.5)
2018
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage)
2019
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
2020
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage)
1394
2021
 
1395
2022
  let allowTrade = ALLOW_TRADE_MUST_TRADE
1396
2023
  if (isLimitOrder) {
@@ -1429,7 +2056,7 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1429
2056
  )
1430
2057
 
1431
2058
  tx.moveCall({
1432
- target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v1`,
2059
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v2`,
1433
2060
  typeArguments: [
1434
2061
  `${this.consts.sudoCore.package}::slp::SLP`,
1435
2062
  this.consts.coins[collateralToken].module,
@@ -1440,10 +2067,6 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1440
2067
  arguments: [
1441
2068
  tx.object(SUI_CLOCK_OBJECT_ID),
1442
2069
  tx.object(this.consts.sudoCore.market),
1443
- tx.object(
1444
- this.consts.sudoCore.vaults[collateralToken].reservingFeeModel,
1445
- ),
1446
- tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1447
2070
  tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
1448
2071
  tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1449
2072
  tx.object(this.consts.pythFeeder.feeder[indexToken]),
@@ -1454,6 +2077,7 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1454
2077
  tx.pure.u64(reserveAmount),
1455
2078
  tx.pure.u256(adjustCollateralPrice),
1456
2079
  tx.pure.u256(adjustPrice),
2080
+ tx.pure.u256(indexPriceThreshold),
1457
2081
  sudoCard,
1458
2082
  ],
1459
2083
  })
@@ -1465,10 +2089,12 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1465
2089
  promise,
1466
2090
  })
1467
2091
  .finalize()
2092
+
1468
2093
  return tx
1469
2094
  }
1470
2095
 
1471
- public async openPositionWithCoinAndSCard(
2096
+ // S Card operations
2097
+ public async openPositionWithCoinAndSCardV2(
1472
2098
  collateralToken: string,
1473
2099
  indexToken: string,
1474
2100
  size: bigint,
@@ -1480,8 +2106,8 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1480
2106
  kioskClient: KioskClient,
1481
2107
  kioskCap: KioskOwnerCap,
1482
2108
  scard: string,
1483
- isLimitOrder = false,
1484
- isIocOrder = false,
2109
+ isLimitOrder?: boolean,
2110
+ isIocOrder?: boolean,
1485
2111
  pricesSlippage = 0.003,
1486
2112
  collateralSlippage = 0.5,
1487
2113
  relayerFee = BigInt(0.5),
@@ -1499,8 +2125,9 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1499
2125
  }
1500
2126
 
1501
2127
  const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
1502
- const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage || 0.003)
1503
- const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage || 0.5)
2128
+ const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage)
2129
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
2130
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage)
1504
2131
 
1505
2132
  let allowTrade = ALLOW_TRADE_MUST_TRADE
1506
2133
  if (isLimitOrder) {
@@ -1532,7 +2159,7 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1532
2159
  const [feeObject] = tx.splitCoins(coinObj, [tx.pure.u64(relayerFee)])
1533
2160
 
1534
2161
  tx.moveCall({
1535
- target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v1`,
2162
+ target: `${this.consts.sudoCore.upgradedPackage}::market::open_position_with_scard_v2`,
1536
2163
  typeArguments: [
1537
2164
  `${this.consts.sudoCore.package}::slp::SLP`,
1538
2165
  this.consts.coins[collateralToken].module,
@@ -1543,10 +2170,6 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1543
2170
  arguments: [
1544
2171
  tx.object(SUI_CLOCK_OBJECT_ID),
1545
2172
  tx.object(this.consts.sudoCore.market),
1546
- tx.object(
1547
- this.consts.sudoCore.vaults[collateralToken].reservingFeeModel,
1548
- ),
1549
- tx.object(this.consts.sudoCore.symbols[symbol].fundingFeeModel),
1550
2173
  tx.object(this.consts.sudoCore.symbols[symbol].positionConfig),
1551
2174
  tx.object(this.consts.pythFeeder.feeder[collateralToken]),
1552
2175
  tx.object(this.consts.pythFeeder.feeder[indexToken]),
@@ -1557,6 +2180,7 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1557
2180
  tx.pure.u64(reserveAmount),
1558
2181
  tx.pure.u256(adjustCollateralPrice),
1559
2182
  tx.pure.u256(adjustPrice),
2183
+ tx.pure.u256(indexPriceThreshold),
1560
2184
  sudoCard,
1561
2185
  ],
1562
2186
  })
@@ -1568,6 +2192,7 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1568
2192
  promise,
1569
2193
  })
1570
2194
  .finalize()
2195
+
1571
2196
  return tx
1572
2197
  }
1573
2198
 
@@ -1688,6 +2313,112 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1688
2313
  return tx
1689
2314
  }
1690
2315
 
2316
+ public async decreasePositionWithSCardV2(
2317
+ pcpId: string,
2318
+ collateralToken: string,
2319
+ indexToken: string,
2320
+ amount: bigint,
2321
+ long: boolean,
2322
+ indexPrice: number,
2323
+ collateralPrice: number,
2324
+ kioskClient: KioskClient,
2325
+ kioskCap: KioskOwnerCap,
2326
+ scard: string,
2327
+ isTriggerOrder = false,
2328
+ isTakeProfitOrder = true,
2329
+ isIocOrder = false,
2330
+ pricesSlippage = 0.003,
2331
+ collateralSlippage = 0.5,
2332
+ relayerFee = BigInt(0.5),
2333
+ coinObjects?: string[],
2334
+ sponsoredTx?: boolean,
2335
+ suiCoinObjectsForPythUpdate?: string[],
2336
+ ): Promise<Transaction> {
2337
+ if (!coinObjects) {
2338
+ throw new Error(`${this.constructor.name}: coinObjects is required`)
2339
+ }
2340
+
2341
+ let tx = new Transaction()
2342
+
2343
+ const kioskTx = new KioskTransaction({
2344
+ transaction: tx,
2345
+ kioskClient,
2346
+ cap: kioskCap,
2347
+ })
2348
+
2349
+ const [sudoCard, promise] = kioskTx.borrow({
2350
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
2351
+ itemId: scard,
2352
+ })
2353
+
2354
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
2355
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
2356
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage)
2357
+
2358
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
2359
+ if (isTriggerOrder) {
2360
+ allowTrade = isIocOrder || !isTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
2361
+ }
2362
+ else {
2363
+ isTakeProfitOrder = true
2364
+ }
2365
+
2366
+ // Handle oracle initialization and coin processing
2367
+ let suiCoinObject
2368
+ if (sponsoredTx) {
2369
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
2370
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx, true, suiCoinObject)
2371
+ }
2372
+ else {
2373
+ tx = await this.initOracleTxb([collateralToken, indexToken], tx)
2374
+ }
2375
+
2376
+ // Process coin splitting
2377
+ const [feeObject] = this.processCoinSplitting(
2378
+ tx,
2379
+ collateralToken,
2380
+ coinObjects,
2381
+ [tx.pure.u64(relayerFee)],
2382
+ sponsoredTx,
2383
+ suiCoinObject,
2384
+ )
2385
+
2386
+ tx.moveCall({
2387
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_with_scard_v2`,
2388
+ typeArguments: [
2389
+ `${this.consts.sudoCore.package}::slp::SLP`,
2390
+ this.consts.coins[collateralToken].module,
2391
+ this.consts.coins[indexToken].module,
2392
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
2393
+ this.consts.coins[collateralToken].module,
2394
+ ],
2395
+ arguments: [
2396
+ tx.object(SUI_CLOCK_OBJECT_ID),
2397
+ tx.object(this.consts.sudoCore.market),
2398
+ tx.object(pcpId),
2399
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
2400
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
2401
+ feeObject,
2402
+ tx.pure.u8(allowTrade),
2403
+ tx.pure.bool(isTakeProfitOrder),
2404
+ tx.pure.u64(amount),
2405
+ tx.pure.u256(adjustCollateralPrice),
2406
+ tx.pure.u256(adjustPrice),
2407
+ tx.pure.u256(indexPriceThreshold),
2408
+ sudoCard,
2409
+ ],
2410
+ })
2411
+
2412
+ kioskTx
2413
+ .return({
2414
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
2415
+ item: sudoCard,
2416
+ promise,
2417
+ })
2418
+ .finalize()
2419
+ return tx
2420
+ }
2421
+
1691
2422
  public async decreaseMultiPositionsWithSCard(
1692
2423
  positions: Array<{
1693
2424
  pcpId: string
@@ -1818,6 +2549,133 @@ export class SLPAPI extends BaseAPI implements ISLPAPI {
1818
2549
  return tx
1819
2550
  }
1820
2551
 
2552
+ public async decreaseMultiPositionsWithSCardV2(
2553
+ positions: Array<{
2554
+ pcpId: string
2555
+ collateralToken: string
2556
+ indexToken: string
2557
+ amount: bigint
2558
+ long: boolean
2559
+ indexPrice: number
2560
+ collateralPrice: number
2561
+ isTriggerOrder?: boolean
2562
+ isTakeProfitOrder?: boolean
2563
+ isIocOrder?: boolean
2564
+ pricesSlippage?: number
2565
+ collateralSlippage?: number
2566
+ relayerFee?: bigint
2567
+ coinObjects?: string[]
2568
+ }>,
2569
+ kioskClient: KioskClient,
2570
+ kioskCap: KioskOwnerCap,
2571
+ scard: string,
2572
+ tx?: Transaction,
2573
+ sponsoredTx?: boolean,
2574
+ suiCoinObjectsForPythUpdate?: string[],
2575
+ ): Promise<Transaction> {
2576
+ if (!tx) {
2577
+ tx = new Transaction()
2578
+ }
2579
+
2580
+ // Handle oracle initialization and coin processing
2581
+ let suiCoinObject
2582
+ if (sponsoredTx) {
2583
+ suiCoinObject = this.processCoins(tx, 'sui', suiCoinObjectsForPythUpdate || [], true)
2584
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx, true, suiCoinObject)
2585
+ }
2586
+ else {
2587
+ tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
2588
+ }
2589
+
2590
+ const kioskTx = new KioskTransaction({
2591
+ transaction: tx,
2592
+ kioskClient,
2593
+ cap: kioskCap,
2594
+ })
2595
+
2596
+ const [sudoCard, promise] = kioskTx.borrow({
2597
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
2598
+ itemId: scard,
2599
+ })
2600
+
2601
+ for (const position of positions) {
2602
+ const {
2603
+ pcpId,
2604
+ collateralToken,
2605
+ indexToken,
2606
+ amount,
2607
+ long,
2608
+ indexPrice,
2609
+ collateralPrice,
2610
+ isTriggerOrder = false,
2611
+ isTakeProfitOrder = true,
2612
+ isIocOrder = false,
2613
+ pricesSlippage = 0.003,
2614
+ collateralSlippage = 0.5,
2615
+ relayerFee = BigInt(0.5),
2616
+ coinObjects = [],
2617
+ } = position
2618
+ let innerIsTakeProfitOrder = isTakeProfitOrder
2619
+
2620
+ const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage)
2621
+ const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage)
2622
+ const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage)
2623
+
2624
+ let allowTrade = ALLOW_TRADE_MUST_TRADE
2625
+ if (isTriggerOrder) {
2626
+ allowTrade = isIocOrder || !innerIsTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE
2627
+ }
2628
+ else {
2629
+ innerIsTakeProfitOrder = true
2630
+ }
2631
+
2632
+ // Process coin splitting
2633
+ const [feeObject] = this.processCoinSplitting(
2634
+ tx,
2635
+ collateralToken,
2636
+ coinObjects,
2637
+ [tx.pure.u64(relayerFee)],
2638
+ sponsoredTx,
2639
+ suiCoinObject,
2640
+ )
2641
+
2642
+ tx.moveCall({
2643
+ target: `${this.consts.sudoCore.upgradedPackage}::market::decrease_position_with_scard_v2`,
2644
+ typeArguments: [
2645
+ `${this.consts.sudoCore.package}::slp::SLP`,
2646
+ this.consts.coins[collateralToken].module,
2647
+ this.consts.coins[indexToken].module,
2648
+ `${this.consts.sudoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
2649
+ this.consts.coins[collateralToken].module,
2650
+ ],
2651
+ arguments: [
2652
+ tx.object(SUI_CLOCK_OBJECT_ID),
2653
+ tx.object(this.consts.sudoCore.market),
2654
+ tx.object(pcpId),
2655
+ tx.object(this.consts.pythFeeder.feeder[collateralToken]),
2656
+ tx.object(this.consts.pythFeeder.feeder[indexToken]),
2657
+ feeObject,
2658
+ tx.pure.u8(allowTrade),
2659
+ tx.pure.bool(innerIsTakeProfitOrder),
2660
+ tx.pure.u64(amount),
2661
+ tx.pure.u256(adjustCollateralPrice),
2662
+ tx.pure.u256(adjustPrice),
2663
+ tx.pure.u256(indexPriceThreshold),
2664
+ sudoCard,
2665
+ ],
2666
+ })
2667
+ }
2668
+
2669
+ kioskTx
2670
+ .return({
2671
+ itemType: `${this.consts.sudoNft.package}::card::SudoCard`,
2672
+ item: sudoCard,
2673
+ promise,
2674
+ })
2675
+ .finalize()
2676
+ return tx
2677
+ }
2678
+
1821
2679
  public addReferral(
1822
2680
  referrer: string,
1823
2681
  tx?: Transaction,