misc.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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 =
  680. await program.account.dataWithFilter.all(
  681. program.provider.wallet.publicKey.toBuffer()
  682. );
  683. const allAccountsFilteredByProgramFilters1 =
  684. await program.account.dataWithFilter.all([
  685. {
  686. memcmp: {
  687. offset: 8,
  688. bytes: program.provider.wallet.publicKey.toBase58(),
  689. },
  690. },
  691. { memcmp: { offset: 40, bytes: filterable1.toBase58() } },
  692. ]);
  693. const allAccountsFilteredByProgramFilters2 =
  694. await program.account.dataWithFilter.all([
  695. {
  696. memcmp: {
  697. offset: 8,
  698. bytes: program.provider.wallet.publicKey.toBase58(),
  699. },
  700. },
  701. { memcmp: { offset: 40, bytes: filterable2.toBase58() } },
  702. ]);
  703. // Without filters there should be 4 accounts.
  704. assert.equal(allAccounts.length, 4);
  705. // Filtering by main wallet there should be 3 accounts.
  706. assert.equal(allAccountsFilteredByBuffer.length, 3);
  707. // Filtering all the main wallet accounts and matching the filterable1 value
  708. // results in a 2 accounts.
  709. assert.equal(allAccountsFilteredByProgramFilters1.length, 2);
  710. // Filtering all the main wallet accounts and matching the filterable2 value
  711. // results in 1 account.
  712. assert.equal(allAccountsFilteredByProgramFilters2.length, 1);
  713. });
  714. it("Can use pdas with empty seeds", async () => {
  715. const [pda, bump] = await PublicKey.findProgramAddress(
  716. [],
  717. program.programId
  718. );
  719. await program.rpc.testInitWithEmptySeeds({
  720. accounts: {
  721. pda: pda,
  722. authority: program.provider.wallet.publicKey,
  723. systemProgram: anchor.web3.SystemProgram.programId,
  724. },
  725. });
  726. await program.rpc.testEmptySeedsConstraint({
  727. accounts: {
  728. pda: pda,
  729. },
  730. });
  731. const [pda2, bump2] = await PublicKey.findProgramAddress(
  732. ["non-empty"],
  733. program.programId
  734. );
  735. await assert.rejects(
  736. program.rpc.testEmptySeedsConstraint({
  737. accounts: {
  738. pda: pda2,
  739. },
  740. }),
  741. (err) => {
  742. assert.equal(err.code, 2006);
  743. return true;
  744. }
  745. );
  746. });
  747. const ifNeededAcc = anchor.web3.Keypair.generate();
  748. it("Can init if needed a new account", async () => {
  749. await program.rpc.testInitIfNeeded(1, {
  750. accounts: {
  751. data: ifNeededAcc.publicKey,
  752. systemProgram: anchor.web3.SystemProgram.programId,
  753. payer: program.provider.wallet.publicKey,
  754. },
  755. signers: [ifNeededAcc],
  756. });
  757. const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
  758. assert.ok(account.data, 1);
  759. });
  760. it("Can init if needed a previously created account", async () => {
  761. await program.rpc.testInitIfNeeded(3, {
  762. accounts: {
  763. data: ifNeededAcc.publicKey,
  764. systemProgram: anchor.web3.SystemProgram.programId,
  765. payer: program.provider.wallet.publicKey,
  766. },
  767. signers: [ifNeededAcc],
  768. });
  769. const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
  770. assert.ok(account.data, 3);
  771. });
  772. it("Should include BASE const in IDL", async () => {
  773. assert(
  774. miscIdl.constants.find(
  775. (c) => c.name === "BASE" && c.type === "u128" && c.value === "1_000_000"
  776. ) !== undefined
  777. );
  778. });
  779. it("Should include DECIMALS const in IDL", async () => {
  780. assert(
  781. miscIdl.constants.find(
  782. (c) => c.name === "DECIMALS" && c.type === "u8" && c.value === "6"
  783. ) !== undefined
  784. );
  785. });
  786. it("Should not include NO_IDL const in IDL", async () => {
  787. assert.equal(
  788. miscIdl.constants.find((c) => c.name === "NO_IDL"),
  789. undefined
  790. );
  791. });
  792. it("init_if_needed throws if account exists but is not owned by the expected program", async () => {
  793. const newAcc = await anchor.web3.PublicKey.findProgramAddress(
  794. [utf8.encode("hello")],
  795. program.programId
  796. );
  797. await program.rpc.testInitIfNeededChecksOwner({
  798. accounts: {
  799. data: newAcc[0],
  800. systemProgram: anchor.web3.SystemProgram.programId,
  801. payer: program.provider.wallet.publicKey,
  802. owner: program.programId,
  803. },
  804. });
  805. try {
  806. await program.rpc.testInitIfNeededChecksOwner({
  807. accounts: {
  808. data: newAcc[0],
  809. systemProgram: anchor.web3.SystemProgram.programId,
  810. payer: program.provider.wallet.publicKey,
  811. owner: anchor.web3.Keypair.generate().publicKey,
  812. },
  813. });
  814. assert.ok(false);
  815. } catch (err) {
  816. assert.equal(err.code, 2004);
  817. }
  818. });
  819. it("init_if_needed throws if pda account exists but does not have the expected seeds", async () => {
  820. const newAcc = await anchor.web3.PublicKey.findProgramAddress(
  821. [utf8.encode("nothello")],
  822. program.programId
  823. );
  824. await program.rpc.testInitIfNeededChecksSeeds("nothello", {
  825. accounts: {
  826. data: newAcc[0],
  827. systemProgram: anchor.web3.SystemProgram.programId,
  828. payer: program.provider.wallet.publicKey,
  829. },
  830. });
  831. // this will throw if it is not a proper PDA
  832. // we need this so we know that the following tx failed
  833. // not because it couldn't create this pda
  834. // but because the two pdas were different
  835. anchor.web3.PublicKey.createProgramAddress(
  836. [utf8.encode("hello")],
  837. program.programId
  838. );
  839. try {
  840. await program.rpc.testInitIfNeededChecksSeeds("hello", {
  841. accounts: {
  842. data: newAcc[0],
  843. systemProgram: anchor.web3.SystemProgram.programId,
  844. payer: program.provider.wallet.publicKey,
  845. owner: anchor.web3.Keypair.generate().publicKey,
  846. },
  847. });
  848. assert.ok(false);
  849. } catch (err) {
  850. assert.equal(err.code, 2006);
  851. }
  852. });
  853. it("init_if_needed throws if account exists but is not the expected space", async () => {
  854. const newAcc = anchor.web3.Keypair.generate();
  855. await program.rpc.initWithSpace(3, {
  856. accounts: {
  857. data: newAcc.publicKey,
  858. systemProgram: anchor.web3.SystemProgram.programId,
  859. payer: program.provider.wallet.publicKey,
  860. },
  861. signers: [newAcc],
  862. });
  863. try {
  864. await program.rpc.testInitIfNeeded(3, {
  865. accounts: {
  866. data: newAcc.publicKey,
  867. systemProgram: anchor.web3.SystemProgram.programId,
  868. payer: program.provider.wallet.publicKey,
  869. },
  870. signers: [newAcc],
  871. });
  872. assert.ok(false);
  873. } catch (err) {
  874. assert.equal(err.code, 2019);
  875. }
  876. });
  877. it("init_if_needed throws if mint exists but has the wrong mint authority", async () => {
  878. const mint = anchor.web3.Keypair.generate();
  879. await program.rpc.testInitMint({
  880. accounts: {
  881. mint: mint.publicKey,
  882. payer: program.provider.wallet.publicKey,
  883. systemProgram: anchor.web3.SystemProgram.programId,
  884. tokenProgram: TOKEN_PROGRAM_ID,
  885. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  886. },
  887. signers: [mint],
  888. });
  889. try {
  890. await program.rpc.testInitMintIfNeeded(6, {
  891. accounts: {
  892. mint: mint.publicKey,
  893. payer: program.provider.wallet.publicKey,
  894. systemProgram: anchor.web3.SystemProgram.programId,
  895. tokenProgram: TOKEN_PROGRAM_ID,
  896. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  897. mintAuthority: anchor.web3.Keypair.generate().publicKey,
  898. freezeAuthority: program.provider.wallet.publicKey,
  899. },
  900. signers: [mint],
  901. });
  902. assert.ok(false);
  903. } catch (err) {
  904. assert.equal(err.code, 2016);
  905. }
  906. });
  907. it("init_if_needed throws if mint exists but has the wrong freeze authority", async () => {
  908. const mint = anchor.web3.Keypair.generate();
  909. await program.rpc.testInitMint({
  910. accounts: {
  911. mint: mint.publicKey,
  912. payer: program.provider.wallet.publicKey,
  913. systemProgram: anchor.web3.SystemProgram.programId,
  914. tokenProgram: TOKEN_PROGRAM_ID,
  915. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  916. },
  917. signers: [mint],
  918. });
  919. try {
  920. await program.rpc.testInitMintIfNeeded(6, {
  921. accounts: {
  922. mint: mint.publicKey,
  923. payer: program.provider.wallet.publicKey,
  924. systemProgram: anchor.web3.SystemProgram.programId,
  925. tokenProgram: TOKEN_PROGRAM_ID,
  926. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  927. mintAuthority: program.provider.wallet.publicKey,
  928. freezeAuthority: anchor.web3.Keypair.generate().publicKey,
  929. },
  930. signers: [mint],
  931. });
  932. assert.ok(false);
  933. } catch (err) {
  934. assert.equal(err.code, 2017);
  935. }
  936. });
  937. it("init_if_needed throws if mint exists but has the wrong decimals", async () => {
  938. const mint = anchor.web3.Keypair.generate();
  939. await program.rpc.testInitMint({
  940. accounts: {
  941. mint: mint.publicKey,
  942. payer: program.provider.wallet.publicKey,
  943. systemProgram: anchor.web3.SystemProgram.programId,
  944. tokenProgram: TOKEN_PROGRAM_ID,
  945. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  946. },
  947. signers: [mint],
  948. });
  949. try {
  950. await program.rpc.testInitMintIfNeeded(9, {
  951. accounts: {
  952. mint: mint.publicKey,
  953. payer: program.provider.wallet.publicKey,
  954. systemProgram: anchor.web3.SystemProgram.programId,
  955. tokenProgram: TOKEN_PROGRAM_ID,
  956. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  957. mintAuthority: program.provider.wallet.publicKey,
  958. freezeAuthority: program.provider.wallet.publicKey,
  959. },
  960. signers: [mint],
  961. });
  962. assert.ok(false);
  963. } catch (err) {
  964. assert.equal(err.code, 2018);
  965. }
  966. });
  967. it("init_if_needed throws if token exists but has the wrong owner", async () => {
  968. const mint = anchor.web3.Keypair.generate();
  969. await program.rpc.testInitMint({
  970. accounts: {
  971. mint: mint.publicKey,
  972. payer: program.provider.wallet.publicKey,
  973. systemProgram: anchor.web3.SystemProgram.programId,
  974. tokenProgram: TOKEN_PROGRAM_ID,
  975. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  976. },
  977. signers: [mint],
  978. });
  979. const token = anchor.web3.Keypair.generate();
  980. await program.rpc.testInitToken({
  981. accounts: {
  982. token: token.publicKey,
  983. mint: mint.publicKey,
  984. payer: program.provider.wallet.publicKey,
  985. systemProgram: anchor.web3.SystemProgram.programId,
  986. tokenProgram: TOKEN_PROGRAM_ID,
  987. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  988. },
  989. signers: [token],
  990. });
  991. try {
  992. await program.rpc.testInitTokenIfNeeded({
  993. accounts: {
  994. token: token.publicKey,
  995. mint: mint.publicKey,
  996. payer: program.provider.wallet.publicKey,
  997. systemProgram: anchor.web3.SystemProgram.programId,
  998. tokenProgram: TOKEN_PROGRAM_ID,
  999. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1000. authority: anchor.web3.Keypair.generate().publicKey,
  1001. },
  1002. signers: [token],
  1003. });
  1004. assert.ok(false);
  1005. } catch (err) {
  1006. assert.equal(err.code, 2015);
  1007. }
  1008. });
  1009. it("init_if_needed throws if token exists but has the wrong mint", async () => {
  1010. const mint = anchor.web3.Keypair.generate();
  1011. await program.rpc.testInitMint({
  1012. accounts: {
  1013. mint: mint.publicKey,
  1014. payer: program.provider.wallet.publicKey,
  1015. systemProgram: anchor.web3.SystemProgram.programId,
  1016. tokenProgram: TOKEN_PROGRAM_ID,
  1017. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1018. },
  1019. signers: [mint],
  1020. });
  1021. const mint2 = anchor.web3.Keypair.generate();
  1022. await program.rpc.testInitMint({
  1023. accounts: {
  1024. mint: mint2.publicKey,
  1025. payer: program.provider.wallet.publicKey,
  1026. systemProgram: anchor.web3.SystemProgram.programId,
  1027. tokenProgram: TOKEN_PROGRAM_ID,
  1028. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1029. },
  1030. signers: [mint2],
  1031. });
  1032. const token = anchor.web3.Keypair.generate();
  1033. await program.rpc.testInitToken({
  1034. accounts: {
  1035. token: token.publicKey,
  1036. mint: mint.publicKey,
  1037. payer: program.provider.wallet.publicKey,
  1038. systemProgram: anchor.web3.SystemProgram.programId,
  1039. tokenProgram: TOKEN_PROGRAM_ID,
  1040. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1041. },
  1042. signers: [token],
  1043. });
  1044. try {
  1045. await program.rpc.testInitTokenIfNeeded({
  1046. accounts: {
  1047. token: token.publicKey,
  1048. mint: mint2.publicKey,
  1049. payer: program.provider.wallet.publicKey,
  1050. systemProgram: anchor.web3.SystemProgram.programId,
  1051. tokenProgram: TOKEN_PROGRAM_ID,
  1052. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1053. authority: program.provider.wallet.publicKey,
  1054. },
  1055. signers: [token],
  1056. });
  1057. assert.ok(false);
  1058. } catch (err) {
  1059. assert.equal(err.code, 2014);
  1060. }
  1061. });
  1062. it("init_if_needed throws if associated token exists but has the wrong owner", async () => {
  1063. const mint = anchor.web3.Keypair.generate();
  1064. await program.rpc.testInitMint({
  1065. accounts: {
  1066. mint: mint.publicKey,
  1067. payer: program.provider.wallet.publicKey,
  1068. systemProgram: anchor.web3.SystemProgram.programId,
  1069. tokenProgram: TOKEN_PROGRAM_ID,
  1070. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1071. },
  1072. signers: [mint],
  1073. });
  1074. const associatedToken = await Token.getAssociatedTokenAddress(
  1075. ASSOCIATED_TOKEN_PROGRAM_ID,
  1076. TOKEN_PROGRAM_ID,
  1077. mint.publicKey,
  1078. program.provider.wallet.publicKey
  1079. );
  1080. await program.rpc.testInitAssociatedToken({
  1081. accounts: {
  1082. token: associatedToken,
  1083. mint: mint.publicKey,
  1084. payer: program.provider.wallet.publicKey,
  1085. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1086. systemProgram: anchor.web3.SystemProgram.programId,
  1087. tokenProgram: TOKEN_PROGRAM_ID,
  1088. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1089. },
  1090. });
  1091. try {
  1092. await program.rpc.testInitAssociatedTokenIfNeeded({
  1093. accounts: {
  1094. token: associatedToken,
  1095. mint: mint.publicKey,
  1096. payer: program.provider.wallet.publicKey,
  1097. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1098. systemProgram: anchor.web3.SystemProgram.programId,
  1099. tokenProgram: TOKEN_PROGRAM_ID,
  1100. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1101. authority: anchor.web3.Keypair.generate().publicKey,
  1102. },
  1103. });
  1104. assert.ok(false);
  1105. } catch (err) {
  1106. assert.equal(err.code, 2015);
  1107. }
  1108. });
  1109. it("init_if_needed throws if associated token exists but has the wrong mint", async () => {
  1110. const mint = anchor.web3.Keypair.generate();
  1111. await program.rpc.testInitMint({
  1112. accounts: {
  1113. mint: mint.publicKey,
  1114. payer: program.provider.wallet.publicKey,
  1115. systemProgram: anchor.web3.SystemProgram.programId,
  1116. tokenProgram: TOKEN_PROGRAM_ID,
  1117. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1118. },
  1119. signers: [mint],
  1120. });
  1121. const mint2 = anchor.web3.Keypair.generate();
  1122. await program.rpc.testInitMint({
  1123. accounts: {
  1124. mint: mint2.publicKey,
  1125. payer: program.provider.wallet.publicKey,
  1126. systemProgram: anchor.web3.SystemProgram.programId,
  1127. tokenProgram: TOKEN_PROGRAM_ID,
  1128. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1129. },
  1130. signers: [mint2],
  1131. });
  1132. const associatedToken = await Token.getAssociatedTokenAddress(
  1133. ASSOCIATED_TOKEN_PROGRAM_ID,
  1134. TOKEN_PROGRAM_ID,
  1135. mint.publicKey,
  1136. program.provider.wallet.publicKey
  1137. );
  1138. await program.rpc.testInitAssociatedToken({
  1139. accounts: {
  1140. token: associatedToken,
  1141. mint: mint.publicKey,
  1142. payer: program.provider.wallet.publicKey,
  1143. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1144. systemProgram: anchor.web3.SystemProgram.programId,
  1145. tokenProgram: TOKEN_PROGRAM_ID,
  1146. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1147. },
  1148. });
  1149. try {
  1150. await program.rpc.testInitAssociatedTokenIfNeeded({
  1151. accounts: {
  1152. token: associatedToken,
  1153. mint: mint2.publicKey,
  1154. payer: program.provider.wallet.publicKey,
  1155. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1156. systemProgram: anchor.web3.SystemProgram.programId,
  1157. tokenProgram: TOKEN_PROGRAM_ID,
  1158. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1159. authority: program.provider.wallet.publicKey,
  1160. },
  1161. });
  1162. assert.ok(false);
  1163. } catch (err) {
  1164. assert.equal(err.code, 2014);
  1165. }
  1166. });
  1167. it("Can use multidimensional array", async () => {
  1168. const array2d = new Array(10).fill(new Array(10).fill(99));
  1169. const data = anchor.web3.Keypair.generate();
  1170. const tx = await program.rpc.testMultidimensionalArray(array2d, {
  1171. accounts: {
  1172. data: data.publicKey,
  1173. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1174. },
  1175. signers: [data],
  1176. instructions: [
  1177. await program.account.dataMultidimensionalArray.createInstruction(data),
  1178. ],
  1179. });
  1180. const dataAccount = await program.account.dataMultidimensionalArray.fetch(
  1181. data.publicKey
  1182. );
  1183. assert.deepStrictEqual(dataAccount.data, array2d);
  1184. });
  1185. });