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