rent.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // Create a new account via a Cross Program Invocation to the system program
  28. const tx = await program.methods
  29. .createSystemAccount(
  30. wallet.publicKey, // payer
  31. newAccount.publicKey, // new account
  32. new anchor.BN(space) // space
  33. )
  34. .accounts({ dataAccount: dataAccount.publicKey })
  35. .signers([newAccount]) // new account keypair required as signer
  36. .remainingAccounts([
  37. {
  38. pubkey: wallet.publicKey,
  39. isWritable: true,
  40. isSigner: true,
  41. },
  42. {
  43. pubkey: newAccount.publicKey, // new account
  44. isWritable: true,
  45. isSigner: true,
  46. },
  47. ])
  48. .rpc()
  49. console.log("Your transaction signature", tx)
  50. })
  51. })