account-data.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. wallet.publicKey, // payer
  19. 10240, // space (10240 bytes is the maximum space allowed when allocating space through a program)
  20. "Joe C", // name
  21. 136, // house number
  22. "Mile High Dr.", // street
  23. "Solana Beach" // city
  24. )
  25. .accounts({ dataAccount: dataAccount.publicKey })
  26. .signers([dataAccount])
  27. .rpc()
  28. console.log("Your transaction signature", tx)
  29. })
  30. // Get the account data
  31. it("Get AddressInfo Data", async () => {
  32. const val = await program.methods
  33. .get()
  34. .accounts({ dataAccount: dataAccount.publicKey })
  35. .view()
  36. console.log("State:", val)
  37. })
  38. // Get the account data size
  39. // Testing how much space is used to store the account data
  40. // However, minimum space required is greater than this
  41. it("Get AddressInfo Size", async () => {
  42. const size = await program.methods
  43. .getAddressInfoSize()
  44. .accounts({ dataAccount: dataAccount.publicKey })
  45. .view()
  46. console.log("Size:", size.toNumber())
  47. })
  48. })