setup.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { PythLazerSolanaContract } from "../target/types/pyth_lazer_solana_contract";
  4. import * as pythLazerSolanaContractIdl from "../target/idl/pyth_lazer_solana_contract.json";
  5. import yargs from "yargs/yargs";
  6. import { readFileSync } from "fs";
  7. import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
  8. // This script initializes the program and updates the trusted signer
  9. //
  10. // There are some assumptions made in this script:
  11. // 1. The program id is derived from the idl file (pytd...).
  12. // 2. The keypair provided is the top authority keypair
  13. async function main() {
  14. let argv = await yargs(process.argv.slice(2))
  15. .options({
  16. url: { type: "string", demandOption: true },
  17. "keypair-path": { type: "string", demandOption: true },
  18. "trusted-signer": { type: "string", demandOption: true },
  19. "expiry-time-seconds": { type: "number", demandOption: true },
  20. })
  21. .parse();
  22. const keypair = anchor.web3.Keypair.fromSecretKey(
  23. new Uint8Array(JSON.parse(readFileSync(argv.keypairPath, "ascii")))
  24. );
  25. const wallet = new NodeWallet(keypair);
  26. const connection = new anchor.web3.Connection(argv.url, {
  27. commitment: "confirmed",
  28. });
  29. const provider = new anchor.AnchorProvider(connection, wallet);
  30. const program: Program<PythLazerSolanaContract> = new Program(
  31. pythLazerSolanaContractIdl as PythLazerSolanaContract,
  32. provider
  33. );
  34. const storage = await program.account.storage.all();
  35. if (storage.length === 0) {
  36. console.log("Initializing the program");
  37. await program.methods
  38. .initialize(keypair.publicKey, anchor.web3.PublicKey.unique())
  39. .accounts({
  40. payer: wallet.publicKey,
  41. })
  42. .rpc();
  43. }
  44. console.log("Updating the trusted signer");
  45. await program.methods
  46. .update(
  47. new anchor.web3.PublicKey(argv.trustedSigner),
  48. new anchor.BN(argv.expiryTimeSeconds)
  49. )
  50. .accounts({})
  51. .rpc();
  52. }
  53. main();