Skip to content

Commit

Permalink
[wip]: Add initialize test for canonical PDAs
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Jan 15, 2025
1 parent 15ecbd6 commit cade8c3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
"typescript": "^5.5.3"
},
"ava": {
"files": [
"test/**/*.test.ts"
],
"nodeArguments": [
"--no-warnings"
],
Expand Down
59 changes: 59 additions & 0 deletions clients/js/test/initialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DataSource,
Encoding,
fetchMetadata,
findCanonicalPda,
findNonCanonicalPda,
Format,
getExternalDataEncoder,
Expand All @@ -22,10 +23,68 @@ import {
import {
createDefaultSolanaClient,
createDefaultTransaction,
createDeployedProgram,
generateKeyPairSignerWithSol,
signAndSendTransaction,
} from './_setup';

test('it initializes a canonical PDA with direct data from instruction data', async (t) => {
// Given the following authority and deployed program.
const client = createDefaultSolanaClient();
const authority = await generateKeyPairSignerWithSol(client);
const [program, programData] = await createDeployedProgram(client, authority);

// And the following metadata seed and data.
const seed = 'dummy';
const data = getUtf8Encoder().encode('Hello, World!');

// And given the metadata account is pre-funded.
const [metadata] = await findCanonicalPda({ program, seed });
const rent = await client.rpc
.getMinimumBalanceForRentExemption(96n + BigInt(data.length))
.send();
const preFund = getTransferSolInstruction({
source: authority,
destination: metadata,
amount: rent,
});

// When we initialize the metadata account with the data on the instruction.
const initialize = await getInitializeInstructionAsync({
authority,
program,
programData,
seed,
encoding: Encoding.Utf8,
compression: Compression.None,
format: Format.None,
dataSource: DataSource.Direct,
data,
});
await pipe(
await createDefaultTransaction(client, authority),
(tx) => appendTransactionMessageInstructions([preFund, initialize], tx),
(tx) => signAndSendTransaction(client, tx)
);

// Then we expect the following metadata account to be created.
const metadataAccount = await fetchMetadata(client.rpc, metadata);
t.like(metadataAccount.data, <Metadata>{
discriminator: AccountDiscriminator.Metadata,
program,
authority: some(authority.address),
mutable: true,
canonical: true,
seed: 'dummy',
encoding: Encoding.Utf8,
compression: Compression.None,
format: Format.None,
dataSource: DataSource.Direct,
dataLength: data.length,
data,
});
});

test('it initializes a non canonical PDA with direct data from instruction data', async (t) => {
// Given the following authority and program.
const client = createDefaultSolanaClient();
Expand Down

0 comments on commit cade8c3

Please sign in to comment.