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