Skip to content

Commit d071ee7

Browse files
committed
Add tests
1 parent 2766eb2 commit d071ee7

File tree

4 files changed

+102
-16
lines changed

4 files changed

+102
-16
lines changed

clients/js/src/generated/instructions/createAccountWithSeed.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ export type CreateAccountWithSeedInstructionData = {
8181
discriminator: number;
8282
base: Address;
8383
seed: string;
84-
amount: bigint;
84+
lamports: bigint;
8585
space: bigint;
8686
programAddress: Address;
8787
};
8888

8989
export type CreateAccountWithSeedInstructionDataArgs = {
9090
base: Address;
9191
seed: string;
92-
amount: number | bigint;
92+
lamports: number | bigint;
9393
space: number | bigint;
9494
programAddress: Address;
9595
};
@@ -100,7 +100,7 @@ export function getCreateAccountWithSeedInstructionDataEncoder(): Encoder<Create
100100
['discriminator', getU32Encoder()],
101101
['base', getAddressEncoder()],
102102
['seed', addEncoderSizePrefix(getUtf8Encoder(), getU64Encoder())],
103-
['amount', getU64Encoder()],
103+
['lamports', getU64Encoder()],
104104
['space', getU64Encoder()],
105105
['programAddress', getAddressEncoder()],
106106
]),
@@ -116,7 +116,7 @@ export function getCreateAccountWithSeedInstructionDataDecoder(): Decoder<Create
116116
['discriminator', getU32Decoder()],
117117
['base', getAddressDecoder()],
118118
['seed', addDecoderSizePrefix(getUtf8Decoder(), getU64Decoder())],
119-
['amount', getU64Decoder()],
119+
['lamports', getU64Decoder()],
120120
['space', getU64Decoder()],
121121
['programAddress', getAddressDecoder()],
122122
]);
@@ -142,7 +142,7 @@ export type CreateAccountWithSeedInput<
142142
baseAccount?: TransactionSigner<TAccountBaseAccount>;
143143
base: CreateAccountWithSeedInstructionDataArgs['base'];
144144
seed: CreateAccountWithSeedInstructionDataArgs['seed'];
145-
amount: CreateAccountWithSeedInstructionDataArgs['amount'];
145+
lamports: CreateAccountWithSeedInstructionDataArgs['lamports'];
146146
space: CreateAccountWithSeedInstructionDataArgs['space'];
147147
programAddress: CreateAccountWithSeedInstructionDataArgs['programAddress'];
148148
};
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {
2+
appendTransactionMessageInstruction,
3+
createAddressWithSeed,
4+
fetchEncodedAccount,
5+
generateKeyPairSigner,
6+
KeyPairSigner,
7+
pipe,
8+
} from '@solana/kit';
9+
import test, { ExecutionContext } from 'ava';
10+
import { getCreateAccountWithSeedInstruction } from '../src';
11+
import {
12+
createDefaultSolanaClient,
13+
createDefaultTransaction,
14+
generateKeyPairSignerWithSol,
15+
signAndSendTransaction,
16+
} from './_setup';
17+
18+
async function createAccountWithSeedTest(
19+
t: ExecutionContext<unknown>,
20+
baseIsPayer: boolean
21+
) {
22+
const client = createDefaultSolanaClient();
23+
const space = 42n;
24+
const [payer, program, lamports] = await Promise.all([
25+
generateKeyPairSignerWithSol(client),
26+
generateKeyPairSigner(),
27+
client.rpc.getMinimumBalanceForRentExemption(space).send(),
28+
]);
29+
let baseAccount = await generateKeyPairSigner();
30+
31+
if (baseIsPayer) {
32+
baseAccount = payer;
33+
}
34+
35+
const programAddress = program.address;
36+
const SEED = '123456789';
37+
const newAccount = await createAddressWithSeed({
38+
baseAddress: baseAccount.address,
39+
programAddress,
40+
seed: SEED,
41+
});
42+
43+
let extraArgs: {} | { baseAccount: KeyPairSigner<string> } = {};
44+
if (!baseIsPayer) {
45+
extraArgs = { baseAccount };
46+
}
47+
48+
// When we call createAccountWithSeed in a transaction.
49+
const createAccount = getCreateAccountWithSeedInstruction({
50+
payer,
51+
newAccount,
52+
base: baseAccount.address,
53+
seed: SEED,
54+
space,
55+
lamports,
56+
programAddress,
57+
...extraArgs,
58+
});
59+
await pipe(
60+
await createDefaultTransaction(client, payer),
61+
(tx) => appendTransactionMessageInstruction(createAccount, tx),
62+
(tx) => signAndSendTransaction(client, tx)
63+
);
64+
65+
// Then we expect the following account data.
66+
const fetchedAccount = await fetchEncodedAccount(client.rpc, newAccount);
67+
t.deepEqual(fetchedAccount, {
68+
executable: false,
69+
lamports,
70+
programAddress,
71+
address: newAccount,
72+
data: new Uint8Array(Array.from({ length: 42 }, () => 0)),
73+
exists: true,
74+
space: 42n,
75+
});
76+
}
77+
78+
test('it creates a new empty account when base is not payer', (t) =>
79+
createAccountWithSeedTest(t, false));
80+
81+
test('it creates a new empty account when base is payer', (t) =>
82+
createAccountWithSeedTest(t, true));

