test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import * as anchor from "@coral-xyz/anchor"
  2. import { AnchorProgramExample } from "../target/types/anchor_program_example"
  3. import {
  4. Keypair,
  5. SystemProgram,
  6. Transaction,
  7. sendAndConfirmTransaction,
  8. } from "@solana/web3.js"
  9. describe("Anchor example", () => {
  10. const provider = anchor.AnchorProvider.env()
  11. anchor.setProvider(provider)
  12. const program = anchor.workspace
  13. .AnchorProgramExample as anchor.Program<AnchorProgramExample>
  14. const wallet = provider.wallet as anchor.Wallet
  15. // We'll create this ahead of time.
  16. // Our program will try to modify it.
  17. const accountToChange = new Keypair()
  18. // Our program will create this.
  19. const accountToCreate = new Keypair()
  20. it("Create an account owned by our program", async () => {
  21. let instruction = SystemProgram.createAccount({
  22. fromPubkey: provider.wallet.publicKey,
  23. newAccountPubkey: accountToChange.publicKey,
  24. lamports: await provider.connection.getMinimumBalanceForRentExemption(0),
  25. space: 0,
  26. programId: program.programId, // Our program
  27. })
  28. const transaction = new Transaction().add(instruction)
  29. await sendAndConfirmTransaction(provider.connection, transaction, [
  30. wallet.payer,
  31. accountToChange,
  32. ])
  33. })
  34. it("Check accounts", async () => {
  35. await program.methods
  36. .checkAccounts()
  37. .accounts({
  38. payer: wallet.publicKey,
  39. accountToCreate: accountToCreate.publicKey,
  40. accountToChange: accountToChange.publicKey,
  41. })
  42. .rpc()
  43. })
  44. })