zo-sdk 0.0.4 → 0.0.5
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/.gitattributes +4 -0
- package/.prettierrc.js +5 -0
- package/babel.config.js +11 -0
- package/dist/api.cjs +466 -0
- package/dist/api.d.cts +26 -0
- package/dist/api.d.cts.map +1 -0
- package/dist/api.d.mts +26 -0
- package/dist/api.d.mts.map +1 -0
- package/dist/api.mjs +462 -0
- package/dist/consts/deployments-mainnet.json +28 -0
- package/dist/consts/deployments-testnet.json +93 -0
- package/dist/consts/index.cjs +101 -0
- package/dist/consts/index.d.cts +66 -0
- package/dist/consts/index.d.cts.map +1 -0
- package/dist/consts/index.d.mts +66 -0
- package/dist/consts/index.d.mts.map +1 -0
- package/dist/consts/index.mjs +94 -0
- package/dist/consts/price_id_to_object_id.mainnet.json +1 -0
- package/dist/consts/price_id_to_object_id.testnet.json +17 -0
- package/dist/consts/staking/deployments-mainnet.json +12 -0
- package/dist/consts/staking/deployments-testnet.json +11 -0
- package/dist/data.cjs +844 -0
- package/dist/data.d.cts +221 -0
- package/dist/data.d.cts.map +1 -0
- package/dist/data.d.mts +221 -0
- package/dist/data.d.mts.map +1 -0
- package/dist/data.mjs +840 -0
- package/dist/index.cjs +21 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +5 -0
- package/dist/oracle.cjs +178 -0
- package/dist/oracle.d.cts +24 -0
- package/dist/oracle.d.cts.map +1 -0
- package/dist/oracle.d.mts +24 -0
- package/dist/oracle.d.mts.map +1 -0
- package/dist/oracle.mjs +173 -0
- package/dist/utils.cjs +144 -0
- package/dist/utils.d.cts +46 -0
- package/dist/utils.d.cts.map +1 -0
- package/dist/utils.d.mts +46 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +129 -0
- package/package.json +3 -2
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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);
|
package/dist/index.d.cts
ADDED
|
@@ -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"}
|
package/dist/index.d.mts
ADDED
|
@@ -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
package/dist/oracle.cjs
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OracleAPI = void 0;
|
|
4
|
+
exports.generatePriceIdToObjectIdJson = generatePriceIdToObjectIdJson;
|
|
5
|
+
const transactions_1 = require("@mysten/sui/transactions");
|
|
6
|
+
const pyth_sui_js_1 = require("@pythnetwork/pyth-sui-js");
|
|
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");
|
|
10
|
+
class OracleAPI {
|
|
11
|
+
constructor(network, provider,
|
|
12
|
+
// todo 需要从配置文件中获取
|
|
13
|
+
connectionURL) {
|
|
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
|
+
});
|
|
62
|
+
this.network = network;
|
|
63
|
+
this.consts = (0, consts_1.getConsts)(network);
|
|
64
|
+
this.connectionURL = connectionURL;
|
|
65
|
+
this.PythFeederToPriceId = (0, consts_1.getPythFeederToPriceId)(network);
|
|
66
|
+
this.PythFeederToId = (0, consts_1.getPythFeederToId)(network);
|
|
67
|
+
this.provider = provider || (0, utils_1.createJsonRpcProvider)(network);
|
|
68
|
+
this.client = new pyth_sui_js_1.SuiPythClient(this.provider, this.consts.pythFeeder.state, this.consts.pythFeeder.wormhole.state);
|
|
69
|
+
}
|
|
70
|
+
validateCache() {
|
|
71
|
+
const now = Date.now() / 1000;
|
|
72
|
+
for (const key in this.priceCache) {
|
|
73
|
+
if (now - (this.priceCache[key].getPriceUnchecked().publishTime || 0) > 7) {
|
|
74
|
+
delete this.priceCache[key];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async getOraclePrice(tokenId) {
|
|
79
|
+
this.validateCache();
|
|
80
|
+
if (this.priceCache[tokenId]) {
|
|
81
|
+
return this.priceCache[tokenId];
|
|
82
|
+
}
|
|
83
|
+
const res = await this.getOraclePrices([tokenId]);
|
|
84
|
+
if (!res || !res[0]) {
|
|
85
|
+
throw new Error(`Unknown token: ${tokenId}`);
|
|
86
|
+
}
|
|
87
|
+
this.priceCache[tokenId] = res[0];
|
|
88
|
+
return res[0];
|
|
89
|
+
}
|
|
90
|
+
async getOraclePrices(tokens) {
|
|
91
|
+
const connection = new pyth_sui_js_1.SuiPriceServiceConnection(this.connectionURL, {
|
|
92
|
+
priceFeedRequestConfig: {
|
|
93
|
+
binary: true,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
const pythObjectIds = tokens.map(token => this.consts.pythFeeder.feeder[token]);
|
|
97
|
+
const priceFeedIds = pythObjectIds.map(pythObjectId => `0x${this.PythFeederToPriceId[pythObjectId]}`);
|
|
98
|
+
const price = await connection.getLatestPriceFeeds(priceFeedIds);
|
|
99
|
+
return price;
|
|
100
|
+
}
|
|
101
|
+
// memory leak
|
|
102
|
+
async subOraclePrices(tokens, callback) {
|
|
103
|
+
const connection = new pyth_sui_js_1.SuiPriceServiceConnection(this.connectionURL, {
|
|
104
|
+
priceFeedRequestConfig: {
|
|
105
|
+
binary: true,
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
const pythObjectIds = tokens.map(token => this.consts.pythFeeder.feeder[token]);
|
|
109
|
+
const priceFeedIds = pythObjectIds.map(pythObjectId => `0x${this.PythFeederToPriceId[pythObjectId]}`);
|
|
110
|
+
await connection.subscribePriceFeedUpdates(priceFeedIds, (price) => {
|
|
111
|
+
price.id = this.PythFeederToId[(0, consts_1.getPriceIdToPythFeeder)(this.network)[price.id]];
|
|
112
|
+
this.priceCache[price.id] = price;
|
|
113
|
+
callback(price);
|
|
114
|
+
});
|
|
115
|
+
return () => {
|
|
116
|
+
connection.unsubscribePriceFeedUpdates(priceFeedIds);
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
async initOracleTxb(tokens, tx) {
|
|
120
|
+
let tx_ = tx;
|
|
121
|
+
if (!tx_) {
|
|
122
|
+
tx_ = new transactions_1.Transaction();
|
|
123
|
+
}
|
|
124
|
+
// Remove redundant tokens first
|
|
125
|
+
const uniqueTokens = [...new Set(tokens)];
|
|
126
|
+
const connection = new pyth_sui_js_1.SuiPriceServiceConnection(this.connectionURL, {
|
|
127
|
+
priceFeedRequestConfig: {
|
|
128
|
+
binary: true,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
const pythObjectIds = uniqueTokens.map(token => this.consts.pythFeeder.feeder[token]);
|
|
132
|
+
const needUpdateObjectIds = (await this.provider.multiGetObjects({
|
|
133
|
+
ids: pythObjectIds,
|
|
134
|
+
options: {
|
|
135
|
+
showContent: true,
|
|
136
|
+
},
|
|
137
|
+
})).map(pythObject => [
|
|
138
|
+
Number.parseInt((pythObject.data?.content).fields.price_info.fields.arrival_time || 0, 10) - Date.now() / 1000,
|
|
139
|
+
pythObject.data?.objectId,
|
|
140
|
+
]).filter((x) => Math.abs(x[0]) >= 0).map(x => x[1]);
|
|
141
|
+
if (needUpdateObjectIds.length === 0) {
|
|
142
|
+
return tx_;
|
|
143
|
+
}
|
|
144
|
+
const priceFeedIds = needUpdateObjectIds
|
|
145
|
+
.map(pythObjectId => this.PythFeederToPriceId[pythObjectId])
|
|
146
|
+
.filter(Boolean)
|
|
147
|
+
.map(x => `0x${x}`);
|
|
148
|
+
const priceUpdateData = await connection.getPriceFeedsUpdateData(priceFeedIds);
|
|
149
|
+
await this.client.updatePriceFeeds(tx_, priceUpdateData, priceFeedIds);
|
|
150
|
+
return tx_;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
exports.OracleAPI = OracleAPI;
|
|
154
|
+
async function generatePriceIdToObjectIdJson() {
|
|
155
|
+
const provider = (0, utils_1.createJsonRpcProvider)(consts_1.Network.MAINNET);
|
|
156
|
+
const wormholeStateId = '0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c';
|
|
157
|
+
const pythStateId = '0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8';
|
|
158
|
+
const client = new pyth_sui_js_1.SuiPythClient(provider, pythStateId, wormholeStateId);
|
|
159
|
+
const ret = {};
|
|
160
|
+
const entries = Object.keys(price_id_to_object_id_mainnet_json_1.default);
|
|
161
|
+
// 使用 Promise.all 并行处理所有请求
|
|
162
|
+
await Promise.all(entries.map(async (key) => {
|
|
163
|
+
try {
|
|
164
|
+
const pythObjectId = await client.getPriceFeedObjectId(key);
|
|
165
|
+
if (pythObjectId) {
|
|
166
|
+
ret[key] = pythObjectId;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
console.warn(`Failed to get pythObjectId for key: ${key}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
console.error(`Error getting pythObjectId for key ${key}:`, error);
|
|
174
|
+
}
|
|
175
|
+
}));
|
|
176
|
+
return ret;
|
|
177
|
+
}
|
|
178
|
+
// generatePriceIdToObjectIdJson()
|
|
@@ -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.cjs";
|
|
6
|
+
import { Network } from "./consts/index.cjs";
|
|
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.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"}
|
package/dist/oracle.mjs
ADDED
|
@@ -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()
|
package/dist/utils.cjs
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createJsonRpcProvider = createJsonRpcProvider;
|
|
4
|
+
exports.createPythConnection = createPythConnection;
|
|
5
|
+
exports.decimalToObject = decimalToObject;
|
|
6
|
+
exports.rateToObject = rateToObject;
|
|
7
|
+
exports.sRateToObject = sRateToObject;
|
|
8
|
+
exports.sDecimalToObject = sDecimalToObject;
|
|
9
|
+
exports.parseValue = parseValue;
|
|
10
|
+
exports.reverseKeyValue = reverseKeyValue;
|
|
11
|
+
exports.parseSymbolKey = parseSymbolKey;
|
|
12
|
+
exports.upperFirstCharacter = upperFirstCharacter;
|
|
13
|
+
exports.joinSymbol = joinSymbol;
|
|
14
|
+
exports.suiSymbolToSymbol = suiSymbolToSymbol;
|
|
15
|
+
exports.base64ToUint8Array = base64ToUint8Array;
|
|
16
|
+
const client_1 = require("@mysten/sui/client");
|
|
17
|
+
const hermes_client_1 = require("@pythnetwork/hermes-client");
|
|
18
|
+
const consts_1 = require("./consts/index.cjs");
|
|
19
|
+
function createJsonRpcProvider(network) {
|
|
20
|
+
let url = '';
|
|
21
|
+
switch (network) {
|
|
22
|
+
case consts_1.Network.DEVNET: {
|
|
23
|
+
url = 'https://explorer-rpc.devnet.sui.io/';
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case consts_1.Network.TESTNET: {
|
|
27
|
+
url = 'https://sui-testnet.blockvision.org/v1/2sXCJEJNOCdIJhLaDtyhKzqwn6k';
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case consts_1.Network.MAINNET: {
|
|
31
|
+
url = 'https://rpc-mainnet.suiscan.xyz';
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
default: {
|
|
35
|
+
url = 'https://explorer-rpc.devnet.sui.io/';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return new client_1.SuiClient({
|
|
39
|
+
transport: new client_1.SuiHTTPTransport({ url }),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function createPythConnection(network) {
|
|
43
|
+
let priceConnectionUrl;
|
|
44
|
+
switch (network) {
|
|
45
|
+
case consts_1.Network.TESTNET: {
|
|
46
|
+
priceConnectionUrl = 'https://xc-testnet.pyth.network';
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
case consts_1.Network.MAINNET: {
|
|
50
|
+
priceConnectionUrl = 'https://xc-mainnet.pyth.network';
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
default: {
|
|
54
|
+
priceConnectionUrl = 'https://xc-testnet.pyth.network';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return new hermes_client_1.HermesClient(priceConnectionUrl);
|
|
58
|
+
}
|
|
59
|
+
function decimalToObject(decimal) {
|
|
60
|
+
return Number(BigInt(decimal.value)) / 1e18;
|
|
61
|
+
}
|
|
62
|
+
function rateToObject(rate) {
|
|
63
|
+
return Number(BigInt(rate.value)) / 1e18;
|
|
64
|
+
}
|
|
65
|
+
function sRateToObject(sRate) {
|
|
66
|
+
const sign = sRate.fields.is_positive ? 1 : -1;
|
|
67
|
+
return Number(BigInt(sRate.fields.value.fields.value)) / 1e18 * sign;
|
|
68
|
+
}
|
|
69
|
+
function sDecimalToObject(sDecimal) {
|
|
70
|
+
const sign = sDecimal.fields.is_positive ? 1 : -1;
|
|
71
|
+
return Number(BigInt(sDecimal.fields.value.fields.value)) / 1e18 * sign;
|
|
72
|
+
}
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
+
function parseValue(field) {
|
|
75
|
+
if (field.type && field.type.endsWith('::decimal::Decimal')) {
|
|
76
|
+
return decimalToObject({ value: field.fields.value });
|
|
77
|
+
}
|
|
78
|
+
else if (field.type && field.type.endsWith('::rate::Rate')) {
|
|
79
|
+
return rateToObject({ value: field.fields.value });
|
|
80
|
+
}
|
|
81
|
+
else if (field.type && field.type.endsWith('::srate::SRate')) {
|
|
82
|
+
return sRateToObject(field);
|
|
83
|
+
}
|
|
84
|
+
else if (field.type && field.type.endsWith('::sdecimal::SDecimal')) {
|
|
85
|
+
return sDecimalToObject(field);
|
|
86
|
+
}
|
|
87
|
+
return Number.parseInt(field, 10);
|
|
88
|
+
}
|
|
89
|
+
function reverseKeyValue(obj) {
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
+
const reversed = {};
|
|
92
|
+
for (const key in obj) {
|
|
93
|
+
if (Object.hasOwn(obj, key)) {
|
|
94
|
+
const value = obj[key];
|
|
95
|
+
reversed[value] = key;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return reversed;
|
|
99
|
+
}
|
|
100
|
+
function parseSymbolKey(input) {
|
|
101
|
+
// This regex will match uppercase letters
|
|
102
|
+
const regex = /[A-Z]/;
|
|
103
|
+
let result = [];
|
|
104
|
+
let wordStart = 0;
|
|
105
|
+
for (let i = 1; i < input.length; i += 1) {
|
|
106
|
+
if (regex.test(input[i])) {
|
|
107
|
+
// Found an uppercase letter, so we split the string here
|
|
108
|
+
result.push(input.slice(wordStart, i));
|
|
109
|
+
wordStart = i;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Add the last word to the result array
|
|
113
|
+
result.push(input.slice(wordStart));
|
|
114
|
+
// Convert the words to lowercase
|
|
115
|
+
result = result.map(word => word.toLowerCase());
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
function upperFirstCharacter(word) {
|
|
119
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
120
|
+
}
|
|
121
|
+
function joinSymbol(direction, token) {
|
|
122
|
+
return `${direction}${upperFirstCharacter(token)}`;
|
|
123
|
+
}
|
|
124
|
+
function suiSymbolToSymbol(symbol, consts) {
|
|
125
|
+
if (symbol === '0x2::sui::SUI') {
|
|
126
|
+
return 'sui';
|
|
127
|
+
}
|
|
128
|
+
const ret = {};
|
|
129
|
+
for (const key of Object.keys(consts.coins)) {
|
|
130
|
+
ret[consts.coins[key].module] = key;
|
|
131
|
+
}
|
|
132
|
+
return ret[symbol];
|
|
133
|
+
}
|
|
134
|
+
function base64ToUint8Array(base64) {
|
|
135
|
+
// Decode Base64 to binary
|
|
136
|
+
const binary = atob(base64);
|
|
137
|
+
// Create a Uint8Array from the binary data
|
|
138
|
+
const uint8Array = new Uint8Array(binary.length);
|
|
139
|
+
// Populate the Uint8Array with the binary data
|
|
140
|
+
for (let i = 0; i < binary.length; i += 1) {
|
|
141
|
+
uint8Array[i] = binary.codePointAt(i);
|
|
142
|
+
}
|
|
143
|
+
return uint8Array;
|
|
144
|
+
}
|
package/dist/utils.d.cts
ADDED
|
@@ -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.cjs";
|
|
4
|
+
import { Network } from "./consts/index.cjs";
|
|
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.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,CAY7E;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"}
|