clients/rust/src/generated/instructions/create_account_with_seed.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl Default for CreateAccountWithSeedInstructionData {
8484
pub struct CreateAccountWithSeedInstructionArgs {
8585
pub base: Pubkey,
8686
pub seed: U64PrefixString,
87-
pub amount: u64,
87+
pub lamports: u64,
8888
pub space: u64,
8989
pub program_address: Pubkey,
9090
}
@@ -103,7 +103,7 @@ pub struct CreateAccountWithSeedBuilder {
103103
base_account: Option<solana_program::pubkey::Pubkey>,
104104
base: Option<Pubkey>,
105105
seed: Option<U64PrefixString>,
106-
amount: Option<u64>,
106+
lamports: Option<u64>,
107107
space: Option<u64>,
108108
program_address: Option<Pubkey>,
109109
__remaining_accounts: Vec<solana_program::instruction::AccountMeta>,
@@ -143,8 +143,8 @@ impl CreateAccountWithSeedBuilder {
143143
self
144144
}
145145
#[inline(always)]
146-
pub fn amount(&mut self, amount: u64) -> &mut Self {
147-
self.amount = Some(amount);
146+
pub fn lamports(&mut self, lamports: u64) -> &mut Self {
147+
self.lamports = Some(lamports);
148148
self
149149
}
150150
#[inline(always)]
@@ -185,7 +185,7 @@ impl CreateAccountWithSeedBuilder {
185185
let args = CreateAccountWithSeedInstructionArgs {
186186
base: self.base.clone().expect("base is not set"),
187187
seed: self.seed.clone().expect("seed is not set"),
188-
amount: self.amount.clone().expect("amount is not set"),
188+
lamports: self.lamports.clone().expect("lamports is not set"),
189189
space: self.space.clone().expect("space is not set"),
190190
program_address: self
191191
.program_address
@@ -339,7 +339,7 @@ impl<'a, 'b> CreateAccountWithSeedCpiBuilder<'a, 'b> {
339339
base_account: None,
340340
base: None,
341341
seed: None,
342-
amount: None,
342+
lamports: None,
343343
space: None,
344344
program_address: None,
345345
__remaining_accounts: Vec::new(),
@@ -379,8 +379,8 @@ impl<'a, 'b> CreateAccountWithSeedCpiBuilder<'a, 'b> {
379379
self
380380
}
381381
#[inline(always)]
382-
pub fn amount(&mut self, amount: u64) -> &mut Self {
383-
self.instruction.amount = Some(amount);
382+
pub fn lamports(&mut self, lamports: u64) -> &mut Self {
383+
self.instruction.lamports = Some(lamports);
384384
self
385385
}
386386
#[inline(always)]
@@ -437,7 +437,11 @@ impl<'a, 'b> CreateAccountWithSeedCpiBuilder<'a, 'b> {
437437
let args = CreateAccountWithSeedInstructionArgs {
438438
base: self.instruction.base.clone().expect("base is not set"),
439439
seed: self.instruction.seed.clone().expect("seed is not set"),
440-
amount: self.instruction.amount.clone().expect("amount is not set"),
440+
lamports: self
441+
.instruction
442+
.lamports
443+
.clone()
444+
.expect("lamports is not set"),
441445
space: self.instruction.space.clone().expect("space is not set"),
442446
program_address: self
443447
.instruction
@@ -473,7 +477,7 @@ struct CreateAccountWithSeedCpiBuilderInstruction<'a, 'b> {
473477
base_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
474478
base: Option<Pubkey>,
475479
seed: Option<U64PrefixString>,
476-
amount: Option<u64>,
480+
lamports: Option<u64>,
477481
space: Option<u64>,
478482
program_address: Option<Pubkey>,
479483
/// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.

program/idl.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
},
297297
{
298298
"kind": "instructionArgumentNode",
299-
"name": "amount",
299+
"name": "lamports",
300300
"type": {
301301
"kind": "numberTypeNode",
302302
"format": "u64",

0 commit comments

Comments
 (0)