test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import * as anchor from "@project-serum/anchor";
  2. import { AnchorProgramExample } from "../target/types/anchor_program_example";
  3. describe("Realloc!", async () => {
  4. const provider = anchor.AnchorProvider.env();
  5. anchor.setProvider(provider);
  6. const payer = provider.wallet as anchor.Wallet;
  7. const program = anchor.workspace.AnchorProgramExample as anchor.Program<AnchorProgramExample>;
  8. const testAccount = anchor.web3.Keypair.generate();
  9. it("Create the account with data", async () => {
  10. console.log(`${testAccount.publicKey}`);
  11. await program.methods.createAddressInfo(
  12. "Jacob",
  13. 123,
  14. "Main St.",
  15. "Chicago",
  16. )
  17. .accounts({
  18. targetAccount: testAccount.publicKey,
  19. payer: payer.publicKey,
  20. systemProgram: anchor.web3.SystemProgram.programId,
  21. })
  22. .signers([payer.payer, testAccount])
  23. .rpc();
  24. await printAddressInfo(testAccount.publicKey);
  25. });
  26. it("Reallocate WITHOUT zero init", async () => {
  27. await program.methods.reallocateWithoutZeroInit(
  28. "Illinois",
  29. 12345,
  30. )
  31. .accounts({
  32. targetAccount: testAccount.publicKey,
  33. payer: payer.publicKey,
  34. systemProgram: anchor.web3.SystemProgram.programId,
  35. })
  36. .signers([payer.payer])
  37. .rpc();
  38. await printEnhancedAddressInfo(testAccount.publicKey);
  39. });
  40. it("Reallocate WITH zero init", async () => {
  41. await program.methods.reallocateZeroInit(
  42. "Pete",
  43. "Engineer",
  44. "Metaplex",
  45. 2,
  46. )
  47. .accounts({
  48. targetAccount: testAccount.publicKey,
  49. payer: payer.publicKey,
  50. systemProgram: anchor.web3.SystemProgram.programId,
  51. })
  52. .signers([payer.payer])
  53. .rpc();
  54. await printWorkInfo(testAccount.publicKey);
  55. });
  56. async function printAddressInfo(pubkey: anchor.web3.PublicKey): Promise<void> {
  57. await delay(2);
  58. const addressInfo = await program.account.addressInfo.fetch(pubkey);
  59. if (addressInfo) {
  60. console.log("Address info:");
  61. console.log(` Name: ${addressInfo.name}`);
  62. console.log(` House Num: ${addressInfo.house_number}`);
  63. console.log(` Street: ${addressInfo.street}`);
  64. console.log(` City: ${addressInfo.city}`);
  65. };
  66. }
  67. async function printEnhancedAddressInfo(pubkey: anchor.web3.PublicKey): Promise<void> {
  68. await delay(2);
  69. const enhancedAddressInfo = await program.account.enhancedAddressInfo.fetch(pubkey);
  70. if (enhancedAddressInfo) {
  71. console.log("Enhanced Address info:");
  72. console.log(` Name: ${enhancedAddressInfo.name}`);
  73. console.log(` House Num: ${enhancedAddressInfo.house_number}`);
  74. console.log(` Street: ${enhancedAddressInfo.street}`);
  75. console.log(` City: ${enhancedAddressInfo.city}`);
  76. console.log(` State: ${enhancedAddressInfo.city}`);
  77. console.log(` Zip: ${enhancedAddressInfo.city}`);
  78. };
  79. }
  80. async function printWorkInfo(pubkey: anchor.web3.PublicKey): Promise<void> {
  81. await delay(2);
  82. const workInfo = await program.account.workInfo.fetch(pubkey);
  83. if (workInfo) {
  84. console.log("Work info:");
  85. console.log(` Name: ${workInfo.name}`);
  86. console.log(` Position: ${workInfo.position}`);
  87. console.log(` Company: ${workInfo.company}`);
  88. console.log(` Years: ${workInfo.years_employed}`);
  89. };
  90. }
  91. function delay(s: number) {
  92. return new Promise( resolve => setTimeout(resolve, s * 1000) );
  93. }
  94. });