@zofai/zo-sdk 0.2.14 → 0.2.15
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.cjs +1 -1
- package/dist/abstract/BaseDataAPI.cjs.map +1 -1
- package/dist/abstract/BaseDataAPI.mjs +1 -1
- package/dist/abstract/BaseDataAPI.mjs.map +1 -1
- package/dist/consts/deployments-usdz-mainnet.json +1 -1
- package/dist/consts/deployments-zo-oracle-mainnet.json +329 -0
- package/dist/consts/index.cjs +35 -1
- package/dist/consts/index.cjs.map +1 -1
- package/dist/consts/index.d.cts +45 -0
- package/dist/consts/index.d.cts.map +1 -1
- package/dist/consts/index.d.mts +45 -0
- package/dist/consts/index.d.mts.map +1 -1
- package/dist/consts/index.mjs +31 -0
- package/dist/consts/index.mjs.map +1 -1
- package/dist/data.cjs +1 -1
- package/dist/data.cjs.map +1 -1
- package/dist/data.mjs +1 -1
- package/dist/data.mjs.map +1 -1
- package/dist/implementations/USDZAPI.cjs +426 -78
- package/dist/implementations/USDZAPI.cjs.map +1 -1
- package/dist/implementations/USDZAPI.d.cts +54 -0
- package/dist/implementations/USDZAPI.d.cts.map +1 -1
- package/dist/implementations/USDZAPI.d.mts +54 -0
- package/dist/implementations/USDZAPI.d.mts.map +1 -1
- package/dist/implementations/USDZAPI.mjs +427 -79
- package/dist/implementations/USDZAPI.mjs.map +1 -1
- package/dist/implementations/USDZDataAPI.cjs +38 -0
- package/dist/implementations/USDZDataAPI.cjs.map +1 -1
- package/dist/implementations/USDZDataAPI.d.cts +16 -0
- package/dist/implementations/USDZDataAPI.d.cts.map +1 -1
- package/dist/implementations/USDZDataAPI.d.mts +16 -0
- package/dist/implementations/USDZDataAPI.d.mts.map +1 -1
- package/dist/implementations/USDZDataAPI.mjs +38 -0
- package/dist/implementations/USDZDataAPI.mjs.map +1 -1
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -0
- package/dist/index.mjs.map +1 -1
- package/dist/interfaces/base.d.cts +19 -1
- package/dist/interfaces/base.d.cts.map +1 -1
- package/dist/interfaces/base.d.mts +19 -1
- package/dist/interfaces/base.d.mts.map +1 -1
- package/dist/interfaces/usdz.d.cts +17 -0
- package/dist/interfaces/usdz.d.cts.map +1 -1
- package/dist/interfaces/usdz.d.mts +17 -0
- package/dist/interfaces/usdz.d.mts.map +1 -1
- package/dist/oracle.cjs +119 -1
- package/dist/oracle.cjs.map +1 -1
- package/dist/oracle.d.cts +57 -1
- package/dist/oracle.d.cts.map +1 -1
- package/dist/oracle.d.mts +57 -1
- package/dist/oracle.d.mts.map +1 -1
- package/dist/oracle.mjs +120 -2
- package/dist/oracle.mjs.map +1 -1
- package/dist/oraclePro.cjs +214 -0
- package/dist/oraclePro.cjs.map +1 -0
- package/dist/oraclePro.d.cts +92 -0
- package/dist/oraclePro.d.cts.map +1 -0
- package/dist/oraclePro.d.mts +92 -0
- package/dist/oraclePro.d.mts.map +1 -0
- package/dist/oraclePro.mjs +196 -0
- package/dist/oraclePro.mjs.map +1 -0
- package/dist/pythProClient.cjs +277 -0
- package/dist/pythProClient.cjs.map +1 -0
- package/dist/pythProClient.d.cts +143 -0
- package/dist/pythProClient.d.cts.map +1 -0
- package/dist/pythProClient.d.mts +143 -0
- package/dist/pythProClient.d.mts.map +1 -0
- package/dist/pythProClient.mjs +269 -0
- package/dist/pythProClient.mjs.map +1 -0
- package/dist/storkClient.cjs +217 -0
- package/dist/storkClient.cjs.map +1 -0
- package/dist/storkClient.d.cts +90 -0
- package/dist/storkClient.d.cts.map +1 -0
- package/dist/storkClient.d.mts +90 -0
- package/dist/storkClient.d.mts.map +1 -0
- package/dist/storkClient.mjs +208 -0
- package/dist/storkClient.mjs.map +1 -0
- package/dist/storkOracle.cjs +61 -0
- package/dist/storkOracle.cjs.map +1 -0
- package/dist/storkOracle.d.cts +21 -0
- package/dist/storkOracle.d.cts.map +1 -0
- package/dist/storkOracle.d.mts +21 -0
- package/dist/storkOracle.d.mts.map +1 -0
- package/dist/storkOracle.mjs +56 -0
- package/dist/storkOracle.mjs.map +1 -0
- package/package.json +1 -1
- package/src/abstract/BaseDataAPI.ts +1 -1
- package/src/consts/deployments-usdz-mainnet.json +1 -1
- package/src/consts/deployments-zo-oracle-mainnet.json +329 -0
- package/src/consts/index.ts +80 -0
- package/src/data.ts +1 -1
- package/src/implementations/USDZAPI.ts +731 -101
- package/src/implementations/USDZDataAPI.ts +42 -0
- package/src/index.ts +3 -0
- package/src/interfaces/base.ts +54 -1
- package/src/interfaces/usdz.ts +125 -0
- package/src/oracle.ts +222 -1
- package/src/oraclePro.ts +312 -0
- package/src/pythProClient.ts +406 -0
- package/src/storkClient.ts +328 -0
- package/src/storkOracle.ts +82 -0
- package/tests/pythProClient.test.ts +155 -0
- package/tests/storkClient.test.ts +316 -0
|
@@ -6,14 +6,69 @@ import { KioskTransaction } from "@mysten/kiosk";
|
|
|
6
6
|
import { Transaction } from "@mysten/sui/transactions";
|
|
7
7
|
import { SUI_CLOCK_OBJECT_ID } from "@mysten/sui/utils";
|
|
8
8
|
import { BaseAPI } from "../abstract/index.mjs";
|
|
9
|
+
import { resolveSpendableSuiCoin } from "../coins.mjs";
|
|
9
10
|
import { ALLOW_TRADE_CAN_TRADE, ALLOW_TRADE_MUST_TRADE, ALLOW_TRADE_NO_TRADE, LPToken } from "../consts/index.mjs";
|
|
10
|
-
import {
|
|
11
|
+
import { buildOracleInputsAlt, oracleArgsAlt, oracleArgsStandard, oracleArgsWithClock } from "../oraclePro.mjs";
|
|
12
|
+
import { joinSymbol, parseSymbolKey } from "../utils.mjs";
|
|
11
13
|
import { USDZDataAPI } from "./USDZDataAPI.mjs";
|
|
12
14
|
export class USDZAPI extends BaseAPI {
|
|
13
15
|
constructor(network, provider, apiEndpoint, connectionURL) {
|
|
14
16
|
super(network, provider, apiEndpoint, connectionURL, LPToken.USDZ);
|
|
15
17
|
this.dataAPI = new USDZDataAPI(network, provider, apiEndpoint, connectionURL);
|
|
16
18
|
}
|
|
19
|
+
usdzLpType() {
|
|
20
|
+
return `${this.consts.zoCore.package}::usdz::USDZ`;
|
|
21
|
+
}
|
|
22
|
+
positionTypeArgs(collateralToken, indexToken, long) {
|
|
23
|
+
return [
|
|
24
|
+
this.usdzLpType(),
|
|
25
|
+
this.consts.coins[collateralToken].module,
|
|
26
|
+
this.consts.coins[indexToken].module,
|
|
27
|
+
`${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
|
|
28
|
+
this.consts.coins[collateralToken].module,
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
redeemTypeArgs(collateralToken, indexToken, long) {
|
|
32
|
+
return [
|
|
33
|
+
this.usdzLpType(),
|
|
34
|
+
this.consts.coins[collateralToken].module,
|
|
35
|
+
this.consts.coins[indexToken].module,
|
|
36
|
+
`${this.consts.zoCore.package}::market::${long ? 'LONG' : 'SHORT'}`,
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
async initV3OracleForTokens(tokens, tx, pythProUpdateBytes, includeEmaPrice, options) {
|
|
40
|
+
const result = await this.initV3OracleTxb(tokens, tx, {
|
|
41
|
+
rawUpdateBytes: pythProUpdateBytes,
|
|
42
|
+
includeEmaPrice,
|
|
43
|
+
...options,
|
|
44
|
+
});
|
|
45
|
+
return { tx: result.tx, oracle: result.oracle };
|
|
46
|
+
}
|
|
47
|
+
/** Vault + symbol index tokens required for valuate_vault_v2 / valuate_symbol_v2. */
|
|
48
|
+
getValuationOracleTokens() {
|
|
49
|
+
const tokens = new Set(Object.keys(this.consts.zoCore.vaults));
|
|
50
|
+
for (const key of Object.keys(this.consts.zoCore.symbols)) {
|
|
51
|
+
const [, token] = parseSymbolKey(key);
|
|
52
|
+
tokens.add(token);
|
|
53
|
+
}
|
|
54
|
+
return [...tokens];
|
|
55
|
+
}
|
|
56
|
+
/** Pyth Pro oracle init for deposit / withdraw market valuation. */
|
|
57
|
+
async initValuationOracle(tx) {
|
|
58
|
+
const tokens = this.getValuationOracleTokens();
|
|
59
|
+
const pythProUpdateBytes = await this.fetchPythProUpdateBytesForTokens(tokens);
|
|
60
|
+
return this.initV3OracleForTokens(tokens, tx, pythProUpdateBytes);
|
|
61
|
+
}
|
|
62
|
+
async resolveSponsoredSui(tx, collateralToken, relayerFee, sponsoredTx, sender) {
|
|
63
|
+
if (!sponsoredTx) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
const additional = collateralToken === 'sui' ? relayerFee : 0n;
|
|
67
|
+
if (additional === 0n) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
return resolveSpendableSuiCoin(tx, this.provider, this.requireSenderForSponsored(sender), additional);
|
|
71
|
+
}
|
|
17
72
|
calcPositionReserveFeeAmount(_position) {
|
|
18
73
|
throw new Error(`Method not implemented in ${this.constructor.name}.`);
|
|
19
74
|
}
|
|
@@ -686,35 +741,10 @@ export class USDZAPI extends BaseAPI {
|
|
|
686
741
|
if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
|
|
687
742
|
tx = await this.addReferral(referralAddress, tx);
|
|
688
743
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
const oracle = await this.initOracleTxb(pythFeederKeys, tx, true, {
|
|
694
|
-
sender: this.requireSenderForSponsored(sender),
|
|
695
|
-
additionalSuiAmount: coin === 'sui' ? BigInt(amount) : 0n,
|
|
696
|
-
});
|
|
697
|
-
tx = oracle.tx;
|
|
698
|
-
const depositObject = await this.resolveSplitCoinObject(tx, coin, BigInt(amount), coinObjects, { sponsoredTx: true, suiCoinObject: oracle.suiCoinObject, sender });
|
|
699
|
-
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
|
|
700
|
-
tx.moveCall({
|
|
701
|
-
target: `${this.consts.zoCore.upgradedPackage}::market::deposit`,
|
|
702
|
-
typeArguments: [`${this.consts.zoCore.package}::usdz::USDZ`, this.consts.coins[coin].module],
|
|
703
|
-
arguments: [
|
|
704
|
-
tx.object(this.consts.zoCore.market),
|
|
705
|
-
tx.object(this.consts.zoCore.rebaseFeeModel),
|
|
706
|
-
depositObject,
|
|
707
|
-
tx.pure.u64(minAmountOut),
|
|
708
|
-
vaultsValuation,
|
|
709
|
-
symbolsValuation,
|
|
710
|
-
],
|
|
711
|
-
});
|
|
712
|
-
return tx;
|
|
713
|
-
}
|
|
714
|
-
// Handle non-sponsored transaction case
|
|
715
|
-
tx = (await this.initOracleTxb(pythFeederKeys, tx)).tx;
|
|
716
|
-
const depositObject = await this.resolveSplitCoinObject(tx, coin, BigInt(amount), coinObjects, { sender });
|
|
717
|
-
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
|
|
744
|
+
const { tx: txWithOracle, oracle: oracleBundle } = await this.initValuationOracle(tx);
|
|
745
|
+
tx = txWithOracle;
|
|
746
|
+
const depositObject = await this.resolveSplitCoinObject(tx, coin, BigInt(amount), coinObjects, { sponsoredTx, sender });
|
|
747
|
+
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuateV2(tx, oracleBundle);
|
|
718
748
|
tx.moveCall({
|
|
719
749
|
target: `${this.consts.zoCore.upgradedPackage}::market::deposit`,
|
|
720
750
|
typeArguments: [
|
|
@@ -743,35 +773,10 @@ export class USDZAPI extends BaseAPI {
|
|
|
743
773
|
if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
|
|
744
774
|
tx = await this.addReferral(referralAddress, tx);
|
|
745
775
|
}
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
const oracle = await this.initOracleTxb(pythFeederKeys, tx, true, {
|
|
751
|
-
sender: this.requireSenderForSponsored(sender),
|
|
752
|
-
additionalSuiAmount: coin === 'sui' ? BigInt(amount) : 0n,
|
|
753
|
-
});
|
|
754
|
-
tx = oracle.tx;
|
|
755
|
-
const depositObject = await this.resolveSplitCoinObject(tx, coin, BigInt(amount), coinObjects, { sponsoredTx: true, suiCoinObject: oracle.suiCoinObject, sender });
|
|
756
|
-
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
|
|
757
|
-
const [mintedCoin] = tx.moveCall({
|
|
758
|
-
target: `${this.consts.zoCore.upgradedPackage}::market::deposit_ptb`,
|
|
759
|
-
typeArguments: [`${this.consts.zoCore.package}::usdz::USDZ`, this.consts.coins[coin].module],
|
|
760
|
-
arguments: [
|
|
761
|
-
tx.object(this.consts.zoCore.market),
|
|
762
|
-
tx.object(this.consts.zoCore.rebaseFeeModel),
|
|
763
|
-
depositObject,
|
|
764
|
-
tx.pure.u64(minAmountOut),
|
|
765
|
-
vaultsValuation,
|
|
766
|
-
symbolsValuation,
|
|
767
|
-
],
|
|
768
|
-
});
|
|
769
|
-
return mintedCoin;
|
|
770
|
-
}
|
|
771
|
-
// Handle non-sponsored transaction case
|
|
772
|
-
tx = (await this.initOracleTxb(pythFeederKeys, tx)).tx;
|
|
773
|
-
const depositObject = await this.resolveSplitCoinObject(tx, coin, BigInt(amount), coinObjects, { sender });
|
|
774
|
-
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
|
|
776
|
+
const { tx: txWithOracle, oracle: oracleBundle } = await this.initValuationOracle(tx);
|
|
777
|
+
tx = txWithOracle;
|
|
778
|
+
const depositObject = await this.resolveSplitCoinObject(tx, coin, BigInt(amount), coinObjects, { sponsoredTx, sender });
|
|
779
|
+
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuateV2(tx, oracleBundle);
|
|
775
780
|
const [mintedCoin] = tx.moveCall({
|
|
776
781
|
target: `${this.consts.zoCore.upgradedPackage}::market::deposit_ptb`,
|
|
777
782
|
typeArguments: [
|
|
@@ -797,22 +802,10 @@ export class USDZAPI extends BaseAPI {
|
|
|
797
802
|
throw new Error(`${this.constructor.name}: lpCoinObjects or sender is required`);
|
|
798
803
|
}
|
|
799
804
|
let tx = new Transaction();
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
const oracle = await this.initOracleTxb(pythFeederKeys, tx, true, {
|
|
805
|
-
sender: this.requireSenderForSponsored(sender),
|
|
806
|
-
additionalSuiAmount: 0n,
|
|
807
|
-
});
|
|
808
|
-
tx = oracle.tx;
|
|
809
|
-
suiCoinObject = oracle.suiCoinObject;
|
|
810
|
-
}
|
|
811
|
-
else {
|
|
812
|
-
tx = (await this.initOracleTxb(pythFeederKeys, tx)).tx;
|
|
813
|
-
}
|
|
814
|
-
const withdrawObject = await this.resolveSplitCoinObject(tx, 'usdz', BigInt(amount), lpCoinObjects, { sponsoredTx, suiCoinObject, sender });
|
|
815
|
-
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
|
|
805
|
+
const { tx: txWithOracle, oracle: oracleBundle } = await this.initValuationOracle(tx);
|
|
806
|
+
tx = txWithOracle;
|
|
807
|
+
const withdrawObject = await this.resolveSplitCoinObject(tx, 'usdz', BigInt(amount), lpCoinObjects, { sponsoredTx, sender });
|
|
808
|
+
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuateV2(tx, oracleBundle);
|
|
816
809
|
tx.moveCall({
|
|
817
810
|
target: `${this.consts.zoCore.upgradedPackage}::market::withdraw`,
|
|
818
811
|
typeArguments: [
|
|
@@ -837,10 +830,9 @@ export class USDZAPI extends BaseAPI {
|
|
|
837
830
|
if (!tx) {
|
|
838
831
|
tx = new Transaction();
|
|
839
832
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuate(tx);
|
|
833
|
+
const { tx: txWithOracle, oracle: oracleBundle } = await this.initValuationOracle(tx);
|
|
834
|
+
tx = txWithOracle;
|
|
835
|
+
const { vaultsValuation, symbolsValuation } = this.dataAPI.valuateV2(tx, oracleBundle);
|
|
844
836
|
tx.moveCall({
|
|
845
837
|
target: `${this.consts.zoCore.upgradedPackage}::market::withdraw`,
|
|
846
838
|
typeArguments: [
|
|
@@ -1779,5 +1771,361 @@ export class USDZAPI extends BaseAPI {
|
|
|
1779
1771
|
],
|
|
1780
1772
|
});
|
|
1781
1773
|
}
|
|
1774
|
+
// --- Pyth Pro + Stork (v3) ---
|
|
1775
|
+
/**
|
|
1776
|
+
* Swaps tokens using swap_v3 (Pyth Pro + Stork). Transfers output to sender.
|
|
1777
|
+
*/
|
|
1778
|
+
async swapV3(fromToken, toToken, fromAmount, fromCoinObjects, pythProUpdateBytes, minAmountOut, tx) {
|
|
1779
|
+
if (!tx) {
|
|
1780
|
+
tx = new Transaction();
|
|
1781
|
+
}
|
|
1782
|
+
const { tx: tx_, oracle } = await this.initV3OracleForTokens(Object.keys(this.consts.zoCore.vaults), tx, pythProUpdateBytes, true);
|
|
1783
|
+
const fromCoinObject = this.processCoins(tx_, fromToken, fromCoinObjects);
|
|
1784
|
+
const [fromDepositObject] = tx_.splitCoins(fromCoinObject, [tx_.pure.u64(fromAmount)]);
|
|
1785
|
+
const vaultsValuation = this.dataAPI.valuateVaultsV2(tx_, oracle);
|
|
1786
|
+
tx_.moveCall({
|
|
1787
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::swap_v3`,
|
|
1788
|
+
typeArguments: [
|
|
1789
|
+
this.usdzLpType(),
|
|
1790
|
+
this.consts.coins[fromToken].module,
|
|
1791
|
+
this.consts.coins[toToken].module,
|
|
1792
|
+
],
|
|
1793
|
+
arguments: [
|
|
1794
|
+
tx_.object(this.consts.zoCore.market),
|
|
1795
|
+
tx_.object(this.consts.zoCore.rebaseFeeModel),
|
|
1796
|
+
fromDepositObject,
|
|
1797
|
+
tx_.pure.u64(minAmountOut || 0),
|
|
1798
|
+
vaultsValuation,
|
|
1799
|
+
...oracleArgsWithClock(oracle),
|
|
1800
|
+
],
|
|
1801
|
+
});
|
|
1802
|
+
return tx_;
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* Swaps tokens and returns the output coin (swap_v3_ptb).
|
|
1806
|
+
*/
|
|
1807
|
+
async swapV3Ptb(fromToken, toToken, fromAmount, fromCoinObjects, pythProUpdateBytes, minAmountOut, tx) {
|
|
1808
|
+
if (!tx) {
|
|
1809
|
+
tx = new Transaction();
|
|
1810
|
+
}
|
|
1811
|
+
const { tx: tx_, oracle } = await this.initV3OracleForTokens(Object.keys(this.consts.zoCore.vaults), tx, pythProUpdateBytes, true);
|
|
1812
|
+
const fromCoinObject = this.processCoins(tx_, fromToken, fromCoinObjects);
|
|
1813
|
+
const [fromDepositObject] = tx_.splitCoins(fromCoinObject, [tx_.pure.u64(fromAmount)]);
|
|
1814
|
+
const vaultsValuation = this.dataAPI.valuateVaultsV2(tx_, oracle);
|
|
1815
|
+
const [outputCoin] = tx_.moveCall({
|
|
1816
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::swap_v3_ptb`,
|
|
1817
|
+
typeArguments: [
|
|
1818
|
+
this.usdzLpType(),
|
|
1819
|
+
this.consts.coins[fromToken].module,
|
|
1820
|
+
this.consts.coins[toToken].module,
|
|
1821
|
+
],
|
|
1822
|
+
arguments: [
|
|
1823
|
+
tx_.object(this.consts.zoCore.market),
|
|
1824
|
+
tx_.object(this.consts.zoCore.rebaseFeeModel),
|
|
1825
|
+
fromDepositObject,
|
|
1826
|
+
tx_.pure.u64(minAmountOut || 0),
|
|
1827
|
+
vaultsValuation,
|
|
1828
|
+
...oracleArgsWithClock(oracle),
|
|
1829
|
+
],
|
|
1830
|
+
});
|
|
1831
|
+
return outputCoin;
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Opens a position using open_position_v3 (Pyth Pro + Stork).
|
|
1835
|
+
*/
|
|
1836
|
+
async openPositionV3(collateralToken, indexToken, size, collateralAmount, coinObjects, long, reserveAmount, indexPrice, collateralPrice, pythProUpdateBytes, isLimitOrder, isIocOrder, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, sponsoredTx) {
|
|
1837
|
+
let tx = new Transaction();
|
|
1838
|
+
if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
|
|
1839
|
+
tx = await this.addReferral(referralAddress, tx);
|
|
1840
|
+
}
|
|
1841
|
+
const symbol = joinSymbol(long ? 'long' : 'short', indexToken);
|
|
1842
|
+
const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage);
|
|
1843
|
+
const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
|
|
1844
|
+
const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
|
|
1845
|
+
let allowTrade = ALLOW_TRADE_MUST_TRADE;
|
|
1846
|
+
if (isLimitOrder) {
|
|
1847
|
+
allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE;
|
|
1848
|
+
}
|
|
1849
|
+
const { tx: tx_, oracle } = await this.initV3OracleForTokens([collateralToken, indexToken], tx, pythProUpdateBytes);
|
|
1850
|
+
tx = tx_;
|
|
1851
|
+
const suiCoinObject = await this.resolveSponsoredSui(tx, collateralToken, relayerFee, sponsoredTx, sender);
|
|
1852
|
+
const [depositObject, feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
|
|
1853
|
+
tx.moveCall({
|
|
1854
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::open_position_v3`,
|
|
1855
|
+
typeArguments: this.positionTypeArgs(collateralToken, indexToken, long),
|
|
1856
|
+
arguments: [
|
|
1857
|
+
tx.object(SUI_CLOCK_OBJECT_ID),
|
|
1858
|
+
tx.object(this.consts.zoCore.market),
|
|
1859
|
+
tx.object(this.consts.zoCore.symbols[symbol].positionConfig),
|
|
1860
|
+
...oracleArgsStandard(oracle),
|
|
1861
|
+
depositObject,
|
|
1862
|
+
feeObject,
|
|
1863
|
+
tx.pure.u8(allowTrade),
|
|
1864
|
+
tx.pure.u64(size),
|
|
1865
|
+
tx.pure.u64(reserveAmount),
|
|
1866
|
+
tx.pure.u256(adjustCollateralPrice),
|
|
1867
|
+
tx.pure.u256(adjustPrice),
|
|
1868
|
+
tx.pure.u256(indexPriceThreshold),
|
|
1869
|
+
],
|
|
1870
|
+
});
|
|
1871
|
+
return tx;
|
|
1872
|
+
}
|
|
1873
|
+
/**
|
|
1874
|
+
* Opens a position with S Card using open_position_with_scard_v3.
|
|
1875
|
+
*/
|
|
1876
|
+
async openPositionWithSCardV3(collateralToken, indexToken, size, collateralAmount, coinObjects, long, reserveAmount, indexPrice, collateralPrice, kioskClient, kioskCap, scard, pythProUpdateBytes, isLimitOrder, isIocOrder, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), referralAddress, sender, sponsoredTx) {
|
|
1877
|
+
let tx = new Transaction();
|
|
1878
|
+
if (referralAddress && !(await this.dataAPI.hasReferral(sender || ''))) {
|
|
1879
|
+
tx = await this.addReferral(referralAddress, tx);
|
|
1880
|
+
}
|
|
1881
|
+
const symbol = joinSymbol(long ? 'long' : 'short', indexToken);
|
|
1882
|
+
const adjustPrice = this.processSlippage(indexPrice, long, isLimitOrder ? 0 : pricesSlippage);
|
|
1883
|
+
const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
|
|
1884
|
+
const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
|
|
1885
|
+
let allowTrade = ALLOW_TRADE_MUST_TRADE;
|
|
1886
|
+
if (isLimitOrder) {
|
|
1887
|
+
allowTrade = isIocOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE;
|
|
1888
|
+
}
|
|
1889
|
+
const kioskTx = new KioskTransaction({
|
|
1890
|
+
transaction: tx,
|
|
1891
|
+
kioskClient,
|
|
1892
|
+
cap: kioskCap,
|
|
1893
|
+
});
|
|
1894
|
+
const [sudoCard, promise] = kioskTx.borrow({
|
|
1895
|
+
itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
|
|
1896
|
+
itemId: scard,
|
|
1897
|
+
});
|
|
1898
|
+
const { tx: tx_, oracle } = await this.initV3OracleForTokens([collateralToken, indexToken], tx, pythProUpdateBytes);
|
|
1899
|
+
tx = tx_;
|
|
1900
|
+
const suiCoinObject = await this.resolveSponsoredSui(tx, collateralToken, relayerFee, sponsoredTx, sender);
|
|
1901
|
+
const [depositObject, feeObject] = this.processCoinSplitting(tx, collateralToken, coinObjects, [tx.pure.u64(collateralAmount), tx.pure.u64(relayerFee)], sponsoredTx, suiCoinObject);
|
|
1902
|
+
tx.moveCall({
|
|
1903
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::open_position_with_scard_v3`,
|
|
1904
|
+
typeArguments: this.positionTypeArgs(collateralToken, indexToken, long),
|
|
1905
|
+
arguments: [
|
|
1906
|
+
tx.object(SUI_CLOCK_OBJECT_ID),
|
|
1907
|
+
tx.object(this.consts.zoCore.market),
|
|
1908
|
+
tx.object(this.consts.zoCore.symbols[symbol].positionConfig),
|
|
1909
|
+
...oracleArgsStandard(oracle),
|
|
1910
|
+
depositObject,
|
|
1911
|
+
feeObject,
|
|
1912
|
+
tx.pure.u8(allowTrade),
|
|
1913
|
+
tx.pure.u64(size),
|
|
1914
|
+
tx.pure.u64(reserveAmount),
|
|
1915
|
+
tx.pure.u256(adjustCollateralPrice),
|
|
1916
|
+
tx.pure.u256(adjustPrice),
|
|
1917
|
+
tx.pure.u256(indexPriceThreshold),
|
|
1918
|
+
sudoCard,
|
|
1919
|
+
],
|
|
1920
|
+
});
|
|
1921
|
+
kioskTx
|
|
1922
|
+
.return({
|
|
1923
|
+
itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
|
|
1924
|
+
item: sudoCard,
|
|
1925
|
+
promise,
|
|
1926
|
+
})
|
|
1927
|
+
.finalize();
|
|
1928
|
+
return tx;
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* Decreases a position using decrease_position_v3.
|
|
1932
|
+
*/
|
|
1933
|
+
async decreasePositionV3(pcpId, collateralToken, indexToken, amount, long, indexPrice, collateralPrice, pythProUpdateBytes, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), coinObjects, sponsoredTx, sender) {
|
|
1934
|
+
if (!coinObjects?.length && !sender) {
|
|
1935
|
+
throw new Error(`${this.constructor.name}: coinObjects or sender is required`);
|
|
1936
|
+
}
|
|
1937
|
+
let tx = new Transaction();
|
|
1938
|
+
const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
|
|
1939
|
+
const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
|
|
1940
|
+
const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
|
|
1941
|
+
let allowTrade = ALLOW_TRADE_MUST_TRADE;
|
|
1942
|
+
if (isTriggerOrder) {
|
|
1943
|
+
allowTrade = isIocOrder || !isTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE;
|
|
1944
|
+
}
|
|
1945
|
+
else {
|
|
1946
|
+
isTakeProfitOrder = true;
|
|
1947
|
+
}
|
|
1948
|
+
const { tx: tx_, oracle } = await this.initV3OracleForTokens([collateralToken, indexToken], tx, pythProUpdateBytes);
|
|
1949
|
+
tx = tx_;
|
|
1950
|
+
const suiCoinObject = await this.resolveSponsoredSui(tx, collateralToken, relayerFee, sponsoredTx, sender);
|
|
1951
|
+
const feeObject = await this.resolveRelayerFeeObject(tx, collateralToken, coinObjects ?? [], relayerFee, sponsoredTx, suiCoinObject, sender);
|
|
1952
|
+
tx.moveCall({
|
|
1953
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_v3`,
|
|
1954
|
+
typeArguments: this.positionTypeArgs(collateralToken, indexToken, long),
|
|
1955
|
+
arguments: [
|
|
1956
|
+
tx.object(SUI_CLOCK_OBJECT_ID),
|
|
1957
|
+
tx.object(this.consts.zoCore.market),
|
|
1958
|
+
tx.object(pcpId),
|
|
1959
|
+
...oracleArgsStandard(oracle),
|
|
1960
|
+
feeObject,
|
|
1961
|
+
tx.pure.u8(allowTrade),
|
|
1962
|
+
tx.pure.bool(isTakeProfitOrder),
|
|
1963
|
+
tx.pure.u64(amount),
|
|
1964
|
+
tx.pure.u256(adjustCollateralPrice),
|
|
1965
|
+
tx.pure.u256(adjustPrice),
|
|
1966
|
+
tx.pure.u256(indexPriceThreshold),
|
|
1967
|
+
],
|
|
1968
|
+
});
|
|
1969
|
+
return tx;
|
|
1970
|
+
}
|
|
1971
|
+
/**
|
|
1972
|
+
* Decreases a position with S Card using decrease_position_with_scard_v3.
|
|
1973
|
+
*/
|
|
1974
|
+
async decreasePositionWithSCardV3(pcpId, collateralToken, indexToken, amount, long, indexPrice, collateralPrice, kioskClient, kioskCap, scard, pythProUpdateBytes, isTriggerOrder = false, isTakeProfitOrder = true, isIocOrder = false, pricesSlippage = 0.003, collateralSlippage = 0.5, relayerFee = BigInt(0.5), coinObjects, sponsoredTx, sender) {
|
|
1975
|
+
if (!coinObjects?.length && !sender) {
|
|
1976
|
+
throw new Error(`${this.constructor.name}: coinObjects or sender is required`);
|
|
1977
|
+
}
|
|
1978
|
+
let tx = new Transaction();
|
|
1979
|
+
const kioskTx = new KioskTransaction({
|
|
1980
|
+
transaction: tx,
|
|
1981
|
+
kioskClient,
|
|
1982
|
+
cap: kioskCap,
|
|
1983
|
+
});
|
|
1984
|
+
const [sudoCard, promise] = kioskTx.borrow({
|
|
1985
|
+
itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
|
|
1986
|
+
itemId: scard,
|
|
1987
|
+
});
|
|
1988
|
+
const adjustPrice = this.processSlippage(indexPrice, !long, isTriggerOrder ? 0 : pricesSlippage);
|
|
1989
|
+
const adjustCollateralPrice = this.processSlippage(collateralPrice, false, collateralSlippage);
|
|
1990
|
+
const indexPriceThreshold = this.processPriceThreshold(indexPrice, pricesSlippage);
|
|
1991
|
+
let allowTrade = ALLOW_TRADE_MUST_TRADE;
|
|
1992
|
+
if (isTriggerOrder) {
|
|
1993
|
+
allowTrade = isIocOrder || !isTakeProfitOrder ? ALLOW_TRADE_NO_TRADE : ALLOW_TRADE_CAN_TRADE;
|
|
1994
|
+
}
|
|
1995
|
+
else {
|
|
1996
|
+
isTakeProfitOrder = true;
|
|
1997
|
+
}
|
|
1998
|
+
const { tx: tx_, oracle } = await this.initV3OracleForTokens([collateralToken, indexToken], tx, pythProUpdateBytes);
|
|
1999
|
+
tx = tx_;
|
|
2000
|
+
const suiCoinObject = await this.resolveSponsoredSui(tx, collateralToken, relayerFee, sponsoredTx, sender);
|
|
2001
|
+
const feeObject = await this.resolveRelayerFeeObject(tx, collateralToken, coinObjects ?? [], relayerFee, sponsoredTx, suiCoinObject, sender);
|
|
2002
|
+
tx.moveCall({
|
|
2003
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_with_scard_v3`,
|
|
2004
|
+
typeArguments: this.positionTypeArgs(collateralToken, indexToken, long),
|
|
2005
|
+
arguments: [
|
|
2006
|
+
tx.object(SUI_CLOCK_OBJECT_ID),
|
|
2007
|
+
tx.object(this.consts.zoCore.market),
|
|
2008
|
+
tx.object(pcpId),
|
|
2009
|
+
...oracleArgsStandard(oracle),
|
|
2010
|
+
feeObject,
|
|
2011
|
+
tx.pure.u8(allowTrade),
|
|
2012
|
+
tx.pure.bool(isTakeProfitOrder),
|
|
2013
|
+
tx.pure.u64(amount),
|
|
2014
|
+
tx.pure.u256(adjustCollateralPrice),
|
|
2015
|
+
tx.pure.u256(adjustPrice),
|
|
2016
|
+
tx.pure.u256(indexPriceThreshold),
|
|
2017
|
+
sudoCard,
|
|
2018
|
+
],
|
|
2019
|
+
});
|
|
2020
|
+
kioskTx
|
|
2021
|
+
.return({
|
|
2022
|
+
itemType: `0xe7e651e4974fe367aa2837712d68081efb299c470242a15e2b9c26ea326159ec::card::SudoCard`,
|
|
2023
|
+
item: sudoCard,
|
|
2024
|
+
promise,
|
|
2025
|
+
})
|
|
2026
|
+
.finalize();
|
|
2027
|
+
return tx;
|
|
2028
|
+
}
|
|
2029
|
+
/**
|
|
2030
|
+
* Redeems collateral from a position via redeem_from_position_v2 (Pyth Pro path).
|
|
2031
|
+
*/
|
|
2032
|
+
async redeemFromPositionV2(pcpId, collateralToken, indexToken, amount, long, pythProUpdateBytes, tx) {
|
|
2033
|
+
if (!tx) {
|
|
2034
|
+
tx = new Transaction();
|
|
2035
|
+
}
|
|
2036
|
+
const { tx: tx_, oracle } = await this.initV3OracleForTokens([collateralToken, indexToken], tx, pythProUpdateBytes);
|
|
2037
|
+
tx_.moveCall({
|
|
2038
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::redeem_from_position_v2`,
|
|
2039
|
+
typeArguments: this.redeemTypeArgs(collateralToken, indexToken, long),
|
|
2040
|
+
arguments: [
|
|
2041
|
+
tx_.object(SUI_CLOCK_OBJECT_ID),
|
|
2042
|
+
tx_.object(this.consts.zoCore.market),
|
|
2043
|
+
tx_.object(pcpId),
|
|
2044
|
+
...oracleArgsStandard(oracle),
|
|
2045
|
+
tx_.pure.u64(amount),
|
|
2046
|
+
],
|
|
2047
|
+
});
|
|
2048
|
+
return tx_;
|
|
2049
|
+
}
|
|
2050
|
+
/**
|
|
2051
|
+
* Liquidates a position (PTB-only public fun). Use liquidate_position_v3_1, not v3.
|
|
2052
|
+
*/
|
|
2053
|
+
liquidatePositionV3_1(tx, owner, positionId, collateralToken, indexToken, long, oracle) {
|
|
2054
|
+
tx.moveCall({
|
|
2055
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::liquidate_position_v3_1`,
|
|
2056
|
+
typeArguments: this.redeemTypeArgs(collateralToken, indexToken, long),
|
|
2057
|
+
arguments: [
|
|
2058
|
+
tx.object(this.consts.zoCore.market),
|
|
2059
|
+
...oracleArgsAlt(oracle),
|
|
2060
|
+
tx.object(owner),
|
|
2061
|
+
tx.object(positionId),
|
|
2062
|
+
],
|
|
2063
|
+
});
|
|
2064
|
+
}
|
|
2065
|
+
/**
|
|
2066
|
+
* Builds a full liquidate PTB including Pyth Pro verification.
|
|
2067
|
+
*/
|
|
2068
|
+
async liquidatePositionV3_1Tx(owner, positionId, collateralToken, indexToken, long, pythProUpdateBytes, tx) {
|
|
2069
|
+
if (!tx) {
|
|
2070
|
+
tx = new Transaction();
|
|
2071
|
+
}
|
|
2072
|
+
const { tx: tx_, oracle } = await this.initV3OracleForTokens([collateralToken, indexToken], tx, pythProUpdateBytes);
|
|
2073
|
+
this.liquidatePositionV3_1(tx_, owner, positionId, collateralToken, indexToken, long, buildOracleInputsAlt(oracle));
|
|
2074
|
+
return tx_;
|
|
2075
|
+
}
|
|
2076
|
+
/** Keeper/bot: execute_open_position_order_v3_1 (compose in PTB). */
|
|
2077
|
+
executeOpenPositionOrderV3_1(tx, owner, orderId, collateralToken, indexToken, long, oracle) {
|
|
2078
|
+
tx.moveCall({
|
|
2079
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::execute_open_position_order_v3_1`,
|
|
2080
|
+
typeArguments: this.positionTypeArgs(collateralToken, indexToken, long),
|
|
2081
|
+
arguments: [
|
|
2082
|
+
tx.object(this.consts.zoCore.market),
|
|
2083
|
+
...oracleArgsAlt(oracle),
|
|
2084
|
+
tx.object(owner),
|
|
2085
|
+
tx.object(orderId),
|
|
2086
|
+
],
|
|
2087
|
+
});
|
|
2088
|
+
}
|
|
2089
|
+
/** Keeper/bot: execute_open_market_order_v3_1. */
|
|
2090
|
+
executeOpenMarketOrderV3_1(tx, owner, orderId, collateralToken, indexToken, long, oracle) {
|
|
2091
|
+
tx.moveCall({
|
|
2092
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::execute_open_market_order_v3_1`,
|
|
2093
|
+
typeArguments: this.positionTypeArgs(collateralToken, indexToken, long),
|
|
2094
|
+
arguments: [
|
|
2095
|
+
tx.object(this.consts.zoCore.market),
|
|
2096
|
+
...oracleArgsAlt(oracle),
|
|
2097
|
+
tx.object(owner),
|
|
2098
|
+
tx.object(orderId),
|
|
2099
|
+
],
|
|
2100
|
+
});
|
|
2101
|
+
}
|
|
2102
|
+
/** Keeper/bot: execute_decrease_market_order_v3_1. */
|
|
2103
|
+
executeDecreaseMarketOrderV3_1(tx, owner, orderId, positionId, collateralToken, indexToken, long, oracle) {
|
|
2104
|
+
tx.moveCall({
|
|
2105
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::execute_decrease_market_order_v3_1`,
|
|
2106
|
+
typeArguments: this.positionTypeArgs(collateralToken, indexToken, long),
|
|
2107
|
+
arguments: [
|
|
2108
|
+
tx.object(this.consts.zoCore.market),
|
|
2109
|
+
...oracleArgsAlt(oracle),
|
|
2110
|
+
tx.object(owner),
|
|
2111
|
+
tx.object(orderId),
|
|
2112
|
+
tx.object(positionId),
|
|
2113
|
+
],
|
|
2114
|
+
});
|
|
2115
|
+
}
|
|
2116
|
+
/** Keeper/bot: execute_decrease_position_order_v3_1. */
|
|
2117
|
+
executeDecreasePositionOrderV3_1(tx, owner, orderId, positionId, collateralToken, indexToken, long, oracle) {
|
|
2118
|
+
tx.moveCall({
|
|
2119
|
+
target: `${this.consts.zoCore.upgradedPackage}::market::execute_decrease_position_order_v3_1`,
|
|
2120
|
+
typeArguments: this.positionTypeArgs(collateralToken, indexToken, long),
|
|
2121
|
+
arguments: [
|
|
2122
|
+
tx.object(this.consts.zoCore.market),
|
|
2123
|
+
...oracleArgsAlt(oracle),
|
|
2124
|
+
tx.object(owner),
|
|
2125
|
+
tx.object(orderId),
|
|
2126
|
+
tx.object(positionId),
|
|
2127
|
+
],
|
|
2128
|
+
});
|
|
2129
|
+
}
|
|
1782
2130
|
}
|
|
1783
2131
|
//# sourceMappingURL=USDZAPI.mjs.map
|