test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import * as anchor from "@project-serum/anchor";
  2. import { CreateSystemAccount } from "../target/types/create_system_account";
  3. describe("Create a system account", () => {
  4. const provider = anchor.AnchorProvider.env();
  5. anchor.setProvider(provider);
  6. const wallet = provider.wallet as anchor.Wallet;
  7. const program = anchor.workspace.CreateSystemAccount as anchor.Program<CreateSystemAccount>;
  8. it("Create the account", async () => {
  9. const newKeypair = anchor.web3.Keypair.generate();
  10. await program.methods.createSystemAccount()
  11. .accounts({
  12. payer: wallet.publicKey,
  13. newAccount: newKeypair.publicKey,
  14. systemProgram: anchor.web3.SystemProgram.programId
  15. })
  16. .signers([wallet.payer, newKeypair])
  17. .rpc();
  18. });
  19. it("Change ownership for the account", async () => {
  20. const newKeypair = anchor.web3.Keypair.generate();
  21. let ix = anchor.web3.SystemProgram.assign({
  22. accountPubkey: newKeypair.publicKey,
  23. programId: program.programId,
  24. })
  25. await anchor.web3.sendAndConfirmTransaction(
  26. provider.connection,
  27. new anchor.web3.Transaction().add(ix),
  28. [wallet.payer, newKeypair]
  29. );
  30. });
  31. it("Change it again using the System Program", async () => {
  32. const newKeypair = anchor.web3.Keypair.generate();
  33. let ix = anchor.web3.SystemProgram.assign({
  34. accountPubkey: newKeypair.publicKey,
  35. programId: anchor.web3.SystemProgram.programId,
  36. })
  37. await anchor.web3.sendAndConfirmTransaction(
  38. provider.connection,
  39. new anchor.web3.Transaction().add(ix),
  40. [wallet.payer, newKeypair]
  41. );
  42. });
  43. });