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
3 changes: 2 additions & 1 deletion basics/account-data/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
},
"dependencies": {
"@solana/web3.js": "^1.47.3",
"borsh": "^0.7.0",
"fs": "^0.0.1-security"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@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
182 changes: 115 additions & 67 deletions basics/account-data/native/pnpm-lock.yaml

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

60 changes: 38 additions & 22 deletions basics/account-data/native/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from "node:buffer";
import { readFileSync } from "node:fs";
import { describe, test } from "node:test";
import {
Keypair,
Expand All @@ -8,7 +9,7 @@ import {
TransactionInstruction,
} from "@solana/web3.js";
import * as borsh from "borsh";
import { start } from "solana-bankrun";
import { LiteSVM } from "litesvm";

class Assignable {
constructor(properties) {
Expand All @@ -19,10 +20,6 @@ class Assignable {
}

class AddressInfo extends Assignable {
street: string;
city: string;
name: string;
house_number: number;
toBuffer() {
return Buffer.from(borsh.serialize(AddressInfoSchema, this));
}
Expand All @@ -46,18 +43,34 @@ const AddressInfoSchema = new Map([
],
]);

describe("Account Data!", async () => {
describe("Account Data!", () => {
const addressInfoAccount = Keypair.generate();
const PROGRAM_ID = PublicKey.unique();
const context = await start(
[{ name: "account_data_native_program", programId: PROGRAM_ID }],
[],
);
const client = context.banksClient;

test("Create the address info account", async () => {
const payer = context.payer;
// Load the program keypair
const programKeypairPath = new URL(
"./fixtures/account_data_native_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;

const litesvm = new LiteSVM();
const payer = Keypair.generate();

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

// Fund the payer account
litesvm.airdrop(payer.publicKey, BigInt(100000000000));

test("Create the address info account", () => {
console.log(`Program Address : ${PROGRAM_ID}`);
console.log(`Payer Address : ${payer.publicKey}`);
console.log(`Address Info Acct : ${addressInfoAccount.publicKey}`);
Expand All @@ -81,16 +94,19 @@ describe("Account Data!", async () => {
}).toBuffer(),
});

const blockhash = context.lastBlockhash;

const tx = new Transaction();
tx.recentBlockhash = blockhash;
tx.add(ix).sign(payer, addressInfoAccount);
await client.processTransaction(tx);
const tx = new Transaction().add(ix);
tx.feePayer = payer.publicKey;
tx.recentBlockhash = litesvm.latestBlockhash();
tx.sign(payer, addressInfoAccount);
litesvm.sendTransaction(tx);
});

test("Read the new account's data", async () => {
const accountInfo = await client.getAccount(addressInfoAccount.publicKey);
test("Read the new account's data", () => {
const accountInfo = litesvm.getAccount(addressInfoAccount.publicKey);

if (!accountInfo) {
throw new Error("Failed to fetch account info");
}

const readAddressInfo = AddressInfo.fromBuffer(
Buffer.from(accountInfo.data),
Expand Down