setup.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { PYTH_LAZER_SOLANA_CONTRACT_IDL, type PythLazerSolanaContract } from "../src/index.js";
  3. import yargs from "yargs/yargs";
  4. import { readFileSync } from "fs";
  5. import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet.js";
  6. // This script initializes the program. It should be run once after the program is deployed. Additionally, if the
  7. // top authority be the same as the given keypair and the trusted signer and expiry time are provided, the trusted
  8. // signer will be updated.
  9. //
  10. // Note: the program id is derived from the idl file (pytd...). Run `anchor test` to generate it.
  11. async function main() {
  12. let argv = await yargs(process.argv.slice(2))
  13. .options({
  14. url: { type: "string", demandOption: true },
  15. "keypair-path": { type: "string", demandOption: true },
  16. "top-authority": { type: "string", demandOption: true },
  17. treasury: { type: "string", demandOption: true },
  18. "trusted-signer": { type: "string", demandOption: false },
  19. "expiry-time-seconds": { type: "number", demandOption: false },
  20. })
  21. .parse();
  22. const keypair = anchor.web3.Keypair.fromSecretKey(
  23. new Uint8Array(JSON.parse(readFileSync(argv.keypairPath, "ascii"))),
  24. );
  25. const topAuthority = new anchor.web3.PublicKey(argv.topAuthority);
  26. const treasury = new anchor.web3.PublicKey(argv.treasury);
  27. const wallet = new NodeWallet(keypair);
  28. const connection = new anchor.web3.Connection(argv.url, {
  29. commitment: "confirmed",
  30. });
  31. const provider = new anchor.AnchorProvider(connection, wallet);
  32. const program: anchor.Program<PythLazerSolanaContract> = new anchor.Program(
  33. PYTH_LAZER_SOLANA_CONTRACT_IDL,
  34. provider,
  35. );
  36. const storage = await program.account.storage.all();
  37. if (storage.length === 0) {
  38. console.log("Initializing the program");
  39. await program.methods
  40. .initialize(topAuthority, treasury)
  41. .accounts({
  42. payer: wallet.publicKey,
  43. })
  44. .rpc();
  45. }
  46. if (
  47. topAuthority.equals(wallet.publicKey) &&
  48. argv.trustedSigner &&
  49. argv.expiryTimeSeconds
  50. ) {
  51. console.log("Updating the trusted signer");
  52. await program.methods
  53. .update(
  54. new anchor.web3.PublicKey(argv.trustedSigner),
  55. new anchor.BN(argv.expiryTimeSeconds),
  56. )
  57. .accounts({})
  58. .rpc();
  59. }
  60. }
  61. main();