account-data.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { AccountData } from "../target/types/account_data";
  4. describe("account-data", () => {
  5. // Configure the client to use the local cluster.
  6. const provider = anchor.AnchorProvider.env();
  7. anchor.setProvider(provider);
  8. // Generate a new random keypair for the data account.
  9. const dataAccount = anchor.web3.Keypair.generate();
  10. const wallet = provider.wallet;
  11. const program = anchor.workspace.AccountData as Program<AccountData>;
  12. // Create the new account
  13. // Using 10240 bytes of space, because its unclear how to correctly calculate the minimum space needed for the account
  14. // Space calculation is different from regular Native/Anchor Solana programs
  15. it("Is initialized!", async () => {
  16. const tx = await program.methods
  17. .new(
  18. 10240, // space (10240 bytes is the maximum space allowed when allocating space through a program)
  19. "Joe C", // name
  20. 136, // house number
  21. "Mile High Dr.", // street
  22. "Solana Beach" // city
  23. )
  24. .accounts({ dataAccount: dataAccount.publicKey })
  25. .signers([dataAccount])
  26. .rpc();
  27. console.log("Your transaction signature", tx);
  28. });
  29. // Get the account data
  30. it("Get AddressInfo Data", async () => {
  31. const val = await program.methods
  32. .get()
  33. .accounts({ dataAccount: dataAccount.publicKey })
  34. .view();
  35. console.log("State:", val);
  36. });
  37. // Get the account data size
  38. // Testing how much space is used to store the account data
  39. // However, minimum space required is greater than this
  40. it("Get AddressInfo Size", async () => {
  41. const size = await program.methods
  42. .getAddressInfoSize()
  43. .accounts({ dataAccount: dataAccount.publicKey })
  44. .view();
  45. console.log("Size:", size.toNumber());
  46. });
  47. });