pyth_deploy.ts 2.5 KB

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