misc.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 use u16", async () => {
  36. const data = new anchor.web3.Account();
  37. const tx = await program.rpc.testU16(99, {
  38. accounts: {
  39. myAccount: data.publicKey,
  40. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  41. },
  42. signers: [data],
  43. instructions: [await program.account.dataU16.createInstruction(data)],
  44. });
  45. const dataAccount = await program.account.dataU16(data.publicKey);
  46. assert.ok(dataAccount.data === 99);
  47. });
  48. it("Can embed programs into genesis from the Anchor.toml", async () => {
  49. const pid = new anchor.web3.PublicKey(
  50. "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
  51. );
  52. let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
  53. assert.ok(accInfo.executable);
  54. });
  55. it("Can use the owner constraint", async () => {
  56. await program.rpc.testOwner({
  57. accounts: {
  58. data: data.publicKey,
  59. misc: program.programId,
  60. },
  61. });
  62. await assert.rejects(
  63. async () => {
  64. await program.rpc.testOwner({
  65. accounts: {
  66. data: program.provider.wallet.publicKey,
  67. misc: program.programId,
  68. },
  69. });
  70. },
  71. (err) => {
  72. return true;
  73. }
  74. );
  75. });
  76. it("Can use the executable attribute", async () => {
  77. await program.rpc.testExecutable({
  78. accounts: {
  79. program: program.programId,
  80. },
  81. });
  82. await assert.rejects(
  83. async () => {
  84. await program.rpc.testExecutable({
  85. accounts: {
  86. program: program.provider.wallet.publicKey,
  87. },
  88. });
  89. },
  90. (err) => {
  91. return true;
  92. }
  93. );
  94. });
  95. it("Can CPI to state instructions", async () => {
  96. const oldData = new anchor.BN(0);
  97. await misc2Program.state.rpc.new({
  98. accounts: {
  99. authority: program.provider.wallet.publicKey,
  100. },
  101. });
  102. let stateAccount = await misc2Program.state();
  103. assert.ok(stateAccount.data.eq(oldData));
  104. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  105. const newData = new anchor.BN(2134);
  106. await program.rpc.testStateCpi(newData, {
  107. accounts: {
  108. authority: program.provider.wallet.publicKey,
  109. cpiState: await misc2Program.state.address(),
  110. misc2Program: misc2Program.programId,
  111. },
  112. });
  113. stateAccount = await misc2Program.state();
  114. assert.ok(stateAccount.data.eq(newData));
  115. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  116. });
  117. it("Can create an associated program account", async () => {
  118. const state = await program.state.address();
  119. // Manual associated address calculation for test only. Clients should use
  120. // the generated methods.
  121. const [
  122. associatedAccount,
  123. nonce,
  124. ] = await anchor.web3.PublicKey.findProgramAddress(
  125. [
  126. Buffer.from([97, 110, 99, 104, 111, 114]), // b"anchor".
  127. program.provider.wallet.publicKey.toBuffer(),
  128. state.toBuffer(),
  129. data.publicKey.toBuffer(),
  130. ],
  131. program.programId
  132. );
  133. await assert.rejects(
  134. async () => {
  135. await program.account.testData(associatedAccount);
  136. },
  137. (err) => {
  138. assert.ok(
  139. err.toString() ===
  140. `Error: Account does not exist ${associatedAccount.toString()}`
  141. );
  142. return true;
  143. }
  144. );
  145. await program.rpc.testAssociatedAccountCreation(new anchor.BN(1234), {
  146. accounts: {
  147. myAccount: associatedAccount,
  148. authority: program.provider.wallet.publicKey,
  149. state,
  150. data: data.publicKey,
  151. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  152. systemProgram: anchor.web3.SystemProgram.programId,
  153. },
  154. });
  155. // Try out the generated associated method.
  156. const account = await program.account.testData.associated(
  157. program.provider.wallet.publicKey,
  158. state,
  159. data.publicKey
  160. );
  161. assert.ok(account.data.toNumber() === 1234);
  162. });
  163. it("Can retrieve events when simulating a transaction", async () => {
  164. const resp = await program.simulate.testSimulate(44);
  165. const expectedRaw = [
  166. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ invoke [1]",
  167. "Program log: NgyCA9omwbMsAAAA",
  168. "Program log: fPhuIELK/k7SBAAA",
  169. "Program log: jvbowsvlmkcJAAAA",
  170. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ consumed 4819 of 200000 compute units",
  171. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ success",
  172. ];
  173. assert.ok(JSON.stringify(expectedRaw), resp.raw);
  174. assert.ok(resp.events[0].name === "E1");
  175. assert.ok(resp.events[0].data.data === 44);
  176. assert.ok(resp.events[1].name === "E2");
  177. assert.ok(resp.events[1].data.data === 1234);
  178. assert.ok(resp.events[2].name === "E3");
  179. assert.ok(resp.events[2].data.data === 9);
  180. });
  181. });