realloc.test.ts 4.0 KB

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