misc.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.fetch();
  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 = anchor.web3.Keypair.generate();
  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.fetch(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 = anchor.web3.Keypair.generate();
  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.fetch(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.fetch();
  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.fetch();
  114. assert.ok(stateAccount.data.eq(newData));
  115. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  116. });
  117. it("Can init 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.fetch(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.testInitAssociatedAccount(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 use an associated program account", async () => {
  164. const state = await program.state.address();
  165. const [
  166. associatedAccount,
  167. nonce,
  168. ] = await anchor.web3.PublicKey.findProgramAddress(
  169. [
  170. Buffer.from([97, 110, 99, 104, 111, 114]), // b"anchor".
  171. program.provider.wallet.publicKey.toBuffer(),
  172. state.toBuffer(),
  173. data.publicKey.toBuffer(),
  174. ],
  175. program.programId
  176. );
  177. await program.rpc.testAssociatedAccount(new anchor.BN(5), {
  178. accounts: {
  179. myAccount: associatedAccount,
  180. authority: program.provider.wallet.publicKey,
  181. state,
  182. data: data.publicKey,
  183. },
  184. });
  185. // Try out the generated associated method.
  186. const account = await program.account.testData.associated(
  187. program.provider.wallet.publicKey,
  188. state,
  189. data.publicKey
  190. );
  191. assert.ok(account.data.toNumber() === 5);
  192. });
  193. it("Can retrieve events when simulating a transaction", async () => {
  194. const resp = await program.simulate.testSimulate(44);
  195. const expectedRaw = [
  196. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ invoke [1]",
  197. "Program log: NgyCA9omwbMsAAAA",
  198. "Program log: fPhuIELK/k7SBAAA",
  199. "Program log: jvbowsvlmkcJAAAA",
  200. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ consumed 4819 of 200000 compute units",
  201. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ success",
  202. ];
  203. assert.ok(JSON.stringify(expectedRaw), resp.raw);
  204. assert.ok(resp.events[0].name === "E1");
  205. assert.ok(resp.events[0].data.data === 44);
  206. assert.ok(resp.events[1].name === "E2");
  207. assert.ok(resp.events[1].data.data === 1234);
  208. assert.ok(resp.events[2].name === "E3");
  209. assert.ok(resp.events[2].data.data === 9);
  210. });
  211. it("Can use i8 in the idl", async () => {
  212. const data = anchor.web3.Keypair.generate();
  213. await program.rpc.testI8(-3, {
  214. accounts: {
  215. data: data.publicKey,
  216. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  217. },
  218. instructions: [await program.account.dataI8.createInstruction(data)],
  219. signers: [data],
  220. });
  221. const dataAccount = await program.account.dataI8.fetch(data.publicKey);
  222. assert.ok(dataAccount.data === -3);
  223. });
  224. let dataPubkey;
  225. it("Can use i16 in the idl", async () => {
  226. const data = anchor.web3.Keypair.generate();
  227. await program.rpc.testI16(-2048, {
  228. accounts: {
  229. data: data.publicKey,
  230. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  231. },
  232. instructions: [await program.account.dataI16.createInstruction(data)],
  233. signers: [data],
  234. });
  235. const dataAccount = await program.account.dataI16.fetch(data.publicKey);
  236. assert.ok(dataAccount.data === -2048);
  237. dataPubkey = data.publicKey;
  238. });
  239. it("Can use base58 strings to fetch an account", async () => {
  240. const dataAccount = await program.account.dataI16.fetch(dataPubkey.toString());
  241. assert.ok(dataAccount.data === -2048);
  242. });
  243. });