zo-sdk 0.0.13 → 0.0.14

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 (48) hide show
  1. package/dist/api.cjs +469 -0
  2. package/dist/{api.d.ts → api.d.cts} +6 -5
  3. package/dist/api.d.cts.map +1 -0
  4. package/dist/api.d.mts +26 -0
  5. package/dist/api.d.mts.map +1 -0
  6. package/dist/api.mjs +465 -0
  7. package/dist/consts/{index.js → index.cjs} +8 -12
  8. package/dist/consts/{index.d.ts → index.d.cts} +1 -0
  9. package/dist/consts/index.d.cts.map +1 -0
  10. package/dist/consts/index.d.mts +66 -0
  11. package/dist/consts/index.d.mts.map +1 -0
  12. package/dist/consts/index.mjs +92 -0
  13. package/dist/{data.js → data.cjs} +246 -155
  14. package/dist/{data.d.ts → data.d.cts} +9 -8
  15. package/dist/data.d.cts.map +1 -0
  16. package/dist/data.d.mts +221 -0
  17. package/dist/data.d.mts.map +1 -0
  18. package/dist/data.mjs +841 -0
  19. package/dist/{index.js → index.cjs} +5 -6
  20. package/dist/index.d.cts +6 -0
  21. package/dist/index.d.cts.map +1 -0
  22. package/dist/index.d.mts +6 -0
  23. package/dist/index.d.mts.map +1 -0
  24. package/dist/index.mjs +5 -0
  25. package/dist/{oracle.js → oracle.cjs} +51 -8
  26. package/dist/{oracle.d.ts → oracle.d.cts} +7 -6
  27. package/dist/oracle.d.cts.map +1 -0
  28. package/dist/oracle.d.mts +24 -0
  29. package/dist/oracle.d.mts.map +1 -0
  30. package/dist/oracle.mjs +173 -0
  31. package/dist/{utils.js → utils.cjs} +1 -2
  32. package/dist/{utils.d.ts → utils.d.cts} +5 -4
  33. package/dist/utils.d.cts.map +1 -0
  34. package/dist/utils.d.mts +46 -0
  35. package/dist/utils.d.mts.map +1 -0
  36. package/dist/utils.mjs +128 -0
  37. package/package.json +23 -13
  38. package/src/consts/index.ts +1 -1
  39. package/tsconfig.json +19 -9
  40. package/babel.config.js +0 -11
  41. package/dist/api.js +0 -400
  42. package/dist/api.js.map +0 -1
  43. package/dist/consts/index.js.map +0 -1
  44. package/dist/data.js.map +0 -1
  45. package/dist/index.d.ts +0 -5
  46. package/dist/index.js.map +0 -1
  47. package/dist/oracle.js.map +0 -1
  48. package/dist/utils.js.map +0 -1
@@ -14,9 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./api"), exports);
18
- __exportStar(require("./consts"), exports);
19
- __exportStar(require("./data"), exports);
20
- __exportStar(require("./oracle"), exports);
21
- __exportStar(require("./utils"), exports);
22
- //# sourceMappingURL=index.js.map
17
+ __exportStar(require("./api.cjs"), exports);
18
+ __exportStar(require("./consts/index.cjs"), exports);
19
+ __exportStar(require("./data.cjs"), exports);
20
+ __exportStar(require("./oracle.cjs"), exports);
21
+ __exportStar(require("./utils.cjs"), exports);
@@ -0,0 +1,6 @@
1
+ export * from "./api.cjs";
2
+ export * from "./consts/index.cjs";
3
+ export * from "./data.cjs";
4
+ export * from "./oracle.cjs";
5
+ export * from "./utils.cjs";
6
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0BAAqB;AACrB,mCAAwB;AACxB,2BAAsB;AACtB,6BAAwB;AACxB,4BAAuB"}
@@ -0,0 +1,6 @@
1
+ export * from "./api.mjs";
2
+ export * from "./consts/index.mjs";
3
+ export * from "./data.mjs";
4
+ export * from "./oracle.mjs";
5
+ export * from "./utils.mjs";
6
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0BAAqB;AACrB,mCAAwB;AACxB,2BAAsB;AACtB,6BAAwB;AACxB,4BAAuB"}
package/dist/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./api.mjs";
2
+ export * from "./consts/index.mjs";
3
+ export * from "./data.mjs";
4
+ export * from "./oracle.mjs";
5
+ export * from "./utils.mjs";
@@ -1,20 +1,64 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.OracleAPI = void 0;
7
4
  exports.generatePriceIdToObjectIdJson = generatePriceIdToObjectIdJson;
