|
| 1 | +import { SuiClient } from "@mysten/sui/client"; |
| 2 | +import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519"; |
| 3 | +import { Transaction } from "@mysten/sui/transactions"; |
| 4 | +import type { Request as SubscriptionRequest } from "@pythnetwork/pyth-lazer-sdk"; |
| 5 | +import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk"; |
| 6 | +import yargs from "yargs"; |
| 7 | +import { hideBin } from "yargs/helpers"; |
| 8 | + |
| 9 | +import { addParseAndVerifyLeEcdsaUpdateCall } from "../src/client.js"; |
| 10 | + |
| 11 | +async function getOneLeEcdsaUpdate(urls: string[], token: string) { |
| 12 | + const lazer = await PythLazerClient.create({ |
| 13 | + urls, |
| 14 | + token, |
| 15 | + numConnections: 1, |
| 16 | + }); |
| 17 | + |
| 18 | + const subscription: SubscriptionRequest = { |
| 19 | + subscriptionId: 1, |
| 20 | + type: "subscribe", |
| 21 | + priceFeedIds: [1], |
| 22 | + properties: ["price", "bestBidPrice", "bestAskPrice", "exponent"], |
| 23 | + formats: ["leEcdsa"], |
| 24 | + channel: "fixed_rate@200ms", |
| 25 | + deliveryFormat: "binary", |
| 26 | + jsonBinaryEncoding: "hex", |
| 27 | + }; |
| 28 | + |
| 29 | + lazer.subscribe(subscription); |
| 30 | + |
| 31 | + return new Promise<Uint8Array>((resolve) => { |
| 32 | + lazer.addMessageListener((event) => { |
| 33 | + if (event.type === "binary" && event.value.leEcdsa) { |
| 34 | + const buf = event.value.leEcdsa; |
| 35 | + |
| 36 | + // For the purposes of this example, we only need one update. |
| 37 | + lazer.shutdown(); |
| 38 | + resolve(buf); |
| 39 | + } |
| 40 | + }); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +async function main() { |
| 45 | + const args = await yargs(hideBin(process.argv)) |
| 46 | + .option("fullnodeUrl", { |
| 47 | + type: "string", |
| 48 | + description: |
| 49 | + "URL of the full Sui node RPC endpoint. e.g: https://fullnode.testnet.sui.io:443", |
| 50 | + demandOption: true, |
| 51 | + }) |
| 52 | + .option("packageId", { |
| 53 | + type: "string", |
| 54 | + description: "Lazer contract package ID", |
| 55 | + demandOption: true, |
| 56 | + }) |
| 57 | + .option("stateObjectId", { |
| 58 | + type: "string", |
| 59 | + description: "Lazer contract shared State object ID", |
| 60 | + demandOption: true, |
| 61 | + }) |
| 62 | + .option("lazerUrls", { |
| 63 | + type: "array", |
| 64 | + string: true, |
| 65 | + description: "Lazer WebSocket URLs", |
| 66 | + default: [ |
| 67 | + "wss://pyth-lazer-0.dourolabs.app/v1/stream", |
| 68 | + "wss://pyth-lazer-1.dourolabs.app/v1/stream", |
| 69 | + ], |
| 70 | + }) |
| 71 | + .option("lazerToken", { |
| 72 | + type: "string", |
| 73 | + description: "Lazer authentication token", |
| 74 | + demandOption: true, |
| 75 | + }) |
| 76 | + .help() |
| 77 | + .parseAsync(); |
| 78 | + |
| 79 | + // Defined as a dependency in turbo.json |
| 80 | + // eslint-disable-next-line n/no-process-env |
| 81 | + if (process.env.SUI_KEY === undefined) { |
| 82 | + throw new Error( |
| 83 | + `SUI_KEY environment variable should be set to your Sui private key in hex format.`, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + const provider = new SuiClient({ url: args.fullnodeUrl }); |
| 88 | + |
| 89 | + // Fetch the price update |
| 90 | + const updateBytes = await getOneLeEcdsaUpdate( |
| 91 | + args.lazerUrls, |
| 92 | + args.lazerToken, |
| 93 | + ); |
| 94 | + |
| 95 | + // Build the Sui transaction |
| 96 | + const tx = new Transaction(); |
| 97 | + |
| 98 | + // Add the parse and verify call |
| 99 | + addParseAndVerifyLeEcdsaUpdateCall({ |
| 100 | + tx, |
| 101 | + packageId: args.packageId, |
| 102 | + stateObjectId: args.stateObjectId, |
| 103 | + updateBytes, |
| 104 | + }); |
| 105 | + |
| 106 | + // --- You can add more calls to the transaction that consume the parsed update here --- |
| 107 | + |
| 108 | + const wallet = Ed25519Keypair.fromSecretKey( |
| 109 | + // eslint-disable-next-line n/no-process-env |
| 110 | + Buffer.from(process.env.SUI_KEY, "hex"), |
| 111 | + ); |
| 112 | + const res = await provider.signAndExecuteTransaction({ |
| 113 | + signer: wallet, |
| 114 | + transaction: tx, |
| 115 | + options: { showEffects: true, showEvents: true }, |
| 116 | + }); |
| 117 | + |
| 118 | + // eslint-disable-next-line no-console |
| 119 | + console.log("Execution result:", JSON.stringify(res, undefined, 2)); |
| 120 | +} |
| 121 | + |
| 122 | +// eslint-disable-next-line unicorn/prefer-top-level-await |
| 123 | +main().catch((error: unknown) => { |
| 124 | + throw error; |
| 125 | +}); |
0 commit comments