realloc.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { describe, test } from 'node:test';
  2. import { Keypair, PublicKey, Transaction } from '@solana/web3.js';
  3. import { start } from 'solana-bankrun';
  4. import {
  5. AddressInfo,
  6. EnhancedAddressInfo,
  7. WorkInfo,
  8. createCreateInstruction,
  9. createReallocateWithoutZeroInitInstruction,
  10. createReallocateZeroInitInstruction,
  11. } from '../ts';
  12. describe('Realloc!', async () => {
  13. const PROGRAM_ID = PublicKey.unique();
  14. const context = await start([{ name: 'realloc_program', programId: PROGRAM_ID }], []);
  15. const client = context.banksClient;
  16. const payer = context.payer;
  17. const testAccount = Keypair.generate();
  18. test('Create the account with data', async () => {
  19. console.log(`${testAccount.publicKey}`);
  20. const ix = createCreateInstruction(testAccount.publicKey, payer.publicKey, PROGRAM_ID, 'Jacob', 123, 'Main St.', 'Chicago');
  21. const tx = new Transaction();
  22. tx.recentBlockhash = context.lastBlockhash;
  23. tx.add(ix).sign(payer, testAccount);
  24. await client.processTransaction(tx);
  25. await printAddressInfo(testAccount.publicKey);
  26. });
  27. test('Reallocate WITHOUT zero init', async () => {
  28. const ix = createReallocateWithoutZeroInitInstruction(testAccount.publicKey, payer.publicKey, PROGRAM_ID, 'Illinois', 12345);
  29. const tx = new Transaction();
  30. const [blockHash, _blockHeight] = await client.getLatestBlockhash();
  31. tx.recentBlockhash = blockHash;
  32. tx.add(ix).sign(payer);
  33. await client.processTransaction(tx);
  34. await printEnhancedAddressInfo(testAccount.publicKey);
  35. });
  36. test('Reallocate WITH zero init', async () => {
  37. const ix = createReallocateZeroInitInstruction(testAccount.publicKey, payer.publicKey, PROGRAM_ID, 'Pete', 'Engineer', 'Solana Labs', 2);
  38. const tx = new Transaction();
  39. const [blockHash, _blockHeight] = await client.getLatestBlockhash();
  40. tx.recentBlockhash = blockHash;
  41. tx.add(ix).sign(payer);
  42. await client.processTransaction(tx);
  43. await printEnhancedAddressInfo(testAccount.publicKey);
  44. await printWorkInfo(testAccount.publicKey);
  45. });
  46. async function printAddressInfo(pubkey: PublicKey): Promise<void> {
  47. await sleep(2);
  48. const data = (await client.getAccount(pubkey))?.data;
  49. if (data) {
  50. const addressInfo = AddressInfo.fromBuffer(Buffer.from(data));
  51. console.log('Address info:');
  52. console.log(` Name: ${addressInfo.name}`);
  53. console.log(` House Num: ${addressInfo.house_number}`);
  54. console.log(` Street: ${addressInfo.street}`);
  55. console.log(` City: ${addressInfo.city}`);
  56. }
  57. }
  58. async function printEnhancedAddressInfo(pubkey: PublicKey): Promise<void> {
  59. await sleep(2);
  60. const data = (await client.getAccount(pubkey))?.data;
  61. if (data) {
  62. const enhancedAddressInfo = EnhancedAddressInfo.fromBuffer(Buffer.from(data));
  63. console.log('Enhanced Address info:');
  64. console.log(` Name: ${enhancedAddressInfo.name}`);
  65. console.log(` House Num: ${enhancedAddressInfo.house_number}`);
  66. console.log(` Street: ${enhancedAddressInfo.street}`);
  67. console.log(` City: ${enhancedAddressInfo.city}`);
  68. console.log(` State: ${enhancedAddressInfo.state}`);
  69. console.log(` Zip: ${enhancedAddressInfo.zip}`);
  70. }
  71. }
  72. async function printWorkInfo(pubkey: PublicKey): Promise<void> {
  73. await sleep(2);
  74. const data = (await client.getAccount(pubkey))?.data;
  75. if (data) {
  76. const workInfo = WorkInfo.fromBuffer(Buffer.from(data));
  77. console.log('Work info:');
  78. console.log(` Name: ${workInfo.name}`);
  79. console.log(` Position: ${workInfo.position}`);
  80. console.log(` Company: ${workInfo.company}`);
  81. console.log(` Years: ${workInfo.years_employed}`);
  82. }
  83. }
  84. function sleep(s: number) {
  85. const SECONDS = 1000;
  86. return new Promise((resolve) => setTimeout(resolve, s * SECONDS));
  87. }
  88. });