Skip to content

Commit 7b81cf6

Browse files
author
adpthegreat
committed
updated tests
1 parent 1dbf63d commit 7b81cf6

File tree

5 files changed

+144
-52
lines changed

5 files changed

+144
-52
lines changed

basics/close-account/poseidon/close_account_program/Anchor.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ resolution = true
55
skip-lint = false
66

77
[programs.localnet]
8-
close_account_program = "DDhy3V9AQE4wrJ3DXC5Yop2p56J6TiBRaz4zjnYnK8ao"
8+
close_account_program = "AtUc6zMfozxrQoK4PbDUnd5daS86XCPuT2og1293P5XX"
99

1010
[registry]
1111
url = "https://api.apr.dev"

basics/close-account/poseidon/close_account_program/programs/close_account_program/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anchor_lang::prelude::*;
2-
declare_id!("DDhy3V9AQE4wrJ3DXC5Yop2p56J6TiBRaz4zjnYnK8ao");
2+
declare_id!("AtUc6zMfozxrQoK4PbDUnd5daS86XCPuT2og1293P5XX");
33
#[program]
44
pub mod close_account {
55
use super::*;

basics/close-account/poseidon/close_account_program/tests/bankrun.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { BankrunProvider } from 'anchor-bankrun';
1616
import { BanksClient, BanksTransactionResultWithMeta, startAnchor } from 'solana-bankrun';
1717
import type { CloseAccount } from '../target/types/close_account';
1818

19-
const IDL = require('../target/idl/close_account');
19+
const IDL = require('../target/idl/close_account.json');
2020
const PROGRAM_ID = new PublicKey(IDL.address);
2121

2222
async function createAndProcessTransaction(
Lines changed: 131 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,131 @@
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+
});

basics/close-account/poseidon/close_account_program/ts-programs/src/close_account_program.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { Account, String as PoseidonString, Pubkey, Result, Signer, u8 } from '@solanaturbine/poseidon';
22

33
export default class CloseAccount {
4-
static PROGRAM_ID = new Pubkey('DDhy3V9AQE4wrJ3DXC5Yop2p56J6TiBRaz4zjnYnK8ao');
4+
static PROGRAM_ID = new Pubkey(
5+
"AtUc6zMfozxrQoK4PbDUnd5daS86XCPuT2og1293P5XXo"
6+
);
57

6-
createUser(user: Signer, userState: UserState, name: PoseidonString<50>): Result {
7-
userState.derive(['USER', user.key]).init();
8+
createUser(
9+
user: Signer,
10+
userState: UserState,
11+
name: PoseidonString<50>
12+
): Result {
13+
userState.derive(["USER", user.key]).init();
814

915
userState.userBump = userState.getBump();
1016

@@ -13,7 +19,7 @@ export default class CloseAccount {
1319
userState.name = name;
1420
}
1521
closeUser(userAccount: UserState, user: Signer): Result {
16-
userAccount.derive(['USER', user.key]).close(user);
22+
userAccount.derive(["USER", user.key]).close(user);
1723
}
1824
}
1925

0 commit comments

Comments
 (0)