|
1 | | -// import assert from 'node:assert'; |
2 | | -// import * as anchor from '@coral-xyz/anchor'; |
3 | | -// import type { Program } from '@coral-xyz/anchor'; |
4 | | -// import { PublicKey } from '@solana/web3.js'; |
5 | | -// import type { CloseAccount } from '../target/types/close_account'; |
6 | | - |
7 | | -// describe('Close an account', () => { |
8 | | -// // Configure the client to use the local cluster. |
9 | | -// const provider = anchor.AnchorProvider.env(); |
10 | | -// anchor.setProvider(provider); |
11 | | - |
12 | | -// const program = anchor.workspace.CloseAccountProgram as Program<CloseAccount>; |
13 | | -// const payer = provider.wallet as anchor.Wallet; |
14 | | - |
15 | | -// // Derive the PDA for the user's account. |
16 | | -// const [userAccountAddress] = PublicKey.findProgramAddressSync([Buffer.from('USER'), payer.publicKey.toBuffer()], program.programId); |
17 | | - |
18 | | -// it('can create an account', async () => { |
19 | | -// const userId = anchor.BN(76362); |
20 | | - |
21 | | -// await program.methods |
22 | | -// .createUser(userId) |
23 | | -// .accounts({ |
24 | | -// user: payer.publicKey, |
25 | | -// }) |
26 | | -// .rpc(); |
27 | | - |
28 | | -// // Fetch the account data |
29 | | -// const userAccount = await program.account.userAccount.fetch(userAccountAddress); |
30 | | -// assert.notEqual(userAccount, null); |
31 | | -// }); |
32 | | - |
33 | | -// it('can close an account', async () => { |
34 | | -// await program.methods |
35 | | -// .closeUser() |
36 | | -// .accounts({ |
37 | | -// user: payer.publicKey, |
38 | | -// }) |
39 | | -// .rpc(); |
40 | | - |
41 | | -// // The account should no longer exist, returning null. |
42 | | -// const userAccount = await program.account.userAccount.fetchNullable(userAccountAddress); |
43 | | -// assert.equal(userAccount, null); |
44 | | -// }); |
45 | | -// }); |
| 1 | +import assert from "node:assert"; |
| 2 | +import * as anchor from "@coral-xyz/anchor"; |
| 3 | +import type { Program } from "@coral-xyz/anchor"; |
| 4 | +import { |
| 5 | + Connection, |
| 6 | + Keypair, |
| 7 | + LAMPORTS_PER_SOL, |
| 8 | + PublicKey, |
| 9 | + SystemProgram, |
| 10 | + Transaction, |
| 11 | + TransactionInstruction, |
| 12 | + TransactionMessage, |
| 13 | + VersionedTransaction, |
| 14 | +} from "@solana/web3.js"; |
| 15 | +import type { CloseAccount } from "../target/types/close_account"; |
| 16 | + |
| 17 | + const connection = new Connection("https://api.devnet.solana.com"); |
| 18 | + |
| 19 | + async function createAndProcessTransaction( |
| 20 | + payer: Keypair, |
| 21 | + instruction: TransactionInstruction, |
| 22 | + additionalSigners: Keypair[] = [] |
| 23 | + ): Promise<{ transaction: VersionedTransaction; signature: string | null }> { |
| 24 | + // Get the latest blockhash |
| 25 | + const { blockhash, lastValidBlockHeight } = await getLatestBlockhash(); |
| 26 | + const message = new TransactionMessage({ |
| 27 | + payerKey: payer.publicKey, |
| 28 | + recentBlockhash: blockhash, |
| 29 | + instructions: [instruction], |
| 30 | + }).compileToV0Message(); |
| 31 | + |
| 32 | + const tx = new VersionedTransaction(message); |
| 33 | + |
| 34 | + try { |
| 35 | + const signature = await sendAndConfirmTransaction(tx); |
| 36 | + return { transaction: tx, signature }; |
| 37 | + } catch (err) { |
| 38 | + return { transaction: tx, signature: null }; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + async function getLatestBlockhash(): Promise<{ |
| 43 | + blockhash: string; |
| 44 | + lastValidBlockHeight: number; |
| 45 | + }> { |
| 46 | + const { blockhash, lastValidBlockHeight } = |
| 47 | + await connection.getLatestBlockhash("finalized"); |
| 48 | + return { blockhash, lastValidBlockHeight }; |
| 49 | + } |
| 50 | + |
| 51 | + async function sendAndConfirmTransaction(tx: VersionedTransaction): Promise<string> { |
| 52 | + |
| 53 | + const signature = await connection.sendTransaction(tx); |
| 54 | + |
| 55 | + const { blockhash, lastValidBlockHeight } = await getLatestBlockhash(); |
| 56 | + |
| 57 | + await connection.confirmTransaction({ |
| 58 | + blockhash: blockhash, |
| 59 | + lastValidBlockHeight: lastValidBlockHeight, |
| 60 | + signature: signature, |
| 61 | + }); |
| 62 | + |
| 63 | + return signature; |
| 64 | +} |
| 65 | + |
| 66 | +describe("Close an account", () => { |
| 67 | + // Configure the client to use the local cluster. |
| 68 | + const provider = anchor.AnchorProvider.env(); |
| 69 | + anchor.setProvider(provider); |
| 70 | + |
| 71 | + const program = anchor.workspace.CloseAccountProgram as Program<CloseAccount>; |
| 72 | + const payer = provider.wallet as anchor.Wallet; |
| 73 | + |
| 74 | + const user = Keypair.generate(); // Generate a new user keypair |
| 75 | + |
| 76 | + before(async () => { |
| 77 | + //Transfer SOL to the user account to cover rent |
| 78 | + const transferInstruction = SystemProgram.transfer({ |
| 79 | + fromPubkey: payer.publicKey, |
| 80 | + toPubkey: user.publicKey, |
| 81 | + lamports: 2 * LAMPORTS_PER_SOL, |
| 82 | + }); |
| 83 | + |
| 84 | + await createAndProcessTransaction(payer.payer, transferInstruction, [ |
| 85 | + payer.payer, |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + // Derive the PDA for the user's account. |
| 90 | + const [userAccount, userAccountBump] = PublicKey.findProgramAddressSync( |
| 91 | + [Buffer.from("USER"), payer.publicKey.toBuffer()], |
| 92 | + program.programId |
| 93 | + ); |
| 94 | + |
| 95 | + it("Can create an account", async () => { |
| 96 | + await program.methods |
| 97 | + .createUser("Jacob") |
| 98 | + .accounts({ |
| 99 | + user: user.publicKey, |
| 100 | + }) |
| 101 | + .signers([user]) |
| 102 | + .rpc(); |
| 103 | + |
| 104 | + // Fetch the account data |
| 105 | + const userAccountData = await program.account.userState.fetch(userAccount); |
| 106 | + assert.equal(userAccountData.name, "Jacob"); |
| 107 | + assert.equal(userAccountData.user.toBase58(), user.publicKey.toBase58()); |
| 108 | + assert.notEqual(userAccountData, null); |
| 109 | + }); |
| 110 | + |
| 111 | + it("Can close an Account", async () => { |
| 112 | + await program.methods |
| 113 | + .closeUser() |
| 114 | + .accounts({ |
| 115 | + user: user.publicKey, |
| 116 | + }) |
| 117 | + .signers([user]) |
| 118 | + .rpc(); |
| 119 | + |
| 120 | + // The account should no longer exist, returning null. |
| 121 | + try { |
| 122 | + const userAccountData = await program.account.userState.fetchNullable( |
| 123 | + userAccount |
| 124 | + ); |
| 125 | + assert.equal(userAccountData, null); |
| 126 | + } catch (err) { |
| 127 | + // Won't return null and will throw an error in anchor-bankrun' |
| 128 | + assert.equal(err.message, `Could not find ${userAccount}`); |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
0 commit comments