|
1 | 1 | import { Buffer } from "node:buffer"; |
2 | 2 | import { describe, test } from "node:test"; |
3 | 3 | import { |
4 | | - Keypair, |
5 | | - PublicKey, |
6 | | - SystemProgram, |
7 | | - Transaction, |
8 | | - TransactionInstruction, |
| 4 | + Keypair, |
| 5 | + PublicKey, |
| 6 | + SystemProgram, |
| 7 | + Transaction, |
| 8 | + TransactionInstruction, |
9 | 9 | } from "@solana/web3.js"; |
10 | 10 | import * as borsh from "borsh"; |
11 | 11 | import { start } from "solana-bankrun"; |
12 | 12 |
|
13 | 13 | class Assignable { |
14 | | - constructor(properties) { |
15 | | - for (const [key, value] of Object.entries(properties)) { |
16 | | - this[key] = value; |
17 | | - } |
18 | | - } |
| 14 | + constructor(properties) { |
| 15 | + for (const [key, value] of Object.entries(properties)) { |
| 16 | + this[key] = value; |
| 17 | + } |
| 18 | + } |
19 | 19 | } |
20 | 20 |
|
21 | 21 | class AddressInfo extends Assignable { |
22 | | - street: string; |
23 | | - city: string; |
24 | | - name: string; |
25 | | - house_number: number; |
26 | | - toBuffer() { |
27 | | - return Buffer.from(borsh.serialize(AddressInfoSchema, this)); |
28 | | - } |
| 22 | + street: string; |
| 23 | + city: string; |
| 24 | + name: string; |
| 25 | + house_number: number; |
| 26 | + toBuffer() { |
| 27 | + return Buffer.from(borsh.serialize(AddressInfoSchema, this)); |
| 28 | + } |
29 | 29 |
|
30 | | - static fromBuffer(buffer: Buffer) { |
31 | | - return borsh.deserialize(AddressInfoSchema, AddressInfo, buffer); |
32 | | - } |
| 30 | + static fromBuffer(buffer: Buffer) { |
| 31 | + return borsh.deserialize(AddressInfoSchema, AddressInfo, buffer); |
| 32 | + } |
33 | 33 | } |
34 | 34 | const AddressInfoSchema = new Map([ |
35 | | - [ |
36 | | - AddressInfo, |
37 | | - { |
38 | | - kind: "struct", |
39 | | - fields: [ |
40 | | - ["name", "string"], |
41 | | - ["house_number", "u8"], |
42 | | - ["street", "string"], |
43 | | - ["city", "string"], |
44 | | - ], |
45 | | - }, |
46 | | - ], |
| 35 | + [ |
| 36 | + AddressInfo, |
| 37 | + { |
| 38 | + kind: "struct", |
| 39 | + fields: [ |
| 40 | + ["name", "string"], |
| 41 | + ["house_number", "u8"], |
| 42 | + ["street", "string"], |
| 43 | + ["city", "string"], |
| 44 | + ], |
| 45 | + }, |
| 46 | + ], |
47 | 47 | ]); |
48 | 48 |
|
49 | 49 | describe("Account Data!", async () => { |
50 | | - const addressInfoAccount = Keypair.generate(); |
51 | | - const PROGRAM_ID = PublicKey.unique(); |
52 | | - const context = await start( |
53 | | - [{ name: "account_data_native_program", programId: PROGRAM_ID }], |
54 | | - [], |
55 | | - ); |
56 | | - const client = context.banksClient; |
| 50 | + const addressInfoAccount = Keypair.generate(); |
| 51 | + const PROGRAM_ID = PublicKey.unique(); |
| 52 | + const context = await start( |
| 53 | + [{ name: "account_data_native_program", programId: PROGRAM_ID }], |
| 54 | + [], |
| 55 | + ); |
| 56 | + const client = context.banksClient; |
57 | 57 |
|
58 | | - test("Create the address info account", async () => { |
59 | | - const payer = context.payer; |
| 58 | + test("Create the address info account", async () => { |
| 59 | + const payer = context.payer; |
60 | 60 |
|
61 | | - console.log(`Program Address : ${PROGRAM_ID}`); |
62 | | - console.log(`Payer Address : ${payer.publicKey}`); |
63 | | - console.log(`Address Info Acct : ${addressInfoAccount.publicKey}`); |
| 61 | + console.log(`Program Address : ${PROGRAM_ID}`); |
| 62 | + console.log(`Payer Address : ${payer.publicKey}`); |
| 63 | + console.log(`Address Info Acct : ${addressInfoAccount.publicKey}`); |
64 | 64 |
|
65 | | - const ix = new TransactionInstruction({ |
66 | | - keys: [ |
67 | | - { |
68 | | - pubkey: addressInfoAccount.publicKey, |
69 | | - isSigner: true, |
70 | | - isWritable: true, |
71 | | - }, |
72 | | - { pubkey: payer.publicKey, isSigner: true, isWritable: true }, |
73 | | - { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, |
74 | | - ], |
75 | | - programId: PROGRAM_ID, |
76 | | - data: new AddressInfo({ |
77 | | - name: "Joe C", |
78 | | - house_number: 136, |
79 | | - street: "Mile High Dr.", |
80 | | - city: "Solana Beach", |
81 | | - }).toBuffer(), |
82 | | - }); |
| 65 | + const ix = new TransactionInstruction({ |
| 66 | + keys: [ |
| 67 | + { |
| 68 | + pubkey: addressInfoAccount.publicKey, |
| 69 | + isSigner: true, |
| 70 | + isWritable: true, |
| 71 | + }, |
| 72 | + { pubkey: payer.publicKey, isSigner: true, isWritable: true }, |
| 73 | + { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, |
| 74 | + ], |
| 75 | + programId: PROGRAM_ID, |
| 76 | + data: new AddressInfo({ |
| 77 | + name: "Joe C", |
| 78 | + house_number: 136, |
| 79 | + street: "Mile High Dr.", |
| 80 | + city: "Solana Beach", |
| 81 | + }).toBuffer(), |
| 82 | + }); |
83 | 83 |
|
84 | | - const blockhash = context.lastBlockhash; |
| 84 | + const blockhash = context.lastBlockhash; |
85 | 85 |
|
86 | | - const tx = new Transaction(); |
87 | | - tx.recentBlockhash = blockhash; |
88 | | - tx.add(ix).sign(payer, addressInfoAccount); |
89 | | - await client.processTransaction(tx); |
90 | | - }); |
| 86 | + const tx = new Transaction(); |
| 87 | + tx.recentBlockhash = blockhash; |
| 88 | + tx.add(ix).sign(payer, addressInfoAccount); |
| 89 | + await client.processTransaction(tx); |
| 90 | + }); |
91 | 91 |
|
92 | | - test("Read the new account's data", async () => { |
93 | | - const accountInfo = await client.getAccount(addressInfoAccount.publicKey); |
| 92 | + test("Read the new account's data", async () => { |
| 93 | + const accountInfo = await client.getAccount(addressInfoAccount.publicKey); |
94 | 94 |
|
95 | | - const readAddressInfo = AddressInfo.fromBuffer( |
96 | | - Buffer.from(accountInfo.data), |
97 | | - ); |
98 | | - console.log(`Name : ${readAddressInfo.name}`); |
99 | | - console.log(`House Num: ${readAddressInfo.house_number}`); |
100 | | - console.log(`Street : ${readAddressInfo.street}`); |
101 | | - console.log(`City : ${readAddressInfo.city}`); |
102 | | - }); |
| 95 | + const readAddressInfo = AddressInfo.fromBuffer( |
| 96 | + Buffer.from(accountInfo.data), |
| 97 | + ); |
| 98 | + console.log(`Name : ${readAddressInfo.name}`); |
| 99 | + console.log(`House Num: ${readAddressInfo.house_number}`); |
| 100 | + console.log(`Street : ${readAddressInfo.street}`); |
| 101 | + console.log(`City : ${readAddressInfo.city}`); |
| 102 | + }); |
103 | 103 | }); |
| 104 | + |
0 commit comments