misc.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const anchor = require("@project-serum/anchor");
  2. const serumCmn = require("@project-serum/common");
  3. const assert = require("assert");
  4. describe("misc", () => {
  5. // Configure the client to use the local cluster.
  6. anchor.setProvider(anchor.Provider.env());
  7. const program = anchor.workspace.Misc;
  8. const misc2Program = anchor.workspace.Misc2;
  9. it("Can allocate extra space for a state constructor", async () => {
  10. const tx = await program.state.rpc.new();
  11. const addr = await program.state.address();
  12. const state = await program.state();
  13. const accountInfo = await program.provider.connection.getAccountInfo(addr);
  14. assert.ok(state.v.equals(Buffer.from([])));
  15. assert.ok(accountInfo.data.length === 99);
  16. });
  17. const data = new anchor.web3.Account();
  18. it("Can use u128 and i128", async () => {
  19. const tx = await program.rpc.initialize(
  20. new anchor.BN(1234),
  21. new anchor.BN(22),
  22. {
  23. accounts: {
  24. data: data.publicKey,
  25. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  26. },
  27. signers: [data],
  28. instructions: [await program.account.data.createInstruction(data)],
  29. }
  30. );
  31. const dataAccount = await program.account.data(data.publicKey);
  32. assert.ok(dataAccount.udata.eq(new anchor.BN(1234)));
  33. assert.ok(dataAccount.idata.eq(new anchor.BN(22)));
  34. });
  35. it("Can embed programs into genesis from the Anchor.toml", async () => {
  36. const pid = new anchor.web3.PublicKey(
  37. "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
  38. );
  39. let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
  40. assert.ok(accInfo.executable);
  41. });
  42. it("Can use the owner constraint", async () => {
  43. await program.rpc.testOwner({
  44. accounts: {
  45. data: data.publicKey,
  46. misc: program.programId,
  47. },
  48. });
  49. await assert.rejects(
  50. async () => {
  51. await program.rpc.testOwner({
  52. accounts: {
  53. data: program.provider.wallet.publicKey,
  54. misc: program.programId,
  55. },
  56. });
  57. },
  58. (err) => {
  59. return true;
  60. }
  61. );
  62. });
  63. it("Can use the executable attribute", async () => {
  64. await program.rpc.testExecutable({
  65. accounts: {
  66. program: program.programId,
  67. },
  68. });
  69. await assert.rejects(
  70. async () => {
  71. await program.rpc.testExecutable({
  72. accounts: {
  73. program: program.provider.wallet.publicKey,
  74. },
  75. });
  76. },
  77. (err) => {
  78. return true;
  79. }
  80. );
  81. });
  82. it("Can CPI to state instructions", async () => {
  83. const oldData = new anchor.BN(0);
  84. await misc2Program.state.rpc.new({
  85. accounts: {
  86. authority: program.provider.wallet.publicKey,
  87. },
  88. });
  89. let stateAccount = await misc2Program.state();
  90. assert.ok(stateAccount.data.eq(oldData));
  91. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  92. const newData = new anchor.BN(2134);
  93. await program.rpc.testStateCpi(newData, {
  94. accounts: {
  95. authority: program.provider.wallet.publicKey,
  96. cpiState: await misc2Program.state.address(),
  97. misc2Program: misc2Program.programId,
  98. },
  99. });
  100. stateAccount = await misc2Program.state();
  101. assert.ok(stateAccount.data.eq(newData));
  102. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  103. });
  104. it("Can create an associated program account", async () => {
  105. const state = await program.state.address();
  106. // Manual associated address calculation for test only. Clients should use
  107. // the generated methods.
  108. const [
  109. associatedAccount,
  110. nonce,
  111. ] = await anchor.web3.PublicKey.findProgramAddress(
  112. [
  113. Buffer.from([97, 110, 99, 104, 111, 114]), // b"anchor".
  114. program.provider.wallet.publicKey.toBuffer(),
  115. state.toBuffer(),
  116. ],
  117. program.programId
  118. );
  119. await assert.rejects(
  120. async () => {
  121. await program.account.testData(associatedAccount);
  122. },
  123. (err) => {
  124. assert.ok(
  125. err.toString() ===
  126. `Error: Account does not exist ${associatedAccount.toString()}`
  127. );
  128. return true;
  129. }
  130. );
  131. await program.rpc.testAssociatedAccountCreation(new anchor.BN(1234), {
  132. accounts: {
  133. myAccount: associatedAccount,
  134. authority: program.provider.wallet.publicKey,
  135. state,
  136. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  137. systemProgram: anchor.web3.SystemProgram.programId,
  138. },
  139. });
  140. // Try out the generated associated method.
  141. const account = await program.account.testData.associated(
  142. program.provider.wallet.publicKey,
  143. state
  144. );
  145. assert.ok(account.data.toNumber() === 1234);
  146. });
  147. });