test.ts 2.5 KB

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