setup.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. It should be run once after the program is deployed. Additionally, if the
  9. // top authority be the same as the given keypair and the trusted signer and expiry time are provided, the trusted
  10. // signer will be updated.
  11. //
  12. // Note: the program id is derived from the idl file (pytd...). Run `anchor test` to generate it.
  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. "top-authority": { type: "string", demandOption: true },
  19. treasury: { type: "string", demandOption: true },
  20. "trusted-signer": { type: "string", demandOption: false },
  21. "expiry-time-seconds": { type: "number", demandOption: false },
  22. })
  23. .parse();
  24. const keypair = anchor.web3.Keypair.fromSecretKey(
  25. new Uint8Array(JSON.parse(readFileSync(argv.keypairPath, "ascii")))
  26. );
  27. const topAuthority = new anchor.web3.PublicKey(argv.topAuthority);
  28. const treasury = new anchor.web3.PublicKey(argv.treasury);
  29. const wallet = new NodeWallet(keypair);
  30. const connection = new anchor.web3.Connection(argv.url, {
  31. commitment: "confirmed",
  32. });
  33. const provider = new anchor.AnchorProvider(connection, wallet);
  34. const program: Program<PythLazerSolanaContract> = new Program(
  35. pythLazerSolanaContractIdl as PythLazerSolanaContract,
  36. provider
  37. );
  38. const storage = await program.account.storage.all();
  39. if (storage.length === 0) {
  40. console.log("Initializing the program");
  41. await program.methods
  42. .initialize(topAuthority, treasury)
  43. .accounts({
  44. payer: wallet.publicKey,
  45. })
  46. .rpc();
  47. }
  48. if (
  49. topAuthority.equals(wallet.publicKey) &&
  50. argv.trustedSigner &&
  51. argv.expiryTimeSeconds
  52. ) {
  53. console.log("Updating the trusted signer");
  54. await program.methods
  55. .update(
  56. new anchor.web3.PublicKey(argv.trustedSigner),
  57. new anchor.BN(argv.expiryTimeSeconds)
  58. )
  59. .accounts({})
  60. .rpc();
  61. }
  62. }
  63. main();