Skip to content

Commit 2ab93ce

Browse files
committed
feat(tests): Add test to initialize multiple accounts with the same payer
1 parent 74f1898 commit 2ab93ce

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

tests/duplicate-mutable-accounts/programs/duplicate-mutable-accounts/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ pub mod duplicate_mutable_accounts {
5656
}
5757
Ok(())
5858
}
59+
60+
// Test initializing multiple accounts with the same payer
61+
pub fn init_multiple_with_same_payer(
62+
ctx: Context<InitMultipleWithSamePayer>,
63+
initial1: u64,
64+
initial2: u64,
65+
) -> Result<()> {
66+
ctx.accounts.data_account1.count = initial1;
67+
ctx.accounts.data_account2.count = initial2;
68+
Ok(())
69+
}
5970
}
6071

6172
#[account]
@@ -115,3 +126,15 @@ pub struct UseRemainingAccounts<'info> {
115126
#[account(mut)]
116127
pub account1: Account<'info, Counter>,
117128
}
129+
130+
// Test initializing multiple accounts with the same payer
131+
#[derive(Accounts)]
132+
pub struct InitMultipleWithSamePayer<'info> {
133+
#[account(init, payer = user, space = 8 + 8)]
134+
pub data_account1: Account<'info, Counter>,
135+
#[account(init, payer = user, space = 8 + 8)]
136+
pub data_account2: Account<'info, Counter>,
137+
#[account(mut)]
138+
pub user: Signer<'info>,
139+
pub system_program: Program<'info, System>,
140+
}

tests/duplicate-mutable-accounts/tests/duplicate-mutable-accounts.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,44 @@ describe("duplicate-mutable-accounts", () => {
191191
"Account1 should be incremented"
192192
);
193193
});
194+
195+
it("Should allow initializing multiple accounts with the same payer", async () => {
196+
// Create two new keypairs for the accounts to be initialized
197+
const newAccount1 = anchor.web3.Keypair.generate();
198+
const newAccount2 = anchor.web3.Keypair.generate();
199+
200+
// Initialize both accounts using the same payer
201+
// This should succeed because:
202+
// 1. The payer is a Signer, which is excluded from duplicate checks
203+
// 2. The accounts being initialized are different (newAccount1 vs newAccount2)
204+
await program.methods
205+
.initMultipleWithSamePayer(new anchor.BN(500), new anchor.BN(600))
206+
.accounts({
207+
dataAccount1: newAccount1.publicKey,
208+
dataAccount2: newAccount2.publicKey,
209+
user: user_wallet.publicKey,
210+
systemProgram: anchor.web3.SystemProgram.programId,
211+
})
212+
.signers([user_wallet, newAccount1, newAccount2])
213+
.rpc();
214+
215+
// Verify both accounts were created with correct values
216+
const account1Data = await program.account.counter.fetch(
217+
newAccount1.publicKey
218+
);
219+
const account2Data = await program.account.counter.fetch(
220+
newAccount2.publicKey
221+
);
222+
223+
assert.equal(
224+
account1Data.count.toNumber(),
225+
500,
226+
"First account should have count 500"
227+
);
228+
assert.equal(
229+
account2Data.count.toNumber(),
230+
600,
231+
"Second account should have count 600"
232+
);
233+
});
194234
});

0 commit comments

Comments
 (0)