|
| 1 | +import express, { Request, Response } from "express"; |
| 2 | +import cors from "cors"; |
| 3 | +import { |
| 4 | + Cluster, |
| 5 | + Connection, |
| 6 | + Keypair, |
| 7 | + PublicKey, |
| 8 | + TransactionInstruction, |
| 9 | +} from "@solana/web3.js"; |
| 10 | +import { |
| 11 | + envOrErr, |
| 12 | + getMultisigCluster, |
| 13 | + isRemoteCluster, |
| 14 | + PRICE_FEED_MULTISIG, |
| 15 | + proposeInstructions, |
| 16 | + WORMHOLE_ADDRESS, |
| 17 | +} from "xc_admin_common"; |
| 18 | +import * as fs from "fs"; |
| 19 | +import { getPythClusterApiUrl, PythCluster } from "@pythnetwork/client"; |
| 20 | +import SquadsMesh from "@sqds/mesh"; |
| 21 | +import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet"; |
| 22 | + |
| 23 | +const PORT: number = Number(process.env.PORT ?? "3000"); |
| 24 | +const KEYPAIR: Keypair = Keypair.fromSecretKey( |
| 25 | + Uint8Array.from(JSON.parse(fs.readFileSync(envOrErr("WALLET"), "ascii"))) |
| 26 | +); |
| 27 | +const MAINNET_RPC: string = |
| 28 | + process.env.MAINNET_RPC ?? getPythClusterApiUrl("mainnet-beta"); |
| 29 | +const DEVNET_RPC: string = |
| 30 | + process.env.DEVNET_RPC ?? getPythClusterApiUrl("devnet"); |
| 31 | +const TESTNET_RPC: string = |
| 32 | + process.env.TESTNET_RPC ?? getPythClusterApiUrl("testnet"); |
| 33 | +const LOCALNET_RPC: string = |
| 34 | + process.env.LOCALNET_RPC ?? getPythClusterApiUrl("localnet"); |
| 35 | + |
| 36 | +const RPC_URLS: Record<Cluster | "localnet", string> = { |
| 37 | + "mainnet-beta": MAINNET_RPC, |
| 38 | + devnet: DEVNET_RPC, |
| 39 | + testnet: TESTNET_RPC, |
| 40 | + localnet: LOCALNET_RPC, |
| 41 | +}; |
| 42 | + |
| 43 | +const app = express(); |
| 44 | + |
| 45 | +app.use(cors()); |
| 46 | +app.use(express.json()); |
| 47 | + |
| 48 | +app.post("/api/propose", async (req: Request, res: Response) => { |
| 49 | + try { |
| 50 | + const instructions: TransactionInstruction[] = req.body.instructions.map( |
| 51 | + (ix: any) => |
| 52 | + new TransactionInstruction({ |
| 53 | + data: Buffer.from(ix.data), |
| 54 | + programId: new PublicKey(ix.programId), |
| 55 | + keys: ix.keys.map((key: any) => { |
| 56 | + return { |
| 57 | + isSigner: key.isSigner, |
| 58 | + isWritable: key.isWritable, |
| 59 | + pubkey: new PublicKey(key.pubkey), |
| 60 | + }; |
| 61 | + }), |
| 62 | + }) |
| 63 | + ); |
| 64 | + |
| 65 | + const cluster: PythCluster = req.body.cluster; |
| 66 | + |
| 67 | + const proposeSquads: SquadsMesh = new SquadsMesh({ |
| 68 | + connection: new Connection(RPC_URLS[getMultisigCluster(cluster)]), |
| 69 | + wallet: new NodeWallet(KEYPAIR), |
| 70 | + }); |
| 71 | + |
| 72 | + const proposalPubkey = await proposeInstructions( |
| 73 | + proposeSquads, |
| 74 | + PRICE_FEED_MULTISIG[getMultisigCluster(cluster)], |
| 75 | + instructions, |
| 76 | + isRemoteCluster(cluster), |
| 77 | + WORMHOLE_ADDRESS[getMultisigCluster(cluster)] |
| 78 | + ); |
| 79 | + res.status(200).json({ proposalPubkey }); |
| 80 | + } catch (error) { |
| 81 | + if (error instanceof Error) { |
| 82 | + res.status(500).json(error.message); |
| 83 | + } else { |
| 84 | + res.status(500).json("An unknown error occurred"); |
| 85 | + } |
| 86 | + } |
| 87 | +}); |
| 88 | + |
| 89 | +app.listen(PORT); |
0 commit comments