Skip to content

Commit c7d9c92

Browse files
committed
update format
1 parent 107de7a commit c7d9c92

File tree

4 files changed

+122
-108
lines changed

4 files changed

+122
-108
lines changed

basics/close-account/poseidon/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
"mocha": "^9.0.3",
1919
"ts-mocha": "^10.0.0",
2020
"typescript": "^4.3.5"
21+
2122
}
2223
}
Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,63 @@
1-
import assert from 'node:assert';
2-
import { describe, it } from 'node:test';
3-
import * as anchor from '@coral-xyz/anchor';
4-
import { PublicKey } from '@solana/web3.js';
5-
import { BankrunProvider } from 'anchor-bankrun';
6-
import { startAnchor } from 'solana-bankrun';
7-
import type { CloseAccountProgram } from '../target/types/close_account_program';
1+
import assert from "node:assert";
2+
import { describe, it } from "node:test";
3+
import * as anchor from "@coral-xyz/anchor";
4+
import { PublicKey } from "@solana/web3.js";
5+
import { BankrunProvider } from "anchor-bankrun";
6+
import { startAnchor } from "solana-bankrun";
7+
import type { CloseAccountProgram } from "../target/types/close_account_program";
88

9-
const IDL = require('../target/idl/close_account_program.json');
9+
const IDL = require("../target/idl/close_account_program.json");
1010
const PROGRAM_ID = new PublicKey(IDL.address);
1111

12-
describe('close-account', async () => {
13-
// Configure the client to use the local cluster.
14-
const context = await startAnchor('', [{ name: 'close_account_program', programId: PROGRAM_ID }], []);
15-
const provider = new BankrunProvider(context);
12+
describe("close-account", async () => {
13+
// Configure the client to use the local cluster.
14+
const context = await startAnchor(
15+
"",
16+
[{ name: "close_account_program", programId: PROGRAM_ID }],
17+
[],
18+
);
19+
const provider = new BankrunProvider(context);
1620

17-
const payer = provider.wallet as anchor.Wallet;
18-
const program = new anchor.Program<CloseAccountProgram>(IDL, provider);
19-
// Derive the PDA for the user's account.
20-
const [userAccountAddress] = PublicKey.findProgramAddressSync([Buffer.from('user'), payer.publicKey.toBuffer()], program.programId);
21+
const payer = provider.wallet as anchor.Wallet;
22+
const program = new anchor.Program<CloseAccountProgram>(IDL, provider);
23+
// Derive the PDA for the user's account.
24+
const [userAccountAddress] = PublicKey.findProgramAddressSync(
25+
[Buffer.from("user"), payer.publicKey.toBuffer()],
26+
program.programId,
27+
);
2128

22-
it('Create Account', async () => {
23-
await program.methods
24-
.createUser()
25-
.accounts({
26-
user: payer.publicKey,
27-
})
28-
.rpc();
29+
it("Create Account", async () => {
30+
await program.methods
31+
.createUser()
32+
.accounts({
33+
user: payer.publicKey,
34+
})
35+
.rpc();
2936

30-
// Fetch the account data
31-
const userAccount = await program.account.closeAccountState.fetch(userAccountAddress);
32-
assert.equal(userAccount.user.toBase58(), payer.publicKey.toBase58());
33-
});
37+
// Fetch the account data
38+
const userAccount =
39+
await program.account.closeAccountState.fetch(userAccountAddress);
40+
assert.equal(userAccount.user.toBase58(), payer.publicKey.toBase58());
41+
});
3442

35-
it('Close Account', async () => {
36-
await program.methods
37-
.closeUser()
38-
.accounts({
39-
user: payer.publicKey,
40-
})
41-
.rpc();
43+
it("Close Account", async () => {
44+
await program.methods
45+
.closeUser()
46+
.accounts({
47+
user: payer.publicKey,
48+
})
49+
.rpc();
4250

43-
// The account should no longer exist, returning null.
44-
try {
45-
const userAccount = await program.account.closeAccountState.fetchNullable(userAccountAddress);
46-
assert.equal(userAccount, null);
47-
} catch (err) {
48-
// Won't return null and will throw an error in anchor-bankrun'
49-
assert.equal(err.message, `Could not find ${userAccountAddress}`);
50-
}
51-
});
52-
});
51+
// The account should no longer exist, returning null.
52+
try {
53+
const userAccount =
54+
await program.account.closeAccountState.fetchNullable(
55+
userAccountAddress,
56+
);
57+
assert.equal(userAccount, null);
58+
} catch (err) {
59+
// Won't return null and will throw an error in anchor-bankrun'
60+
assert.equal(err.message, `Could not find ${userAccountAddress}`);
61+
}
62+
});
63+
});
Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,52 @@
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';
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";
55
import { CloseAccountProgram } from "../target/types/close_account_program";
66

