Skip to content

Commit 82e640e

Browse files
author
adpthegreat
committed
feat:updated counter tests
1 parent d291364 commit 82e640e

File tree

7 files changed

+171
-53
lines changed

7 files changed

+171
-53
lines changed

basics/counter/poseidon/counter_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-
counter_program = "EvcknV23Y3dkbSa4afZNGw2PgoowcfxCy4qvP8Ghogwu"
8+
counter_program = "3DRpGvotDMHtXzHahF1jdzYEiYa52cwpQcqGiNPw9vRd"
99

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

basics/counter/poseidon/counter_program/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"dependencies": {
99
"@coral-xyz/anchor": "^0.30.1",
10+
"@solana/web3.js": "^1.95.4",
1011
"anchor-bankrun": "^0.5.0",
1112
"solana-bankrun": "^0.4.0"
1213
},

basics/counter/poseidon/counter_program/pnpm-lock.yaml

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

basics/counter/poseidon/counter_program/programs/counter_program/src/lib.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use anchor_lang::prelude::*;
2-
declare_id!("EvcknV23Y3dkbSa4afZNGw2PgoowcfxCy4qvP8Ghogwu");
2+
declare_id!("3DRpGvotDMHtXzHahF1jdzYEiYa52cwpQcqGiNPw9vRd");
33
#[program]
44
pub mod counter_program {
55
use super::*;
66
pub fn initialize_counter(ctx: Context<InitializeCounterContext>) -> Result<()> {
77
ctx.accounts.counter.count = 0;
8+
ctx.accounts.counter.payer = ctx.accounts.payer.key();
89
Ok(())
910
}
1011
pub fn increment(ctx: Context<IncrementContext>) -> Result<()> {
@@ -18,26 +19,34 @@ pub mod counter_program {
1819
}
1920
#[derive(Accounts)]
2021
pub struct InitializeCounterContext<'info> {
21-
#[account(init, payer = payer, space = 17, seeds = [b"count"], bump)]
22-
pub counter: Account<'info, Counter>,
2322
#[account(mut)]
2423
pub payer: Signer<'info>,
24+
#[account(
25+
init,
26+
payer = payer,
27+
space = 49,
28+
seeds = [b"count",
29+
payer.key().as_ref()],
30+
bump,
31+
)]
32+
pub counter: Account<'info, Counter>,
2533
pub system_program: Program<'info, System>,
2634
}
2735
#[derive(Accounts)]
2836
pub struct IncrementContext<'info> {
29-
#[account(mut, seeds = [b"count"], bump)]
37+
#[account(mut)]
3038
pub counter: Account<'info, Counter>,
3139
pub system_program: Program<'info, System>,
3240
}
3341
#[derive(Accounts)]
3442
pub struct DecrementContext<'info> {
35-
#[account(mut, seeds = [b"count"], bump)]
43+
#[account(mut)]
3644
pub counter: Account<'info, Counter>,
3745
pub system_program: Program<'info, System>,
3846
}
3947
#[account]
4048
pub struct Counter {
49+
pub payer: Pubkey,
4150
pub count: u64,
4251
pub bump: u8,
4352
}
Lines changed: 84 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
1+
import assert from 'node:assert';
2+
import { before, describe, it } from 'node:test';
13
import * as anchor from '@coral-xyz/anchor';
2-
import { Keypair, PublicKey } from '@solana/web3.js';
4+
import { Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
35
import { BankrunProvider } from 'anchor-bankrun';
4-
import { assert } from 'chai';
5-
import { startAnchor } from 'solana-bankrun';
6+
import { BanksClient, BanksTransactionResultWithMeta, startAnchor } from 'solana-bankrun';
67
import type { CounterProgram } from '../target/types/counter_program';
78

89
const IDL = require('../target/idl/counter_program.json');
10+
911
const PROGRAM_ID = new PublicKey(IDL.address);
1012

13+
async function createAndProcessTransaction(
14+
client: BanksClient,
15+
payer: Keypair,
16+
instruction: TransactionInstruction,
17+
additionalSigners: Keypair[] = [],
18+
): Promise<BanksTransactionResultWithMeta> {
19+
const tx = new Transaction();
20+
// Get the latest blockhash
21+
const [latestBlockhash] = await client.getLatestBlockhash();
22+
tx.recentBlockhash = latestBlockhash;
23+
// Add transaction instructions
24+
tx.add(instruction);
25+
tx.feePayer = payer.publicKey;
26+
//Add signers
27+
tx.sign(payer, ...additionalSigners);
28+
// Process transaction
29+
const result = await client.tryProcessTransaction(tx);
30+
return result;
31+
}
32+
1133
describe('counter_program', async () => {
1234
// Configure the client to use the anchor-bankrun
1335
const context = await startAnchor('', [{ name: 'counter_program', programId: PROGRAM_ID }], []);
@@ -18,65 +40,108 @@ describe('counter_program', async () => {
1840

1941
const program = new anchor.Program<CounterProgram>(IDL, provider);
2042

21-
const [counterState, _] = anchor.web3.PublicKey.findProgramAddressSync([anchor.utils.bytes.utf8.encode('count')], program.programId);
43+
const counterKeypair = Keypair.generate(); // Generate a new user keypair
44+
45+
before(async () => {
46+
//Transfer SOL to the user account to cover rent
47+
const transferInstruction = SystemProgram.transfer({
48+
fromPubkey: payer.publicKey,
49+
toPubkey: counterKeypair.publicKey,
50+
lamports: 2 * LAMPORTS_PER_SOL,
51+
});
52+
53+
await createAndProcessTransaction(context.banksClient, payer.payer, transferInstruction, [payer.payer]);
54+
const userBalance = await context.banksClient.getBalance(counterKeypair.publicKey);
55+
console.log(`User balance after funding: ${userBalance}`);
56+
});
57+
58+
const [counter, _] = anchor.web3.PublicKey.findProgramAddressSync([Buffer.from('count'), counterKeypair.publicKey.toBuffer()], program.programId);
2259

2360
it('Initialize Counter', async () => {
2461
await program.methods
2562
.initializeCounter()
2663
.accounts({
27-
payer: payer.publicKey,
64+
payer: counterKeypair.publicKey,
2865
})
66+
.signers([counterKeypair])
2967
.rpc();
3068

31-
const currentCount = await program.account.counter.fetch(counterState);
69+
const currentCount = await program.account.counter.fetch(counter);
3270

3371
assert(currentCount.count.toNumber() === 0, 'Expected initialized count to be 0');
3472
});
3573

3674
it('Increment Counter', async () => {
37-
await program.methods.increment().accounts({}).rpc();
75+
await program.methods
76+
.increment()
77+
.accounts({
78+
counter: counter,
79+
})
80+
.rpc();
3881

39-
const currentCount = await program.account.counter.fetch(counterState);
82+
const currentCount = await program.account.counter.fetch(counter);
4083

4184
assert(currentCount.count.toNumber() === 1, 'Expected count to be 1');
4285
});
4386

4487
it('Increment Counter Again', async () => {
45-
await program.methods.increment().accounts({ counter: counterState }).rpc();
88+
await program.methods
89+
.increment()
90+
.accounts({
91+
counter: counter,
92+
})
93+
.rpc();
4694

47-
const currentCount = await program.account.counter.fetch(counterState);
95+
const currentCount = await program.account.counter.fetch(counter);
4896

4997
assert(currentCount.count.toNumber() === 2, 'Expected count to be 2');
5098
});
99+
51100
it('Decrement counter', async () => {
52-
await program.methods.decrement().accounts({}).rpc();
101+
await program.methods
102+
.decrement()
103+
.accounts({
104+
counter: counter,
105+
})
106+
.rpc();
53107

54-
const currentCount = await program.account.counter.fetch(counterState);
108+
const currentCount = await program.account.counter.fetch(counter);
55109
assert(currentCount.count.toNumber() === 1, 'Expected count to be 1');
56110
});
111+
57112
it('Increment and decrement multiple times', async () => {
58113
// Increment the counter 5 times
59114
for (let i = 0; i < 5; i++) {
60-
await program.methods.increment().accounts({}).rpc();
115+
await program.methods
116+
.increment()
117+
.accounts({
118+
counter: counter,
119+
})
120+
.rpc();
61121
}
62122

63-
let currentCount = await program.account.counter.fetch(counterState);
123+
let currentCount = await program.account.counter.fetch(counter);
64124
assert.strictEqual(currentCount.count.toNumber(), 6, 'Expected count to be 6 after 5 increments');
65125

66126
// Decrement the counter 4 times
67127
for (let i = 0; i < 4; i++) {
68-
await program.methods.decrement().accounts({}).rpc();
128+
await program.methods
129+
.decrement()
130+
.accounts({
131+
counter: counter,
132+
})
133+
.rpc();
69134
}
70135

71-
currentCount = await program.account.counter.fetch(counterState);
136+
currentCount = await program.account.counter.fetch(counter);
72137
assert.strictEqual(currentCount.count.toNumber(), 2, 'Expected count to be 2 after 4 decrements');
73138
});
74139

75140
it('Cannot decrement below 0', async () => {
76141
// Decrement the counter to 0
77-
await program.methods.decrement().accounts({}).rpc();
78-
await program.methods.decrement().accounts({}).rpc();
79-
const currentCount = await program.account.counter.fetch(counterState);
142+
await program.methods.decrement().accounts({ counter: counter }).rpc();
143+
await program.methods.decrement().accounts({ counter: counter }).rpc();
144+
const currentCount = await program.account.counter.fetch(counter);
80145
assert.strictEqual(currentCount.count.toNumber(), 0, 'Expected count to be 0 after multiple decrements');
81146
});
82147
});
Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { before, describe, it } from 'node:test';
12
import * as anchor from '@coral-xyz/anchor';
23
import { Program } from '@coral-xyz/anchor';
34
import { Keypair } from '@solana/web3.js';
@@ -14,7 +15,7 @@ describe('counter_program', () => {
1415

1516
const program = anchor.workspace.CounterProgram as Program<CounterProgram>;
1617

17-
const [counterState, _] = anchor.web3.PublicKey.findProgramAddressSync([anchor.utils.bytes.utf8.encode('count')], program.programId);
18+
const [counter, _] = anchor.web3.PublicKey.findProgramAddressSync([anchor.utils.bytes.utf8.encode('count')], program.programId);
1819

1920
it('Initialize Counter', async () => {
2021
await program.methods
@@ -24,55 +25,93 @@ describe('counter_program', () => {
2425
})
2526
.rpc();
2627

27-
const currentCount = await program.account.counter.fetch(counterState);
28+
const currentCount = await program.account.counter.fetch(counter);
2829

2930
assert(currentCount.count.toNumber() === 0, 'Expected initialized count to be 0');
3031
});
3132

3233
it('Increment Counter', async () => {
33-
await program.methods.increment().accounts({}).rpc();
34+
await program.methods
35+
.increment()
36+
.accounts({
37+
counter: counter,
38+
})
39+
.rpc();
3440

35-
const currentCount = await program.account.counter.fetch(counterState);
41+
const currentCount = await program.account.counter.fetch(counter);
3642

3743
assert(currentCount.count.toNumber() === 1, 'Expected count to be 1');
3844
});
3945

4046
it('Increment Counter Again', async () => {
41-
await program.methods.increment().accounts({ counter: counterState }).rpc();
47+
await program.methods
48+
.increment()
49+
.accounts({
50+
counter: counter,
51+
})
52+
.rpc();
4253

43-
const currentCount = await program.account.counter.fetch(counterState);
54+
const currentCount = await program.account.counter.fetch(counter);
4455

4556
assert(currentCount.count.toNumber() === 2, 'Expected count to be 2');
4657
});
4758
it('Decrement counter', async () => {
48-
await program.methods.decrement().accounts({}).rpc();
59+
await program.methods
60+
.decrement()
61+
.accounts({
62+
counter: counter,
63+
})
64+
.rpc();
4965

50-
const currentCount = await program.account.counter.fetch(counterState);
66+
const currentCount = await program.account.counter.fetch(counter);
5167
assert(currentCount.count.toNumber() === 1, 'Expected count to be 1');
5268
});
5369
it('Increment and decrement multiple times', async () => {
5470
// Increment the counter 5 times
5571
for (let i = 0; i < 5; i++) {
56-
await program.methods.increment().accounts({}).rpc();
72+
await program.methods
73+
.increment()
74+
.accounts({
75+
counter: counter,
76+
})
77+
.rpc();
5778
}
5879

59-
let currentCount = await program.account.counter.fetch(counterState);
80+
let currentCount = await program.account.counter.fetch(counter);
6081
assert.strictEqual(currentCount.count.toNumber(), 6, 'Expected count to be 6 after 5 increments');
6182

6283
// Decrement the counter 4 times
6384
for (let i = 0; i < 4; i++) {
64-
await program.methods.decrement().accounts({}).rpc();
85+
await program.methods
86+
.decrement()
87+
.accounts({
88+
counter: counter,
89+
})
90+
.rpc();
6591
}
6692

67-
currentCount = await program.account.counter.fetch(counterState);
93+
currentCount = await program.account.counter.fetch(counter);
6894
assert.strictEqual(currentCount.count.toNumber(), 2, 'Expected count to be 2 after 4 decrements');
6995
});
7096

7197
it('Cannot decrement below 0', async () => {
7298
// Decrement the counter to 0
73-
await program.methods.decrement().accounts({}).rpc();
74-
await program.methods.decrement().accounts({}).rpc();
75-
const currentCount = await program.account.counter.fetch(counterState);
99+
await program.methods
100+
.decrement()
101+
.accounts({
102+
counter: counter,
103+
})
104+
.rpc();
105+
106+
await program.methods
107+
.decrement()
108+
.accounts({
109+
counter: counter,
110+
})
111+
.rpc();
112+
113+
const currentCount = await program.account.counter.fetch(counter);
114+
76115
assert.strictEqual(currentCount.count.toNumber(), 0, 'Expected count to be 0 after multiple decrements');
77116
});
78117
});

0 commit comments

Comments
 (0)