Skip to content

Commit abcb12d

Browse files
committed
test: implement bankrun tests for close account program
1 parent dd6510b commit abcb12d

5 files changed

Lines changed: 82 additions & 6 deletions

File tree

basics/close-account/anchor/Anchor.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
solana_version = "3.1.8"
33

44
[features]
5-
seeds = false
5+
resolution = true
66
skip-lint = false
77

88
[programs.localnet]
9-
close_account_program = "99TQtoDdQ5NS2v5Ppha93aqEmv3vV9VZVfHTP5rGST3c"
10-
11-
# [registry] section removed — no longer used in Anchor 1.0
9+
close_account_program = "7vVv26XfvWz8stD4WMnS9KtgLFQq41XCC5sY4WtVjkgv"
1210

1311
[provider]
14-
cluster = "Localnet"
12+
cluster = "localnet"
1513
wallet = "~/.config/solana/id.json"
1614

1715
[scripts]
1816
test = "pnpm ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
17+
18+
[hooks]

basics/close-account/anchor/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"litesvm": "^0.4.0"
1010
},
1111
"devDependencies": {
12+
"@coral-xyz/anchor": "^0.32.1",
1213
"@types/bn.js": "^5.1.0",
1314
"@types/chai": "^4.3.0",
1415
"@types/mocha": "^9.0.0",

basics/close-account/anchor/pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basics/close-account/anchor/programs/close-account/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod instructions;
33
mod state;
44
use instructions::*;
55

6-
declare_id!("99TQtoDdQ5NS2v5Ppha93aqEmv3vV9VZVfHTP5rGST3c");
6+
declare_id!("7vVv26XfvWz8stD4WMnS9KtgLFQq41XCC5sY4WtVjkgv");
77

88
#[program]
99
pub mod close_account_program {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as anchor from "@coral-xyz/anchor";
2+
import { Program } from "@coral-xyz/anchor";
3+
import { Keypair, PublicKey } from "@solana/web3.js";
4+
import { startAnchor } from "solana-bankrun";
5+
import { BankrunProvider } from "anchor-bankrun";
6+
import { assert } from "chai";
7+
import { CloseAccountProgram } from "../target/types/close_account_program";
8+
import IDL from "../target/idl/close_account_program.json" with { type: 'json' };
9+
10+
describe("create_account and close_account", () => {
11+
let provider: BankrunProvider;
12+
let program: Program<CloseAccountProgram>;
13+
let payer: Keypair;
14+
let dataAccount: PublicKey;
15+
16+
before(async () => {
17+
const context = await startAnchor(
18+
"",
19+
[{ name: "close_account_program", programId: new PublicKey(IDL.address) }],
20+
[]
21+
);
22+
provider = new BankrunProvider(context);
23+
anchor.setProvider(provider);
24+
// Note: we might need to cast IDL depending on your setup.
25+
program = new Program<CloseAccountProgram>(IDL as any, provider);
26+
payer = provider.wallet.payer;
27+
const [pda] = PublicKey.findProgramAddressSync(
28+
[Buffer.from("USER"), payer.publicKey.toBuffer()],
29+
program.programId
30+
);
31+
dataAccount = pda;
32+
});
33+
34+
it("create account", async () => {
35+
const tx = await program.methods.createUser("test")
36+
.accounts({
37+
user: payer.publicKey,
38+
userAccount: dataAccount,
39+
})
40+
.signers([payer])
41+
.rpc();
42+
console.log("Your transaction signature", tx);
43+
assert.ok(true);
44+
45+
const account = await program.account.userState.fetch(dataAccount);
46+
assert.equal(account.user.toBase58(), payer.publicKey.toBase58());
47+
assert.equal(account.name, "test");
48+
});
49+
50+
it("close account", async () => {
51+
const tx = await program.methods.closeUser()
52+
.accounts({
53+
user: payer.publicKey,
54+
userAccount: dataAccount,
55+
})
56+
.signers([payer])
57+
.rpc();
58+
console.log("Close account transaction signature", tx);
59+
60+
// When an account is closed, fetching it should fail
61+
try {
62+
await program.account.userState.fetch(dataAccount);
63+
// If it succeeds, the account was not closed properly, so we fail the test
64+
assert.fail("The account should have been closed and not fetchable");
65+
} catch (err: any) {
66+
// We expect an error because the account was closed/deleted
67+
console.log("Account successfully deleted! Fetch error:", err.message);
68+
assert.ok(true);
69+
}
70+
assert.ok(true);
71+
});
72+
});

0 commit comments

Comments
 (0)