77
describe("close_account", () => {
8-
// Configure the client to use the local cluster.
9-
10-
const provider = anchor.AnchorProvider.env();
11-
anchor.setProvider(provider);
12-
13-
const program = anchor.workspace.CloseAccountProgram as Program<CloseAccountProgram>;
14-
const payer = provider.wallet as anchor.Wallet;
15-
16-
// Derive the PDA for the user's account.
17-
const [userAccountAddress] = PublicKey.findProgramAddressSync([Buffer.from('user'), payer.publicKey.toBuffer()], program.programId);
18-
19-
20-
it('Create Account', async () => {
21-
await program.methods
22-
.createUser()
23-
.accounts({
24-
user: payer.publicKey,
25-
userAccount: userAccountAddress,
26-
})
27-
.rpc();
28-
29-
// Fetch the account data
30-
const userAccount = await program.account.closeAccountState.fetch(userAccountAddress);
31-
assert.equal(userAccount.user.toBase58(), payer.publicKey.toBase58());
32-
});
33-
34-
it('Close Account', async () => {
35-
await program.methods
36-
.closeUser()
37-
.accounts({
38-
user: payer.publicKey,
39-
userAccount: userAccountAddress,
40-
})
41-
.rpc();
42-
43-
// The account should no longer exist, returning null.
44-
const userAccount = await program.account.closeAccountState.fetchNullable(userAccountAddress);
45-
assert.equal(userAccount, null);
46-
});
47-
48-
8+
// Configure the client to use the local cluster.
9+
10+
const provider = anchor.AnchorProvider.env();
11+
anchor.setProvider(provider);
12+
13+
const program = anchor.workspace
14+
.CloseAccountProgram as Program<CloseAccountProgram>;
15+
const payer = provider.wallet as anchor.Wallet;
16+
17+
// Derive the PDA for the user's account.
18+
const [userAccountAddress] = PublicKey.findProgramAddressSync(
19+
[Buffer.from("user"), payer.publicKey.toBuffer()],
20+
program.programId,
21+
);
22+
23+
it("Create Account", async () => {
24+
await program.methods
25+
.createUser()
26+
.accounts({
27+
user: payer.publicKey,
28+
userAccount: userAccountAddress,
29+
})
30+
.rpc();
31+
32+
// Fetch the account data
33+
const userAccount =
34+
await program.account.closeAccountState.fetch(userAccountAddress);
35+
assert.equal(userAccount.user.toBase58(), payer.publicKey.toBase58());
36+
});
37+
38+
it("Close Account", async () => {
39+
await program.methods
40+
.closeUser()
41+
.accounts({
42+
user: payer.publicKey,
43+
userAccount: userAccountAddress,
44+
})
45+
.rpc();
46+
47+
// The account should no longer exist, returning null.
48+
const userAccount =
49+
await program.account.closeAccountState.fetchNullable(userAccountAddress);
50+
assert.equal(userAccount, null);
51+
});
4952
});
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
import { Account, Pubkey, Result, u8, Signer } from '@solanaturbine/poseidon';
1+
import { Account, Pubkey, Result, u8, Signer } from "@solanaturbine/poseidon";
22

33
export default class closeAccountProgram {
4-
static PROGRAM_ID = new Pubkey('7U4SZvsUMjGYCwnzqGGE9enJactKhVPF2EaE7VHtBLTd');
4+
static PROGRAM_ID = new Pubkey(
5+
"7U4SZvsUMjGYCwnzqGGE9enJactKhVPF2EaE7VHtBLTd",
6+
);
57

6-
// create user account
8+
// create user account
79

8-
create_user(user_account: closeAccountState, user: Signer): Result {
10+
create_user(user_account: closeAccountState, user: Signer): Result {
11+
user_account.derive(["user", user.key]).init();
912

10-
user_account.derive(['user', user.key]).init();
13+
// Set the initial value to the `user_account` fields
14+
user_account.user = user.key;
15+
user_account.bump = user_account.getBump();
16+
}
1117

12-
// Set the initial value to the `user_account` fields
13-
user_account.user = user.key;
14-
user_account.bump = user_account.getBump();
15-
16-
}
17-
18-
// close user account
19-
20-
close_user(user_account:closeAccountState, user:Signer): Result {
21-
user_account.close(user)
22-
}
18+
// close user account
2319

20+
close_user(user_account: closeAccountState, user: Signer): Result {
21+
user_account.close(user);
22+
}
2423
}
2524

2625
// setup the state
2726

2827
export interface closeAccountState extends Account {
29-
user: Pubkey; // This field store the user pub key
30-
bump: u8; // bump is for PDA (program derieved account, a special type of account which controlled by program on Solana)
31-
}
28+
user: Pubkey; // This field store the user pub key
29+
bump: u8; // bump is for PDA (program derieved account, a special type of account which controlled by program on Solana)
30+
}

0 commit comments

Comments
 (0)