8
5
  const transactions_1 = require("@mysten/sui/transactions");
9
6
  const pyth_sui_js_1 = require("@pythnetwork/pyth-sui-js");
10
- const consts_1 = require("./consts");
11
- const price_id_to_object_id_mainnet_json_1 = __importDefault(require("./consts/price_id_to_object_id.mainnet.json"));
12
- const utils_1 = require("./utils");
7
+ const consts_1 = require("./consts/index.cjs");
8
+ const price_id_to_object_id_mainnet_json_1 = require("./consts/price_id_to_object_id.mainnet.json");
9
+ const utils_1 = require("./utils.cjs");
13
10
  class OracleAPI {
14
11
  constructor(network, provider,
15
12
  // todo 需要从配置文件中获取
16
13
  connectionURL) {
17
- this.priceCache = {};
14
+ Object.defineProperty(this, "network", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: void 0
19
+ });
20
+ Object.defineProperty(this, "consts", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "connectionURL", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "PythFeederToPriceId", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ Object.defineProperty(this, "PythFeederToId", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: void 0
43
+ });
44
+ Object.defineProperty(this, "provider", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: void 0
49
+ });
50
+ Object.defineProperty(this, "client", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: void 0
55
+ });
56
+ Object.defineProperty(this, "priceCache", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: {}
61
+ });
18
62
  this.network = network;
19
63
  this.consts = (0, consts_1.getConsts)(network);
20
64
  this.connectionURL = connectionURL;
@@ -132,4 +176,3 @@ async function generatePriceIdToObjectIdJson() {
132
176
  return ret;
133
177
  }
134
178
  // generatePriceIdToObjectIdJson()
