Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion basics/create-account/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"@types/chai": "^4.3.1",
"@types/mocha": "^9.1.1",
"chai": "^4.3.4",
"litesvm": "^0.3.3",
"mocha": "^9.0.3",
"solana-bankrun": "^0.3.0",
"ts-mocha": "^10.0.0",
"typescript": "^4.3.5"
}
Expand Down
179 changes: 112 additions & 67 deletions basics/create-account/native/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 45 additions & 18 deletions basics/create-account/native/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import { readFileSync } from 'node:fs';
import { describe, test } from 'node:test';
import { Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
import { start } from 'solana-bankrun';
import { LiteSVM } from 'litesvm';

describe('Create a system account', async () => {
const PROGRAM_ID = PublicKey.unique();
const context = await start([{ name: 'create_account_program', programId: PROGRAM_ID }], []);
const client = context.banksClient;
const payer = context.payer;
describe('Create a system account', () => {
// Load the program keypair
const programKeypairPath = new URL(
'./fixtures/create_account_program-keypair.json',
// @ts-ignore
import.meta.url,
).pathname;
const programKeypairData = JSON.parse(readFileSync(programKeypairPath, 'utf-8'));
const programKeypair = Keypair.fromSecretKey(new Uint8Array(programKeypairData));
const PROGRAM_ID = programKeypair.publicKey;

test('Create the account via a cross program invocation', async () => {
const litesvm = new LiteSVM();
const payer = Keypair.generate();

// Load the program
const programPath = new URL(
'./fixtures/create_account_program.so',
// @ts-ignore
import.meta.url,
).pathname;
litesvm.addProgramFromFile(PROGRAM_ID, programPath);

// Fund the payer account
litesvm.airdrop(payer.publicKey, BigInt(100 * LAMPORTS_PER_SOL));

test('Create the account via a cross program invocation', () => {
const newKeypair = Keypair.generate();
const blockhash = context.lastBlockhash;

const ix = new TransactionInstruction({
keys: [
Expand All @@ -22,16 +41,20 @@ describe('Create a system account', async () => {
data: Buffer.alloc(0),
});

const tx = new Transaction();
tx.recentBlockhash = blockhash;
tx.add(ix).sign(payer, newKeypair);
const tx = new Transaction().add(ix);
tx.feePayer = payer.publicKey;
tx.recentBlockhash = litesvm.latestBlockhash();
tx.sign(payer, newKeypair);

await client.processTransaction(tx);
litesvm.sendTransaction(tx);

// Verify the account was created
const accountInfo = litesvm.getAccount(newKeypair.publicKey);
console.log(`Account with public key ${newKeypair.publicKey} successfully created via CPI`);
});

test('Create the account via direct call to system program', async () => {
test('Create the account via direct call to system program', () => {
const newKeypair = Keypair.generate();
const blockhash = context.lastBlockhash;

const ix = SystemProgram.createAccount({
fromPubkey: payer.publicKey,
Expand All @@ -41,11 +64,15 @@ describe('Create a system account', async () => {
programId: SystemProgram.programId,
});

const tx = new Transaction();
tx.recentBlockhash = blockhash;
tx.add(ix).sign(payer, newKeypair);
const tx = new Transaction().add(ix);
tx.feePayer = payer.publicKey;
tx.recentBlockhash = litesvm.latestBlockhash();
tx.sign(payer, newKeypair);

litesvm.sendTransaction(tx);

await client.processTransaction(tx);
// Verify the account was created
const accountInfo = litesvm.getAccount(newKeypair.publicKey);
console.log(`Account with public key ${newKeypair.publicKey} successfully created`);
});
});