|
1 |
| -import * as anchor from "@project-serum/anchor"; |
2 |
| -import { Program } from "@project-serum/anchor"; |
3 |
| -import {Keypair, LAMPORTS_PER_SOL} from "@solana/web3.js"; |
| 1 | +import * as anchor from "@coral-xyz/anchor"; |
| 2 | +import { Program } from "@coral-xyz/anchor"; |
| 3 | +import { Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js"; |
4 | 4 | import { CloseAccountProgram } from "../target/types/close_account_program";
|
5 |
| -import {BlockheightBasedTransactionConfirmationStrategy, PublicKey} from "@solana/web3.js"; |
| 5 | +import { |
| 6 | + BlockheightBasedTransactionConfirmationStrategy, |
| 7 | + PublicKey, |
| 8 | +} from "@solana/web3.js"; |
6 | 9 | import assert from "assert";
|
7 | 10 |
|
8 | 11 | describe("close-an-account", () => {
|
9 |
| - // Configure the client to use the local cluster. |
10 |
| - anchor.setProvider(anchor.AnchorProvider.env()); |
| 12 | + // Configure the client to use the local cluster. |
| 13 | + anchor.setProvider(anchor.AnchorProvider.env()); |
11 | 14 |
|
12 |
| - const program = anchor.workspace.CloseAccountProgram as Program<CloseAccountProgram>; |
13 |
| - const connection = program.provider.connection; |
14 |
| - const payer = Keypair.generate(); |
| 15 | + const program = anchor.workspace |
| 16 | + .CloseAccountProgram as Program<CloseAccountProgram>; |
| 17 | + const connection = program.provider.connection; |
| 18 | + const payer = Keypair.generate(); |
15 | 19 |
|
16 |
| - async function airdrop(receiver: PublicKey, amount: number) { |
17 |
| - const sig = await program.provider.connection.requestAirdrop(receiver, amount); |
18 |
| - const blockStats = await program.provider.connection.getLatestBlockhash(); |
19 |
| - const strategy: BlockheightBasedTransactionConfirmationStrategy = { |
20 |
| - signature: sig, |
21 |
| - blockhash: blockStats.blockhash, |
22 |
| - lastValidBlockHeight: blockStats.lastValidBlockHeight |
23 |
| - } |
24 |
| - await program.provider.connection.confirmTransaction(strategy, "confirmed"); |
25 |
| - } |
| 20 | + async function airdrop(receiver: PublicKey, amount: number) { |
| 21 | + const sig = await program.provider.connection.requestAirdrop( |
| 22 | + receiver, |
| 23 | + amount |
| 24 | + ); |
| 25 | + const blockStats = await program.provider.connection.getLatestBlockhash(); |
| 26 | + const strategy: BlockheightBasedTransactionConfirmationStrategy = { |
| 27 | + signature: sig, |
| 28 | + blockhash: blockStats.blockhash, |
| 29 | + lastValidBlockHeight: blockStats.lastValidBlockHeight, |
| 30 | + }; |
| 31 | + await program.provider.connection.confirmTransaction(strategy, "confirmed"); |
| 32 | + } |
26 | 33 |
|
27 |
| - function getUserAccount(user: PublicKey): [PublicKey, number] { |
28 |
| - return PublicKey.findProgramAddressSync( |
29 |
| - [ |
30 |
| - Buffer.from("USER"), |
31 |
| - user.toBuffer() |
32 |
| - ], program.programId) |
33 |
| - } |
| 34 | + function getUserAccount(user: PublicKey): [PublicKey, number] { |
| 35 | + return PublicKey.findProgramAddressSync( |
| 36 | + [Buffer.from("USER"), user.toBuffer()], |
| 37 | + program.programId |
| 38 | + ); |
| 39 | + } |
34 | 40 |
|
35 |
| - it("Airdrop", async () => { |
36 |
| - const balanceBefore = await connection.getBalance(payer.publicKey); |
37 |
| - await airdrop(payer.publicKey, LAMPORTS_PER_SOL); |
38 |
| - const balanceAfter = await connection.getBalance(payer.publicKey); |
39 |
| - assert.equal(balanceAfter, balanceBefore + LAMPORTS_PER_SOL); |
40 |
| - }); |
| 41 | + it("Airdrop", async () => { |
| 42 | + const balanceBefore = await connection.getBalance(payer.publicKey); |
| 43 | + await airdrop(payer.publicKey, LAMPORTS_PER_SOL); |
| 44 | + const balanceAfter = await connection.getBalance(payer.publicKey); |
| 45 | + assert.equal(balanceAfter, balanceBefore + LAMPORTS_PER_SOL); |
| 46 | + }); |
41 | 47 |
|
| 48 | + it("Create Account", async () => { |
| 49 | + const [userAccountAddress] = getUserAccount(payer.publicKey); |
| 50 | + const userAccountBefore = await program.account.user.fetchNullable( |
| 51 | + userAccountAddress, |
| 52 | + "confirmed" |
| 53 | + ); |
| 54 | + assert.equal(userAccountBefore, null); |
42 | 55 |
|
43 |
| - it("Create Account", async () => { |
44 |
| - const [userAccountAddress] = getUserAccount(payer.publicKey); |
45 |
| - const userAccountBefore = await program.account.user.fetchNullable(userAccountAddress, "confirmed"); |
46 |
| - assert.equal(userAccountBefore, null); |
| 56 | + await program.methods |
| 57 | + .createUser({ |
| 58 | + name: "John Doe", |
| 59 | + }) |
| 60 | + .accounts({ |
| 61 | + payer: payer.publicKey, |
| 62 | + userAccount: userAccountAddress, |
| 63 | + }) |
| 64 | + .signers([payer]) |
| 65 | + .rpc({ commitment: "confirmed", skipPreflight: true }); |
47 | 66 |
|
48 |
| - await program.methods |
49 |
| - .createUser({ |
50 |
| - name: "John Doe" |
51 |
| - }) |
52 |
| - .accounts({ |
53 |
| - payer: payer.publicKey, |
54 |
| - userAccount: userAccountAddress |
55 |
| - }) |
56 |
| - .signers([payer]) |
57 |
| - .rpc({commitment: "confirmed", skipPreflight: true}); |
| 67 | + const userAccountAfter = await program.account.user.fetchNullable( |
| 68 | + userAccountAddress, |
| 69 | + "confirmed" |
| 70 | + ); |
| 71 | + assert.notEqual(userAccountAfter, null); |
| 72 | + assert.equal(userAccountAfter.name, "John Doe"); |
| 73 | + assert.equal(userAccountAfter.user.toBase58(), payer.publicKey.toBase58()); |
| 74 | + }); |
58 | 75 |
|
59 |
| - const userAccountAfter = await program.account.user.fetchNullable(userAccountAddress, "confirmed"); |
60 |
| - assert.notEqual(userAccountAfter, null); |
61 |
| - assert.equal(userAccountAfter.name, "John Doe"); |
62 |
| - assert.equal(userAccountAfter.user.toBase58(), payer.publicKey.toBase58()); |
63 |
| - }) |
| 76 | + it("Close Account", async () => { |
| 77 | + const [userAccountAddress] = getUserAccount(payer.publicKey); |
| 78 | + const userAccountBefore = await program.account.user.fetchNullable( |
| 79 | + userAccountAddress, |
| 80 | + "confirmed" |
| 81 | + ); |
| 82 | + assert.notEqual(userAccountBefore, null); |
64 | 83 |
|
65 |
| - it("Close Account", async () => { |
66 |
| - const [userAccountAddress] = getUserAccount(payer.publicKey); |
67 |
| - const userAccountBefore = await program.account.user.fetchNullable(userAccountAddress, "confirmed"); |
68 |
| - assert.notEqual(userAccountBefore, null); |
| 84 | + await program.methods |
| 85 | + .closeUser() |
| 86 | + .accounts({ |
| 87 | + user: payer.publicKey, |
| 88 | + userAccount: userAccountAddress, |
| 89 | + }) |
| 90 | + .signers([payer]) |
| 91 | + .rpc({ commitment: "confirmed" }); |
69 | 92 |
|
70 |
| - await program.methods |
71 |
| - .closeUser() |
72 |
| - .accounts({ |
73 |
| - user: payer.publicKey, |
74 |
| - userAccount: userAccountAddress |
75 |
| - }) |
76 |
| - .signers([payer]) |
77 |
| - .rpc({commitment: "confirmed"}); |
78 |
| - |
79 |
| - const userAccountAfter = await program.account.user.fetchNullable(userAccountAddress, "processed"); |
80 |
| - assert.equal(userAccountAfter, null); |
81 |
| - }) |
| 93 | + const userAccountAfter = await program.account.user.fetchNullable( |
| 94 | + userAccountAddress, |
| 95 | + "processed" |
| 96 | + ); |
| 97 | + assert.equal(userAccountAfter, null); |
| 98 | + }); |
82 | 99 | });
|
0 commit comments