post_message_shim_test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { deriveFeeCollectorKey } from "@certusone/wormhole-sdk/lib/cjs/solana/wormhole";
  2. import {
  3. AnchorProvider,
  4. Program,
  5. setProvider,
  6. Wallet,
  7. web3,
  8. } from "@coral-xyz/anchor";
  9. import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
  10. import { WormholePostMessageShim } from "../../svm/wormhole-core-shims/anchor/idls/wormhole_post_message_shim";
  11. import WormholePostMessageShimIdl from "../../svm/wormhole-core-shims/anchor/idls/wormhole_post_message_shim.json";
  12. // Usage:
  13. // RPC_URL="https://api.devnet.solana.com" CORE_BRIDGE_PROGRAM_ID=3u8hJUVTA4jH1wYAyUur7FFZVQ8H635K3tSHHF4ssjQ5 SOLANA_KEY="<full_path>.json" MSG="hello wormhole" npx tsx post_message.ts
  14. (async () => {
  15. const RPC_URL = process.env.RPC_URL;
  16. if (!RPC_URL) {
  17. throw new Error("RPC_URL is required");
  18. }
  19. const CORE_BRIDGE_PROGRAM_ID = process.env.CORE_BRIDGE_PROGRAM_ID;
  20. if (!CORE_BRIDGE_PROGRAM_ID) {
  21. throw new Error("CORE_BRIDGE_PROGRAM_ID is required");
  22. }
  23. const coreBridgeAddress = new web3.PublicKey(CORE_BRIDGE_PROGRAM_ID);
  24. const MSG = process.env.MSG;
  25. if (!MSG) {
  26. throw new Error("MSG is required");
  27. }
  28. const connection = new web3.Connection(RPC_URL, "confirmed");
  29. const key = process.env.SOLANA_KEY;
  30. if (!key) {
  31. throw new Error("SOLANA_KEY is required");
  32. }
  33. const payer = web3.Keypair.fromSecretKey(
  34. key.endsWith(".json") ? new Uint8Array(require(key)) : bs58.decode(key)
  35. );
  36. const provider = new AnchorProvider(connection, new Wallet(payer));
  37. setProvider(provider);
  38. const program = new Program<WormholePostMessageShim>(
  39. WormholePostMessageShimIdl as WormholePostMessageShim
  40. );
  41. const tx = await program.methods
  42. .postMessage(0, { confirmed: {} }, Buffer.from(MSG, "ascii"))
  43. // there seems to be an extra "program" field that is not needed and is marked optional in `svm/wormhole-core-shims/tests/wormhole-post-message-shim.ts` using anchor 0.30.1
  44. // @ts-ignore
  45. .accounts({
  46. emitter: payer.publicKey,
  47. wormholeProgram: coreBridgeAddress,
  48. })
  49. .preInstructions([
  50. // gotta pay the fee
  51. web3.SystemProgram.transfer({
  52. fromPubkey: payer.publicKey,
  53. toPubkey: deriveFeeCollectorKey(coreBridgeAddress), // fee collector
  54. lamports: 100, // hardcoded for tilt in devnet_setup.sh
  55. }),
  56. ])
  57. .rpc();
  58. console.log(tx);
  59. })();