deploy.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. JsonRpcProvider,
  9. Ed25519Keypair,
  10. Connection,
  11. } from "@optke3/sui.js";
  12. import { execSync } from "child_process";
  13. import dotenv from "dotenv";
  14. import { REGISTRY, NETWORK } from "../registry";
  15. dotenv.config({ path: "~/.env" });
  16. // ================== Network dependent settings ==================
  17. let network = NETWORK.MAINNET; // <= NOTE: Update this when changing network
  18. const walletPrivateKey = process.env.SUI_MAINNET; // <= NOTE: Update this when changing network
  19. // ================================================================
  20. const registry = REGISTRY[network];
  21. const provider = new JsonRpcProvider(
  22. new Connection({ fullnode: registry["RPC_URL"] })
  23. );
  24. async function main() {
  25. if (walletPrivateKey === undefined) {
  26. throw new Error("Wallet unset in environment");
  27. }
  28. const wallet = new RawSigner(
  29. Ed25519Keypair.fromSecretKey(Buffer.from(walletPrivateKey, "hex")),
  30. provider
  31. );
  32. await publishPackage(wallet, "../contracts");
  33. }
  34. main();
  35. async function publishPackage(signer: RawSigner, packagePath: string) {
  36. try {
  37. // Build contracts
  38. const buildOutput: {
  39. modules: string[];
  40. dependencies: string[];
  41. } = JSON.parse(
  42. execSync(
  43. `sui move build --dump-bytecode-as-base64 --path ${packagePath} 2> /dev/null`,
  44. {
  45. encoding: "utf-8",
  46. }
  47. )
  48. );
  49. console.log("buildOutput: ", buildOutput);
  50. // Publish contracts
  51. const transactionBlock = new TransactionBlock();
  52. transactionBlock.setGasBudget(4000000000);
  53. const [upgradeCap] = transactionBlock.publish({
  54. modules: buildOutput.modules.map((m: string) => Array.from(fromB64(m))),
  55. dependencies: buildOutput.dependencies.map((d: string) =>
  56. normalizeSuiObjectId(d)
  57. ),
  58. });
  59. // Transfer upgrade capability to deployer
  60. transactionBlock.transferObjects(
  61. [upgradeCap],
  62. transactionBlock.pure(await signer.getAddress())
  63. );
  64. // Execute transactions
  65. const res = await signer.signAndExecuteTransactionBlock({
  66. transactionBlock,
  67. options: {
  68. showInput: true,
  69. showObjectChanges: true,
  70. },
  71. });
  72. const publishEvents = getPublishedObjectChanges(res);
  73. if (publishEvents.length !== 1) {
  74. throw new Error(
  75. "No publish event found in transaction:" +
  76. JSON.stringify(res.objectChanges, null, 2)
  77. );
  78. }
  79. return res;
  80. } catch (e) {
  81. throw e;
  82. } finally {
  83. }
  84. }