Skip to content

Commit e243e67

Browse files
authored
[Xc admin] proposer server (#816)
* Proposer server * Add package-lock.json * Add rpc as enviornment variables
1 parent c23fc8d commit e243e67

File tree

4 files changed

+321
-55
lines changed

4 files changed

+321
-55
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "proposer_server",
3+
"version": "0.0.0",
4+
"description": "A server that proposes the instructions that it receives to the multisig",
5+
"private": "true",
6+
"author": "",
7+
"homepage": "https://github.com/pyth-network/pyth-crosschain",
8+
"license": "ISC",
9+
"main": "src/index.ts",
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/pyth-network/pyth-crosschain.git"
13+
},
14+
"bugs": {
15+
"url": "https://github.com/pyth-network/pyth-crosschain/issues"
16+
},
17+
"scripts": {
18+
"build": "tsc",
19+
"format": "prettier --write \"src/**/*.ts\""
20+
},
21+
"dependencies": {
22+
"@coral-xyz/anchor": "^0.27.0",
23+
"@pythnetwork/client": "^2.17.0",
24+
"@solana/web3.js": "^1.76.0",
25+
"@sqds/mesh": "^1.0.6",
26+
"cors": "^2.8.5",
27+
"ts-node": "^10.9.1",
28+
"xc_admin_common": "*"
29+
}
30+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../../../tsconfig.base.json",
3+
"include": ["src"],
4+
"exclude": ["node_modules", "**/__tests__/*"],
5+
"compilerOptions": {
6+
"rootDir": "src/",
7+
"outDir": "./lib",
8+
"skipLibCheck": true
9+
}
10+
}

0 commit comments

Comments
 (0)