initialize-bridge.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { createInitializeInstruction } from "@certusone/wormhole-sdk/lib/cjs/solana/tokenBridge";
  2. import { AnchorProvider, Wallet, web3 } from "@coral-xyz/anchor";
  3. import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
  4. function displayHelp() {
  5. console.log(`
  6. The following environment variables are required:
  7. RPC_URL: The RPC URL of the SVM network (Fogo or Solana).
  8. TOKEN_BRIDGE_PROGRAM_ID: The program ID of the token bridge program.
  9. PRIVATE_KEY: The private key of the account that will be used to initialize the token bridge program.
  10. Can be a keypair-file path or a base58 encoded string.
  11. CORE_BRIDGE_PROGRAM_ID: The program ID of the Core bridge program.
  12. `)
  13. }
  14. (async () => {
  15. const RPC_URL = process.env.RPC_URL;
  16. if (!RPC_URL) {
  17. console.error("RPC_URL is required");
  18. displayHelp();
  19. process.exit(1);
  20. }
  21. const TOKEN_BRIDGE_PROGRAM_ID = process.env.TOKEN_BRIDGE_PROGRAM_ID;
  22. if (!TOKEN_BRIDGE_PROGRAM_ID) {
  23. console.error("TOKEN_BRIDGE_PROGRAM_ID is required");
  24. displayHelp();
  25. process.exit(1);
  26. }
  27. const CORE_BRIDGE_PROGRAM_ID = process.env.CORE_BRIDGE_PROGRAM_ID;
  28. if (!CORE_BRIDGE_PROGRAM_ID) {
  29. console.error("CORE_BRIDGE_PROGRAM_ID is required");
  30. displayHelp();
  31. process.exit(1);
  32. }
  33. const coreBridgeAddress = new web3.PublicKey(CORE_BRIDGE_PROGRAM_ID);
  34. const tokenBridgeAddress = new web3.PublicKey(TOKEN_BRIDGE_PROGRAM_ID);
  35. const connection = new web3.Connection(RPC_URL, "confirmed");
  36. const key = process.env.PRIVATE_KEY;
  37. if (!key) {
  38. console.error("PRIVATE_KEY is required");
  39. displayHelp();
  40. process.exit(1);
  41. }
  42. const payer = web3.Keypair.fromSecretKey(
  43. key.endsWith(".json") ? new Uint8Array(require(key)) : bs58.decode(key)
  44. );
  45. const provider = new AnchorProvider(connection, new Wallet(payer));
  46. const ix = createInitializeInstruction(
  47. tokenBridgeAddress,
  48. payer.publicKey.toString(),
  49. coreBridgeAddress
  50. );
  51. const transaction = new web3.Transaction();
  52. transaction.add(ix);
  53. const tx = await provider.sendAndConfirm(transaction);
  54. console.log(tx);
  55. })();