checking-accounts.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import * as anchor from "@coral-xyz/anchor"
  2. import { Program } from "@coral-xyz/anchor"
  3. import { CheckingAccounts } from "../target/types/checking_accounts"
  4. import {
  5. SystemProgram,
  6. Transaction,
  7. sendAndConfirmTransaction,
  8. } from "@solana/web3.js"
  9. describe("checking-accounts", () => {
  10. // Configure the client to use the local cluster.
  11. const provider = anchor.AnchorProvider.env()
  12. anchor.setProvider(provider)
  13. // Generate a new random keypair for the data account.
  14. const dataAccount = anchor.web3.Keypair.generate()
  15. // Generate a new keypair to represent the account we will change.
  16. const accountToChange = anchor.web3.Keypair.generate()
  17. // Generate a new keypair to represent the account we will create.
  18. const accountToCreate = anchor.web3.Keypair.generate()
  19. const wallet = provider.wallet as anchor.Wallet
  20. const connection = provider.connection
  21. const program = anchor.workspace.CheckingAccounts as Program<CheckingAccounts>
  22. it("Is initialized!", async () => {
  23. // Create the new dataAccount, this is an account required by Solang even though we don't use it
  24. const tx = await program.methods
  25. .new(wallet.publicKey)
  26. .accounts({ dataAccount: dataAccount.publicKey })
  27. .signers([dataAccount])
  28. .rpc({ skipPreflight: true })
  29. console.log("Your transaction signature", tx)
  30. })
  31. it("Create an account owned by our program", async () => {
  32. // Create the new account owned by our program by directly calling the system program
  33. let ix = SystemProgram.createAccount({
  34. fromPubkey: wallet.publicKey,
  35. newAccountPubkey: accountToChange.publicKey,
  36. lamports: await connection.getMinimumBalanceForRentExemption(0),
  37. space: 0,
  38. programId: program.programId, // Our program
  39. })
  40. await sendAndConfirmTransaction(connection, new Transaction().add(ix), [
  41. wallet.payer,
  42. accountToChange,
  43. ])
  44. })
  45. it("Check Accounts", async () => {
  46. // Invoke the checkAccounts instruction on our program, passing in the account we want to "check"
  47. const tx = await program.methods
  48. .checkAccounts(accountToChange.publicKey, accountToCreate.publicKey)
  49. .accounts({ dataAccount: dataAccount.publicKey })
  50. .remainingAccounts([
  51. {
  52. pubkey: accountToChange.publicKey,
  53. isWritable: true,
  54. isSigner: false,
  55. },
  56. {
  57. pubkey: accountToCreate.publicKey,
  58. isWritable: true,
  59. isSigner: true,
  60. },
  61. ])
  62. .signers([accountToCreate])
  63. .rpc({ skipPreflight: true })
  64. console.log("Your transaction signature", tx)
  65. })
  66. })