misc.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. it("Can use remaining accounts for a state instruction", async () => {
  18. await program.state.rpc.remainingAccounts({
  19. remainingAccounts: [
  20. { pubkey: misc2Program.programId, isWritable: false, isSigner: false },
  21. ],
  22. });
  23. });
  24. const data = anchor.web3.Keypair.generate();
  25. it("Can use u128 and i128", async () => {
  26. const tx = await program.rpc.initialize(
  27. new anchor.BN(1234),
  28. new anchor.BN(22),
  29. {
  30. accounts: {
  31. data: data.publicKey,
  32. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  33. },
  34. signers: [data],
  35. instructions: [await program.account.data.createInstruction(data)],
  36. }
  37. );
  38. const dataAccount = await program.account.data.fetch(data.publicKey);
  39. assert.ok(dataAccount.udata.eq(new anchor.BN(1234)));
  40. assert.ok(dataAccount.idata.eq(new anchor.BN(22)));
  41. });
  42. it("Can use u16", async () => {
  43. const data = anchor.web3.Keypair.generate();
  44. const tx = await program.rpc.testU16(99, {
  45. accounts: {
  46. myAccount: data.publicKey,
  47. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  48. },
  49. signers: [data],
  50. instructions: [await program.account.dataU16.createInstruction(data)],
  51. });
  52. const dataAccount = await program.account.dataU16.fetch(data.publicKey);
  53. assert.ok(dataAccount.data === 99);
  54. });
  55. it("Can embed programs into genesis from the Anchor.toml", async () => {
  56. const pid = new anchor.web3.PublicKey(
  57. "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
  58. );
  59. let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
  60. assert.ok(accInfo.executable);
  61. });
  62. it("Can use the owner constraint", async () => {
  63. await program.rpc.testOwner({
  64. accounts: {
  65. data: data.publicKey,
  66. misc: program.programId,
  67. },
  68. });
  69. await assert.rejects(
  70. async () => {
  71. await program.rpc.testOwner({
  72. accounts: {
  73. data: program.provider.wallet.publicKey,
  74. misc: program.programId,
  75. },
  76. });
  77. },
  78. (err) => {
  79. return true;
  80. }
  81. );
  82. });
  83. it("Can use the executable attribute", async () => {
  84. await program.rpc.testExecutable({
  85. accounts: {
  86. program: program.programId,
  87. },
  88. });
  89. await assert.rejects(
  90. async () => {
  91. await program.rpc.testExecutable({
  92. accounts: {
  93. program: program.provider.wallet.publicKey,
  94. },
  95. });
  96. },
  97. (err) => {
  98. return true;
  99. }
  100. );
  101. });
  102. it("Can CPI to state instructions", async () => {
  103. const oldData = new anchor.BN(0);
  104. await misc2Program.state.rpc.new({
  105. accounts: {
  106. authority: program.provider.wallet.publicKey,
  107. },
  108. });
  109. let stateAccount = await misc2Program.state.fetch();
  110. assert.ok(stateAccount.data.eq(oldData));
  111. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  112. const newData = new anchor.BN(2134);
  113. await program.rpc.testStateCpi(newData, {
  114. accounts: {
  115. authority: program.provider.wallet.publicKey,
  116. cpiState: await misc2Program.state.address(),
  117. misc2Program: misc2Program.programId,
  118. },
  119. });
  120. stateAccount = await misc2Program.state.fetch();
  121. assert.ok(stateAccount.data.eq(newData));
  122. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  123. });
  124. it("Can init an associated program account", async () => {
  125. const state = await program.state.address();
  126. // Manual associated address calculation for test only. Clients should use
  127. // the generated methods.
  128. const [
  129. associatedAccount,
  130. nonce,
  131. ] = await anchor.web3.PublicKey.findProgramAddress(
  132. [
  133. Buffer.from([97, 110, 99, 104, 111, 114]), // b"anchor".
  134. program.provider.wallet.publicKey.toBuffer(),
  135. state.toBuffer(),
  136. data.publicKey.toBuffer(),
  137. ],
  138. program.programId
  139. );
  140. await assert.rejects(
  141. async () => {
  142. await program.account.testData.fetch(associatedAccount);
  143. },
  144. (err) => {
  145. assert.ok(
  146. err.toString() ===
  147. `Error: Account does not exist ${associatedAccount.toString()}`
  148. );
  149. return true;
  150. }
  151. );
  152. await program.rpc.testInitAssociatedAccount(new anchor.BN(1234), {
  153. accounts: {
  154. myAccount: associatedAccount,
  155. authority: program.provider.wallet.publicKey,
  156. state,
  157. data: data.publicKey,
  158. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  159. systemProgram: anchor.web3.SystemProgram.programId,
  160. },
  161. });
  162. // Try out the generated associated method.
  163. const account = await program.account.testData.associated(
  164. program.provider.wallet.publicKey,
  165. state,
  166. data.publicKey
  167. );
  168. assert.ok(account.data.toNumber() === 1234);
  169. });
  170. it("Can use an associated program account", async () => {
  171. const state = await program.state.address();
  172. const [
  173. associatedAccount,
  174. nonce,
  175. ] = await anchor.web3.PublicKey.findProgramAddress(
  176. [
  177. Buffer.from([97, 110, 99, 104, 111, 114]), // b"anchor".
  178. program.provider.wallet.publicKey.toBuffer(),
  179. state.toBuffer(),
  180. data.publicKey.toBuffer(),
  181. ],
  182. program.programId
  183. );
  184. await program.rpc.testAssociatedAccount(new anchor.BN(5), {
  185. accounts: {
  186. myAccount: associatedAccount,
  187. authority: program.provider.wallet.publicKey,
  188. state,
  189. data: data.publicKey,
  190. },
  191. });
  192. // Try out the generated associated method.
  193. const account = await program.account.testData.associated(
  194. program.provider.wallet.publicKey,
  195. state,
  196. data.publicKey
  197. );
  198. assert.ok(account.data.toNumber() === 5);
  199. });
  200. it("Can retrieve events when simulating a transaction", async () => {
  201. const resp = await program.simulate.testSimulate(44);
  202. const expectedRaw = [
  203. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ invoke [1]",
  204. "Program log: NgyCA9omwbMsAAAA",
  205. "Program log: fPhuIELK/k7SBAAA",
  206. "Program log: jvbowsvlmkcJAAAA",
  207. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ consumed 4819 of 200000 compute units",
  208. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ success",
  209. ];
  210. assert.ok(JSON.stringify(expectedRaw), resp.raw);
  211. assert.ok(resp.events[0].name === "E1");
  212. assert.ok(resp.events[0].data.data === 44);
  213. assert.ok(resp.events[1].name === "E2");
  214. assert.ok(resp.events[1].data.data === 1234);
  215. assert.ok(resp.events[2].name === "E3");
  216. assert.ok(resp.events[2].data.data === 9);
  217. });
  218. it("Can use i8 in the idl", async () => {
  219. const data = anchor.web3.Keypair.generate();
  220. await program.rpc.testI8(-3, {
  221. accounts: {
  222. data: data.publicKey,
  223. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  224. },
  225. instructions: [await program.account.dataI8.createInstruction(data)],
  226. signers: [data],
  227. });
  228. const dataAccount = await program.account.dataI8.fetch(data.publicKey);
  229. assert.ok(dataAccount.data === -3);
  230. });
  231. let dataPubkey;
  232. it("Can use i16 in the idl", async () => {
  233. const data = anchor.web3.Keypair.generate();
  234. await program.rpc.testI16(-2048, {
  235. accounts: {
  236. data: data.publicKey,
  237. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  238. },
  239. instructions: [await program.account.dataI16.createInstruction(data)],
  240. signers: [data],
  241. });
  242. const dataAccount = await program.account.dataI16.fetch(data.publicKey);
  243. assert.ok(dataAccount.data === -2048);
  244. dataPubkey = data.publicKey;
  245. });
  246. it("Can use base58 strings to fetch an account", async () => {
  247. const dataAccount = await program.account.dataI16.fetch(
  248. dataPubkey.toString()
  249. );
  250. assert.ok(dataAccount.data === -2048);
  251. });
  252. it("Should fail to close an account when sending lamports to itself", async () => {
  253. try {
  254. await program.rpc.testClose({
  255. accounts: {
  256. data: data.publicKey,
  257. solDest: data.publicKey,
  258. },
  259. });
  260. assert.ok(false);
  261. } catch (err) {
  262. const errMsg = "A close constraint was violated";
  263. assert.equal(err.toString(), errMsg);
  264. assert.equal(err.msg, errMsg);
  265. assert.equal(err.code, 151);
  266. }
  267. });
  268. it("Can close an account", async () => {
  269. const openAccount = await program.provider.connection.getAccountInfo(
  270. data.publicKey
  271. );
  272. assert.ok(openAccount !== null);
  273. let beforeBalance = (
  274. await program.provider.connection.getAccountInfo(
  275. program.provider.wallet.publicKey
  276. )
  277. ).lamports;
  278. await program.rpc.testClose({
  279. accounts: {
  280. data: data.publicKey,
  281. solDest: program.provider.wallet.publicKey,
  282. },
  283. });
  284. let afterBalance = (
  285. await program.provider.connection.getAccountInfo(
  286. program.provider.wallet.publicKey
  287. )
  288. ).lamports;
  289. // Retrieved rent exemption sol.
  290. assert.ok(afterBalance > beforeBalance);
  291. const closedAccount = await program.provider.connection.getAccountInfo(
  292. data.publicKey
  293. );
  294. assert.ok(closedAccount === null);
  295. });
  296. });