misc.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. const anchor = require("@project-serum/anchor");
  2. const PublicKey = anchor.web3.PublicKey;
  3. const assert = require("assert");
  4. const {
  5. ASSOCIATED_TOKEN_PROGRAM_ID,
  6. TOKEN_PROGRAM_ID,
  7. Token,
  8. } = require("@solana/spl-token");
  9. const miscIdl = require("../target/idl/misc.json");
  10. const utf8 = anchor.utils.bytes.utf8;
  11. describe("misc", () => {
  12. // Configure the client to use the local cluster.
  13. anchor.setProvider(anchor.Provider.env());
  14. const program = anchor.workspace.Misc;
  15. const misc2Program = anchor.workspace.Misc2;
  16. it("Can allocate extra space for a state constructor", async () => {
  17. const tx = await program.state.rpc.new();
  18. const addr = await program.state.address();
  19. const state = await program.state.fetch();
  20. const accountInfo = await program.provider.connection.getAccountInfo(addr);
  21. assert.ok(state.v.equals(Buffer.from([])));
  22. assert.ok(accountInfo.data.length === 99);
  23. });
  24. it("Can use remaining accounts for a state instruction", async () => {
  25. await program.state.rpc.remainingAccounts({
  26. remainingAccounts: [
  27. { pubkey: misc2Program.programId, isWritable: false, isSigner: false },
  28. ],
  29. });
  30. });
  31. const data = anchor.web3.Keypair.generate();
  32. it("Can use u128 and i128", async () => {
  33. const tx = await program.rpc.initialize(
  34. new anchor.BN(1234),
  35. new anchor.BN(22),
  36. {
  37. accounts: {
  38. data: data.publicKey,
  39. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  40. },
  41. signers: [data],
  42. instructions: [await program.account.data.createInstruction(data)],
  43. }
  44. );
  45. const dataAccount = await program.account.data.fetch(data.publicKey);
  46. assert.ok(dataAccount.udata.eq(new anchor.BN(1234)));
  47. assert.ok(dataAccount.idata.eq(new anchor.BN(22)));
  48. });
  49. it("Can use u16", async () => {
  50. const data = anchor.web3.Keypair.generate();
  51. const tx = await program.rpc.testU16(99, {
  52. accounts: {
  53. myAccount: data.publicKey,
  54. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  55. },
  56. signers: [data],
  57. instructions: [await program.account.dataU16.createInstruction(data)],
  58. });
  59. const dataAccount = await program.account.dataU16.fetch(data.publicKey);
  60. assert.ok(dataAccount.data === 99);
  61. });
  62. it("Can embed programs into genesis from the Anchor.toml", async () => {
  63. const pid = new anchor.web3.PublicKey(
  64. "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
  65. );
  66. let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
  67. assert.ok(accInfo.executable);
  68. });
  69. it("Can use the owner constraint", async () => {
  70. await program.rpc.testOwner({
  71. accounts: {
  72. data: data.publicKey,
  73. misc: program.programId,
  74. },
  75. });
  76. await assert.rejects(
  77. async () => {
  78. await program.rpc.testOwner({
  79. accounts: {
  80. data: program.provider.wallet.publicKey,
  81. misc: program.programId,
  82. },
  83. });
  84. },
  85. (err) => {
  86. return true;
  87. }
  88. );
  89. });
  90. it("Can use the executable attribute", async () => {
  91. await program.rpc.testExecutable({
  92. accounts: {
  93. program: program.programId,
  94. },
  95. });
  96. await assert.rejects(
  97. async () => {
  98. await program.rpc.testExecutable({
  99. accounts: {
  100. program: program.provider.wallet.publicKey,
  101. },
  102. });
  103. },
  104. (err) => {
  105. return true;
  106. }
  107. );
  108. });
  109. it("Can CPI to state instructions", async () => {
  110. const oldData = new anchor.BN(0);
  111. await misc2Program.state.rpc.new({
  112. accounts: {
  113. authority: program.provider.wallet.publicKey,
  114. },
  115. });
  116. let stateAccount = await misc2Program.state.fetch();
  117. assert.ok(stateAccount.data.eq(oldData));
  118. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  119. const newData = new anchor.BN(2134);
  120. await program.rpc.testStateCpi(newData, {
  121. accounts: {
  122. authority: program.provider.wallet.publicKey,
  123. cpiState: await misc2Program.state.address(),
  124. misc2Program: misc2Program.programId,
  125. },
  126. });
  127. stateAccount = await misc2Program.state.fetch();
  128. assert.ok(stateAccount.data.eq(newData));
  129. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  130. });
  131. it("Can retrieve events when simulating a transaction", async () => {
  132. const resp = await program.simulate.testSimulate(44);
  133. const expectedRaw = [
  134. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ invoke [1]",
  135. "Program log: NgyCA9omwbMsAAAA",
  136. "Program log: fPhuIELK/k7SBAAA",
  137. "Program log: jvbowsvlmkcJAAAA",
  138. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ consumed 4819 of 200000 compute units",
  139. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ success",
  140. ];
  141. assert.ok(JSON.stringify(expectedRaw), resp.raw);
  142. assert.ok(resp.events[0].name === "E1");
  143. assert.ok(resp.events[0].data.data === 44);
  144. assert.ok(resp.events[1].name === "E2");
  145. assert.ok(resp.events[1].data.data === 1234);
  146. assert.ok(resp.events[2].name === "E3");
  147. assert.ok(resp.events[2].data.data === 9);
  148. });
  149. let dataI8;
  150. it("Can use i8 in the idl", async () => {
  151. dataI8 = anchor.web3.Keypair.generate();
  152. await program.rpc.testI8(-3, {
  153. accounts: {
  154. data: dataI8.publicKey,
  155. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  156. },
  157. instructions: [await program.account.dataI8.createInstruction(dataI8)],
  158. signers: [dataI8],
  159. });
  160. const dataAccount = await program.account.dataI8.fetch(dataI8.publicKey);
  161. assert.ok(dataAccount.data === -3);
  162. });
  163. let dataPubkey;
  164. it("Can use i16 in the idl", async () => {
  165. const data = anchor.web3.Keypair.generate();
  166. await program.rpc.testI16(-2048, {
  167. accounts: {
  168. data: data.publicKey,
  169. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  170. },
  171. instructions: [await program.account.dataI16.createInstruction(data)],
  172. signers: [data],
  173. });
  174. const dataAccount = await program.account.dataI16.fetch(data.publicKey);
  175. assert.ok(dataAccount.data === -2048);
  176. dataPubkey = data.publicKey;
  177. });
  178. it("Can use base58 strings to fetch an account", async () => {
  179. const dataAccount = await program.account.dataI16.fetch(
  180. dataPubkey.toString()
  181. );
  182. assert.ok(dataAccount.data === -2048);
  183. });
  184. it("Should fail to close an account when sending lamports to itself", async () => {
  185. try {
  186. await program.rpc.testClose({
  187. accounts: {
  188. data: data.publicKey,
  189. solDest: data.publicKey,
  190. },
  191. });
  192. assert.ok(false);
  193. } catch (err) {
  194. const errMsg = "A close constraint was violated";
  195. assert.equal(err.toString(), errMsg);
  196. assert.equal(err.msg, errMsg);
  197. assert.equal(err.code, 2011);
  198. }
  199. });
  200. it("Can close an account", async () => {
  201. const openAccount = await program.provider.connection.getAccountInfo(
  202. data.publicKey
  203. );
  204. assert.ok(openAccount !== null);
  205. let beforeBalance = (
  206. await program.provider.connection.getAccountInfo(
  207. program.provider.wallet.publicKey
  208. )
  209. ).lamports;
  210. await program.rpc.testClose({
  211. accounts: {
  212. data: data.publicKey,
  213. solDest: program.provider.wallet.publicKey,
  214. },
  215. });
  216. let afterBalance = (
  217. await program.provider.connection.getAccountInfo(
  218. program.provider.wallet.publicKey
  219. )
  220. ).lamports;
  221. // Retrieved rent exemption sol.
  222. assert.ok(afterBalance > beforeBalance);
  223. const closedAccount = await program.provider.connection.getAccountInfo(
  224. data.publicKey
  225. );
  226. assert.ok(closedAccount === null);
  227. });
  228. it("Can use instruction data in accounts constraints", async () => {
  229. // b"my-seed"
  230. const seed = Buffer.from([109, 121, 45, 115, 101, 101, 100]);
  231. const [myPda, nonce] = await PublicKey.findProgramAddress(
  232. [seed, anchor.web3.SYSVAR_RENT_PUBKEY.toBuffer()],
  233. program.programId
  234. );
  235. await program.rpc.testInstructionConstraint(nonce, {
  236. accounts: {
  237. myPda,
  238. myAccount: anchor.web3.SYSVAR_RENT_PUBKEY,
  239. },
  240. });
  241. });
  242. it("Can create a PDA account with instruction data", async () => {
  243. const seed = Buffer.from([1, 2, 3, 4]);
  244. const domain = "my-domain";
  245. const foo = anchor.web3.SYSVAR_RENT_PUBKEY;
  246. const [myPda, nonce] = await PublicKey.findProgramAddress(
  247. [
  248. Buffer.from(anchor.utils.bytes.utf8.encode("my-seed")),
  249. Buffer.from(anchor.utils.bytes.utf8.encode(domain)),
  250. foo.toBuffer(),
  251. seed,
  252. ],
  253. program.programId
  254. );
  255. await program.rpc.testPdaInit(domain, seed, nonce, {
  256. accounts: {
  257. myPda,
  258. myPayer: program.provider.wallet.publicKey,
  259. foo,
  260. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  261. systemProgram: anchor.web3.SystemProgram.programId,
  262. },
  263. });
  264. const myPdaAccount = await program.account.dataU16.fetch(myPda);
  265. assert.ok(myPdaAccount.data === 6);
  266. });
  267. it("Can create a zero copy PDA account", async () => {
  268. const [myPda, nonce] = await PublicKey.findProgramAddress(
  269. [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
  270. program.programId
  271. );
  272. await program.rpc.testPdaInitZeroCopy(nonce, {
  273. accounts: {
  274. myPda,
  275. myPayer: program.provider.wallet.publicKey,
  276. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  277. systemProgram: anchor.web3.SystemProgram.programId,
  278. },
  279. });
  280. const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
  281. assert.ok(myPdaAccount.data === 9);
  282. assert.ok((myPdaAccount.bump = nonce));
  283. });
  284. it("Can write to a zero copy PDA account", async () => {
  285. const [myPda, bump] = await PublicKey.findProgramAddress(
  286. [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
  287. program.programId
  288. );
  289. await program.rpc.testPdaMutZeroCopy({
  290. accounts: {
  291. myPda,
  292. myPayer: program.provider.wallet.publicKey,
  293. },
  294. });
  295. const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
  296. assert.ok(myPdaAccount.data === 1234);
  297. assert.ok((myPdaAccount.bump = bump));
  298. });
  299. it("Can create a token account from seeds pda", async () => {
  300. const [mint, mint_bump] = await PublicKey.findProgramAddress(
  301. [Buffer.from(anchor.utils.bytes.utf8.encode("my-mint-seed"))],
  302. program.programId
  303. );
  304. const [myPda, token_bump] = await PublicKey.findProgramAddress(
  305. [Buffer.from(anchor.utils.bytes.utf8.encode("my-token-seed"))],
  306. program.programId
  307. );
  308. await program.rpc.testTokenSeedsInit(token_bump, mint_bump, {
  309. accounts: {
  310. myPda,
  311. mint,
  312. authority: program.provider.wallet.publicKey,
  313. systemProgram: anchor.web3.SystemProgram.programId,
  314. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  315. tokenProgram: TOKEN_PROGRAM_ID,
  316. },
  317. });
  318. const mintAccount = new Token(
  319. program.provider.connection,
  320. mint,
  321. TOKEN_PROGRAM_ID,
  322. program.provider.wallet.payer
  323. );
  324. const account = await mintAccount.getAccountInfo(myPda);
  325. assert.ok(account.state === 1);
  326. assert.ok(account.amount.toNumber() === 0);
  327. assert.ok(account.isInitialized);
  328. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  329. assert.ok(account.mint.equals(mint));
  330. });
  331. it("Can execute a fallback function", async () => {
  332. await assert.rejects(
  333. async () => {
  334. await anchor.utils.rpc.invoke(program.programId);
  335. },
  336. (err) => {
  337. assert.ok(err.toString().includes("custom program error: 0x4d2"));
  338. return true;
  339. }
  340. );
  341. });
  342. it("Can init a random account", async () => {
  343. const data = anchor.web3.Keypair.generate();
  344. await program.rpc.testInit({
  345. accounts: {
  346. data: data.publicKey,
  347. payer: program.provider.wallet.publicKey,
  348. systemProgram: anchor.web3.SystemProgram.programId,
  349. },
  350. signers: [data],
  351. });
  352. const account = await program.account.dataI8.fetch(data.publicKey);
  353. assert.ok(account.data === 3);
  354. });
  355. it("Can init a random account prefunded", async () => {
  356. const data = anchor.web3.Keypair.generate();
  357. await program.rpc.testInit({
  358. accounts: {
  359. data: data.publicKey,
  360. payer: program.provider.wallet.publicKey,
  361. systemProgram: anchor.web3.SystemProgram.programId,
  362. },
  363. signers: [data],
  364. instructions: [
  365. anchor.web3.SystemProgram.transfer({
  366. fromPubkey: program.provider.wallet.publicKey,
  367. toPubkey: data.publicKey,
  368. lamports: 4039280,
  369. }),
  370. ],
  371. });
  372. const account = await program.account.dataI8.fetch(data.publicKey);
  373. assert.ok(account.data === 3);
  374. });
  375. it("Can init a random zero copy account", async () => {
  376. const data = anchor.web3.Keypair.generate();
  377. await program.rpc.testInitZeroCopy({
  378. accounts: {
  379. data: data.publicKey,
  380. payer: program.provider.wallet.publicKey,
  381. systemProgram: anchor.web3.SystemProgram.programId,
  382. },
  383. signers: [data],
  384. });
  385. const account = await program.account.dataZeroCopy.fetch(data.publicKey);
  386. assert.ok(account.data === 10);
  387. assert.ok(account.bump === 2);
  388. });
  389. let mint = undefined;
  390. it("Can create a random mint account", async () => {
  391. mint = anchor.web3.Keypair.generate();
  392. await program.rpc.testInitMint({
  393. accounts: {
  394. mint: mint.publicKey,
  395. payer: program.provider.wallet.publicKey,
  396. systemProgram: anchor.web3.SystemProgram.programId,
  397. tokenProgram: TOKEN_PROGRAM_ID,
  398. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  399. },
  400. signers: [mint],
  401. });
  402. const client = new Token(
  403. program.provider.connection,
  404. mint.publicKey,
  405. TOKEN_PROGRAM_ID,
  406. program.provider.wallet.payer
  407. );
  408. const mintAccount = await client.getMintInfo();
  409. assert.ok(mintAccount.decimals === 6);
  410. assert.ok(
  411. mintAccount.mintAuthority.equals(program.provider.wallet.publicKey)
  412. );
  413. assert.ok(
  414. mintAccount.freezeAuthority.equals(program.provider.wallet.publicKey)
  415. );
  416. });
  417. it("Can create a random mint account prefunded", async () => {
  418. mint = anchor.web3.Keypair.generate();
  419. await program.rpc.testInitMint({
  420. accounts: {
  421. mint: mint.publicKey,
  422. payer: program.provider.wallet.publicKey,
  423. systemProgram: anchor.web3.SystemProgram.programId,
  424. tokenProgram: TOKEN_PROGRAM_ID,
  425. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  426. },
  427. signers: [mint],
  428. instructions: [
  429. anchor.web3.SystemProgram.transfer({
  430. fromPubkey: program.provider.wallet.publicKey,
  431. toPubkey: mint.publicKey,
  432. lamports: 4039280,
  433. }),
  434. ],
  435. });
  436. const client = new Token(
  437. program.provider.connection,
  438. mint.publicKey,
  439. TOKEN_PROGRAM_ID,
  440. program.provider.wallet.payer
  441. );
  442. const mintAccount = await client.getMintInfo();
  443. assert.ok(mintAccount.decimals === 6);
  444. assert.ok(
  445. mintAccount.mintAuthority.equals(program.provider.wallet.publicKey)
  446. );
  447. });
  448. it("Can create a random token account", async () => {
  449. const token = anchor.web3.Keypair.generate();
  450. await program.rpc.testInitToken({
  451. accounts: {
  452. token: token.publicKey,
  453. mint: mint.publicKey,
  454. payer: program.provider.wallet.publicKey,
  455. systemProgram: anchor.web3.SystemProgram.programId,
  456. tokenProgram: TOKEN_PROGRAM_ID,
  457. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  458. },
  459. signers: [token],
  460. });
  461. const client = new Token(
  462. program.provider.connection,
  463. mint.publicKey,
  464. TOKEN_PROGRAM_ID,
  465. program.provider.wallet.payer
  466. );
  467. const account = await client.getAccountInfo(token.publicKey);
  468. assert.ok(account.state === 1);
  469. assert.ok(account.amount.toNumber() === 0);
  470. assert.ok(account.isInitialized);
  471. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  472. assert.ok(account.mint.equals(mint.publicKey));
  473. });
  474. it("Can create a random token with prefunding", async () => {
  475. const token = anchor.web3.Keypair.generate();
  476. await program.rpc.testInitToken({
  477. accounts: {
  478. token: token.publicKey,
  479. mint: mint.publicKey,
  480. payer: program.provider.wallet.publicKey,
  481. systemProgram: anchor.web3.SystemProgram.programId,
  482. tokenProgram: TOKEN_PROGRAM_ID,
  483. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  484. },
  485. signers: [token],
  486. instructions: [
  487. anchor.web3.SystemProgram.transfer({
  488. fromPubkey: program.provider.wallet.publicKey,
  489. toPubkey: token.publicKey,
  490. lamports: 4039280,
  491. }),
  492. ],
  493. });
  494. const client = new Token(
  495. program.provider.connection,
  496. mint.publicKey,
  497. TOKEN_PROGRAM_ID,
  498. program.provider.wallet.payer
  499. );
  500. const account = await client.getAccountInfo(token.publicKey);
  501. assert.ok(account.state === 1);
  502. assert.ok(account.amount.toNumber() === 0);
  503. assert.ok(account.isInitialized);
  504. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  505. assert.ok(account.mint.equals(mint.publicKey));
  506. });
  507. it("Can create a random token with prefunding under the rent exemption", async () => {
  508. const token = anchor.web3.Keypair.generate();
  509. await program.rpc.testInitToken({
  510. accounts: {
  511. token: token.publicKey,
  512. mint: mint.publicKey,
  513. payer: program.provider.wallet.publicKey,
  514. systemProgram: anchor.web3.SystemProgram.programId,
  515. tokenProgram: TOKEN_PROGRAM_ID,
  516. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  517. },
  518. signers: [token],
  519. instructions: [
  520. anchor.web3.SystemProgram.transfer({
  521. fromPubkey: program.provider.wallet.publicKey,
  522. toPubkey: token.publicKey,
  523. lamports: 1,
  524. }),
  525. ],
  526. });
  527. const client = new Token(
  528. program.provider.connection,
  529. mint.publicKey,
  530. TOKEN_PROGRAM_ID,
  531. program.provider.wallet.payer
  532. );
  533. const account = await client.getAccountInfo(token.publicKey);
  534. assert.ok(account.state === 1);
  535. assert.ok(account.amount.toNumber() === 0);
  536. assert.ok(account.isInitialized);
  537. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  538. assert.ok(account.mint.equals(mint.publicKey));
  539. });
  540. it("Can initialize multiple accounts via a composite payer", async () => {
  541. const data1 = anchor.web3.Keypair.generate();
  542. const data2 = anchor.web3.Keypair.generate();
  543. const tx = await program.rpc.testCompositePayer({
  544. accounts: {
  545. composite: {
  546. data: data1.publicKey,
  547. payer: program.provider.wallet.publicKey,
  548. systemProgram: anchor.web3.SystemProgram.programId,
  549. },
  550. data: data2.publicKey,
  551. systemProgram: anchor.web3.SystemProgram.programId,
  552. },
  553. signers: [data1, data2],
  554. });
  555. const account1 = await program.account.dataI8.fetch(data1.publicKey);
  556. assert.equal(account1.data, 1);
  557. const account2 = await program.account.data.fetch(data2.publicKey);
  558. assert.equal(account2.udata, 2);
  559. assert.equal(account2.idata, 3);
  560. });
  561. let associatedToken = null;
  562. it("Can create an associated token account", async () => {
  563. associatedToken = await Token.getAssociatedTokenAddress(
  564. ASSOCIATED_TOKEN_PROGRAM_ID,
  565. TOKEN_PROGRAM_ID,
  566. mint.publicKey,
  567. program.provider.wallet.publicKey
  568. );
  569. await program.rpc.testInitAssociatedToken({
  570. accounts: {
  571. token: associatedToken,
  572. mint: mint.publicKey,
  573. payer: program.provider.wallet.publicKey,
  574. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  575. systemProgram: anchor.web3.SystemProgram.programId,
  576. tokenProgram: TOKEN_PROGRAM_ID,
  577. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  578. },
  579. });
  580. const client = new Token(
  581. program.provider.connection,
  582. mint.publicKey,
  583. TOKEN_PROGRAM_ID,
  584. program.provider.wallet.payer
  585. );
  586. const account = await client.getAccountInfo(associatedToken);
  587. assert.ok(account.state === 1);
  588. assert.ok(account.amount.toNumber() === 0);
  589. assert.ok(account.isInitialized);
  590. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  591. assert.ok(account.mint.equals(mint.publicKey));
  592. });
  593. it("Can validate associated_token constraints", async () => {
  594. await program.rpc.testValidateAssociatedToken({
  595. accounts: {
  596. token: associatedToken,
  597. mint: mint.publicKey,
  598. wallet: program.provider.wallet.publicKey,
  599. },
  600. });
  601. await assert.rejects(
  602. async () => {
  603. await program.rpc.testValidateAssociatedToken({
  604. accounts: {
  605. token: associatedToken,
  606. mint: mint.publicKey,
  607. wallet: anchor.web3.Keypair.generate().publicKey,
  608. },
  609. });
  610. },
  611. (err) => {
  612. assert.equal(err.code, 2009);
  613. return true;
  614. }
  615. );
  616. });
  617. it("Can fetch all accounts of a given type", async () => {
  618. // Initialize the accounts.
  619. const data1 = anchor.web3.Keypair.generate();
  620. const data2 = anchor.web3.Keypair.generate();
  621. const data3 = anchor.web3.Keypair.generate();
  622. const data4 = anchor.web3.Keypair.generate();
  623. // Initialize filterable data.
  624. const filterable1 = anchor.web3.Keypair.generate().publicKey;
  625. const filterable2 = anchor.web3.Keypair.generate().publicKey;
  626. // Set up a secondary wallet and program.
  627. const anotherProgram = new anchor.Program(
  628. miscIdl,
  629. program.programId,
  630. new anchor.Provider(
  631. program.provider.connection,
  632. new anchor.Wallet(anchor.web3.Keypair.generate()),
  633. { commitment: program.provider.connection.commitment }
  634. )
  635. );
  636. // Request airdrop for secondary wallet.
  637. const signature = await program.provider.connection.requestAirdrop(
  638. anotherProgram.provider.wallet.publicKey,
  639. anchor.web3.LAMPORTS_PER_SOL
  640. );
  641. await program.provider.connection.confirmTransaction(signature);
  642. // Create all the accounts.
  643. await Promise.all([
  644. program.rpc.testFetchAll(filterable1, {
  645. accounts: {
  646. data: data1.publicKey,
  647. authority: program.provider.wallet.publicKey,
  648. systemProgram: anchor.web3.SystemProgram.programId,
  649. },
  650. signers: [data1],
  651. }),
  652. program.rpc.testFetchAll(filterable1, {
  653. accounts: {
  654. data: data2.publicKey,
  655. authority: program.provider.wallet.publicKey,
  656. systemProgram: anchor.web3.SystemProgram.programId,
  657. },
  658. signers: [data2],
  659. }),
  660. program.rpc.testFetchAll(filterable2, {
  661. accounts: {
  662. data: data3.publicKey,
  663. authority: program.provider.wallet.publicKey,
  664. systemProgram: anchor.web3.SystemProgram.programId,
  665. },
  666. signers: [data3],
  667. }),
  668. anotherProgram.rpc.testFetchAll(filterable1, {
  669. accounts: {
  670. data: data4.publicKey,
  671. authority: anotherProgram.provider.wallet.publicKey,
  672. systemProgram: anchor.web3.SystemProgram.programId,
  673. },
  674. signers: [data4],
  675. }),
  676. ]);
  677. // Call for multiple kinds of .all.
  678. const allAccounts = await program.account.dataWithFilter.all();
  679. const allAccountsFilteredByBuffer = await program.account.dataWithFilter.all(
  680. program.provider.wallet.publicKey.toBuffer()
  681. );
  682. const allAccountsFilteredByProgramFilters1 = await program.account.dataWithFilter.all(
  683. [
  684. {
  685. memcmp: {
  686. offset: 8,
  687. bytes: program.provider.wallet.publicKey.toBase58(),
  688. },
  689. },
  690. { memcmp: { offset: 40, bytes: filterable1.toBase58() } },
  691. ]
  692. );
  693. const allAccountsFilteredByProgramFilters2 = await program.account.dataWithFilter.all(
  694. [
  695. {
  696. memcmp: {
  697. offset: 8,
  698. bytes: program.provider.wallet.publicKey.toBase58(),
  699. },
  700. },
  701. { memcmp: { offset: 40, bytes: filterable2.toBase58() } },
  702. ]
  703. );
  704. // Without filters there should be 4 accounts.
  705. assert.equal(allAccounts.length, 4);
  706. // Filtering by main wallet there should be 3 accounts.
  707. assert.equal(allAccountsFilteredByBuffer.length, 3);
  708. // Filtering all the main wallet accounts and matching the filterable1 value
  709. // results in a 2 accounts.
  710. assert.equal(allAccountsFilteredByProgramFilters1.length, 2);
  711. // Filtering all the main wallet accounts and matching the filterable2 value
  712. // results in 1 account.
  713. assert.equal(allAccountsFilteredByProgramFilters2.length, 1);
  714. });
  715. it("Can use pdas with empty seeds", async () => {
  716. const [pda, bump] = await PublicKey.findProgramAddress(
  717. [],
  718. program.programId
  719. );
  720. await program.rpc.testInitWithEmptySeeds({
  721. accounts: {
  722. pda: pda,
  723. authority: program.provider.wallet.publicKey,
  724. systemProgram: anchor.web3.SystemProgram.programId,
  725. },
  726. });
  727. await program.rpc.testEmptySeedsConstraint({
  728. accounts: {
  729. pda: pda,
  730. },
  731. });
  732. const [pda2, bump2] = await PublicKey.findProgramAddress(
  733. ["non-empty"],
  734. program.programId
  735. );
  736. await assert.rejects(
  737. program.rpc.testEmptySeedsConstraint({
  738. accounts: {
  739. pda: pda2,
  740. },
  741. }),
  742. (err) => {
  743. assert.equal(err.code, 2006);
  744. return true;
  745. }
  746. );
  747. });
  748. const ifNeededAcc = anchor.web3.Keypair.generate();
  749. it("Can init if needed a new account", async () => {
  750. await program.rpc.testInitIfNeeded(1, {
  751. accounts: {
  752. data: ifNeededAcc.publicKey,
  753. systemProgram: anchor.web3.SystemProgram.programId,
  754. payer: program.provider.wallet.publicKey,
  755. },
  756. signers: [ifNeededAcc],
  757. });
  758. const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
  759. assert.ok(account.data, 1);
  760. });
  761. it("Can init if needed a previously created account", async () => {
  762. await program.rpc.testInitIfNeeded(3, {
  763. accounts: {
  764. data: ifNeededAcc.publicKey,
  765. systemProgram: anchor.web3.SystemProgram.programId,
  766. payer: program.provider.wallet.publicKey,
  767. },
  768. signers: [ifNeededAcc],
  769. });
  770. const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
  771. assert.ok(account.data, 3);
  772. });
  773. it("Should include BASE const in IDL", async () => {
  774. assert(
  775. miscIdl.constants.find(
  776. (c) => c.name === "BASE" && c.type === "u128" && c.value === "1_000_000"
  777. ) !== undefined
  778. );
  779. });
  780. it("Should include DECIMALS const in IDL", async () => {
  781. assert(
  782. miscIdl.constants.find(
  783. (c) => c.name === "DECIMALS" && c.type === "u8" && c.value === "6"
  784. ) !== undefined
  785. );
  786. });
  787. it("Should not include NO_IDL const in IDL", async () => {
  788. assert.equal(
  789. miscIdl.constants.find((c) => c.name === "NO_IDL"),
  790. undefined
  791. );
  792. });
  793. it("init_if_needed throws if account exists but is not owned by the expected program", async () => {
  794. const newAcc = await anchor.web3.PublicKey.findProgramAddress([utf8.encode("hello")], program.programId);
  795. await program.rpc.testInitIfNeededChecksOwner({
  796. accounts: {
  797. data: newAcc[0],
  798. systemProgram: anchor.web3.SystemProgram.programId,
  799. payer: program.provider.wallet.publicKey,
  800. owner: program.programId
  801. }
  802. });
  803. try {
  804. await program.rpc.testInitIfNeededChecksOwner({
  805. accounts: {
  806. data: newAcc[0],
  807. systemProgram: anchor.web3.SystemProgram.programId,
  808. payer: program.provider.wallet.publicKey,
  809. owner: anchor.web3.Keypair.generate().publicKey
  810. },
  811. });
  812. assert.ok(false);
  813. } catch (err) {
  814. assert.equal(err.code, 2004);
  815. }
  816. });
  817. it("init_if_needed throws if pda account exists but does not have the expected seeds", async () => {
  818. const newAcc = await anchor.web3.PublicKey.findProgramAddress([utf8.encode("nothello")], program.programId);
  819. await program.rpc.testInitIfNeededChecksSeeds("nothello", {
  820. accounts: {
  821. data: newAcc[0],
  822. systemProgram: anchor.web3.SystemProgram.programId,
  823. payer: program.provider.wallet.publicKey,
  824. }
  825. });
  826. // this will throw if it is not a proper PDA
  827. // we need this so we know that the following tx failed
  828. // not because it couldn't create this pda
  829. // but because the two pdas were different
  830. anchor.web3.PublicKey.createProgramAddress([utf8.encode("hello")], program.programId);
  831. try {
  832. await program.rpc.testInitIfNeededChecksSeeds("hello", {
  833. accounts: {
  834. data: newAcc[0],
  835. systemProgram: anchor.web3.SystemProgram.programId,
  836. payer: program.provider.wallet.publicKey,
  837. owner: anchor.web3.Keypair.generate().publicKey
  838. },
  839. });
  840. assert.ok(false);
  841. } catch (err) {
  842. assert.equal(err.code, 2006);
  843. }
  844. });
  845. it("init_if_needed throws if account exists but is not the expected space", async () => {
  846. const newAcc = anchor.web3.Keypair.generate();
  847. await program.rpc.initWithSpace(3, {
  848. accounts: {
  849. data: newAcc.publicKey,
  850. systemProgram: anchor.web3.SystemProgram.programId,
  851. payer: program.provider.wallet.publicKey,
  852. },
  853. signers: [newAcc],
  854. });
  855. try {
  856. await program.rpc.testInitIfNeeded(3, {
  857. accounts: {
  858. data: newAcc.publicKey,
  859. systemProgram: anchor.web3.SystemProgram.programId,
  860. payer: program.provider.wallet.publicKey,
  861. },
  862. signers: [newAcc],
  863. });
  864. assert.ok(false);
  865. } catch (err) {
  866. assert.equal(err.code, 2019);
  867. }
  868. });
  869. it("init_if_needed throws if mint exists but has the wrong mint authority", async () => {
  870. const mint = anchor.web3.Keypair.generate();
  871. await program.rpc.testInitMint({
  872. accounts: {
  873. mint: mint.publicKey,
  874. payer: program.provider.wallet.publicKey,
  875. systemProgram: anchor.web3.SystemProgram.programId,
  876. tokenProgram: TOKEN_PROGRAM_ID,
  877. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  878. },
  879. signers: [mint],
  880. });
  881. try {
  882. await program.rpc.testInitMintIfNeeded(6,{
  883. accounts: {
  884. mint: mint.publicKey,
  885. payer: program.provider.wallet.publicKey,
  886. systemProgram: anchor.web3.SystemProgram.programId,
  887. tokenProgram: TOKEN_PROGRAM_ID,
  888. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  889. mintAuthority: anchor.web3.Keypair.generate().publicKey,
  890. freezeAuthority: program.provider.wallet.publicKey
  891. },
  892. signers: [mint],
  893. });
  894. assert.ok(false);
  895. } catch (err) {
  896. assert.equal(err.code, 2016);
  897. }
  898. });
  899. it("init_if_needed throws if mint exists but has the wrong freeze authority", async () => {
  900. const mint = anchor.web3.Keypair.generate();
  901. await program.rpc.testInitMint({
  902. accounts: {
  903. mint: mint.publicKey,
  904. payer: program.provider.wallet.publicKey,
  905. systemProgram: anchor.web3.SystemProgram.programId,
  906. tokenProgram: TOKEN_PROGRAM_ID,
  907. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  908. },
  909. signers: [mint],
  910. });
  911. try {
  912. await program.rpc.testInitMintIfNeeded(6,{
  913. accounts: {
  914. mint: mint.publicKey,
  915. payer: program.provider.wallet.publicKey,
  916. systemProgram: anchor.web3.SystemProgram.programId,
  917. tokenProgram: TOKEN_PROGRAM_ID,
  918. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  919. mintAuthority: program.provider.wallet.publicKey,
  920. freezeAuthority: anchor.web3.Keypair.generate().publicKey
  921. },
  922. signers: [mint],
  923. });
  924. assert.ok(false);
  925. } catch (err) {
  926. assert.equal(err.code, 2017);
  927. }
  928. });
  929. it("init_if_needed throws if mint exists but has the wrong decimals", async () => {
  930. const mint = anchor.web3.Keypair.generate();
  931. await program.rpc.testInitMint({
  932. accounts: {
  933. mint: mint.publicKey,
  934. payer: program.provider.wallet.publicKey,
  935. systemProgram: anchor.web3.SystemProgram.programId,
  936. tokenProgram: TOKEN_PROGRAM_ID,
  937. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  938. },
  939. signers: [mint],
  940. });
  941. try {
  942. await program.rpc.testInitMintIfNeeded(9,{
  943. accounts: {
  944. mint: mint.publicKey,
  945. payer: program.provider.wallet.publicKey,
  946. systemProgram: anchor.web3.SystemProgram.programId,
  947. tokenProgram: TOKEN_PROGRAM_ID,
  948. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  949. mintAuthority: program.provider.wallet.publicKey,
  950. freezeAuthority: program.provider.wallet.publicKey
  951. },
  952. signers: [mint],
  953. });
  954. assert.ok(false);
  955. } catch (err) {
  956. assert.equal(err.code, 2018);
  957. }
  958. });
  959. it("init_if_needed throws if token exists but has the wrong owner", async () => {
  960. const mint = anchor.web3.Keypair.generate();
  961. await program.rpc.testInitMint({
  962. accounts: {
  963. mint: mint.publicKey,
  964. payer: program.provider.wallet.publicKey,
  965. systemProgram: anchor.web3.SystemProgram.programId,
  966. tokenProgram: TOKEN_PROGRAM_ID,
  967. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  968. },
  969. signers: [mint],
  970. });
  971. const token = anchor.web3.Keypair.generate();
  972. await program.rpc.testInitToken({
  973. accounts: {
  974. token: token.publicKey,
  975. mint: mint.publicKey,
  976. payer: program.provider.wallet.publicKey,
  977. systemProgram: anchor.web3.SystemProgram.programId,
  978. tokenProgram: TOKEN_PROGRAM_ID,
  979. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  980. },
  981. signers: [token],
  982. });
  983. try {
  984. await program.rpc.testInitTokenIfNeeded({
  985. accounts: {
  986. token: token.publicKey,
  987. mint: mint.publicKey,
  988. payer: program.provider.wallet.publicKey,
  989. systemProgram: anchor.web3.SystemProgram.programId,
  990. tokenProgram: TOKEN_PROGRAM_ID,
  991. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  992. authority: anchor.web3.Keypair.generate().publicKey,
  993. },
  994. signers: [token],
  995. });
  996. assert.ok(false);
  997. } catch (err) {
  998. assert.equal(err.code, 2015);
  999. }
  1000. });
  1001. it("init_if_needed throws if token exists but has the wrong mint", async () => {
  1002. const mint = anchor.web3.Keypair.generate();
  1003. await program.rpc.testInitMint({
  1004. accounts: {
  1005. mint: mint.publicKey,
  1006. payer: program.provider.wallet.publicKey,
  1007. systemProgram: anchor.web3.SystemProgram.programId,
  1008. tokenProgram: TOKEN_PROGRAM_ID,
  1009. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1010. },
  1011. signers: [mint],
  1012. });
  1013. const mint2 = anchor.web3.Keypair.generate();
  1014. await program.rpc.testInitMint({
  1015. accounts: {
  1016. mint: mint2.publicKey,
  1017. payer: program.provider.wallet.publicKey,
  1018. systemProgram: anchor.web3.SystemProgram.programId,
  1019. tokenProgram: TOKEN_PROGRAM_ID,
  1020. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1021. },
  1022. signers: [mint2],
  1023. });
  1024. const token = anchor.web3.Keypair.generate();
  1025. await program.rpc.testInitToken({
  1026. accounts: {
  1027. token: token.publicKey,
  1028. mint: mint.publicKey,
  1029. payer: program.provider.wallet.publicKey,
  1030. systemProgram: anchor.web3.SystemProgram.programId,
  1031. tokenProgram: TOKEN_PROGRAM_ID,
  1032. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1033. },
  1034. signers: [token],
  1035. });
  1036. try {
  1037. await program.rpc.testInitTokenIfNeeded({
  1038. accounts: {
  1039. token: token.publicKey,
  1040. mint: mint2.publicKey,
  1041. payer: program.provider.wallet.publicKey,
  1042. systemProgram: anchor.web3.SystemProgram.programId,
  1043. tokenProgram: TOKEN_PROGRAM_ID,
  1044. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1045. authority: program.provider.wallet.publicKey,
  1046. },
  1047. signers: [token],
  1048. });
  1049. assert.ok(false);
  1050. } catch (err) {
  1051. assert.equal(err.code, 2014);
  1052. }
  1053. });
  1054. it("init_if_needed throws if associated token exists but has the wrong owner", async () => {
  1055. const mint = anchor.web3.Keypair.generate();
  1056. await program.rpc.testInitMint({
  1057. accounts: {
  1058. mint: mint.publicKey,
  1059. payer: program.provider.wallet.publicKey,
  1060. systemProgram: anchor.web3.SystemProgram.programId,
  1061. tokenProgram: TOKEN_PROGRAM_ID,
  1062. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1063. },
  1064. signers: [mint],
  1065. });
  1066. const associatedToken = await Token.getAssociatedTokenAddress(
  1067. ASSOCIATED_TOKEN_PROGRAM_ID,
  1068. TOKEN_PROGRAM_ID,
  1069. mint.publicKey,
  1070. program.provider.wallet.publicKey
  1071. );
  1072. await program.rpc.testInitAssociatedToken({
  1073. accounts: {
  1074. token: associatedToken,
  1075. mint: mint.publicKey,
  1076. payer: program.provider.wallet.publicKey,
  1077. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1078. systemProgram: anchor.web3.SystemProgram.programId,
  1079. tokenProgram: TOKEN_PROGRAM_ID,
  1080. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1081. },
  1082. });
  1083. try {
  1084. await program.rpc.testInitAssociatedTokenIfNeeded({
  1085. accounts: {
  1086. token: associatedToken,
  1087. mint: mint.publicKey,
  1088. payer: program.provider.wallet.publicKey,
  1089. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1090. systemProgram: anchor.web3.SystemProgram.programId,
  1091. tokenProgram: TOKEN_PROGRAM_ID,
  1092. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1093. authority: anchor.web3.Keypair.generate().publicKey
  1094. },
  1095. });
  1096. assert.ok(false);
  1097. } catch (err) {
  1098. assert.equal(err.code, 2015);
  1099. }
  1100. })
  1101. it("init_if_needed throws if associated token exists but has the wrong mint", async () => {
  1102. const mint = anchor.web3.Keypair.generate();
  1103. await program.rpc.testInitMint({
  1104. accounts: {
  1105. mint: mint.publicKey,
  1106. payer: program.provider.wallet.publicKey,
  1107. systemProgram: anchor.web3.SystemProgram.programId,
  1108. tokenProgram: TOKEN_PROGRAM_ID,
  1109. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1110. },
  1111. signers: [mint],
  1112. });
  1113. const mint2 = anchor.web3.Keypair.generate();
  1114. await program.rpc.testInitMint({
  1115. accounts: {
  1116. mint: mint2.publicKey,
  1117. payer: program.provider.wallet.publicKey,
  1118. systemProgram: anchor.web3.SystemProgram.programId,
  1119. tokenProgram: TOKEN_PROGRAM_ID,
  1120. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1121. },
  1122. signers: [mint2],
  1123. });
  1124. const associatedToken = await Token.getAssociatedTokenAddress(
  1125. ASSOCIATED_TOKEN_PROGRAM_ID,
  1126. TOKEN_PROGRAM_ID,
  1127. mint.publicKey,
  1128. program.provider.wallet.publicKey
  1129. );
  1130. await program.rpc.testInitAssociatedToken({
  1131. accounts: {
  1132. token: associatedToken,
  1133. mint: mint.publicKey,
  1134. payer: program.provider.wallet.publicKey,
  1135. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1136. systemProgram: anchor.web3.SystemProgram.programId,
  1137. tokenProgram: TOKEN_PROGRAM_ID,
  1138. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1139. },
  1140. });
  1141. try {
  1142. await program.rpc.testInitAssociatedTokenIfNeeded({
  1143. accounts: {
  1144. token: associatedToken,
  1145. mint: mint2.publicKey,
  1146. payer: program.provider.wallet.publicKey,
  1147. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1148. systemProgram: anchor.web3.SystemProgram.programId,
  1149. tokenProgram: TOKEN_PROGRAM_ID,
  1150. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1151. authority: program.provider.wallet.publicKey
  1152. },
  1153. });
  1154. assert.ok(false);
  1155. } catch (err) {
  1156. assert.equal(err.code, 2014);
  1157. }
  1158. })
  1159. it("Can use multidimensional array", async () => {
  1160. const array2d = new Array(10).fill(new Array(10).fill(99));
  1161. const data = anchor.web3.Keypair.generate();
  1162. const tx = await program.rpc.testMultidimensionalArray(array2d, {
  1163. accounts: {
  1164. data: data.publicKey,
  1165. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1166. },
  1167. signers: [data],
  1168. instructions: [
  1169. await program.account.dataMultidimensionalArray.createInstruction(data),
  1170. ],
  1171. });
  1172. const dataAccount = await program.account.dataMultidimensionalArray.fetch(
  1173. data.publicKey
  1174. );
  1175. assert.deepStrictEqual(dataAccount.data, array2d);
  1176. });
  1177. });