Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions clients/js/test/allocate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
appendTransactionMessageInstruction,
fetchEncodedAccount,
generateKeyPairSigner,
pipe,
airdropFactory,
lamports,
} from '@solana/kit';
import { it, expect } from 'vitest';
import { getAllocateInstruction } from '../src';
import {
createDefaultSolanaClient,
createDefaultTransaction,
generateKeyPairSignerWithSol,
signAndSendTransaction,
} from './_setup';

it('allocates space for an account', async () => {
// 1. Setup client and payer.
const client = createDefaultSolanaClient();
const [payer, accountToAllocate] = await Promise.all([
generateKeyPairSignerWithSol(client),
generateKeyPairSigner(),
]);

// 2. Airdrop lamports to the account so it exists (system owned, 0 data).
await airdropFactory(client)({
recipientAddress: accountToAllocate.address,
lamports: lamports(await client.rpc.getMinimumBalanceForRentExemption(100n).send()),
commitment: 'confirmed',
});

// Verify initial state
let fetchedAccount = await fetchEncodedAccount(client.rpc, accountToAllocate.address);
expect(fetchedAccount.exists).toBe(true);
if (fetchedAccount.exists) {
expect(fetchedAccount.data.length).toBe(0);
}

// 3. Use getAllocateInstruction to allocate 100 bytes.
const newSpace = 100n;
const allocate = getAllocateInstruction({
newAccount: accountToAllocate,
space: newSpace,
});

// 4. Send transaction
await pipe(
await createDefaultTransaction(client, payer),
tx => appendTransactionMessageInstruction(allocate, tx),
tx => signAndSendTransaction(client, tx),
);

// 5. Verify the account's data length is exactly 100 bytes.
fetchedAccount = await fetchEncodedAccount(client.rpc, accountToAllocate.address);
expect(fetchedAccount.exists).toBe(true);
if (fetchedAccount.exists) {
expect(fetchedAccount.data.length).toBe(100);
}
});