test.ts 1.5 KB

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