Skip to content

Commit 549ef04

Browse files
authored
js: add assign test (#66)
* feat: add validator scripts and assign instruction test * chore: address maintainer feedback
1 parent ab44cc5 commit 549ef04

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

clients/js/test/assign.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { appendTransactionMessageInstruction, fetchEncodedAccount, generateKeyPairSigner, pipe } from '@solana/kit';
2+
import { it, expect } from 'vitest';
3+
import { SYSTEM_PROGRAM_ADDRESS, getAssignInstruction, getTransferSolInstruction } from '../src';
4+
import {
5+
createDefaultSolanaClient,
6+
createDefaultTransaction,
7+
generateKeyPairSignerWithSol,
8+
signAndSendTransaction,
9+
} from './_setup';
10+
11+
it('assigns a new owner to an account', async () => {
12+
// 1. Setup client and payer.
13+
const client = createDefaultSolanaClient();
14+
const [payer, accountToAssign, newOwner] = await Promise.all([
15+
generateKeyPairSignerWithSol(client),
16+
generateKeyPairSigner(),
17+
generateKeyPairSigner(),
18+
]);
19+
20+
// 2. Create the account first (so it exists on-chain).
21+
// The account needs to exist to be assigned.
22+
const space = 0n;
23+
const lamports = await client.rpc.getMinimumBalanceForRentExemption(space).send();
24+
25+
const transfer = getTransferSolInstruction({
26+
source: payer,
27+
destination: accountToAssign.address,
28+
amount: lamports,
29+
});
30+
31+
// 3. Use getAssignInstruction to change the owner of accountToAssign to newOwner.
32+
const assign = getAssignInstruction({
33+
account: accountToAssign,
34+
programAddress: newOwner.address,
35+
});
36+
37+
// 4. Sign and send the transaction.
38+
await pipe(
39+
await createDefaultTransaction(client, payer),
40+
tx => appendTransactionMessageInstruction(transfer, tx),
41+
tx => appendTransactionMessageInstruction(assign, tx),
42+
tx => signAndSendTransaction(client, tx),
43+
);
44+
45+
// 5. Fetch the account data and verify the owner.
46+
const fetchedAccount = await fetchEncodedAccount(client.rpc, accountToAssign.address);
47+
// In solana/kit, 'programAddress' is the field for the account owner.
48+
expect(fetchedAccount).toMatchObject({
49+
exists: true,
50+
programAddress: newOwner.address,
51+
});
52+
});

0 commit comments

Comments
 (0)