miscNonRentExempt.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import * as anchor from "@project-serum/anchor";
  2. import { Program, BN, IdlAccounts, AnchorError } from "@project-serum/anchor";
  3. import {
  4. PublicKey,
  5. Keypair,
  6. SystemProgram,
  7. SYSVAR_RENT_PUBKEY,
  8. } from "@solana/web3.js";
  9. import { Misc } from "../target/types/misc";
  10. const { assert } = require("chai");
  11. describe("miscNonRentExempt", () => {
  12. // Configure the client to use the local cluster.
  13. anchor.setProvider(anchor.AnchorProvider.env());
  14. const program = anchor.workspace.Misc as Program<Misc>;
  15. it("init_if_needed checks rent_exemption if init is not needed", async () => {
  16. const data = Keypair.generate();
  17. await program.rpc.initDecreaseLamports({
  18. accounts: {
  19. data: data.publicKey,
  20. user: anchor.getProvider().wallet.publicKey,
  21. systemProgram: SystemProgram.programId,
  22. },
  23. signers: [data],
  24. });
  25. try {
  26. await program.rpc.initIfNeededChecksRentExemption({
  27. accounts: {
  28. data: data.publicKey,
  29. user: anchor.getProvider().wallet.publicKey,
  30. systemProgram: SystemProgram.programId,
  31. },
  32. signers: [data],
  33. });
  34. assert.ok(false);
  35. } catch (_err) {
  36. assert.isTrue(_err instanceof AnchorError);
  37. const err: AnchorError = _err;
  38. assert.strictEqual(err.error.errorCode.number, 2005);
  39. }
  40. });
  41. it("allows non-rent exempt accounts", async () => {
  42. const data = Keypair.generate();
  43. await program.rpc.initializeNoRentExempt({
  44. accounts: {
  45. data: data.publicKey,
  46. rent: SYSVAR_RENT_PUBKEY,
  47. },
  48. signers: [data],
  49. instructions: [
  50. SystemProgram.createAccount({
  51. programId: program.programId,
  52. space: 8 + 16 + 16,
  53. lamports:
  54. await program.provider.connection.getMinimumBalanceForRentExemption(
  55. 39
  56. ),
  57. fromPubkey: anchor.getProvider().wallet.publicKey,
  58. newAccountPubkey: data.publicKey,
  59. }),
  60. ],
  61. });
  62. await program.rpc.testNoRentExempt({
  63. accounts: {
  64. data: data.publicKey,
  65. },
  66. });
  67. });
  68. it("allows rent exemption to be skipped", async () => {
  69. const data = anchor.web3.Keypair.generate();
  70. await program.rpc.initializeSkipRentExempt({
  71. accounts: {
  72. data: data.publicKey,
  73. rent: SYSVAR_RENT_PUBKEY,
  74. },
  75. signers: [data],
  76. instructions: [
  77. SystemProgram.createAccount({
  78. programId: program.programId,
  79. space: 8 + 16 + 16,
  80. lamports:
  81. await program.provider.connection.getMinimumBalanceForRentExemption(
  82. 39
  83. ),
  84. fromPubkey: anchor.getProvider().wallet.publicKey,
  85. newAccountPubkey: data.publicKey,
  86. }),
  87. ],
  88. });
  89. });
  90. it("can use rent_exempt to enforce rent exemption", async () => {
  91. const data = Keypair.generate();
  92. await program.rpc.initializeSkipRentExempt({
  93. accounts: {
  94. data: data.publicKey,
  95. rent: SYSVAR_RENT_PUBKEY,
  96. },
  97. signers: [data],
  98. instructions: [
  99. SystemProgram.createAccount({
  100. programId: program.programId,
  101. space: 8 + 16 + 16,
  102. lamports:
  103. await program.provider.connection.getMinimumBalanceForRentExemption(
  104. 39
  105. ),
  106. fromPubkey: anchor.getProvider().wallet.publicKey,
  107. newAccountPubkey: data.publicKey,
  108. }),
  109. ],
  110. });
  111. try {
  112. await program.rpc.testEnforceRentExempt({
  113. accounts: {
  114. data: data.publicKey,
  115. },
  116. });
  117. assert.ok(false);
  118. } catch (_err) {
  119. assert.isTrue(_err instanceof AnchorError);
  120. const err: AnchorError = _err;
  121. assert.strictEqual(err.error.errorCode.number, 2005);
  122. assert.strictEqual(
  123. "A rent exemption constraint was violated",
  124. err.error.errorMessage
  125. );
  126. }
  127. });
  128. });