@pythnetwork/pyth-sui-js
Version:
Pyth Network Sui Utilities
98 lines (97 loc) • 3.57 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProvider = getProvider;
const yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
const client_1 = require("@mysten/sui/client");
const transactions_1 = require("@mysten/sui/transactions");
const ed25519_1 = require("@mysten/sui/keypairs/ed25519");
const buffer_1 = require("buffer");
const client_2 = require("../client");
const index_1 = require("../index");
const argvPromise = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
.option("feed-id", {
description: "Price feed ids to update without the leading 0x (e.g f9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b). Can be provided multiple times for multiple feed updates",
string: true,
type: "array",
demandOption: true,
})
.option("hermes", {
description: "Endpoint URL for Hermes. e.g: https://hermes.pyth.network",
type: "string",
demandOption: true,
})
.option("full-node", {
description: "URL of the full Sui node RPC endpoint. e.g: https://fullnode.testnet.sui.io:443",
type: "string",
demandOption: true,
})
.option("pyth-state-id", {
description: "Pyth state object id.",
type: "string",
demandOption: true,
})
.option("wormhole-state-id", {
description: "Wormhole state object id.",
type: "string",
demandOption: true,
}).argv;
function getProvider(url) {
return new client_1.SuiClient({ url });
}
async function run() {
if (process.env.SUI_KEY === undefined) {
throw new Error(`SUI_KEY environment variable should be set.`);
}
const argv = await argvPromise;
// Fetch the latest price feed update data from the Price Service
const connection = new index_1.SuiPriceServiceConnection(argv["hermes"]);
const feeds = argv["feed-id"];
if (!Array.isArray(feeds)) {
throw new Error("Not a valid input!");
}
const provider = getProvider(argv["full-node"]);
const wormholeStateId = argv["wormhole-state-id"];
const pythStateId = argv["pyth-state-id"];
const client = new client_2.SuiPythClient(provider, pythStateId, wormholeStateId);
const newFeeds = [];
const existingFeeds = [];
for (const feed of feeds) {
if (typeof feed !== "string") {
throw new Error(`Not a valid string input ${feed}`);
}
if ((await client.getPriceFeedObjectId(feed)) == undefined) {
newFeeds.push(feed);
}
else {
existingFeeds.push(feed);
}
}
console.log({
newFeeds,
existingFeeds,
});
const tx = new transactions_1.Transaction();
if (existingFeeds.length > 0) {
const updateData = await connection.getPriceFeedsUpdateData(existingFeeds);
await client.updatePriceFeeds(tx, updateData, existingFeeds);
}
if (newFeeds.length > 0) {
const updateData = await connection.getPriceFeedsUpdateData(newFeeds);
await client.createPriceFeed(tx, updateData);
}
const wallet = ed25519_1.Ed25519Keypair.fromSecretKey(buffer_1.Buffer.from(process.env.SUI_KEY, "hex"));
const result = await provider.signAndExecuteTransaction({
signer: wallet,
transaction: tx,
options: {
showEffects: true,
showEvents: true,
},
});
console.dir(result, { depth: null });
}
run();