|
| 1 | +import { Options } from "yargs"; |
| 2 | +import * as options from "../options"; |
| 3 | +import { readPriceConfigFile } from "../price-config"; |
| 4 | +import { PriceServiceConnection } from "@pythnetwork/price-service-client"; |
| 5 | +import { PythPriceListener } from "../pyth-price-listener"; |
| 6 | +import { SolanaPriceListener, SolanaPricePusher } from "./solana"; |
| 7 | +import { Controller } from "../controller"; |
| 8 | +import { PythSolanaReceiver } from "@pythnetwork/pyth-solana-receiver"; |
| 9 | +import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet"; |
| 10 | +import { Keypair, Connection } from "@solana/web3.js"; |
| 11 | +import fs from "fs"; |
| 12 | +import { PublicKey } from "@solana/web3.js"; |
| 13 | + |
| 14 | +export default { |
| 15 | + command: "solana", |
| 16 | + describe: "run price pusher for solana", |
| 17 | + builder: { |
| 18 | + endpoint: { |
| 19 | + description: "Solana RPC API endpoint", |
| 20 | + type: "string", |
| 21 | + required: true, |
| 22 | + } as Options, |
| 23 | + "keypair-file": { |
| 24 | + description: "Path to a keypair file", |
| 25 | + type: "string", |
| 26 | + required: true, |
| 27 | + } as Options, |
| 28 | + "shard-id": { |
| 29 | + description: "Shard ID", |
| 30 | + type: "number", |
| 31 | + required: true, |
| 32 | + } as Options, |
| 33 | + "compute-unit-price-micro-lamports": { |
| 34 | + description: "Priority fee per compute unit", |
| 35 | + type: "number", |
| 36 | + default: 50000, |
| 37 | + } as Options, |
| 38 | + ...options.priceConfigFile, |
| 39 | + ...options.priceServiceEndpoint, |
| 40 | + ...options.pythContractAddress, |
| 41 | + ...options.pollingFrequency, |
| 42 | + ...options.pushingFrequency, |
| 43 | + }, |
| 44 | + handler: function (argv: any) { |
| 45 | + const { |
| 46 | + endpoint, |
| 47 | + keypairFile, |
| 48 | + shardId, |
| 49 | + computeUnitPriceMicroLamports, |
| 50 | + priceConfigFile, |
| 51 | + priceServiceEndpoint, |
| 52 | + pythContractAddress, |
| 53 | + pushingFrequency, |
| 54 | + pollingFrequency, |
| 55 | + } = argv; |
| 56 | + |
| 57 | + const priceConfigs = readPriceConfigFile(priceConfigFile); |
| 58 | + |
| 59 | + const priceServiceConnection = new PriceServiceConnection( |
| 60 | + priceServiceEndpoint, |
| 61 | + { |
| 62 | + logger: { |
| 63 | + // Log only warnings and errors from the price service client |
| 64 | + info: () => undefined, |
| 65 | + warn: console.warn, |
| 66 | + error: console.error, |
| 67 | + debug: () => undefined, |
| 68 | + trace: () => undefined, |
| 69 | + }, |
| 70 | + } |
| 71 | + ); |
| 72 | + |
| 73 | + const priceItems = priceConfigs.map(({ id, alias }) => ({ id, alias })); |
| 74 | + |
| 75 | + const pythListener = new PythPriceListener( |
| 76 | + priceServiceConnection, |
| 77 | + priceItems |
| 78 | + ); |
| 79 | + |
| 80 | + const wallet = new NodeWallet( |
| 81 | + Keypair.fromSecretKey( |
| 82 | + Uint8Array.from(JSON.parse(fs.readFileSync(keypairFile, "ascii"))) |
| 83 | + ) |
| 84 | + ); |
| 85 | + |
| 86 | + const pythSolanaReceiver = new PythSolanaReceiver({ |
| 87 | + connection: new Connection(endpoint), |
| 88 | + wallet, |
| 89 | + pushOracleProgramId: new PublicKey(pythContractAddress), |
| 90 | + }); |
| 91 | + |
| 92 | + const solanaPricePusher = new SolanaPricePusher( |
| 93 | + pythSolanaReceiver, |
| 94 | + priceServiceConnection, |
| 95 | + shardId, |
| 96 | + computeUnitPriceMicroLamports |
| 97 | + ); |
| 98 | + const solanaPriceListener = new SolanaPriceListener( |
| 99 | + pythSolanaReceiver, |
| 100 | + shardId, |
| 101 | + priceItems, |
| 102 | + { pollingFrequency } |
| 103 | + ); |
| 104 | + |
| 105 | + const controller = new Controller( |
| 106 | + priceConfigs, |
| 107 | + pythListener, |
| 108 | + solanaPriceListener, |
| 109 | + solanaPricePusher, |
| 110 | + { pushingFrequency } |
| 111 | + ); |
| 112 | + |
| 113 | + controller.start(); |
| 114 | + }, |
| 115 | +}; |
0 commit comments