rent.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import * as anchor from "@coral-xyz/anchor"
  2. import { Program } from "@coral-xyz/anchor"
  3. import { Rent } from "../target/types/rent"
  4. describe("rent", () => {
  5. // Configure the client to use the local cluster.
  6. const provider = anchor.AnchorProvider.env()
  7. anchor.setProvider(provider)
  8. // Generate a new keypair for the data account for the program
  9. const dataAccount = anchor.web3.Keypair.generate()
  10. const wallet = provider.wallet
  11. const connection = provider.connection
  12. const program = anchor.workspace.Rent as Program<Rent>
  13. it("Is initialized!", async () => {
  14. // Initialize data account for the program, which is required by Solang
  15. const tx = await program.methods
  16. .new(wallet.publicKey)
  17. .accounts({ dataAccount: dataAccount.publicKey })
  18. .signers([dataAccount])
  19. .rpc()
  20. console.log("Your transaction signature", tx)
  21. })
  22. it("Create new account", async () => {
  23. // Generate a new keypair for the new account
  24. const newAccount = anchor.web3.Keypair.generate()
  25. // Number of bytes of space to allocate for the account
  26. const space = 100
  27. // Get the minimum balance required for the account for the given space
  28. const lamports = await connection.getMinimumBalanceForRentExemption(space)
  29. // Create a new account via a Cross Program Invocation to the system program
  30. const tx = await program.methods
  31. .createSystemAccount(
  32. wallet.publicKey, // payer
  33. newAccount.publicKey, // new account
  34. new anchor.BN(lamports), // lamports
  35. new anchor.BN(space) // space
  36. )
  37. .accounts({ dataAccount: dataAccount.publicKey })
  38. .signers([newAccount]) // new account keypair required as signer
  39. .remainingAccounts([
  40. {
  41. pubkey: wallet.publicKey,
  42. isWritable: true,
  43. isSigner: true,
  44. },
  45. {
  46. pubkey: newAccount.publicKey, // new account
  47. isWritable: true,
  48. isSigner: true,
  49. },
  50. ])
  51. .rpc()
  52. console.log("Your transaction signature", tx)
  53. })
  54. })