realloc.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {
  2. Connection,
  3. Keypair,
  4. PublicKey,
  5. sendAndConfirmTransaction,
  6. Transaction,
  7. } from '@solana/web3.js';
  8. import {
  9. createCreateInstruction,
  10. createKeypairFromFile,
  11. createReallocateWithoutZeroInitInstruction,
  12. createReallocateZeroInitInstruction,
  13. AddressInfo,
  14. EnhancedAddressInfo,
  15. WorkInfo,
  16. } from '../ts';
  17. describe("Realloc!", async () => {
  18. const connection = new Connection(`https://api.devnet.solana.com`, 'confirmed');
  19. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  20. const program = createKeypairFromFile('./program/target/so/program-keypair.json');
  21. const testAccount = Keypair.generate();
  22. it("Create the account with data", async () => {
  23. console.log(`${testAccount.publicKey}`);
  24. const ix = createCreateInstruction(
  25. testAccount.publicKey,
  26. payer.publicKey,
  27. program.publicKey,
  28. "Jacob",
  29. 123,
  30. "Main St.",
  31. "Chicago",
  32. );
  33. await sendAndConfirmTransaction(
  34. connection,
  35. new Transaction().add(ix),
  36. [payer, testAccount]
  37. );
  38. await printAddressInfo(testAccount.publicKey);
  39. });
  40. it("Reallocate WITHOUT zero init", async () => {
  41. const ix = createReallocateWithoutZeroInitInstruction(
  42. testAccount.publicKey,
  43. payer.publicKey,
  44. program.publicKey,
  45. "Illinois",
  46. 12345,
  47. );
  48. await sendAndConfirmTransaction(
  49. connection,
  50. new Transaction().add(ix),
  51. [payer]
  52. );
  53. await printEnhancedAddressInfo(testAccount.publicKey);
  54. });
  55. it("Reallocate WITH zero init", async () => {
  56. const ix = createReallocateZeroInitInstruction(
  57. testAccount.publicKey,
  58. payer.publicKey,
  59. program.publicKey,
  60. "Pete",
  61. "Engineer",
  62. "Solana Labs",
  63. 2,
  64. );
  65. await sendAndConfirmTransaction(
  66. connection,
  67. new Transaction().add(ix),
  68. [payer]
  69. );
  70. await printWorkInfo(testAccount.publicKey);
  71. });
  72. async function printAddressInfo(pubkey: PublicKey): Promise<void> {
  73. await delay(2);
  74. const data = (await connection.getAccountInfo(pubkey))?.data;
  75. if (data) {
  76. const addressInfo = AddressInfo.fromBuffer(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 delay(2);
  86. const data = (await connection.getAccountInfo(pubkey))?.data;
  87. if (data) {
  88. const enhancedAddressInfo = EnhancedAddressInfo.fromBuffer(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 delay(2);
  100. const data = (await connection.getAccountInfo(pubkey))?.data;
  101. if (data) {
  102. const workInfo = WorkInfo.fromBuffer(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 delay(s: number) {
  111. return new Promise( resolve => setTimeout(resolve, s * 1000) );
  112. }
  113. });