|
1 | | -import { |
2 | | - type Blockhash, |
3 | | - Keypair, |
4 | | - LAMPORTS_PER_SOL, |
5 | | - PublicKey, |
6 | | - SystemProgram, |
7 | | - Transaction, |
8 | | - TransactionInstruction, |
9 | | -} from "@solana/web3.js"; |
10 | | -import { assert } from "chai"; |
11 | | -import { before, describe, it } from "mocha"; |
12 | | -import { |
13 | | - type BanksClient, |
14 | | - type ProgramTestContext, |
15 | | - start, |
16 | | -} from "solana-bankrun"; |
| 1 | +import { type Blockhash, Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'; |
| 2 | +import { assert } from 'chai'; |
| 3 | +import { before, describe, it } from 'mocha'; |
| 4 | +import { type BanksClient, type ProgramTestContext, start } from 'solana-bankrun'; |
17 | 5 |
|
18 | | -const PROGRAM_ID = new PublicKey( |
19 | | - "12rpZ18eGj7BeKvSFRZ45cni97HctTbKziBnW3MsH3NG", |
20 | | -); |
21 | | -const SEED = Buffer.from("createaccount"); // Convert to binary (bytes) |
| 6 | +const PROGRAM_ID = new PublicKey('12rpZ18eGj7BeKvSFRZ45cni97HctTbKziBnW3MsH3NG'); |
22 | 7 |
|
23 | | -describe("Create a system account", () => { |
| 8 | +const instructionDiscriminators = { |
| 9 | + InitializeAccount: Buffer.from([0]), |
| 10 | +}; |
| 11 | + |
| 12 | +describe('Create a system account', () => { |
24 | 13 | let context: ProgramTestContext; |
25 | 14 | let lastBlock: Blockhash; |
26 | 15 | let client: BanksClient; |
27 | 16 | let payer: Keypair; |
28 | 17 |
|
29 | 18 | before(async () => { |
30 | | - context = await start( |
31 | | - [{ name: "create_account_program", programId: PROGRAM_ID }], |
32 | | - [], |
33 | | - ); |
| 19 | + context = await start([{ name: 'create_account_program', programId: PROGRAM_ID }], []); |
34 | 20 | client = context.banksClient; |
35 | 21 | payer = context.payer; |
36 | 22 | lastBlock = context.lastBlockhash; |
37 | 23 | }); |
38 | 24 |
|
39 | | - it("Create the account via a cross program invocation", async () => { |
40 | | - const [PDA, _] = await PublicKey.findProgramAddressSync( |
41 | | - [SEED, payer.publicKey.toBuffer()], |
42 | | - PROGRAM_ID, |
43 | | - ); |
| 25 | + it('should create the account via a cross program invocation', async () => { |
| 26 | + const newAccount = Keypair.generate(); |
44 | 27 |
|
45 | 28 | const ix = new TransactionInstruction({ |
46 | 29 | keys: [ |
47 | 30 | { pubkey: payer.publicKey, isSigner: true, isWritable: true }, |
48 | | - { pubkey: PDA, isSigner: false, isWritable: true }, |
| 31 | + { pubkey: newAccount.publicKey, isSigner: true, isWritable: true }, |
49 | 32 | { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, |
50 | 33 | ], |
51 | 34 | programId: PROGRAM_ID, |
52 | | - data: Buffer.from([0]), |
53 | | - }); |
54 | | - |
55 | | - const tx = new Transaction(); |
56 | | - tx.recentBlockhash = lastBlock; |
57 | | - tx.add(ix).sign(payer); |
58 | | - |
59 | | - // Process Transaction with all the instructions |
60 | | - const transaction = await client.processTransaction(tx); |
61 | | - |
62 | | - assert( |
63 | | - transaction.logMessages[3].startsWith( |
64 | | - "Program log: A new account has been created and initialized!", |
65 | | - ), |
66 | | - ); |
67 | | - }); |
68 | | - |
69 | | - it("Create the account via direct call to system program", async () => { |
70 | | - const newKeypair = Keypair.generate(); |
71 | | - |
72 | | - const ix = SystemProgram.createAccount({ |
73 | | - fromPubkey: payer.publicKey, |
74 | | - newAccountPubkey: newKeypair.publicKey, |
75 | | - lamports: LAMPORTS_PER_SOL, |
76 | | - space: 0, |
77 | | - programId: SystemProgram.programId, |
| 35 | + data: Buffer.concat([instructionDiscriminators.InitializeAccount]), |
78 | 36 | }); |
79 | 37 |
|
80 | 38 | const tx = new Transaction(); |
81 | 39 | tx.recentBlockhash = lastBlock; |
82 | | - tx.add(ix).sign(payer, newKeypair); |
| 40 | + tx.add(ix).sign(payer, newAccount); |
83 | 41 |
|
| 42 | + // No other tests required besides confirming if the transaction is processed |
| 43 | + // Since transactions are atomic, we can be certain the account was created |
84 | 44 | await client.processTransaction(tx); |
85 | | - console.log( |
86 | | - `Account with public key ${newKeypair.publicKey} successfully created`, |
87 | | - ); |
88 | 45 | }); |
89 | 46 | }); |
0 commit comments