@zofai/zo-sdk 0.2.8 → 0.2.10
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/BaseAPI.cjs +7 -1
- package/dist/abstract/BaseAPI.cjs.map +1 -1
- package/dist/abstract/BaseAPI.d.cts +8 -7
- package/dist/abstract/BaseAPI.d.cts.map +1 -1
- package/dist/abstract/BaseAPI.d.mts +8 -7
- package/dist/abstract/BaseAPI.d.mts.map +1 -1
- package/dist/abstract/BaseAPI.mjs +7 -1
- package/dist/abstract/BaseAPI.mjs.map +1 -1
- package/dist/api.cjs +12 -12
- package/dist/api.cjs.map +1 -1
- package/dist/api.d.cts.map +1 -1
- package/dist/api.d.mts.map +1 -1
- package/dist/api.mjs +12 -12
- package/dist/api.mjs.map +1 -1
- package/dist/coins.cjs +74 -0
- package/dist/coins.cjs.map +1 -0
- package/dist/coins.d.cts +37 -0
- package/dist/coins.d.cts.map +1 -0
- package/dist/coins.d.mts +37 -0
- package/dist/coins.d.mts.map +1 -0
- package/dist/coins.mjs +67 -0
- package/dist/coins.mjs.map +1 -0
- package/dist/consts/deployments-usdz-mainnet.json +1 -1
- package/dist/implementations/SLPAPI.cjs +192 -100
- package/dist/implementations/SLPAPI.cjs.map +1 -1
- package/dist/implementations/SLPAPI.d.cts +23 -23
- package/dist/implementations/SLPAPI.d.cts.map +1 -1
- package/dist/implementations/SLPAPI.d.mts +23 -23
- package/dist/implementations/SLPAPI.d.mts.map +1 -1
- package/dist/implementations/SLPAPI.mjs +192 -100
- package/dist/implementations/SLPAPI.mjs.map +1 -1
- package/dist/implementations/SLPDataAPI.cjs +2 -2
- package/dist/implementations/SLPDataAPI.cjs.map +1 -1
- package/dist/implementations/SLPDataAPI.mjs +2 -2
- package/dist/implementations/SLPDataAPI.mjs.map +1 -1
- package/dist/implementations/USDZAPI.cjs +176 -92
- package/dist/implementations/USDZAPI.cjs.map +1 -1
- package/dist/implementations/USDZAPI.d.cts +21 -21
- package/dist/implementations/USDZAPI.d.cts.map +1 -1
- package/dist/implementations/USDZAPI.d.mts +21 -21
- package/dist/implementations/USDZAPI.d.mts.map +1 -1
- package/dist/implementations/USDZAPI.mjs +176 -92
- package/dist/implementations/USDZAPI.mjs.map +1 -1
- package/dist/implementations/ZBTCVCAPI.cjs +84 -46
- package/dist/implementations/ZBTCVCAPI.cjs.map +1 -1
- package/dist/implementations/ZBTCVCAPI.d.cts +11 -11
- package/dist/implementations/ZBTCVCAPI.d.cts.map +1 -1
- package/dist/implementations/ZBTCVCAPI.d.mts +11 -11
- package/dist/implementations/ZBTCVCAPI.d.mts.map +1 -1
- package/dist/implementations/ZBTCVCAPI.mjs +84 -46
- package/dist/implementations/ZBTCVCAPI.mjs.map +1 -1
- package/dist/implementations/ZLPAPI.cjs +192 -100
- package/dist/implementations/ZLPAPI.cjs.map +1 -1
- package/dist/implementations/ZLPAPI.d.cts +23 -23
- package/dist/implementations/ZLPAPI.d.cts.map +1 -1
- package/dist/implementations/ZLPAPI.d.mts +23 -23
- package/dist/implementations/ZLPAPI.d.mts.map +1 -1
- package/dist/implementations/ZLPAPI.mjs +192 -100
- package/dist/implementations/ZLPAPI.mjs.map +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/interfaces/base.d.cts +25 -21
- package/dist/interfaces/base.d.cts.map +1 -1
- package/dist/interfaces/base.d.mts +25 -21
- package/dist/interfaces/base.d.mts.map +1 -1
- package/dist/interfaces/slp.d.cts +1 -1
- package/dist/interfaces/slp.d.cts.map +1 -1
- package/dist/interfaces/slp.d.mts +1 -1
- package/dist/interfaces/slp.d.mts.map +1 -1
- package/dist/interfaces/usdz.d.cts +1 -1
- package/dist/interfaces/usdz.d.cts.map +1 -1
- package/dist/interfaces/usdz.d.mts +1 -1
- package/dist/interfaces/usdz.d.mts.map +1 -1
- package/dist/interfaces/zbtcvc.d.cts +1 -1
- package/dist/interfaces/zbtcvc.d.cts.map +1 -1
- package/dist/interfaces/zbtcvc.d.mts +1 -1
- package/dist/interfaces/zbtcvc.d.mts.map +1 -1
- package/dist/interfaces/zlp.d.cts +1 -1
- package/dist/interfaces/zlp.d.cts.map +1 -1
- package/dist/interfaces/zlp.d.mts +1 -1
- package/dist/interfaces/zlp.d.mts.map +1 -1
- package/dist/oracle.cjs +27 -22
- package/dist/oracle.cjs.map +1 -1
- package/dist/oracle.d.cts +13 -1
- package/dist/oracle.d.cts.map +1 -1
- package/dist/oracle.d.mts +13 -1
- package/dist/oracle.d.mts.map +1 -1
- package/dist/oracle.mjs +27 -22
- package/dist/oracle.mjs.map +1 -1
- package/package.json +1 -1
- package/src/abstract/BaseAPI.ts +10 -8
- package/src/api.ts +17 -13
- package/src/coins.ts +115 -0
- package/src/consts/deployments-usdz-mainnet.json +1 -1
- package/src/implementations/SLPAPI.ts +201 -117
- package/src/implementations/SLPDataAPI.ts +2 -2
- package/src/implementations/USDZAPI.ts +184 -94
- package/src/implementations/ZBTCVCAPI.ts +98 -48
- package/src/implementations/ZLPAPI.ts +199 -102
- package/src/index.ts +1 -0
- package/src/interfaces/base.ts +13 -21
- package/src/interfaces/slp.ts +0 -1
- package/src/interfaces/usdz.ts +0 -1
- package/src/interfaces/zbtcvc.ts +0 -1
- package/src/interfaces/zlp.ts +0 -1
- package/src/oracle.ts +64 -28
package/dist/oracle.cjs
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.OracleAPI = void 0;
|
|
4
4
|
const transactions_1 = require("@mysten/sui/transactions");
|
|
5
5
|
const pyth_sui_js_1 = require("@pythnetwork/pyth-sui-js");
|
|
6
|
+
const coins_1 = require("./coins.cjs");
|
|
6
7
|
const consts_1 = require("./consts/index.cjs");
|
|
7
8
|
const utils_1 = require("./utils.cjs");
|
|
8
9
|
class OracleAPI {
|
|
@@ -68,32 +69,31 @@ class OracleAPI {
|
|
|
68
69
|
connection.unsubscribePriceFeedUpdates(priceFeedIds);
|
|
69
70
|
};
|
|
70
71
|
}
|
|
71
|
-
async
|
|
72
|
-
let tx_ = tx;
|
|
73
|
-
if (!tx_) {
|
|
74
|
-
tx_ = new transactions_1.Transaction();
|
|
75
|
-
}
|
|
76
|
-
// Remove redundant tokens first
|
|
72
|
+
async getStalePriceFeedIds(tokens) {
|
|
77
73
|
const uniqueTokens = Array.from(new Set(tokens));
|
|
78
|
-
const connection = new pyth_sui_js_1.SuiPriceServiceConnection(this.connectionURL, {
|
|
79
|
-
priceFeedRequestConfig: {
|
|
80
|
-
binary: true,
|
|
81
|
-
},
|
|
82
|
-
});
|
|
83
74
|
let pythObjectIds = uniqueTokens.map(token => this.consts.pythFeeder.feeder[token]);
|
|
84
|
-
// remove dupe object ids. If any of the object ids are duplicates the call will fail
|
|
85
75
|
pythObjectIds = [...new Set(pythObjectIds)].filter(Boolean);
|
|
86
|
-
const
|
|
76
|
+
const objects = await this.provider.multiGetObjects({
|
|
87
77
|
ids: pythObjectIds,
|
|
88
|
-
options: {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
})).map(pythObject => [
|
|
78
|
+
options: { showContent: true },
|
|
79
|
+
});
|
|
80
|
+
return objects.map(pythObject => [
|
|
92
81
|
Number.parseInt((pythObject.data?.content).fields.price_info.fields.arrival_time || 0, 10) - Date.now() / 1000,
|
|
93
82
|
pythObject.data?.objectId,
|
|
94
83
|
]).filter((x) => Math.abs(x[0]) >= 0).map(x => x[1]);
|
|
84
|
+
}
|
|
85
|
+
async initOracleTxb(tokens, tx, sponsoredTx, sponsorOptions) {
|
|
86
|
+
const tx_ = tx ?? new transactions_1.Transaction();
|
|
87
|
+
const connection = new pyth_sui_js_1.SuiPriceServiceConnection(this.connectionURL, {
|
|
88
|
+
priceFeedRequestConfig: { binary: true },
|
|
89
|
+
});
|
|
90
|
+
const needUpdateObjectIds = await this.getStalePriceFeedIds(tokens);
|
|
95
91
|
if (needUpdateObjectIds.length === 0) {
|
|
96
|
-
|
|
92
|
+
if (sponsoredTx && sponsorOptions && (sponsorOptions.additionalSuiAmount ?? 0n) > 0n) {
|
|
93
|
+
const suiCoinObject = await (0, coins_1.resolveSpendableSuiCoin)(tx_, this.provider, sponsorOptions.sender, sponsorOptions.additionalSuiAmount);
|
|
94
|
+
return { tx: tx_, suiCoinObject };
|
|
95
|
+
}
|
|
96
|
+
return { tx: tx_ };
|
|
97
97
|
}
|
|
98
98
|
const priceFeedIds = needUpdateObjectIds
|
|
99
99
|
.map(pythObjectId => this.PythFeederToPriceId[pythObjectId])
|
|
@@ -101,14 +101,19 @@ class OracleAPI {
|
|
|
101
101
|
.map(x => `0x${x}`);
|
|
102
102
|
const priceUpdateData = await connection.getPriceFeedsUpdateData(priceFeedIds);
|
|
103
103
|
if (sponsoredTx) {
|
|
104
|
+
if (!sponsorOptions?.sender) {
|
|
105
|
+
throw new Error('sender is required for sponsored transactions that update Pyth price feeds');
|
|
106
|
+
}
|
|
104
107
|
const baseUpdateFee = await this.getBaseUpdateFee();
|
|
108
|
+
const pythCost = BigInt(baseUpdateFee) * BigInt(priceFeedIds.length);
|
|
109
|
+
const totalSui = pythCost + (sponsorOptions.additionalSuiAmount ?? 0n);
|
|
110
|
+
const suiCoinObject = await (0, coins_1.resolveSpendableSuiCoin)(tx_, this.provider, sponsorOptions.sender, totalSui);
|
|
105
111
|
const pythUpdateCoins = tx_.splitCoins(suiCoinObject, priceFeedIds.map(() => tx_.pure.u64(baseUpdateFee)));
|
|
106
112
|
await this.client.updatePriceFeedsWithCoins(tx_, priceUpdateData, priceFeedIds, pythUpdateCoins);
|
|
113
|
+
return { tx: tx_, suiCoinObject };
|
|
107
114
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
return tx_;
|
|
115
|
+
await this.client.updatePriceFeeds(tx_, priceUpdateData, priceFeedIds);
|
|
116
|
+
return { tx: tx_ };
|
|
112
117
|
}
|
|
113
118
|
async getBaseUpdateFee() {
|
|
114
119
|
return await this.client.getBaseUpdateFee();
|
package/dist/oracle.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oracle.cjs","sourceRoot":"","sources":["../src/oracle.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"oracle.cjs","sourceRoot":"","sources":["../src/oracle.ts"],"names":[],"mappings":";;;AAEA,2DAAsD;AAEtD,0DAAmF;AAEnF,uCAAiD;AAEjD,+CAAiI;AACjI,uCAA+C;AAc/C,MAAa,SAAS;IAYpB,YACE,OAAgB,EAChB,QAA0B;IAC1B,kBAAkB;IAClB,aAAqB,EACrB,UAAmB,gBAAO,CAAC,GAAG;QAPxB,eAAU,GAA8B,EAAE,CAAA;QAShD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,MAAM,GAAG,IAAA,kBAAS,EAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACzC,IAAI,CAAC,YAAY,GAAG,IAAA,wBAAe,EAAC,OAAO,CAAC,CAAA;QAC5C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAA;QAClC,IAAI,CAAC,mBAAmB,GAAG,IAAA,+BAAsB,EAAC,OAAO,CAAC,CAAA;QAC1D,IAAI,CAAC,cAAc,GAAG,IAAA,0BAAiB,EAAC,OAAO,CAAC,CAAA;QAChD,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,IAAA,6BAAqB,EAAC,OAAO,CAAC,CAAA;QAC1D,IAAI,CAAC,MAAM,GAAG,IAAI,2BAAa,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;IACrH,CAAC;IAED,aAAa;QACX,MA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AE,CAAC,CAAA;YAEtE,MAAM,aAAa,GAAG,MAAM,IAAA,+BAAuB,EACjD,GAAG,EACH,IAAI,CAAC,QAAQ,EACb,cAAc,CAAC,MAAM,EACrB,QAAQ,CACT,CAAA;YAED,MAAM,eAAe,GAAG,GAAG,CAAC,UAAU,CACpC,aAAa,EACb,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CACpD,CAAA;YACD,MAAM,IAAI,CAAC,MAAM,CAAC,yBAAyB,CAAC,GAAG,EAAE,eAAe,EAAE,YAAY,EAAE,eAAe,CAAC,CAAA;YAEhG,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,aAAa,EAAE,CAAA;QACnC,CAAC;QAED,MAAM,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC,GAAG,EAAE,eAAe,EAAE,YAAY,CAAC,CAAA;QACtE,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,CAAA;IACpB,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,gBAAgB,EAAE,CAAA;IAC7C,CAAC;CACF;AArKD,8BAqKC"}
|
package/dist/oracle.d.cts
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import type { SuiClient } from "@mysten/sui/client";
|
|
2
|
+
import type { TransactionObjectArgument } from "@mysten/sui/transactions";
|
|
2
3
|
import { Transaction } from "@mysten/sui/transactions";
|
|
3
4
|
import type { PriceFeed } from "@pythnetwork/pyth-sui-js";
|
|
4
5
|
import { SuiPythClient } from "@pythnetwork/pyth-sui-js";
|
|
5
6
|
import type { IConsts, Network } from "./consts/index.cjs";
|
|
6
7
|
import { LPToken } from "./consts/index.cjs";
|
|
8
|
+
export interface IInitOracleSponsorOptions {
|
|
9
|
+
sender: string;
|
|
10
|
+
/** SUI (MIST) needed beyond Pyth update fees, e.g. SUI collateral deposit + relayer fee. */
|
|
11
|
+
additionalSuiAmount?: bigint;
|
|
12
|
+
}
|
|
13
|
+
export interface IInitOracleTxbResult {
|
|
14
|
+
tx: Transaction;
|
|
15
|
+
/** Remaining SUI coin after Pyth fee splits; present when sponsored and SUI was resolved. */
|
|
16
|
+
suiCoinObject?: TransactionObjectArgument;
|
|
17
|
+
}
|
|
7
18
|
export declare class OracleAPI {
|
|
8
19
|
network: Network;
|
|
9
20
|
consts: IConsts;
|
|
@@ -19,7 +30,8 @@ export declare class OracleAPI {
|
|
|
19
30
|
getOraclePrice(tokenId: string): Promise<PriceFeed>;
|
|
20
31
|
getOraclePrices(tokens: string[]): Promise<PriceFeed[]>;
|
|
21
32
|
subOraclePrices(tokens: string[], callback: (price: PriceFeed) => void): Promise<() => void>;
|
|
22
|
-
|
|
33
|
+
getStalePriceFeedIds(tokens: string[]): Promise<string[]>;
|
|
34
|
+
initOracleTxb(tokens: string[], tx?: Transaction, sponsoredTx?: boolean, sponsorOptions?: IInitOracleSponsorOptions): Promise<IInitOracleTxbResult>;
|
|
23
35
|
getBaseUpdateFee(): Promise<number>;
|
|
24
36
|
}
|
|
25
37
|
//# sourceMappingURL=oracle.d.cts.map
|
package/dist/oracle.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oracle.d.cts","sourceRoot":"","sources":["../src/oracle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,2BAA0B;AACnD,OAAO,EAAE,WAAW,EAAE,iCAAgC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,iCAAgC;AACzD,OAAO,EAA6B,aAAa,EAAE,iCAAgC;
|
|
1
|
+
{"version":3,"file":"oracle.d.cts","sourceRoot":"","sources":["../src/oracle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,2BAA0B;AACnD,OAAO,KAAK,EAAE,yBAAyB,EAAE,iCAAgC;AACzE,OAAO,EAAE,WAAW,EAAE,iCAAgC;AACtD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,MAAM,EAAE,CAAC;IAgBzD,aAAa,CACjB,MAAM,EAAE,MAAM,EAAE,EAChB,EAAE,CAAC,EAAE,WAAW,EAChB,WAAW,CAAC,EAAE,OAAO,EACrB,cAAc,CAAC,EAAE,yBAAyB,GACzC,OAAO,CAAC,oBAAoB,CAAC;IA0D1B,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;CAG1C"}
|
package/dist/oracle.d.mts
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import type { SuiClient } from "@mysten/sui/client";
|
|
2
|
+
import type { TransactionObjectArgument } from "@mysten/sui/transactions";
|
|
2
3
|
import { Transaction } from "@mysten/sui/transactions";
|
|
3
4
|
import type { PriceFeed } from "@pythnetwork/pyth-sui-js";
|
|
4
5
|
import { SuiPythClient } from "@pythnetwork/pyth-sui-js";
|
|
5
6
|
import type { IConsts, Network } from "./consts/index.mjs";
|
|
6
7
|
import { LPToken } from "./consts/index.mjs";
|
|
8
|
+
export interface IInitOracleSponsorOptions {
|
|
9
|
+
sender: string;
|
|
10
|
+
/** SUI (MIST) needed beyond Pyth update fees, e.g. SUI collateral deposit + relayer fee. */
|
|
11
|
+
additionalSuiAmount?: bigint;
|
|
12
|
+
}
|
|
13
|
+
export interface IInitOracleTxbResult {
|
|
14
|
+
tx: Transaction;
|
|
15
|
+
/** Remaining SUI coin after Pyth fee splits; present when sponsored and SUI was resolved. */
|
|
16
|
+
suiCoinObject?: TransactionObjectArgument;
|
|
17
|
+
}
|
|
7
18
|
export declare class OracleAPI {
|
|
8
19
|
network: Network;
|
|
9
20
|
consts: IConsts;
|
|
@@ -19,7 +30,8 @@ export declare class OracleAPI {
|
|
|
19
30
|
getOraclePrice(tokenId: string): Promise<PriceFeed>;
|
|
20
31
|
getOraclePrices(tokens: string[]): Promise<PriceFeed[]>;
|
|
21
32
|
subOraclePrices(tokens: string[], callback: (price: PriceFeed) => void): Promise<() => void>;
|
|
22
|
-
|
|
33
|
+
getStalePriceFeedIds(tokens: string[]): Promise<string[]>;
|
|
34
|
+
initOracleTxb(tokens: string[], tx?: Transaction, sponsoredTx?: boolean, sponsorOptions?: IInitOracleSponsorOptions): Promise<IInitOracleTxbResult>;
|
|
23
35
|
getBaseUpdateFee(): Promise<number>;
|
|
24
36
|
}
|
|
25
37
|
//# sourceMappingURL=oracle.d.mts.map
|
package/dist/oracle.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oracle.d.mts","sourceRoot":"","sources":["../src/oracle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,2BAA0B;AACnD,OAAO,EAAE,WAAW,EAAE,iCAAgC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,iCAAgC;AACzD,OAAO,EAA6B,aAAa,EAAE,iCAAgC;
|
|
1
|
+
{"version":3,"file":"oracle.d.mts","sourceRoot":"","sources":["../src/oracle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,2BAA0B;AACnD,OAAO,KAAK,EAAE,yBAAyB,EAAE,iCAAgC;AACzE,OAAO,EAAE,WAAW,EAAE,iCAAgC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,iCAAgC;AACzD,OAAO,EAA6B,aAAa,EAAE,iCAAgC;AAGnF,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,2BAAgB;AAChD,OAAO,EAAiG,OAAO,EAAE,2BAAgB;AAGjI,MAAM,WAAW,yBAAyB;IACxC,MAAM,EAAE,MAAM,CAAA;IACd,4FAA4F;IAC5F,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC7B;AAED,MAAM,WAAW,oBAAoB;IACnC,EAAE,EAAE,WAAW,CAAA;IACf,6FAA6F;IAC7F,aAAa,CAAC,EAAE,yBAAyB,CAAA;CAC1C;AAED,qBAAa,SAAS;IACpB,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,OAAO,CAAA;IACf,YAAY,EAAE,OAAO,CAAA;IACrB,aAAa,EAAE,MAAM,CAAA;IACrB,mBAAmB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC3C,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACtC,QAAQ,EAAE,SAAS,CAAA;IACnB,MAAM,EAAE,aAAa,CAAA;IAErB,OAAO,CAAC,UAAU,CAAgC;gBAGhD,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,SAAS,GAAG,IAAI,EAE1B,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,OAAqB;IAYhC,aAAa;IASP,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAanD,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IAavD,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,GAAG,OAAO,CAAC,MAAM,IAAI,CAAC;IAmB5F,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAgBzD,aAAa,CACjB,MAAM,EAAE,MAAM,EAAE,EAChB,EAAE,CAAC,EAAE,WAAW,EAChB,WAAW,CAAC,EAAE,OAAO,EACrB,cAAc,CAAC,EAAE,yBAAyB,GACzC,OAAO,CAAC,oBAAoB,CAAC;IA0D1B,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;CAG1C"}
|
package/dist/oracle.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Transaction } from "@mysten/sui/transactions";
|
|
2
2
|
import { SuiPriceServiceConnection, SuiPythClient } from "@pythnetwork/pyth-sui-js";
|
|
3
|
+
import { resolveSpendableSuiCoin } from "./coins.mjs";
|
|
3
4
|
import { getConsts, getPriceIdToPythFeeder, getPythFeederToId, getPythFeederToPriceId, getSharedConfig, LPToken } from "./consts/index.mjs";
|
|
4
5
|
import { createJsonRpcProvider } from "./utils.mjs";
|
|
5
6
|
export class OracleAPI {
|
|
@@ -65,32 +66,31 @@ export class OracleAPI {
|
|
|
65
66
|
connection.unsubscribePriceFeedUpdates(priceFeedIds);
|
|
66
67
|
};
|
|
67
68
|
}
|
|
68
|
-
async
|
|
69
|
-
let tx_ = tx;
|
|
70
|
-
if (!tx_) {
|
|
71
|
-
tx_ = new Transaction();
|
|
72
|
-
}
|
|
73
|
-
// Remove redundant tokens first
|
|
69
|
+
async getStalePriceFeedIds(tokens) {
|
|
74
70
|
const uniqueTokens = Array.from(new Set(tokens));
|
|
75
|
-
const connection = new SuiPriceServiceConnection(this.connectionURL, {
|
|
76
|
-
priceFeedRequestConfig: {
|
|
77
|
-
binary: true,
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
71
|
let pythObjectIds = uniqueTokens.map(token => this.consts.pythFeeder.feeder[token]);
|
|
81
|
-
// remove dupe object ids. If any of the object ids are duplicates the call will fail
|
|
82
72
|
pythObjectIds = [...new Set(pythObjectIds)].filter(Boolean);
|
|
83
|
-
const
|
|
73
|
+
const objects = await this.provider.multiGetObjects({
|
|
84
74
|
ids: pythObjectIds,
|
|
85
|
-
options: {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
})).map(pythObject => [
|
|
75
|
+
options: { showContent: true },
|
|
76
|
+
});
|
|
77
|
+
return objects.map(pythObject => [
|
|
89
78
|
Number.parseInt((pythObject.data?.content).fields.price_info.fields.arrival_time || 0, 10) - Date.now() / 1000,
|
|
90
79
|
pythObject.data?.objectId,
|
|
91
80
|
]).filter((x) => Math.abs(x[0]) >= 0).map(x => x[1]);
|
|
81
|
+
}
|
|
82
|
+
async initOracleTxb(tokens, tx, sponsoredTx, sponsorOptions) {
|
|
83
|
+
const tx_ = tx ?? new Transaction();
|
|
84
|
+
const connection = new SuiPriceServiceConnection(this.connectionURL, {
|
|
85
|
+
priceFeedRequestConfig: { binary: true },
|
|
86
|
+
});
|
|
87
|
+
const needUpdateObjectIds = await this.getStalePriceFeedIds(tokens);
|
|
92
88
|
if (needUpdateObjectIds.length === 0) {
|
|
93
|
-
|
|
89
|
+
if (sponsoredTx && sponsorOptions && (sponsorOptions.additionalSuiAmount ?? 0n) > 0n) {
|
|
90
|
+
const suiCoinObject = await resolveSpendableSuiCoin(tx_, this.provider, sponsorOptions.sender, sponsorOptions.additionalSuiAmount);
|
|
91
|
+
return { tx: tx_, suiCoinObject };
|
|
92
|
+
}
|
|
93
|
+
return { tx: tx_ };
|
|
94
94
|
}
|
|
95
95
|
const priceFeedIds = needUpdateObjectIds
|
|
96
96
|
.map(pythObjectId => this.PythFeederToPriceId[pythObjectId])
|
|
@@ -98,14 +98,19 @@ export class OracleAPI {
|
|
|
98
98
|
.map(x => `0x${x}`);
|
|
99
99
|
const priceUpdateData = await connection.getPriceFeedsUpdateData(priceFeedIds);
|
|
100
100
|
if (sponsoredTx) {
|
|
101
|
+
if (!sponsorOptions?.sender) {
|
|
102
|
+
throw new Error('sender is required for sponsored transactions that update Pyth price feeds');
|
|
103
|
+
}
|
|
101
104
|
const baseUpdateFee = await this.getBaseUpdateFee();
|
|
105
|
+
const pythCost = BigInt(baseUpdateFee) * BigInt(priceFeedIds.length);
|
|
106
|
+
const totalSui = pythCost + (sponsorOptions.additionalSuiAmount ?? 0n);
|
|
107
|
+
const suiCoinObject = await resolveSpendableSuiCoin(tx_, this.provider, sponsorOptions.sender, totalSui);
|
|
102
108
|
const pythUpdateCoins = tx_.splitCoins(suiCoinObject, priceFeedIds.map(() => tx_.pure.u64(baseUpdateFee)));
|
|
103
109
|
await this.client.updatePriceFeedsWithCoins(tx_, priceUpdateData, priceFeedIds, pythUpdateCoins);
|
|
110
|
+
return { tx: tx_, suiCoinObject };
|
|
104
111
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
return tx_;
|
|
112
|
+
await this.client.updatePriceFeeds(tx_, priceUpdateData, priceFeedIds);
|
|
113
|
+
return { tx: tx_ };
|
|
109
114
|
}
|
|
110
115
|
async getBaseUpdateFee() {
|
|
111
116
|
return await this.client.getBaseUpdateFee();
|
package/dist/oracle.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oracle.mjs","sourceRoot":"","sources":["../src/oracle.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"oracle.mjs","sourceRoot":"","sources":["../src/oracle.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,iCAAgC;AAEtD,OAAO,EAAE,yBAAyB,EAAE,aAAa,EAAE,iCAAgC;AAEnF,OAAO,EAAE,uBAAuB,EAAE,oBAAe;AAEjD,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,eAAe,EAAE,OAAO,EAAE,2BAAgB;AACjI,OAAO,EAAE,qBAAqB,EAAE,oBAAe;AAc/C,MAAM,OAAO,SAAS;IAYpB,YACE,OAAgB,EAChB,QAA0B;IAC1B,kBAAkB;IAClB,aAAqB,EACrB,UAAmB,OAAO,CAAC,GAAG;QAPxB,eAAU,GAA8B,EAAE,CAAA;QAShD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACzC,IAAI,CAAC,YAAY,GAAG,eAAe,CAAC,OAAO,CAAC,CAAA;QAC5C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAA;QAClC,IAAI,CAAC,mBAAmB,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAA;QAC1D,IAAI,CAAC,cAAc,GAAG,iBAAiB,CAAC,OAAO,CAAC,CAAA;QAChD,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,qBAAqB,CAAC,OAAO,CAAC,CAAA;QAC1D,IAAI,CAAC,MAAM,GAAG,IAAI,aAAa,CAAC,IAAI,CAAC,QAAQ,EAAE,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AAa,CAAC,GAAG,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,CAAA;YACpE,MAAM,QAAQ,GAAG,QAAQ,GAAG,CAAC,cAAc,CAAC,mBAAmB,IAAI,EAAE,CAAC,CAAA;YAEtE,MAAM,aAAa,GAAG,MAAM,uBAAuB,CACjD,GAAG,EACH,IAAI,CAAC,QAAQ,EACb,cAAc,CAAC,MAAM,EACrB,QAAQ,CACT,CAAA;YAED,MAAM,eAAe,GAAG,GAAG,CAAC,UAAU,CACpC,aAAa,EACb,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CACpD,CAAA;YACD,MAAM,IAAI,CAAC,MAAM,CAAC,yBAAyB,CAAC,GAAG,EAAE,eAAe,EAAE,YAAY,EAAE,eAAe,CAAC,CAAA;YAEhG,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,aAAa,EAAE,CAAA;QACnC,CAAC;QAED,MAAM,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC,GAAG,EAAE,eAAe,EAAE,YAAY,CAAC,CAAA;QACtE,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,CAAA;IACpB,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,gBAAgB,EAAE,CAAA;IAC7C,CAAC;CACF"}
|
package/package.json
CHANGED
package/src/abstract/BaseAPI.ts
CHANGED
|
@@ -12,9 +12,9 @@ import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui/utils'
|
|
|
12
12
|
import type { LPToken, Network } from '../consts'
|
|
13
13
|
import { ALLOW_TRADE_CAN_TRADE, ALLOW_TRADE_MUST_TRADE, ALLOW_TRADE_NO_TRADE } from '../consts'
|
|
14
14
|
import type {
|
|
15
|
-
IBaseDataAPI,
|
|
16
15
|
IBaseAPI,
|
|
17
16
|
IBaseCredential,
|
|
17
|
+
IBaseDataAPI,
|
|
18
18
|
IBaseHistoryResponse,
|
|
19
19
|
IBaseMarketInfo,
|
|
20
20
|
IBaseMarketValuationInfo,
|
|
@@ -135,6 +135,13 @@ export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
|
|
|
135
135
|
return this.dataAPI.getReferralData(referree)
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
protected requireSenderForSponsored(sender: string | undefined): string {
|
|
139
|
+
if (!sender) {
|
|
140
|
+
throw new Error(`${this.constructor.name}: sender is required for sponsored transactions`)
|
|
141
|
+
}
|
|
142
|
+
return sender
|
|
143
|
+
}
|
|
144
|
+
|
|
138
145
|
/**
|
|
139
146
|
* Processes coin objects for transactions
|
|
140
147
|
* Handles SUI gas coin and merges multiple coin objects if needed
|
|
@@ -200,7 +207,6 @@ export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
|
|
|
200
207
|
referralAddress?: string,
|
|
201
208
|
sender?: string,
|
|
202
209
|
sponsoredTx?: boolean,
|
|
203
|
-
suiCoinObjectsForPythUpdate?: string[],
|
|
204
210
|
): Promise<Transaction>
|
|
205
211
|
|
|
206
212
|
public abstract withdraw(
|
|
@@ -209,7 +215,6 @@ export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
|
|
|
209
215
|
amount: number,
|
|
210
216
|
minAmountOut?: number,
|
|
211
217
|
sponsoredTx?: boolean,
|
|
212
|
-
suiCoinObjectsForPythUpdate?: string[],
|
|
213
218
|
): Promise<Transaction>
|
|
214
219
|
|
|
215
220
|
public abstract swap(
|
|
@@ -253,7 +258,6 @@ export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
|
|
|
253
258
|
referralAddress?: string,
|
|
254
259
|
sender?: string,
|
|
255
260
|
sponsoredTx?: boolean,
|
|
256
|
-
suiCoinObjectsForPythUpdate?: string[],
|
|
257
261
|
): Promise<Transaction>
|
|
258
262
|
|
|
259
263
|
public abstract decreasePosition(
|
|
@@ -272,7 +276,7 @@ export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
|
|
|
272
276
|
relayerFee?: bigint,
|
|
273
277
|
coinObjects?: string[],
|
|
274
278
|
sponsoredTx?: boolean,
|
|
275
|
-
|
|
279
|
+
sender?: string,
|
|
276
280
|
): Promise<Transaction>
|
|
277
281
|
|
|
278
282
|
public abstract decreaseMultiPositions(
|
|
@@ -302,7 +306,6 @@ export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
|
|
|
302
306
|
coinObjects: string[],
|
|
303
307
|
long: boolean,
|
|
304
308
|
sponsoredTx?: boolean,
|
|
305
|
-
suiCoinObjectsForPythUpdate?: string[],
|
|
306
309
|
): Promise<Transaction>
|
|
307
310
|
|
|
308
311
|
public abstract redeemFromPosition(
|
|
@@ -312,7 +315,6 @@ export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
|
|
|
312
315
|
amount: number,
|
|
313
316
|
long: boolean,
|
|
314
317
|
sponsoredTx?: boolean,
|
|
315
|
-
suiCoinObjectsForPythUpdate?: string[],
|
|
316
318
|
): Promise<Transaction>
|
|
317
319
|
|
|
318
320
|
public abstract cancelOrder(
|
|
@@ -488,7 +490,7 @@ export abstract class BaseAPI extends BaseDataAPI implements IBaseAPI {
|
|
|
488
490
|
*/
|
|
489
491
|
protected async initOracleTransaction(tokens: string[], tx?: Transaction): Promise<Transaction> {
|
|
490
492
|
const txToUse = tx ?? new Transaction()
|
|
491
|
-
return await this.initOracleTxb(tokens, txToUse)
|
|
493
|
+
return (await this.initOracleTxb(tokens, txToUse)).tx
|
|
492
494
|
}
|
|
493
495
|
|
|
494
496
|
/**
|
package/src/api.ts
CHANGED
|
@@ -50,7 +50,7 @@ export class API extends DataAPI {
|
|
|
50
50
|
return tx.object(coinObjects[0])
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
#processSlippage(indexPrice: number, long: boolean, slippage: number) {
|
|
53
|
+
#processSlippage(indexPrice: number, long: boolean, slippage: number): bigint {
|
|
54
54
|
const raw = long ? indexPrice * (1 + slippage) : indexPrice * (1 - slippage)
|
|
55
55
|
return BigInt(Math.round(raw * 1e18))
|
|
56
56
|
}
|
|
@@ -64,7 +64,7 @@ export class API extends DataAPI {
|
|
|
64
64
|
sender = '',
|
|
65
65
|
) => {
|
|
66
66
|
let tx = new Transaction()
|
|
67
|
-
tx = await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder), tx)
|
|
67
|
+
tx = (await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder), tx)).tx
|
|
68
68
|
if (referralAddress && !(await this.hasReferral(sender || ''))) {
|
|
69
69
|
tx = await this.addReferral(referralAddress, tx)
|
|
70
70
|
}
|
|
@@ -90,7 +90,7 @@ export class API extends DataAPI {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
withdraw = async (coin: string, zlpCoinObjects: string[], amount: number, minAmountOut = 0) => {
|
|
93
|
-
const tx = await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder))
|
|
93
|
+
const tx = (await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder))).tx
|
|
94
94
|
const zlpCoinObject = this.#processCoins(tx, 'zlp', zlpCoinObjects)
|
|
95
95
|
const [withdrawObject] = tx.splitCoins(zlpCoinObject, [tx.pure.u64(amount)])
|
|
96
96
|
|
|
@@ -133,7 +133,7 @@ export class API extends DataAPI {
|
|
|
133
133
|
if (referralAddress && !(await this.hasReferral(sender || ''))) {
|
|
134
134
|
tx = await this.addReferral(referralAddress, tx)
|
|
135
135
|
}
|
|
136
|
-
tx = await this.initOracleTxb([collateralToken, indexToken], tx)
|
|
136
|
+
tx = (await this.initOracleTxb([collateralToken, indexToken], tx)).tx
|
|
137
137
|
const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
|
|
138
138
|
const [depositObject, feeObject] = tx.splitCoins(coinObject, [
|
|
139
139
|
tx.pure.u64(collateralAmount),
|
|
@@ -203,7 +203,7 @@ export class API extends DataAPI {
|
|
|
203
203
|
if (referralAddress && !(await this.hasReferral(sender || ''))) {
|
|
204
204
|
tx = await this.addReferral(referralAddress, tx)
|
|
205
205
|
}
|
|
206
|
-
tx = await this.initOracleTxb([collateralToken, indexToken], tx)
|
|
206
|
+
tx = (await this.initOracleTxb([collateralToken, indexToken], tx)).tx
|
|
207
207
|
const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
|
|
208
208
|
const [depositObject, feeObject] = tx.splitCoins(coinObject, [
|
|
209
209
|
tx.pure.u64(collateralAmount),
|
|
@@ -276,7 +276,7 @@ export class API extends DataAPI {
|
|
|
276
276
|
coinObjects: string[],
|
|
277
277
|
long: boolean,
|
|
278
278
|
) => {
|
|
279
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
279
|
+
const tx = (await this.initOracleTxb([collateralToken, indexToken])).tx
|
|
280
280
|
const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
|
|
281
281
|
const [depositObject] = tx.splitCoins(coinObject, [tx.pure.u64(amount)])
|
|
282
282
|
|
|
@@ -300,7 +300,7 @@ export class API extends DataAPI {
|
|
|
300
300
|
amount: number,
|
|
301
301
|
long: boolean,
|
|
302
302
|
) => {
|
|
303
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
303
|
+
const tx = (await this.initOracleTxb([collateralToken, indexToken])).tx
|
|
304
304
|
const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
|
|
305
305
|
|
|
306
306
|
tx.moveCall({
|
|
@@ -342,7 +342,7 @@ export class API extends DataAPI {
|
|
|
342
342
|
collateralSlippage = 0.5,
|
|
343
343
|
relayerFee = BigInt(0.5),
|
|
344
344
|
) => {
|
|
345
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
345
|
+
const tx = (await this.initOracleTxb([collateralToken, indexToken])).tx
|
|
346
346
|
const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
|
|
347
347
|
const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
|
|
348
348
|
const feeObject = tx.splitCoins(coinObject, [tx.pure.u64(relayerFee)])
|
|
@@ -358,6 +358,7 @@ export class API extends DataAPI {
|
|
|
358
358
|
isTakeProfitOrder = true
|
|
359
359
|
}
|
|
360
360
|
|
|
361
|
+
|
|
361
362
|
tx.moveCall({
|
|
362
363
|
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position`,
|
|
363
364
|
typeArguments: [
|
|
@@ -406,7 +407,7 @@ export class API extends DataAPI {
|
|
|
406
407
|
collateralSlippage = 0.5,
|
|
407
408
|
relayerFee = BigInt(0.5),
|
|
408
409
|
) => {
|
|
409
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
410
|
+
const tx = (await this.initOracleTxb([collateralToken, indexToken])).tx
|
|
410
411
|
|
|
411
412
|
const kioskTx = new KioskTransaction({
|
|
412
413
|
transaction: tx,
|
|
@@ -434,6 +435,7 @@ export class API extends DataAPI {
|
|
|
434
435
|
isTakeProfitOrder = true
|
|
435
436
|
}
|
|
436
437
|
|
|
438
|
+
|
|
437
439
|
tx.moveCall({
|
|
438
440
|
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_with_scard`,
|
|
439
441
|
typeArguments: [
|
|
@@ -489,7 +491,7 @@ export class API extends DataAPI {
|
|
|
489
491
|
if (!tx) {
|
|
490
492
|
tx = new Transaction()
|
|
491
493
|
}
|
|
492
|
-
tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
|
|
494
|
+
tx = (await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)).tx
|
|
493
495
|
|
|
494
496
|
for (const position of positions) {
|
|
495
497
|
const {
|
|
@@ -523,6 +525,7 @@ export class API extends DataAPI {
|
|
|
523
525
|
innerIsTakeProfitOrder = true
|
|
524
526
|
}
|
|
525
527
|
|
|
528
|
+
|
|
526
529
|
tx.moveCall({
|
|
527
530
|
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position`,
|
|
528
531
|
typeArguments: [
|
|
@@ -570,7 +573,7 @@ export class API extends DataAPI {
|
|
|
570
573
|
if (!tx) {
|
|
571
574
|
tx = new Transaction()
|
|
572
575
|
}
|
|
573
|
-
tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
|
|
576
|
+
tx = (await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)).tx
|
|
574
577
|
|
|
575
578
|
const kioskTx = new KioskTransaction({
|
|
576
579
|
transaction: tx,
|
|
@@ -615,6 +618,7 @@ export class API extends DataAPI {
|
|
|
615
618
|
innerIsTakeProfitOrder = true
|
|
616
619
|
}
|
|
617
620
|
|
|
621
|
+
|
|
618
622
|
tx.moveCall({
|
|
619
623
|
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_with_scard`,
|
|
620
624
|
typeArguments: [
|
|
@@ -744,7 +748,7 @@ export class API extends DataAPI {
|
|
|
744
748
|
fromAmount: bigint,
|
|
745
749
|
fromCoinObjects: string[],
|
|
746
750
|
) => {
|
|
747
|
-
const tx = await this.initOracleTxb(Object.keys(this.consts.zoCore.vaults))
|
|
751
|
+
const tx = (await this.initOracleTxb(Object.keys(this.consts.zoCore.vaults))).tx
|
|
748
752
|
const fromCoinObject = this.#processCoins(tx, fromToken, fromCoinObjects)
|
|
749
753
|
const [fromDepositObject] = tx.splitCoins(fromCoinObject, [tx.pure.u64(fromAmount)])
|
|
750
754
|
const vaultsValuation = this.valuateVaults(tx)
|
|
@@ -897,7 +901,7 @@ export class API extends DataAPI {
|
|
|
897
901
|
|
|
898
902
|
// admin methods
|
|
899
903
|
adminUpdatePriceFeed = async (collateralToken: string, indexToken: string) => {
|
|
900
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
904
|
+
const tx = (await this.initOracleTxb([collateralToken, indexToken])).tx
|
|
901
905
|
return tx
|
|
902
906
|
}
|
|
903
907
|
|
package/src/coins.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { SuiClient } from '@mysten/sui/client'
|
|
2
|
+
import type { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions'
|
|
3
|
+
import { coinWithBalance } from '@mysten/sui/transactions'
|
|
4
|
+
import { SUI_TYPE_ARG } from '@mysten/sui/utils'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sui wallets can hold funds in two places for the same coin type:
|
|
8
|
+
* - coin objects (what getCoins returns)
|
|
9
|
+
* - address balance (funds held at the address until redeemed in a tx)
|
|
10
|
+
*/
|
|
11
|
+
export interface ICoinBalanceBreakdown {
|
|
12
|
+
coinType: string
|
|
13
|
+
totalBalance: bigint
|
|
14
|
+
coinObjectBalance: bigint
|
|
15
|
+
addressBalance: bigint
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ICoinFundingOptions {
|
|
19
|
+
/** bigint values matching each split in processCoinSplitting (sum = total spend). */
|
|
20
|
+
splitAmounts?: bigint[]
|
|
21
|
+
/** Use coinWithBalance (redeems address balance and/or merges coin objects). */
|
|
22
|
+
useAddressBalance?: boolean
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function sumSplitAmounts(amounts: bigint[]): bigint {
|
|
26
|
+
return amounts.reduce((total, amount) => total + amount, 0n)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function getCoinBalanceBreakdown(
|
|
30
|
+
provider: SuiClient,
|
|
31
|
+
owner: string,
|
|
32
|
+
coinType: string,
|
|
33
|
+
): Promise<ICoinBalanceBreakdown> {
|
|
34
|
+
const balance = await provider.getBalance({ owner, coinType })
|
|
35
|
+
const totalBalance = BigInt(balance.totalBalance)
|
|
36
|
+
|
|
37
|
+
const extended = balance as { coinBalance?: string, addressBalance?: string }
|
|
38
|
+
if (extended.coinBalance !== undefined && extended.addressBalance !== undefined) {
|
|
39
|
+
return {
|
|
40
|
+
coinType: balance.coinType,
|
|
41
|
+
totalBalance,
|
|
42
|
+
coinObjectBalance: BigInt(extended.coinBalance),
|
|
43
|
+
addressBalance: BigInt(extended.addressBalance),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let coinObjectBalance = 0n
|
|
48
|
+
let cursor: string | null | undefined = null
|
|
49
|
+
do {
|
|
50
|
+
const page = await provider.getCoins({ owner, coinType, cursor })
|
|
51
|
+
for (const coin of page.data) {
|
|
52
|
+
coinObjectBalance += BigInt(coin.balance)
|
|
53
|
+
}
|
|
54
|
+
cursor = page.hasNextPage ? page.nextCursor : null
|
|
55
|
+
} while (cursor)
|
|
56
|
+
|
|
57
|
+
const addressBalance = totalBalance > coinObjectBalance ? totalBalance - coinObjectBalance : 0n
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
coinType: balance.coinType,
|
|
61
|
+
totalBalance,
|
|
62
|
+
coinObjectBalance,
|
|
63
|
+
addressBalance,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Prefer coinWithBalance when the wallet has address balance and/or coin objects alone
|
|
69
|
+
* cannot cover `amount`. processCoins / processCoinSplitting only spend explicit object IDs otherwise.
|
|
70
|
+
*/
|
|
71
|
+
export function needsCoinWithBalanceFunding(
|
|
72
|
+
breakdown: ICoinBalanceBreakdown,
|
|
73
|
+
amount: bigint,
|
|
74
|
+
): boolean {
|
|
75
|
+
if (breakdown.totalBalance < amount) {
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
78
|
+
if (breakdown.addressBalance > 0n) {
|
|
79
|
+
return true
|
|
80
|
+
}
|
|
81
|
+
return breakdown.coinObjectBalance < amount
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Creates a spendable Coin<T> inside an open transaction.
|
|
86
|
+
* At build time Mysten resolves this via coin::redeem_funds (address balance) and/or merging coin objects.
|
|
87
|
+
*/
|
|
88
|
+
export function createSpendableCoin(
|
|
89
|
+
tx: Transaction,
|
|
90
|
+
coinType: string,
|
|
91
|
+
amount: bigint,
|
|
92
|
+
useGasCoin = false,
|
|
93
|
+
): TransactionObjectArgument {
|
|
94
|
+
return coinWithBalance({ balance: amount, type: coinType, useGasCoin })(tx)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Resolves a spendable SUI coin for sponsored transactions.
|
|
99
|
+
* Uses coinWithBalance so address balance and coin objects are both supported.
|
|
100
|
+
*/
|
|
101
|
+
export async function resolveSpendableSuiCoin(
|
|
102
|
+
tx: Transaction,
|
|
103
|
+
provider: SuiClient,
|
|
104
|
+
sender: string,
|
|
105
|
+
amount: bigint,
|
|
106
|
+
): Promise<TransactionObjectArgument> {
|
|
107
|
+
const breakdown = await getCoinBalanceBreakdown(provider, sender, SUI_TYPE_ARG)
|
|
108
|
+
if (breakdown.totalBalance < amount) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`Insufficient SUI balance: need ${amount} MIST, have ${breakdown.totalBalance} MIST `
|
|
111
|
+
+ `(coin objects: ${breakdown.coinObjectBalance}, address balance: ${breakdown.addressBalance})`,
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
return createSpendableCoin(tx, SUI_TYPE_ARG, amount, false)
|
|
115
|
+
}
|