deploy.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /// Deploy Pyth to Sui testnet (devnet deploy can be done via CLI)
  2. import {
  3. fromB64,
  4. getPublishedObjectChanges,
  5. normalizeSuiObjectId,
  6. RawSigner,
  7. TransactionBlock,
  8. SUI_CLOCK_OBJECT_ID,
  9. JsonRpcProvider,
  10. Ed25519Keypair,
  11. testnetConnection,
  12. Connection,
  13. } from "@mysten/sui.js";
  14. import { execSync } from "child_process";
  15. import fs from "fs";
  16. import { resolve } from "path";
  17. import dotenv from "dotenv";
  18. import { REGISTRY, NETWORK } from "../registry";
  19. dotenv.config({ path: "~/.env" });
  20. // Network dependent settings.
  21. let network = NETWORK.TESTNET; // <= NOTE: Update this when changing network
  22. const walletPrivateKey = process.env.SUI_TESTNET; // <= NOTE: Update this when changing network
  23. // Load registry and provider.
  24. const registry = REGISTRY[network];
  25. const provider = new JsonRpcProvider(
  26. new Connection({ fullnode: registry["RPC_URL"] })
  27. );
  28. async function main() {
  29. if (walletPrivateKey === undefined) {
  30. throw new Error("SUI_TESTNET unset in environment");
  31. }
  32. const wallet = new RawSigner(
  33. Ed25519Keypair.fromSecretKey(Buffer.from(walletPrivateKey, "hex")),
  34. provider
  35. );
  36. await publishPackage(wallet, "~/developer/wormhole/sui/wormhole");
  37. }
  38. main();
  39. async function publishPackage(
  40. signer: RawSigner,
  41. //network: Network,
  42. packagePath: string
  43. ) {
  44. try {
  45. // Build contracts
  46. const buildOutput: {
  47. modules: string[];
  48. dependencies: string[];
  49. } = JSON.parse(
  50. execSync(
  51. `sui move build --dump-bytecode-as-base64 --path ${packagePath} 2> /dev/null`,
  52. {
  53. encoding: "utf-8",
  54. }
  55. )
  56. );
  57. console.log("buildOutput: ", buildOutput);
  58. // Publish contracts
  59. const transactionBlock = new TransactionBlock();
  60. // important
  61. transactionBlock.setGasBudget(5000000000);
  62. const [upgradeCap] = transactionBlock.publish({
  63. modules: buildOutput.modules.map((m: string) => Array.from(fromB64(m))),
  64. dependencies: buildOutput.dependencies.map((d: string) =>
  65. normalizeSuiObjectId(d)
  66. ),
  67. });
  68. // Transfer upgrade capability to deployer
  69. transactionBlock.transferObjects(
  70. [upgradeCap],
  71. transactionBlock.pure(await signer.getAddress())
  72. );
  73. // Execute transactions
  74. const res = await signer.signAndExecuteTransactionBlock({
  75. transactionBlock,
  76. options: {
  77. showInput: true,
  78. showObjectChanges: true,
  79. },
  80. });
  81. // Update network-specific Move.toml with package ID
  82. const publishEvents = getPublishedObjectChanges(res);
  83. if (publishEvents.length !== 1) {
  84. throw new Error(
  85. "No publish event found in transaction:" +
  86. JSON.stringify(res.objectChanges, null, 2)
  87. );
  88. }
  89. // Return publish transaction info
  90. return res;
  91. } catch (e) {
  92. throw e;
  93. } finally {
  94. }
  95. }