init_state.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import dotenv from "dotenv";
  2. import {
  3. RawSigner,
  4. TransactionBlock,
  5. JsonRpcProvider,
  6. Ed25519Keypair,
  7. Connection,
  8. } from "@mysten/sui.js";
  9. import { REGISTRY, NETWORK, INITIAL_DATA_SOURCES } from "../registry";
  10. dotenv.config({ path: "~/.env" });
  11. // ================== Network dependent settings ==================
  12. let network = NETWORK.MAINNET; // <= NOTE: Update this when changing network
  13. let walletPrivateKey = process.env.SUI_MAINNET; // <= NOTE: Update this when changing network
  14. // ================================================================
  15. const registry = REGISTRY[network];
  16. const initial_data_sources = INITIAL_DATA_SOURCES[network];
  17. const provider = new JsonRpcProvider(
  18. new Connection({ fullnode: registry["RPC_URL"] })
  19. );
  20. async function main() {
  21. if (walletPrivateKey === undefined) {
  22. throw new Error("Wallet key unset in environment");
  23. }
  24. const wallet = new RawSigner(
  25. Ed25519Keypair.fromSecretKey(Buffer.from(walletPrivateKey, "hex")),
  26. provider
  27. );
  28. const PYTH_PACKAGE = registry["PYTH_PACKAGE_ID"];
  29. // NOTE: Set these before calling init_pyth
  30. const upgradeCap =
  31. "0x82d5b1ab3bd40d121f1a9b073815f241ccd696edf324d2a386d38477f0834082";
  32. const deployerCap =
  33. "0x5bc07a9e7627534680313a2a0499cbbaa76c4b50db30eb86e85ae211c26fc911";
  34. init_pyth(wallet, PYTH_PACKAGE, deployerCap, upgradeCap);
  35. }
  36. main();
  37. /// Use a programmable transaction block to call
  38. /// the Pyth pyth::pyth::init_pyth function.
  39. async function init_pyth(
  40. signer: RawSigner,
  41. pythPackage: string,
  42. deployerCap: string,
  43. upgradeCap: string
  44. ) {
  45. console.log("GOVERNANCE_CHAIN: ", initial_data_sources["GOVERNANCE_CHAIN"]);
  46. console.log("GOVERNANCE_ADDRESS: ", [
  47. ...Buffer.from(initial_data_sources["GOVERNANCE_ADDRESS"], "hex"),
  48. ]);
  49. console.log(
  50. "DATA_SOURCE_CHAINS: ",
  51. initial_data_sources["DATA_SOURCE_CHAINS"]
  52. );
  53. console.log(
  54. "DATA_SOURCE_ADDRESSES: ",
  55. initial_data_sources["DATA_SOURCE_ADDRESSES"].map((x) => [
  56. ...Buffer.from(x, "hex"),
  57. ])
  58. );
  59. const tx = new TransactionBlock();
  60. tx.moveCall({
  61. target: `${pythPackage}::pyth::init_pyth`,
  62. arguments: [
  63. tx.object(deployerCap),
  64. tx.object(upgradeCap),
  65. tx.pure(60), // stale price threshold
  66. tx.pure(initial_data_sources["GOVERNANCE_CHAIN"]), // governance emitter chain id
  67. tx.pure([
  68. ...Buffer.from(initial_data_sources["GOVERNANCE_ADDRESS"], "hex"),
  69. ]), // governance emitter chain address
  70. tx.pure(initial_data_sources["DATA_SOURCE_CHAINS"]), // data source emitter chain ids
  71. tx.pure(
  72. initial_data_sources["DATA_SOURCE_ADDRESSES"].map((x) => [
  73. ...Buffer.from(x, "hex"),
  74. ])
  75. ), // data source addresses
  76. tx.pure(1), // base update fee
  77. ],
  78. });
  79. tx.setGasBudget(2_000_000_000n);
  80. let result = await signer.signAndExecuteTransactionBlock({
  81. transactionBlock: tx,
  82. options: {
  83. showInput: true,
  84. showEffects: true,
  85. showEvents: true,
  86. showObjectChanges: true,
  87. showBalanceChanges: true,
  88. },
  89. });
  90. console.log(result);
  91. return result;
  92. }