test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { start } from 'solana-bankrun';
  2. import {
  3. Keypair,
  4. PublicKey,
  5. SystemProgram,
  6. Transaction,
  7. TransactionInstruction,
  8. } from '@solana/web3.js';
  9. import * as borsh from 'borsh';
  10. import { Buffer } from 'buffer';
  11. import { describe, test } from 'node:test';
  12. class Assignable {
  13. constructor(properties) {
  14. Object.keys(properties).map(key => {
  15. return (this[key] = properties[key]);
  16. });
  17. }
  18. }
  19. class AddressInfo extends Assignable {
  20. street: any;
  21. city: any;
  22. name: any;
  23. house_number: any;
  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([{ name: 'account_data_program', programId: PROGRAM_ID }],[]);
  49. const client = context.banksClient;
  50. test('Create the address info account', async () => {
  51. const payer = context.payer;
  52. console.log(`Program Address : ${PROGRAM_ID}`);
  53. console.log(`Payer Address : ${payer.publicKey}`);
  54. console.log(`Address Info Acct : ${addressInfoAccount.publicKey}`);
  55. let ix = new TransactionInstruction({
  56. keys: [
  57. {
  58. pubkey: addressInfoAccount.publicKey,
  59. isSigner: true,
  60. isWritable: true,
  61. },
  62. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  63. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  64. ],
  65. programId: PROGRAM_ID,
  66. data: new AddressInfo({
  67. name: 'Joe C',
  68. house_number: 136,
  69. street: 'Mile High Dr.',
  70. city: 'Solana Beach',
  71. }).toBuffer(),
  72. });
  73. const blockhash = context.lastBlockhash;
  74. const tx = new Transaction();
  75. tx.recentBlockhash = blockhash;
  76. tx.add(ix).sign(payer, addressInfoAccount);
  77. await client.processTransaction(tx);
  78. });
  79. test("Read the new account's data", async () => {
  80. const accountInfo = await client.getAccount(addressInfoAccount.publicKey);
  81. const readAddressInfo = AddressInfo.fromBuffer(
  82. Buffer.from(accountInfo.data)
  83. );
  84. console.log(`Name : ${readAddressInfo.name}`);
  85. console.log(`House Num: ${readAddressInfo.house_number}`);
  86. console.log(`Street : ${readAddressInfo.street}`);
  87. console.log(`City : ${readAddressInfo.city}`);
  88. });
  89. });