Skip to content

Commit a1ecdc7

Browse files
committed
feat: add LiteSVM test for account data operations for anchor program
1 parent d0aa2c0 commit a1ecdc7

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { BorshCoder } from "@coral-xyz/anchor";
2+
import {
3+
Keypair,
4+
PublicKey,
5+
SystemProgram,
6+
Transaction,
7+
TransactionInstruction,
8+
} from "@solana/web3.js";
9+
import { LiteSVM } from "litesvm";
10+
11+
import IDL from "../target/idl/account_data_anchor_program.json" with {
12+
type: "json",
13+
};
14+
15+
describe("Account Data!", () => {
16+
let litesvm: LiteSVM;
17+
let programId: PublicKey;
18+
let payer: Keypair;
19+
let addressInfoAccount: Keypair;
20+
const coder = new BorshCoder(IDL);
21+
22+
before(() => {
23+
litesvm = new LiteSVM();
24+
programId = new PublicKey(IDL.address);
25+
payer = Keypair.generate();
26+
addressInfoAccount = Keypair.generate();
27+
28+
const programPath = new URL(
29+
"../target/deploy/account_data_anchor_program.so",
30+
// @ts-ignore
31+
import.meta.url,
32+
).pathname;
33+
litesvm.addProgramFromFile(programId, programPath);
34+
35+
litesvm.airdrop(payer.publicKey, BigInt(100000000000));
36+
});
37+
38+
it("Create the address info account", () => {
39+
console.log(`Payer Address : ${payer.publicKey}`);
40+
console.log(`Address Info Acct : ${addressInfoAccount.publicKey}`);
41+
42+
// Instruction Ix data
43+
const addressInfoIns = {
44+
name: "Joe C",
45+
house_number: 136,
46+
street: "Mile High Dr.",
47+
city: "Solana Beach",
48+
};
49+
50+
/**
51+
* Convert into buffer and encode of instruction and arguments
52+
*/
53+
const data = coder.instruction.encode(
54+
"create_address_info",
55+
addressInfoIns,
56+
);
57+
58+
/**
59+
* Create Transactions
60+
*/
61+
62+
const ix = new TransactionInstruction({
63+
keys: [
64+
{
65+
pubkey: payer.publicKey,
66+
isSigner: true,
67+
isWritable: true,
68+
},
69+
{
70+
pubkey: addressInfoAccount.publicKey,
71+
isSigner: true,
72+
isWritable: true,
73+
},
74+
{
75+
pubkey: SystemProgram.programId,
76+
isSigner: false,
77+
isWritable: false,
78+
},
79+
],
80+
programId,
81+
data,
82+
});
83+
84+
const tx = new Transaction().add(ix);
85+
tx.feePayer = payer.publicKey;
86+
tx.recentBlockhash = litesvm.latestBlockhash();
87+
tx.sign(payer, addressInfoAccount);
88+
const res = litesvm.sendTransaction(tx);
89+
// console.log(res.toString());
90+
});
91+
it("Read the new account's data", () => {
92+
const accountInfoAcc = litesvm.getAccount(addressInfoAccount.publicKey);
93+
if (!accountInfoAcc) {
94+
throw new Error("Failed to fetch account info");
95+
}
96+
97+
// console.log(accountInfoAcc)
98+
99+
/**
100+
* Decode the accounts' data
101+
*/
102+
const addressInfo = coder.accounts.decode(
103+
"AddressInfo",
104+
Buffer.from(accountInfoAcc.data),
105+
);
106+
107+
console.log(`Name : ${addressInfo.name}`);
108+
console.log(`House Num: ${addressInfo.house_number}`);
109+
console.log(`Street : ${addressInfo.street}`);
110+
console.log(`City : ${addressInfo.city}`);
111+
});
112+
});

0 commit comments

Comments
 (0)