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