135
- //# sourceMappingURL=oracle.js.map
@@ -1,9 +1,9 @@
1
- import type { SuiClient } from '@mysten/sui/client';
2
- import { Transaction } from '@mysten/sui/transactions';
3
- import type { PriceFeed } from '@pythnetwork/pyth-sui-js';
4
- import { SuiPythClient } from '@pythnetwork/pyth-sui-js';
5
- import type { IConsts } from './consts';
6
- import { Network } from './consts';
1
+ import type { SuiClient } from "@mysten/sui/client";
2
+ import { Transaction } from "@mysten/sui/transactions";
3
+ import type { PriceFeed } from "@pythnetwork/pyth-sui-js";
4
+ import { SuiPythClient } from "@pythnetwork/pyth-sui-js";
5
+ import type { IConsts } from "./consts/index.cjs";
6
+ import { Network } from "./consts/index.cjs";
7
7
  export declare class OracleAPI {
8
8
  network: Network;
9
9
  consts: IConsts;
@@ -21,3 +21,4 @@ export declare class OracleAPI {
21
21
  initOracleTxb(tokens: string[], tx?: Transaction): Promise<Transaction>;
22
22
  }
23
23
  export declare function generatePriceIdToObjectIdJson(): Promise<any>;
24
+ //# sourceMappingURL=oracle.d.cts.map
@@ -0,0 +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;AAEnF,OAAO,KAAK,EAAE,OAAO,EAAE,2BAAgB;AACvC,OAAO,EAAgF,OAAO,EAAE,2BAAgB;AAIhH,qBAAa,SAAS;IACpB,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,OAAO,CAAA;IACf,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;IAWvB,aAAa;IASP,cAAc,CAAC,OAAO,EAAE,MAAM;IAa9B,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE;IAahC,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI;IAmBtE,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,EAAE,WAAW;CAsCvD;AAED,wBAAsB,6BAA6B,iBA4BlD"}
@@ -0,0 +1,24 @@
1
+ import type { SuiClient } from "@mysten/sui/client";
2
+ import { Transaction } from "@mysten/sui/transactions";
3
+ import type { PriceFeed } from "@pythnetwork/pyth-sui-js";
4
+ import { SuiPythClient } from "@pythnetwork/pyth-sui-js";
5
+ import type { IConsts } from "./consts/index.mjs";
6
+ import { Network } from "./consts/index.mjs";
7
+ export declare class OracleAPI {
8
+ network: Network;
9
+ consts: IConsts;
10
+ connectionURL: string;
11
+ PythFeederToPriceId: Record<string, string>;
12
+ PythFeederToId: Record<string, string>;
13
+ provider: SuiClient;
14
+ client: SuiPythClient;
15
+ private priceCache;
16
+ constructor(network: Network, provider: SuiClient | null, connectionURL: string);
17
+ validateCache(): void;
18
+ getOraclePrice(tokenId: string): Promise<PriceFeed>;
19
+ getOraclePrices(tokens: string[]): Promise<PriceFeed[] | undefined>;
20
+ subOraclePrices(tokens: string[], callback: (price: PriceFeed) => void): Promise<() => void>;
21
+ initOracleTxb(tokens: string[], tx?: Transaction): Promise<Transaction>;
22
+ }
23
+ export declare function generatePriceIdToObjectIdJson(): Promise<any>;
24
+ //# sourceMappingURL=oracle.d.mts.map
@@ -0,0 +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;AAEnF,OAAO,KAAK,EAAE,OAAO,EAAE,2BAAgB;AACvC,OAAO,EAAgF,OAAO,EAAE,2BAAgB;AAIhH,qBAAa,SAAS;IACpB,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,OAAO,CAAA;IACf,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;IAWvB,aAAa;IASP,cAAc,CAAC,OAAO,EAAE,MAAM;IAa9B,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE;IAahC,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI;IAmBtE,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,EAAE,WAAW;CAsCvD;AAED,wBAAsB,6BAA6B,iBA4BlD"}
@@ -0,0 +1,173 @@
1
+ import { Transaction } from "@mysten/sui/transactions";
2
+ import { SuiPriceServiceConnection, SuiPythClient } from "@pythnetwork/pyth-sui-js";
3
+ import { getConsts, getPriceIdToPythFeeder, getPythFeederToId, getPythFeederToPriceId, Network } from "./consts/index.mjs";
4
+ import jsonFile from "./consts/price_id_to_object_id.mainnet.json" with { type: "json" };
5
+ import { createJsonRpcProvider } from "./utils.mjs";
6
+ export class OracleAPI {
7
+ constructor(network, provider,
8
+ // todo 需要从配置文件中获取
9
+ connectionURL) {
10
+ Object.defineProperty(this, "network", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ Object.defineProperty(this, "consts", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ Object.defineProperty(this, "connectionURL", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ Object.defineProperty(this, "PythFeederToPriceId", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ Object.defineProperty(this, "PythFeederToId", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ Object.defineProperty(this, "provider", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: void 0
45
+ });
46
+ Object.defineProperty(this, "client", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: void 0
51
+ });
52
+ Object.defineProperty(this, "priceCache", {
53
+ enumerable: true,
54
+ configurable: true,
55
+ writable: true,
56
+ value: {}
57
+ });
58
+ this.network = network;
59
+ this.consts = getConsts(network);
60
+ this.connectionURL = connectionURL;
61
+ this.PythFeederToPriceId = getPythFeederToPriceId(network);
62
+ this.PythFeederToId = getPythFeederToId(network);
63
+ this.provider = provider || createJsonRpcProvider(network);
64
+ this.client = new SuiPythClient(this.provider, this.consts.pythFeeder.state, this.consts.pythFeeder.wormhole.state);
65
+ }
66
+ validateCache() {
67
+ const now = Date.now() / 1000;
68
+ for (const key in this.priceCache) {
69
+ if (now - (this.priceCache[key].getPriceUnchecked().publishTime || 0) > 7) {
70
+ delete this.priceCache[key];
71
+ }
72
+ }
73
+ }
74
+ async getOraclePrice(tokenId) {
75
+ this.validateCache();
76
+ if (this.priceCache[tokenId]) {
77
+ return this.priceCache[tokenId];
78
+ }
79
+ const res = await this.getOraclePrices([tokenId]);
80
+ if (!res || !res[0]) {
81
+ throw new Error(`Unknown token: ${tokenId}`);
82
+ }
83
+ this.priceCache[tokenId] = res[0];
84
+ return res[0];
85
+ }
86
+ async getOraclePrices(tokens) {
87
+ const connection = new SuiPriceServiceConnection(this.connectionURL, {
88
+ priceFeedRequestConfig: {
89
+ binary: true,
90
+ },
91
+ });
92
+ const pythObjectIds = tokens.map(token => this.consts.pythFeeder.feeder[token]);
93
+ const priceFeedIds = pythObjectIds.map(pythObjectId => `0x${this.PythFeederToPriceId[pythObjectId]}`);
94
+ const price = await connection.getLatestPriceFeeds(priceFeedIds);
95
+ return price;
96
+ }
97
+ // memory leak
98
+ async subOraclePrices(tokens, callback) {
99
+ const connection = new SuiPriceServiceConnection(this.connectionURL, {
100
+ priceFeedRequestConfig: {
101
+ binary: true,
102
+ },
103
+ });
104
+ const pythObjectIds = tokens.map(token => this.consts.pythFeeder.feeder[token]);
105
+ const priceFeedIds = pythObjectIds.map(pythObjectId => `0x${this.PythFeederToPriceId[pythObjectId]}`);
106
+ await connection.subscribePriceFeedUpdates(priceFeedIds, (price) => {
107
+ price.id = this.PythFeederToId[getPriceIdToPythFeeder(this.network)[price.id]];
108
+ this.priceCache[price.id] = price;
109
+ callback(price);
110
+ });
111
+ return () => {
112
+ connection.unsubscribePriceFeedUpdates(priceFeedIds);
113
+ };
114
+ }
115
+ async initOracleTxb(tokens, tx) {
116
+ let tx_ = tx;
117
+ if (!tx_) {
118
+ tx_ = new Transaction();
119
+ }
120
+ // Remove redundant tokens first
121
+ const uniqueTokens = [...new Set(tokens)];
122
+ const connection = new SuiPriceServiceConnection(this.connectionURL, {
123
+ priceFeedRequestConfig: {
124
+ binary: true,
125
+ },
126
+ });
127
+ const pythObjectIds = uniqueTokens.map(token => this.consts.pythFeeder.feeder[token]);
128
+ const needUpdateObjectIds = (await this.provider.multiGetObjects({
129
+ ids: pythObjectIds,
130
+ options: {
131
+ showContent: true,
132
+ },
133
+ })).map(pythObject => [
134
+ Number.parseInt((pythObject.data?.content).fields.price_info.fields.arrival_time || 0, 10) - Date.now() / 1000,
135
+ pythObject.data?.objectId,
136
+ ]).filter((x) => Math.abs(x[0]) >= 0).map(x => x[1]);
137
+ if (needUpdateObjectIds.length === 0) {
138
+ return tx_;
139
+ }
140
+ const priceFeedIds = needUpdateObjectIds
141
+ .map(pythObjectId => this.PythFeederToPriceId[pythObjectId])
142
+ .filter(Boolean)
143
+ .map(x => `0x${x}`);
144
+ const priceUpdateData = await connection.getPriceFeedsUpdateData(priceFeedIds);
145
+ await this.client.updatePriceFeeds(tx_, priceUpdateData, priceFeedIds);
146
+ return tx_;
147
+ }
148
+ }
149
+ export async function generatePriceIdToObjectIdJson() {
150
+ const provider = createJsonRpcProvider(Network.MAINNET);
151
+ const wormholeStateId = '0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c';
152
+ const pythStateId = '0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8';
153
+ const client = new SuiPythClient(provider, pythStateId, wormholeStateId);
154
+ const ret = {};
155
+ const entries = Object.keys(jsonFile);
156
+ // 使用 Promise.all 并行处理所有请求
157
+ await Promise.all(entries.map(async (key) => {
158
+ try {
159
+ const pythObjectId = await client.getPriceFeedObjectId(key);
160
+ if (pythObjectId) {
161
+ ret[key] = pythObjectId;
162
+ }
163
+ else {
164
+ console.warn(`Failed to get pythObjectId for key: ${key}`);
165
+ }
166
+ }
167
+ catch (error) {
168
+ console.error(`Error getting pythObjectId for key ${key}:`, error);
169
+ }
170
+ }));
171
+ return ret;
172
+ }
173
+ // generatePriceIdToObjectIdJson()
@@ -15,7 +15,7 @@ exports.suiSymbolToSymbol = suiSymbolToSymbol;
15
15
  exports.base64ToUint8Array = base64ToUint8Array;
16
16
  const client_1 = require("@mysten/sui/client");
17
17
  const hermes_client_1 = require("@pythnetwork/hermes-client");
18
- const consts_1 = require("./consts");
18
+ const consts_1 = require("./consts/index.cjs");
19
19
  function createJsonRpcProvider(network) {
20
20
  let url = '';
21
21
  switch (network) {
@@ -141,4 +141,3 @@ function base64ToUint8Array(base64) {
141
141
  }
142
142
  return uint8Array;
143
143
  }
144
- //# sourceMappingURL=utils.js.map
@@ -1,7 +1,7 @@
1
- import { SuiClient } from '@mysten/sui/client';
2
- import { HermesClient } from '@pythnetwork/hermes-client';
3
- import type { IConsts } from './consts';
4
- import { Network } from './consts';
1
+ import { SuiClient } from "@mysten/sui/client";
2
+ import { HermesClient } from "@pythnetwork/hermes-client";
3
+ import type { IConsts } from "./consts/index.cjs";
4
+ import { Network } from "./consts/index.cjs";
5
5
  export declare function createJsonRpcProvider(network: Network): SuiClient;
6
6
  export declare function createPythConnection(network: Network): HermesClient;
7
7
  export declare function decimalToObject(decimal: {
@@ -43,3 +43,4 @@ export declare function joinSymbol(direction: string, token: string): string;
43
43
  export declare function suiSymbolToSymbol(symbol: string, consts: IConsts): string;
44
44
  export declare function base64ToUint8Array(base64: string): Uint8Array<ArrayBuffer>;
45
45
  export {};
46
+ //# sourceMappingURL=utils.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAoB,2BAA0B;AAChE,OAAO,EAAE,YAAY,EAAE,mCAAkC;AAEzD,OAAO,KAAK,EAAE,OAAO,EAAE,2BAAgB;AACvC,OAAO,EAAE,OAAO,EAAE,2BAAgB;AAElC,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,aAuBrD;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,OAAO,gBAiBpD;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,UAEzD;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,UAEnD;AAED,UAAU,KAAK;IACb,MAAM,EAAE;QACN,WAAW,EAAE,OAAO,CAAA;QACpB,KAAK,EAAE;YACL,MAAM,EAAE;gBACN,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;aACvB,CAAA;SACF,CAAA;KACF,CAAA;CACF;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,UAGzC;AAED,UAAU,UAAU;IAClB,MAAM,EAAE;QACN,WAAW,EAAE,OAAO,CAAA;QACpB,KAAK,EAAE;YACL,MAAM,EAAE;gBACN,KAAK,EAAE,MAAM,CAAA;aACd,CAAA;SACF,CAAA;KACF,CAAA;CACF;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,UAAU,UAGpD;AAGD,wBAAgB,UAAU,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM,CAc7C;AAED,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,MAAM,IAAI;KAC9C,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,GAChC,MAAM,CAAC,GACP,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,GAAG,MAAM,CAAC,GAC5B,CAAC,GACD,KAAK;CACZ,CAAA;AAED,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAW7E;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAqBtD;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAExD;AAED,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAEnE;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CASzE;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,2BAYhD"}
@@ -0,0 +1,46 @@
1
+ import { SuiClient } from "@mysten/sui/client";
2
+ import { HermesClient } from "@pythnetwork/hermes-client";
3
+ import type { IConsts } from "./consts/index.mjs";
4
+ import { Network } from "./consts/index.mjs";
5
+ export declare function createJsonRpcProvider(network: Network): SuiClient;
6
+ export declare function createPythConnection(network: Network): HermesClient;
7
+ export declare function decimalToObject(decimal: {
8
+ value: string;
9
+ }): number;
10
+ export declare function rateToObject(rate: {
11
+ value: string;
12
+ }): number;
13
+ interface SRate {
14
+ fields: {
15
+ is_positive: boolean;
16
+ value: {
17
+ fields: {
18
+ value: string | number;
19
+ };
20
+ };
21
+ };
22
+ }
23
+ export declare function sRateToObject(sRate: SRate): number;
24
+ interface SuiDecimal {
25
+ fields: {
26
+ is_positive: boolean;
27
+ value: {
28
+ fields: {
29
+ value: string;
30
+ };
31
+ };
32
+ };
33
+ }
34
+ export declare function sDecimalToObject(sDecimal: SuiDecimal): number;
35
+ export declare function parseValue(field: any): number;
36
+ export type ReversedKeyValue<T extends object> = {
37
+ [K in keyof T]: T[K] extends keyof T ? keyof T : T[K] extends keyof T | infer V ? V : never;
38
+ };
39
+ export declare function reverseKeyValue<T extends object>(obj: T): ReversedKeyValue<T>;
40
+ export declare function parseSymbolKey(input: string): string[];
41
+ export declare function upperFirstCharacter(word: string): string;
42
+ export declare function joinSymbol(direction: string, token: string): string;
43
+ export declare function suiSymbolToSymbol(symbol: string, consts: IConsts): string;
44
+ export declare function base64ToUint8Array(base64: string): Uint8Array<ArrayBuffer>;
45
+ export {};
46
+ //# sourceMappingURL=utils.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAoB,2BAA0B;AAChE,OAAO,EAAE,YAAY,EAAE,mCAAkC;AAEzD,OAAO,KAAK,EAAE,OAAO,EAAE,2BAAgB;AACvC,OAAO,EAAE,OAAO,EAAE,2BAAgB;AAElC,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,aAuBrD;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,OAAO,gBAiBpD;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,UAEzD;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,UAEnD;AAED,UAAU,KAAK;IACb,MAAM,EAAE;QACN,WAAW,EAAE,OAAO,CAAA;QACpB,KAAK,EAAE;YACL,MAAM,EAAE;gBACN,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;aACvB,CAAA;SACF,CAAA;KACF,CAAA;CACF;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,UAGzC;AAED,UAAU,UAAU;IAClB,MAAM,EAAE;QACN,WAAW,EAAE,OAAO,CAAA;QACpB,KAAK,EAAE;YACL,MAAM,EAAE;gBACN,KAAK,EAAE,MAAM,CAAA;aACd,CAAA;SACF,CAAA;KACF,CAAA;CACF;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,UAAU,UAGpD;AAGD,wBAAgB,UAAU,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM,CAc7C;AAED,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,MAAM,IAAI;KAC9C,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,GAChC,MAAM,CAAC,GACP,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,GAAG,MAAM,CAAC,GAC5B,CAAC,GACD,KAAK;CACZ,CAAA;AAED,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAW7E;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAqBtD;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAExD;AAED,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAEnE;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CASzE;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,2BAYhD"}
package/dist/utils.mjs ADDED
@@ -0,0 +1,128 @@
1
+ import { SuiClient, SuiHTTPTransport } from "@mysten/sui/client";
2
+ import { HermesClient } from "@pythnetwork/hermes-client";
3
+ import { Network } from "./consts/index.mjs";
4
+ export function createJsonRpcProvider(network) {
5
+ let url = '';
6
+ switch (network) {
7
+ case Network.DEVNET: {
8
+ url = 'https://explorer-rpc.devnet.sui.io/';
9
+ break;
10
+ }
11
+ case Network.TESTNET: {
12
+ url = 'https://sui-testnet.blockvision.org/v1/2sXCJEJNOCdIJhLaDtyhKzqwn6k';
13
+ break;
14
+ }
15
+ case Network.MAINNET: {
16
+ url = 'https://rpc-mainnet.suiscan.xyz';
17
+ break;
18
+ }
19
+ default: {
20
+ url = 'https://explorer-rpc.devnet.sui.io/';
21
+ }
22
+ }
23
+ return new SuiClient({
24
+ transport: new SuiHTTPTransport({ url }),
25
+ });
26
+ }
27
+ export function createPythConnection(network) {
28
+ let priceConnectionUrl;
29
+ switch (network) {
30
+ case Network.TESTNET: {
31
+ priceConnectionUrl = 'https://xc-testnet.pyth.network';
32
+ break;
33
+ }
34
+ case Network.MAINNET: {
35
+ priceConnectionUrl = 'https://xc-mainnet.pyth.network';
36
+ break;
37
+ }
38
+ default: {
39
+ priceConnectionUrl = 'https://xc-testnet.pyth.network';
40
+ }
41
+ }
42
+ return new HermesClient(priceConnectionUrl);
43
+ }
44
+ export function decimalToObject(decimal) {
45
+ return Number(BigInt(decimal.value)) / 1e18;
46
+ }
47
+ export function rateToObject(rate) {
48
+ return Number(BigInt(rate.value)) / 1e18;
49
+ }
50
+ export function sRateToObject(sRate) {
51
+ const sign = sRate.fields.is_positive ? 1 : -1;
52
+ return Number(BigInt(sRate.fields.value.fields.value)) / 1e18 * sign;
53
+ }
54
+ export function sDecimalToObject(sDecimal) {
55
+ const sign = sDecimal.fields.is_positive ? 1 : -1;
56
+ return Number(BigInt(sDecimal.fields.value.fields.value)) / 1e18 * sign;
57
+ }
58
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
+ export function parseValue(field) {
60
+ if (field.type && field.type.endsWith('::decimal::Decimal')) {
61
+ return decimalToObject({ value: field.fields.value });
62
+ }
63
+ else if (field.type && field.type.endsWith('::rate::Rate')) {
64
+ return rateToObject({ value: field.fields.value });
65
+ }
66
+ else if (field.type && field.type.endsWith('::srate::SRate')) {
67
+ return sRateToObject(field);
68
+ }
69
+ else if (field.type && field.type.endsWith('::sdecimal::SDecimal')) {
70
+ return sDecimalToObject(field);
71
+ }
72
+ return Number.parseInt(field, 10);
73
+ }
74
+ export function reverseKeyValue(obj) {
75
+ const reversed = {};
76
+ for (const key in obj) {
77
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
78
+ const value = obj[key];
79
+ reversed[value] = key;
80
+ }
81
+ }
82
+ return reversed;
83
+ }
84
+ export function parseSymbolKey(input) {
85
+ // This regex will match uppercase letters
86
+ const regex = /[A-Z]/;
87
+ let result = [];
88
+ let wordStart = 0;
89
+ for (let i = 1; i < input.length; i += 1) {
90
+ if (regex.test(input[i])) {
91
+ // Found an uppercase letter, so we split the string here
92
+ result.push(input.slice(wordStart, i));
93
+ wordStart = i;
94
+ }
95
+ }
96
+ // Add the last word to the result array
97
+ result.push(input.slice(wordStart));
98
+ // Convert the words to lowercase
99
+ result = result.map(word => word.toLowerCase());
100
+ return result;
101
+ }
102
+ export function upperFirstCharacter(word) {
103
+ return word.charAt(0).toUpperCase() + word.slice(1);
104
+ }
105
+ export function joinSymbol(direction, token) {
106
+ return `${direction}${upperFirstCharacter(token)}`;
107
+ }
108
+ export function suiSymbolToSymbol(symbol, consts) {
109
+ if (symbol === '0x2::sui::SUI') {
110
+ return 'sui';
111
+ }
112
+ const ret = {};
113
+ for (const key of Object.keys(consts.coins)) {
114
+ ret[consts.coins[key].module] = key;
115
+ }
116
+ return ret[symbol];
117
+ }
118
+ export function base64ToUint8Array(base64) {
119
+ // Decode Base64 to binary
120
+ const binary = atob(base64);
121
+ // Create a Uint8Array from the binary data
122
+ const uint8Array = new Uint8Array(binary.length);
123
+ // Populate the Uint8Array with the binary data
124
+ for (let i = 0; i < binary.length; i += 1) {
125
+ uint8Array[i] = binary.codePointAt(i);
126
+ }
127
+ return uint8Array;
128
+ }
package/package.json CHANGED
@@ -1,21 +1,35 @@
1
1
  {
2
2
  "name": "zo-sdk",
3
- "version": "0.0.13",
4
- "main": "dist/index.js",
3
+ "version": "0.0.14",
4
+ "exports": {
5
+ ".": {
6
+ "import": {
7
+ "types": "./dist/index.d.mts",
8
+ "default": "./dist/index.mjs"
9
+ },
10
+ "require": {
11
+ "types": "./dist/index.d.cts",
12
+ "default": "./dist/index.cjs"
13
+ }
14
+ }
15
+ },
16
+ "main": "./dist/index.cjs",
17
+ "module": "./dist/index.mjs",
5
18
  "types": "dist/index.d.ts",
6
19
  "scripts": {
7
- "start": "ts-node src/index.ts",
8
- "build": "tsc",
9
- "serve": "node dist/index.js",
10
- "pub": "tsc && npm publish",
20
+ "build": "ts-bridge build --project ./tsconfig.json --clean",
21
+ "serve": "tsx dist/index.js",
22
+ "start": "tsx src/index.ts",
23
+ "pub": "pnpm build && npm publish",
11
24
  "test": "vitest"
12
25
  },
13
26
  "keywords": [],
14
27
  "author": "",
15
28
  "license": "MIT",
16
29
  "devDependencies": {
17
- "ts-node": "^10.9.1",
18
- "typescript": "^5.1.6"
30
+ "@ts-bridge/cli": "^0.6.3",
31
+ "tsx": "^4.19.3",
32
+ "typescript": "^5.8.2"
19
33
  },
20
34
  "dependencies": {
21
35
  "@mysten/bcs": "0.7.1",
@@ -23,9 +37,5 @@
23
37
  "@pythnetwork/hermes-client": "^2.0.0",
24
38
  "@pythnetwork/pyth-sui-js": "2.1.0",
25
39
  "decimal.js": "10.4.3"
26
- },
27
- "resolutions": {
28
- "typescript": "5.1.6"
29
- },
30
- "packageManager": "yarn@1.22.1"
40
+ }
31
41
  }
@@ -63,7 +63,7 @@ export interface IConsts {
63
63
  }
64
64
 
65
65
  function toCamelCase(str: string): string {
66
- return str.replace(/_([a-z])/g, (match, letter) => letter.toUpperCase());
66
+ return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
67
67
  }
68
68
 
69
69
  function parse(obj: any): any {