Skip to content

Commit d6be417

Browse files
authored
js: add allocate test (#67)
* docs: improve IDL and add allocate test * chore: revert generated files to main
1 parent 549ef04 commit d6be417

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

clients/js/test/allocate.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {
2+
appendTransactionMessageInstruction,
3+
fetchEncodedAccount,
4+
generateKeyPairSigner,
5+
pipe,
6+
airdropFactory,
7+
lamports,
8+
} from '@solana/kit';
9+
import { it, expect } from 'vitest';
10+
import { getAllocateInstruction } from '../src';
11+
import {
12+
createDefaultSolanaClient,
13+
createDefaultTransaction,
14+
generateKeyPairSignerWithSol,
15+
signAndSendTransaction,
16+
} from './_setup';
17+
18+
it('allocates space for an account', async () => {
19+
// 1. Setup client and payer.
20+
const client = createDefaultSolanaClient();
21+
const [payer, accountToAllocate] = await Promise.all([
22+
generateKeyPairSignerWithSol(client),
23+
generateKeyPairSigner(),
24+
]);
25+
26+
// 2. Airdrop lamports to the account so it exists (system owned, 0 data).
27+
await airdropFactory(client)({
28+
recipientAddress: accountToAllocate.address,
29+
lamports: lamports(await client.rpc.getMinimumBalanceForRentExemption(100n).send()),
30+
commitment: 'confirmed',
31+
});
32+
33+
// Verify initial state
34+
let fetchedAccount = await fetchEncodedAccount(client.rpc, accountToAllocate.address);
35+
expect(fetchedAccount.exists).toBe(true);
36+
if (fetchedAccount.exists) {
37+
expect(fetchedAccount.data.length).toBe(0);
38+
}
39+
40+
// 3. Use getAllocateInstruction to allocate 100 bytes.
41+
const newSpace = 100n;
42+
const allocate = getAllocateInstruction({
43+
newAccount: accountToAllocate,
44+
space: newSpace,
45+
});
46+
47+
// 4. Send transaction
48+
await pipe(
49+
await createDefaultTransaction(client, payer),
50+
tx => appendTransactionMessageInstruction(allocate, tx),
51+
tx => signAndSendTransaction(client, tx),
52+
);
53+
54+
// 5. Verify the account's data length is exactly 100 bytes.
55+
fetchedAccount = await fetchEncodedAccount(client.rpc, accountToAllocate.address);
56+
expect(fetchedAccount.exists).toBe(true);
57+
if (fetchedAccount.exists) {
58+
expect(fetchedAccount.data.length).toBe(100);
59+
}
60+
});

0 commit comments

Comments
 (0)