test.ts 2.6 KB

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