misc.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. describe("misc", () => {
  11. // Configure the client to use the local cluster.
  12. anchor.setProvider(anchor.Provider.env());
  13. const program = anchor.workspace.Misc;
  14. const misc2Program = anchor.workspace.Misc2;
  15. it("Can allocate extra space for a state constructor", async () => {
  16. const tx = await program.state.rpc.new();
  17. const addr = await program.state.address();
  18. const state = await program.state.fetch();
  19. const accountInfo = await program.provider.connection.getAccountInfo(addr);
  20. assert.ok(state.v.equals(Buffer.from([])));
  21. assert.ok(accountInfo.data.length === 99);
  22. });
  23. it("Can use remaining accounts for a state instruction", async () => {
  24. await program.state.rpc.remainingAccounts({
  25. remainingAccounts: [
  26. { pubkey: misc2Program.programId, isWritable: false, isSigner: false },
  27. ],
  28. });
  29. });
  30. const data = anchor.web3.Keypair.generate();
  31. it("Can use u128 and i128", async () => {
  32. const tx = await program.rpc.initialize(
  33. new anchor.BN(1234),
  34. new anchor.BN(22),
  35. {
  36. accounts: {
  37. data: data.publicKey,
  38. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  39. },
  40. signers: [data],
  41. instructions: [await program.account.data.createInstruction(data)],
  42. }
  43. );
  44. const dataAccount = await program.account.data.fetch(data.publicKey);
  45. assert.ok(dataAccount.udata.eq(new anchor.BN(1234)));
  46. assert.ok(dataAccount.idata.eq(new anchor.BN(22)));
  47. });
  48. it("Can use u16", async () => {
  49. const data = anchor.web3.Keypair.generate();
  50. const tx = await program.rpc.testU16(99, {
  51. accounts: {
  52. myAccount: data.publicKey,
  53. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  54. },
  55. signers: [data],
  56. instructions: [await program.account.dataU16.createInstruction(data)],
  57. });
  58. const dataAccount = await program.account.dataU16.fetch(data.publicKey);
  59. assert.ok(dataAccount.data === 99);
  60. });
  61. it("Can embed programs into genesis from the Anchor.toml", async () => {
  62. const pid = new anchor.web3.PublicKey(
  63. "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
  64. );
  65. let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
  66. assert.ok(accInfo.executable);
  67. });
  68. it("Can use the owner constraint", async () => {
  69. await program.rpc.testOwner({
  70. accounts: {
  71. data: data.publicKey,
  72. misc: program.programId,
  73. },
  74. });
  75. await assert.rejects(
  76. async () => {
  77. await program.rpc.testOwner({
  78. accounts: {
  79. data: program.provider.wallet.publicKey,
  80. misc: program.programId,
  81. },
  82. });
  83. },
  84. (err) => {
  85. return true;
  86. }
  87. );
  88. });
  89. it("Can use the executable attribute", async () => {
  90. await program.rpc.testExecutable({
  91. accounts: {
  92. program: program.programId,
  93. },
  94. });
  95. await assert.rejects(
  96. async () => {
  97. await program.rpc.testExecutable({
  98. accounts: {
  99. program: program.provider.wallet.publicKey,
  100. },
  101. });
  102. },
  103. (err) => {
  104. return true;
  105. }
  106. );
  107. });
  108. it("Can CPI to state instructions", async () => {
  109. const oldData = new anchor.BN(0);
  110. await misc2Program.state.rpc.new({
  111. accounts: {
  112. authority: program.provider.wallet.publicKey,
  113. },
  114. });
  115. let stateAccount = await misc2Program.state.fetch();
  116. assert.ok(stateAccount.data.eq(oldData));
  117. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  118. const newData = new anchor.BN(2134);
  119. await program.rpc.testStateCpi(newData, {
  120. accounts: {
  121. authority: program.provider.wallet.publicKey,
  122. cpiState: await misc2Program.state.address(),
  123. misc2Program: misc2Program.programId,
  124. },
  125. });
  126. stateAccount = await misc2Program.state.fetch();
  127. assert.ok(stateAccount.data.eq(newData));
  128. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  129. });
  130. it("Can retrieve events when simulating a transaction", async () => {
  131. const resp = await program.simulate.testSimulate(44);
  132. const expectedRaw = [
  133. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ invoke [1]",
  134. "Program log: NgyCA9omwbMsAAAA",
  135. "Program log: fPhuIELK/k7SBAAA",
  136. "Program log: jvbowsvlmkcJAAAA",
  137. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ consumed 4819 of 200000 compute units",
  138. "Program Z2Ddx1Lcd8CHTV9tkWtNnFQrSz6kxz2H38wrr18zZRZ success",
  139. ];
  140. assert.ok(JSON.stringify(expectedRaw), resp.raw);
  141. assert.ok(resp.events[0].name === "E1");
  142. assert.ok(resp.events[0].data.data === 44);
  143. assert.ok(resp.events[1].name === "E2");
  144. assert.ok(resp.events[1].data.data === 1234);
  145. assert.ok(resp.events[2].name === "E3");
  146. assert.ok(resp.events[2].data.data === 9);
  147. });
  148. let dataI8;
  149. it("Can use i8 in the idl", async () => {
  150. dataI8 = anchor.web3.Keypair.generate();
  151. await program.rpc.testI8(-3, {
  152. accounts: {
  153. data: dataI8.publicKey,
  154. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  155. },
  156. instructions: [await program.account.dataI8.createInstruction(dataI8)],
  157. signers: [dataI8],
  158. });
  159. const dataAccount = await program.account.dataI8.fetch(dataI8.publicKey);
  160. assert.ok(dataAccount.data === -3);
  161. });
  162. let dataPubkey;
  163. it("Can use i16 in the idl", async () => {
  164. const data = anchor.web3.Keypair.generate();
  165. await program.rpc.testI16(-2048, {
  166. accounts: {
  167. data: data.publicKey,
  168. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  169. },
  170. instructions: [await program.account.dataI16.createInstruction(data)],
  171. signers: [data],
  172. });
  173. const dataAccount = await program.account.dataI16.fetch(data.publicKey);
  174. assert.ok(dataAccount.data === -2048);
  175. dataPubkey = data.publicKey;
  176. });
  177. it("Can use base58 strings to fetch an account", async () => {
  178. const dataAccount = await program.account.dataI16.fetch(
  179. dataPubkey.toString()
  180. );
  181. assert.ok(dataAccount.data === -2048);
  182. });
  183. it("Should fail to close an account when sending lamports to itself", async () => {
  184. try {
  185. await program.rpc.testClose({
  186. accounts: {
  187. data: data.publicKey,
  188. solDest: data.publicKey,
  189. },
  190. });
  191. assert.ok(false);
  192. } catch (err) {
  193. const errMsg = "A close constraint was violated";
  194. assert.equal(err.toString(), errMsg);
  195. assert.equal(err.msg, errMsg);
  196. assert.equal(err.code, 151);
  197. }
  198. });
  199. it("Can close an account", async () => {
  200. const openAccount = await program.provider.connection.getAccountInfo(
  201. data.publicKey
  202. );
  203. assert.ok(openAccount !== null);
  204. let beforeBalance = (
  205. await program.provider.connection.getAccountInfo(
  206. program.provider.wallet.publicKey
  207. )
  208. ).lamports;
  209. await program.rpc.testClose({
  210. accounts: {
  211. data: data.publicKey,
  212. solDest: program.provider.wallet.publicKey,
  213. },
  214. });
  215. let afterBalance = (
  216. await program.provider.connection.getAccountInfo(
  217. program.provider.wallet.publicKey
  218. )
  219. ).lamports;
  220. // Retrieved rent exemption sol.
  221. assert.ok(afterBalance > beforeBalance);
  222. const closedAccount = await program.provider.connection.getAccountInfo(
  223. data.publicKey
  224. );
  225. assert.ok(closedAccount === null);
  226. });
  227. it("Can use instruction data in accounts constraints", async () => {
  228. // b"my-seed"
  229. const seed = Buffer.from([109, 121, 45, 115, 101, 101, 100]);
  230. const [myPda, nonce] = await PublicKey.findProgramAddress(
  231. [seed, anchor.web3.SYSVAR_RENT_PUBKEY.toBuffer()],
  232. program.programId
  233. );
  234. await program.rpc.testInstructionConstraint(nonce, {
  235. accounts: {
  236. myPda,
  237. myAccount: anchor.web3.SYSVAR_RENT_PUBKEY,
  238. },
  239. });
  240. });
  241. it("Can create a PDA account with instruction data", async () => {
  242. const seed = Buffer.from([1, 2, 3, 4]);
  243. const domain = "my-domain";
  244. const foo = anchor.web3.SYSVAR_RENT_PUBKEY;
  245. const [myPda, nonce] = await PublicKey.findProgramAddress(
  246. [
  247. Buffer.from(anchor.utils.bytes.utf8.encode("my-seed")),
  248. Buffer.from(anchor.utils.bytes.utf8.encode(domain)),
  249. foo.toBuffer(),
  250. seed,
  251. ],
  252. program.programId
  253. );
  254. await program.rpc.testPdaInit(domain, seed, nonce, {
  255. accounts: {
  256. myPda,
  257. myPayer: program.provider.wallet.publicKey,
  258. foo,
  259. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  260. systemProgram: anchor.web3.SystemProgram.programId,
  261. },
  262. });
  263. const myPdaAccount = await program.account.dataU16.fetch(myPda);
  264. assert.ok(myPdaAccount.data === 6);
  265. });
  266. it("Can create a zero copy PDA account", async () => {
  267. const [myPda, nonce] = await PublicKey.findProgramAddress(
  268. [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
  269. program.programId
  270. );
  271. await program.rpc.testPdaInitZeroCopy(nonce, {
  272. accounts: {
  273. myPda,
  274. myPayer: program.provider.wallet.publicKey,
  275. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  276. systemProgram: anchor.web3.SystemProgram.programId,
  277. },
  278. });
  279. const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
  280. assert.ok(myPdaAccount.data === 9);
  281. assert.ok((myPdaAccount.bump = nonce));
  282. });
  283. it("Can write to a zero copy PDA account", async () => {
  284. const [myPda, bump] = await PublicKey.findProgramAddress(
  285. [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
  286. program.programId
  287. );
  288. await program.rpc.testPdaMutZeroCopy({
  289. accounts: {
  290. myPda,
  291. myPayer: program.provider.wallet.publicKey,
  292. },
  293. });
  294. const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
  295. assert.ok(myPdaAccount.data === 1234);
  296. assert.ok((myPdaAccount.bump = bump));
  297. });
  298. it("Can create a token account from seeds pda", async () => {
  299. const [mint, mint_bump] = await PublicKey.findProgramAddress(
  300. [Buffer.from(anchor.utils.bytes.utf8.encode("my-mint-seed"))],
  301. program.programId
  302. );
  303. const [myPda, token_bump] = await PublicKey.findProgramAddress(
  304. [Buffer.from(anchor.utils.bytes.utf8.encode("my-token-seed"))],
  305. program.programId
  306. );
  307. await program.rpc.testTokenSeedsInit(token_bump, mint_bump, {
  308. accounts: {
  309. myPda,
  310. mint,
  311. authority: program.provider.wallet.publicKey,
  312. systemProgram: anchor.web3.SystemProgram.programId,
  313. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  314. tokenProgram: TOKEN_PROGRAM_ID,
  315. },
  316. });
  317. const mintAccount = new Token(
  318. program.provider.connection,
  319. mint,
  320. TOKEN_PROGRAM_ID,
  321. program.provider.wallet.payer
  322. );
  323. const account = await mintAccount.getAccountInfo(myPda);
  324. assert.ok(account.state === 1);
  325. assert.ok(account.amount.toNumber() === 0);
  326. assert.ok(account.isInitialized);
  327. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  328. assert.ok(account.mint.equals(mint));
  329. });
  330. it("Can execute a fallback function", async () => {
  331. await assert.rejects(
  332. async () => {
  333. await anchor.utils.rpc.invoke(program.programId);
  334. },
  335. (err) => {
  336. assert.ok(err.toString().includes("custom program error: 0x4d2"));
  337. return true;
  338. }
  339. );
  340. });
  341. it("Can init a random account", async () => {
  342. const data = anchor.web3.Keypair.generate();
  343. await program.rpc.testInit({
  344. accounts: {
  345. data: data.publicKey,
  346. payer: program.provider.wallet.publicKey,
  347. systemProgram: anchor.web3.SystemProgram.programId,
  348. },
  349. signers: [data],
  350. });
  351. const account = await program.account.dataI8.fetch(data.publicKey);
  352. assert.ok(account.data === 3);
  353. });
  354. it("Can init a random account prefunded", async () => {
  355. const data = anchor.web3.Keypair.generate();
  356. await program.rpc.testInit({
  357. accounts: {
  358. data: data.publicKey,
  359. payer: program.provider.wallet.publicKey,
  360. systemProgram: anchor.web3.SystemProgram.programId,
  361. },
  362. signers: [data],
  363. instructions: [
  364. anchor.web3.SystemProgram.transfer({
  365. fromPubkey: program.provider.wallet.publicKey,
  366. toPubkey: data.publicKey,
  367. lamports: 4039280,
  368. }),
  369. ],
  370. });
  371. const account = await program.account.dataI8.fetch(data.publicKey);
  372. assert.ok(account.data === 3);
  373. });
  374. it("Can init a random zero copy account", async () => {
  375. const data = anchor.web3.Keypair.generate();
  376. await program.rpc.testInitZeroCopy({
  377. accounts: {
  378. data: data.publicKey,
  379. payer: program.provider.wallet.publicKey,
  380. systemProgram: anchor.web3.SystemProgram.programId,
  381. },
  382. signers: [data],
  383. });
  384. const account = await program.account.dataZeroCopy.fetch(data.publicKey);
  385. assert.ok(account.data === 10);
  386. assert.ok(account.bump === 2);
  387. });
  388. let mint = undefined;
  389. it("Can create a random mint account", async () => {
  390. mint = anchor.web3.Keypair.generate();
  391. await program.rpc.testInitMint({
  392. accounts: {
  393. mint: mint.publicKey,
  394. payer: program.provider.wallet.publicKey,
  395. systemProgram: anchor.web3.SystemProgram.programId,
  396. tokenProgram: TOKEN_PROGRAM_ID,
  397. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  398. },
  399. signers: [mint],
  400. });
  401. const client = new Token(
  402. program.provider.connection,
  403. mint.publicKey,
  404. TOKEN_PROGRAM_ID,
  405. program.provider.wallet.payer
  406. );
  407. const mintAccount = await client.getMintInfo();
  408. assert.ok(mintAccount.decimals === 6);
  409. assert.ok(
  410. mintAccount.mintAuthority.equals(program.provider.wallet.publicKey)
  411. );
  412. assert.ok(
  413. mintAccount.freezeAuthority.equals(program.provider.wallet.publicKey)
  414. );
  415. });
  416. it("Can create a random mint account prefunded", async () => {
  417. mint = anchor.web3.Keypair.generate();
  418. await program.rpc.testInitMint({
  419. accounts: {
  420. mint: mint.publicKey,
  421. payer: program.provider.wallet.publicKey,
  422. systemProgram: anchor.web3.SystemProgram.programId,
  423. tokenProgram: TOKEN_PROGRAM_ID,
  424. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  425. },
  426. signers: [mint],
  427. instructions: [
  428. anchor.web3.SystemProgram.transfer({
  429. fromPubkey: program.provider.wallet.publicKey,
  430. toPubkey: mint.publicKey,
  431. lamports: 4039280,
  432. }),
  433. ],
  434. });
  435. const client = new Token(
  436. program.provider.connection,
  437. mint.publicKey,
  438. TOKEN_PROGRAM_ID,
  439. program.provider.wallet.payer
  440. );
  441. const mintAccount = await client.getMintInfo();
  442. assert.ok(mintAccount.decimals === 6);
  443. assert.ok(
  444. mintAccount.mintAuthority.equals(program.provider.wallet.publicKey)
  445. );
  446. });
  447. it("Can create a random token account", async () => {
  448. const token = anchor.web3.Keypair.generate();
  449. await program.rpc.testInitToken({
  450. accounts: {
  451. token: token.publicKey,
  452. mint: mint.publicKey,
  453. payer: program.provider.wallet.publicKey,
  454. systemProgram: anchor.web3.SystemProgram.programId,
  455. tokenProgram: TOKEN_PROGRAM_ID,
  456. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  457. },
  458. signers: [token],
  459. });
  460. const client = new Token(
  461. program.provider.connection,
  462. mint.publicKey,
  463. TOKEN_PROGRAM_ID,
  464. program.provider.wallet.payer
  465. );
  466. const account = await client.getAccountInfo(token.publicKey);
  467. assert.ok(account.state === 1);
  468. assert.ok(account.amount.toNumber() === 0);
  469. assert.ok(account.isInitialized);
  470. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  471. assert.ok(account.mint.equals(mint.publicKey));
  472. });
  473. it("Can create a random token with prefunding", async () => {
  474. const token = anchor.web3.Keypair.generate();
  475. await program.rpc.testInitToken({
  476. accounts: {
  477. token: token.publicKey,
  478. mint: mint.publicKey,
  479. payer: program.provider.wallet.publicKey,
  480. systemProgram: anchor.web3.SystemProgram.programId,
  481. tokenProgram: TOKEN_PROGRAM_ID,
  482. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  483. },
  484. signers: [token],
  485. instructions: [
  486. anchor.web3.SystemProgram.transfer({
  487. fromPubkey: program.provider.wallet.publicKey,
  488. toPubkey: token.publicKey,
  489. lamports: 4039280,
  490. }),
  491. ],
  492. });
  493. const client = new Token(
  494. program.provider.connection,
  495. mint.publicKey,
  496. TOKEN_PROGRAM_ID,
  497. program.provider.wallet.payer
  498. );
  499. const account = await client.getAccountInfo(token.publicKey);
  500. assert.ok(account.state === 1);
  501. assert.ok(account.amount.toNumber() === 0);
  502. assert.ok(account.isInitialized);
  503. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  504. assert.ok(account.mint.equals(mint.publicKey));
  505. });
  506. it("Can create a random token with prefunding under the rent exemption", async () => {
  507. const token = anchor.web3.Keypair.generate();
  508. await program.rpc.testInitToken({
  509. accounts: {
  510. token: token.publicKey,
  511. mint: mint.publicKey,
  512. payer: program.provider.wallet.publicKey,
  513. systemProgram: anchor.web3.SystemProgram.programId,
  514. tokenProgram: TOKEN_PROGRAM_ID,
  515. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  516. },
  517. signers: [token],
  518. instructions: [
  519. anchor.web3.SystemProgram.transfer({
  520. fromPubkey: program.provider.wallet.publicKey,
  521. toPubkey: token.publicKey,
  522. lamports: 1,
  523. }),
  524. ],
  525. });
  526. const client = new Token(
  527. program.provider.connection,
  528. mint.publicKey,
  529. TOKEN_PROGRAM_ID,
  530. program.provider.wallet.payer
  531. );
  532. const account = await client.getAccountInfo(token.publicKey);
  533. assert.ok(account.state === 1);
  534. assert.ok(account.amount.toNumber() === 0);
  535. assert.ok(account.isInitialized);
  536. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  537. assert.ok(account.mint.equals(mint.publicKey));
  538. });
  539. it("Can initialize multiple accounts via a composite payer", async () => {
  540. const data1 = anchor.web3.Keypair.generate();
  541. const data2 = anchor.web3.Keypair.generate();
  542. const tx = await program.rpc.testCompositePayer({
  543. accounts: {
  544. composite: {
  545. data: data1.publicKey,
  546. payer: program.provider.wallet.publicKey,
  547. systemProgram: anchor.web3.SystemProgram.programId,
  548. },
  549. data: data2.publicKey,
  550. systemProgram: anchor.web3.SystemProgram.programId,
  551. },
  552. signers: [data1, data2],
  553. });
  554. const account1 = await program.account.dataI8.fetch(data1.publicKey);
  555. assert.equal(account1.data, 1);
  556. const account2 = await program.account.data.fetch(data2.publicKey);
  557. assert.equal(account2.udata, 2);
  558. assert.equal(account2.idata, 3);
  559. });
  560. it("Can create an associated token account", async () => {
  561. const token = await Token.getAssociatedTokenAddress(
  562. ASSOCIATED_TOKEN_PROGRAM_ID,
  563. TOKEN_PROGRAM_ID,
  564. mint.publicKey,
  565. program.provider.wallet.publicKey
  566. );
  567. await program.rpc.testInitAssociatedToken({
  568. accounts: {
  569. token,
  570. mint: mint.publicKey,
  571. payer: program.provider.wallet.publicKey,
  572. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  573. systemProgram: anchor.web3.SystemProgram.programId,
  574. tokenProgram: TOKEN_PROGRAM_ID,
  575. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  576. },
  577. });
  578. const client = new Token(
  579. program.provider.connection,
  580. mint.publicKey,
  581. TOKEN_PROGRAM_ID,
  582. program.provider.wallet.payer
  583. );
  584. const account = await client.getAccountInfo(token);
  585. assert.ok(account.state === 1);
  586. assert.ok(account.amount.toNumber() === 0);
  587. assert.ok(account.isInitialized);
  588. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  589. assert.ok(account.mint.equals(mint.publicKey));
  590. });
  591. it("Can fetch all accounts of a given type", async () => {
  592. // Initialize the accounts.
  593. const data1 = anchor.web3.Keypair.generate();
  594. const data2 = anchor.web3.Keypair.generate();
  595. const data3 = anchor.web3.Keypair.generate();
  596. const data4 = anchor.web3.Keypair.generate();
  597. // Initialize filterable data.
  598. const filterable1 = anchor.web3.Keypair.generate().publicKey;
  599. const filterable2 = anchor.web3.Keypair.generate().publicKey;
  600. // Set up a secondary wallet and program.
  601. const anotherProgram = new anchor.Program(
  602. miscIdl,
  603. program.programId,
  604. new anchor.Provider(
  605. program.provider.connection,
  606. new anchor.Wallet(anchor.web3.Keypair.generate()),
  607. { commitment: program.provider.connection.commitment }
  608. )
  609. );
  610. // Request airdrop for secondary wallet.
  611. const signature = await program.provider.connection.requestAirdrop(
  612. anotherProgram.provider.wallet.publicKey,
  613. anchor.web3.LAMPORTS_PER_SOL
  614. );
  615. await program.provider.connection.confirmTransaction(signature);
  616. // Create all the accounts.
  617. await Promise.all([
  618. program.rpc.testFetchAll(filterable1, {
  619. accounts: {
  620. data: data1.publicKey,
  621. authority: program.provider.wallet.publicKey,
  622. systemProgram: anchor.web3.SystemProgram.programId,
  623. },
  624. signers: [data1],
  625. }),
  626. program.rpc.testFetchAll(filterable1, {
  627. accounts: {
  628. data: data2.publicKey,
  629. authority: program.provider.wallet.publicKey,
  630. systemProgram: anchor.web3.SystemProgram.programId,
  631. },
  632. signers: [data2],
  633. }),
  634. program.rpc.testFetchAll(filterable2, {
  635. accounts: {
  636. data: data3.publicKey,
  637. authority: program.provider.wallet.publicKey,
  638. systemProgram: anchor.web3.SystemProgram.programId,
  639. },
  640. signers: [data3],
  641. }),
  642. anotherProgram.rpc.testFetchAll(filterable1, {
  643. accounts: {
  644. data: data4.publicKey,
  645. authority: anotherProgram.provider.wallet.publicKey,
  646. systemProgram: anchor.web3.SystemProgram.programId,
  647. },
  648. signers: [data4],
  649. }),
  650. ]);
  651. // Call for multiple kinds of .all.
  652. const allAccounts = await program.account.dataWithFilter.all();
  653. const allAccountsFilteredByBuffer =
  654. await program.account.dataWithFilter.all(
  655. program.provider.wallet.publicKey.toBuffer()
  656. );
  657. const allAccountsFilteredByProgramFilters1 =
  658. await program.account.dataWithFilter.all([
  659. {
  660. memcmp: {
  661. offset: 8,
  662. bytes: program.provider.wallet.publicKey.toBase58(),
  663. },
  664. },
  665. { memcmp: { offset: 40, bytes: filterable1.toBase58() } },
  666. ]);
  667. const allAccountsFilteredByProgramFilters2 =
  668. await program.account.dataWithFilter.all([
  669. {
  670. memcmp: {
  671. offset: 8,
  672. bytes: program.provider.wallet.publicKey.toBase58(),
  673. },
  674. },
  675. { memcmp: { offset: 40, bytes: filterable2.toBase58() } },
  676. ]);
  677. // Without filters there should be 4 accounts.
  678. assert.equal(allAccounts.length, 4);
  679. // Filtering by main wallet there should be 3 accounts.
  680. assert.equal(allAccountsFilteredByBuffer.length, 3);
  681. // Filtering all the main wallet accounts and matching the filterable1 value
  682. // results in a 2 accounts.
  683. assert.equal(allAccountsFilteredByProgramFilters1.length, 2);
  684. // Filtering all the main wallet accounts and matching the filterable2 value
  685. // results in 1 account.
  686. assert.equal(allAccountsFilteredByProgramFilters2.length, 1);
  687. });
  688. });