misc.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. const anchor = require("@project-serum/anchor");
  2. const assert = require("assert");
  3. const {
  4. ASSOCIATED_TOKEN_PROGRAM_ID,
  5. TOKEN_PROGRAM_ID,
  6. Token,
  7. } = require("@solana/spl-token");
  8. const miscIdl = require("../target/idl/misc.json");
  9. const {
  10. SystemProgram,
  11. Keypair,
  12. PublicKey,
  13. SYSVAR_RENT_PUBKEY,
  14. } = require("@solana/web3.js");
  15. const utf8 = anchor.utils.bytes.utf8;
  16. describe("misc", () => {
  17. // Configure the client to use the local cluster.
  18. anchor.setProvider(anchor.Provider.env());
  19. const program = anchor.workspace.Misc;
  20. const misc2Program = anchor.workspace.Misc2;
  21. it("Can allocate extra space for a state constructor", async () => {
  22. const tx = await program.state.rpc.new();
  23. const addr = await program.state.address();
  24. const state = await program.state.fetch();
  25. const accountInfo = await program.provider.connection.getAccountInfo(addr);
  26. assert.ok(state.v.equals(Buffer.from([])));
  27. assert.ok(accountInfo.data.length === 99);
  28. });
  29. it("Can use remaining accounts for a state instruction", async () => {
  30. await program.state.rpc.remainingAccounts({
  31. remainingAccounts: [
  32. { pubkey: misc2Program.programId, isWritable: false, isSigner: false },
  33. ],
  34. });
  35. });
  36. const data = anchor.web3.Keypair.generate();
  37. it("Can use u128 and i128", async () => {
  38. const tx = await program.rpc.initialize(
  39. new anchor.BN(1234),
  40. new anchor.BN(22),
  41. {
  42. accounts: {
  43. data: data.publicKey,
  44. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  45. },
  46. signers: [data],
  47. instructions: [await program.account.data.createInstruction(data)],
  48. }
  49. );
  50. const dataAccount = await program.account.data.fetch(data.publicKey);
  51. assert.ok(dataAccount.udata.eq(new anchor.BN(1234)));
  52. assert.ok(dataAccount.idata.eq(new anchor.BN(22)));
  53. });
  54. it("Can use u16", async () => {
  55. const data = anchor.web3.Keypair.generate();
  56. const tx = await program.rpc.testU16(99, {
  57. accounts: {
  58. myAccount: data.publicKey,
  59. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  60. },
  61. signers: [data],
  62. instructions: [await program.account.dataU16.createInstruction(data)],
  63. });
  64. const dataAccount = await program.account.dataU16.fetch(data.publicKey);
  65. assert.ok(dataAccount.data === 99);
  66. });
  67. it("Can embed programs into genesis from the Anchor.toml", async () => {
  68. const pid = new anchor.web3.PublicKey(
  69. "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
  70. );
  71. let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
  72. assert.ok(accInfo.executable);
  73. });
  74. it("Can use the owner constraint", async () => {
  75. await program.rpc.testOwner({
  76. accounts: {
  77. data: data.publicKey,
  78. misc: program.programId,
  79. },
  80. });
  81. await assert.rejects(
  82. async () => {
  83. await program.rpc.testOwner({
  84. accounts: {
  85. data: program.provider.wallet.publicKey,
  86. misc: program.programId,
  87. },
  88. });
  89. },
  90. (err) => {
  91. return true;
  92. }
  93. );
  94. });
  95. it("Can use the executable attribute", async () => {
  96. await program.rpc.testExecutable({
  97. accounts: {
  98. program: program.programId,
  99. },
  100. });
  101. await assert.rejects(
  102. async () => {
  103. await program.rpc.testExecutable({
  104. accounts: {
  105. program: program.provider.wallet.publicKey,
  106. },
  107. });
  108. },
  109. (err) => {
  110. return true;
  111. }
  112. );
  113. });
  114. it("Can CPI to state instructions", async () => {
  115. const oldData = new anchor.BN(0);
  116. await misc2Program.state.rpc.new({
  117. accounts: {
  118. authority: program.provider.wallet.publicKey,
  119. },
  120. });
  121. let stateAccount = await misc2Program.state.fetch();
  122. assert.ok(stateAccount.data.eq(oldData));
  123. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  124. const newData = new anchor.BN(2134);
  125. await program.rpc.testStateCpi(newData, {
  126. accounts: {
  127. authority: program.provider.wallet.publicKey,
  128. cpiState: await misc2Program.state.address(),
  129. misc2Program: misc2Program.programId,
  130. },
  131. });
  132. stateAccount = await misc2Program.state.fetch();
  133. assert.ok(stateAccount.data.eq(newData));
  134. assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
  135. });
  136. it("Can retrieve events when simulating a transaction", async () => {
  137. const resp = await program.simulate.testSimulate(44);
  138. const expectedRaw = [
  139. "Program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS invoke [1]",
  140. "Program log: Instruction: TestSimulate",
  141. "Program data: NgyCA9omwbMsAAAA",
  142. "Program data: fPhuIELK/k7SBAAA",
  143. "Program data: jvbowsvlmkcJAAAA",
  144. "Program data: zxM5neEnS1kBAgMEBQYHCAkK",
  145. "Program data: g06Ei2GL1gIBAgMEBQYHCAkKCw==",
  146. "Program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS consumed 5320 of 200000 compute units",
  147. "Program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS success",
  148. ];
  149. assert.deepStrictEqual(expectedRaw, resp.raw);
  150. assert.ok(resp.events[0].name === "E1");
  151. assert.ok(resp.events[0].data.data === 44);
  152. assert.ok(resp.events[1].name === "E2");
  153. assert.ok(resp.events[1].data.data === 1234);
  154. assert.ok(resp.events[2].name === "E3");
  155. assert.ok(resp.events[2].data.data === 9);
  156. assert.ok(resp.events[3].name === "E5");
  157. assert.deepStrictEqual(
  158. resp.events[3].data.data,
  159. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  160. );
  161. assert.ok(resp.events[4].name === "E6");
  162. assert.deepStrictEqual(
  163. resp.events[4].data.data,
  164. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  165. );
  166. });
  167. let dataI8;
  168. it("Can use i8 in the idl", async () => {
  169. dataI8 = anchor.web3.Keypair.generate();
  170. await program.rpc.testI8(-3, {
  171. accounts: {
  172. data: dataI8.publicKey,
  173. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  174. },
  175. instructions: [await program.account.dataI8.createInstruction(dataI8)],
  176. signers: [dataI8],
  177. });
  178. const dataAccount = await program.account.dataI8.fetch(dataI8.publicKey);
  179. assert.ok(dataAccount.data === -3);
  180. });
  181. let dataPubkey;
  182. it("Can use i16 in the idl", async () => {
  183. const data = anchor.web3.Keypair.generate();
  184. await program.rpc.testI16(-2048, {
  185. accounts: {
  186. data: data.publicKey,
  187. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  188. },
  189. instructions: [await program.account.dataI16.createInstruction(data)],
  190. signers: [data],
  191. });
  192. const dataAccount = await program.account.dataI16.fetch(data.publicKey);
  193. assert.ok(dataAccount.data === -2048);
  194. dataPubkey = data.publicKey;
  195. });
  196. it("Can use base58 strings to fetch an account", async () => {
  197. const dataAccount = await program.account.dataI16.fetch(
  198. dataPubkey.toString()
  199. );
  200. assert.ok(dataAccount.data === -2048);
  201. });
  202. it("Should fail to close an account when sending lamports to itself", async () => {
  203. try {
  204. await program.rpc.testClose({
  205. accounts: {
  206. data: data.publicKey,
  207. solDest: data.publicKey,
  208. },
  209. });
  210. assert.ok(false);
  211. } catch (err) {
  212. const errMsg = "A close constraint was violated";
  213. assert.equal(err.toString(), errMsg);
  214. assert.equal(err.msg, errMsg);
  215. assert.equal(err.code, 2011);
  216. }
  217. });
  218. it("Can close an account", async () => {
  219. const openAccount = await program.provider.connection.getAccountInfo(
  220. data.publicKey
  221. );
  222. assert.ok(openAccount !== null);
  223. let beforeBalance = (
  224. await program.provider.connection.getAccountInfo(
  225. program.provider.wallet.publicKey
  226. )
  227. ).lamports;
  228. await program.rpc.testClose({
  229. accounts: {
  230. data: data.publicKey,
  231. solDest: program.provider.wallet.publicKey,
  232. },
  233. });
  234. let afterBalance = (
  235. await program.provider.connection.getAccountInfo(
  236. program.provider.wallet.publicKey
  237. )
  238. ).lamports;
  239. // Retrieved rent exemption sol.
  240. assert.ok(afterBalance > beforeBalance);
  241. const closedAccount = await program.provider.connection.getAccountInfo(
  242. data.publicKey
  243. );
  244. assert.ok(closedAccount === null);
  245. });
  246. it("Can use instruction data in accounts constraints", async () => {
  247. // b"my-seed"
  248. const seed = Buffer.from([109, 121, 45, 115, 101, 101, 100]);
  249. const [myPda, nonce] = await PublicKey.findProgramAddress(
  250. [seed, anchor.web3.SYSVAR_RENT_PUBKEY.toBuffer()],
  251. program.programId
  252. );
  253. await program.rpc.testInstructionConstraint(nonce, {
  254. accounts: {
  255. myPda,
  256. myAccount: anchor.web3.SYSVAR_RENT_PUBKEY,
  257. },
  258. });
  259. });
  260. it("Can create a PDA account with instruction data", async () => {
  261. const seed = Buffer.from([1, 2, 3, 4]);
  262. const domain = "my-domain";
  263. const foo = anchor.web3.SYSVAR_RENT_PUBKEY;
  264. const [myPda, nonce] = await PublicKey.findProgramAddress(
  265. [
  266. Buffer.from(anchor.utils.bytes.utf8.encode("my-seed")),
  267. Buffer.from(anchor.utils.bytes.utf8.encode(domain)),
  268. foo.toBuffer(),
  269. seed,
  270. ],
  271. program.programId
  272. );
  273. await program.rpc.testPdaInit(domain, seed, nonce, {
  274. accounts: {
  275. myPda,
  276. myPayer: program.provider.wallet.publicKey,
  277. foo,
  278. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  279. systemProgram: anchor.web3.SystemProgram.programId,
  280. },
  281. });
  282. const myPdaAccount = await program.account.dataU16.fetch(myPda);
  283. assert.ok(myPdaAccount.data === 6);
  284. });
  285. it("Can create a zero copy PDA account", async () => {
  286. const [myPda, nonce] = await PublicKey.findProgramAddress(
  287. [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
  288. program.programId
  289. );
  290. await program.rpc.testPdaInitZeroCopy({
  291. accounts: {
  292. myPda,
  293. myPayer: program.provider.wallet.publicKey,
  294. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  295. systemProgram: anchor.web3.SystemProgram.programId,
  296. },
  297. });
  298. const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
  299. assert.ok(myPdaAccount.data === 9);
  300. assert.ok(myPdaAccount.bump === nonce);
  301. });
  302. it("Can write to a zero copy PDA account", async () => {
  303. const [myPda, bump] = await PublicKey.findProgramAddress(
  304. [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
  305. program.programId
  306. );
  307. await program.rpc.testPdaMutZeroCopy({
  308. accounts: {
  309. myPda,
  310. myPayer: program.provider.wallet.publicKey,
  311. },
  312. });
  313. const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
  314. assert.ok(myPdaAccount.data === 1234);
  315. assert.ok(myPdaAccount.bump === bump);
  316. });
  317. it("Can create a token account from seeds pda", async () => {
  318. const [mint, mint_bump] = await PublicKey.findProgramAddress(
  319. [Buffer.from(anchor.utils.bytes.utf8.encode("my-mint-seed"))],
  320. program.programId
  321. );
  322. const [myPda, token_bump] = await PublicKey.findProgramAddress(
  323. [Buffer.from(anchor.utils.bytes.utf8.encode("my-token-seed"))],
  324. program.programId
  325. );
  326. await program.rpc.testTokenSeedsInit({
  327. accounts: {
  328. myPda,
  329. mint,
  330. authority: program.provider.wallet.publicKey,
  331. systemProgram: anchor.web3.SystemProgram.programId,
  332. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  333. tokenProgram: TOKEN_PROGRAM_ID,
  334. },
  335. });
  336. const mintAccount = new Token(
  337. program.provider.connection,
  338. mint,
  339. TOKEN_PROGRAM_ID,
  340. program.provider.wallet.payer
  341. );
  342. const account = await mintAccount.getAccountInfo(myPda);
  343. assert.ok(account.state === 1);
  344. assert.ok(account.amount.toNumber() === 0);
  345. assert.ok(account.isInitialized);
  346. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  347. assert.ok(account.mint.equals(mint));
  348. });
  349. it("Can execute a fallback function", async () => {
  350. await assert.rejects(
  351. async () => {
  352. await anchor.utils.rpc.invoke(program.programId);
  353. },
  354. (err) => {
  355. assert.ok(err.toString().includes("custom program error: 0x4d2"));
  356. return true;
  357. }
  358. );
  359. });
  360. it("Can init a random account", async () => {
  361. const data = anchor.web3.Keypair.generate();
  362. await program.rpc.testInit({
  363. accounts: {
  364. data: data.publicKey,
  365. payer: program.provider.wallet.publicKey,
  366. systemProgram: anchor.web3.SystemProgram.programId,
  367. },
  368. signers: [data],
  369. });
  370. const account = await program.account.dataI8.fetch(data.publicKey);
  371. assert.ok(account.data === 3);
  372. });
  373. it("Can init a random account prefunded", async () => {
  374. const data = anchor.web3.Keypair.generate();
  375. await program.rpc.testInit({
  376. accounts: {
  377. data: data.publicKey,
  378. payer: program.provider.wallet.publicKey,
  379. systemProgram: anchor.web3.SystemProgram.programId,
  380. },
  381. signers: [data],
  382. instructions: [
  383. anchor.web3.SystemProgram.transfer({
  384. fromPubkey: program.provider.wallet.publicKey,
  385. toPubkey: data.publicKey,
  386. lamports: 4039280,
  387. }),
  388. ],
  389. });
  390. const account = await program.account.dataI8.fetch(data.publicKey);
  391. assert.ok(account.data === 3);
  392. });
  393. it("Can init a random zero copy account", async () => {
  394. const data = anchor.web3.Keypair.generate();
  395. await program.rpc.testInitZeroCopy({
  396. accounts: {
  397. data: data.publicKey,
  398. payer: program.provider.wallet.publicKey,
  399. systemProgram: anchor.web3.SystemProgram.programId,
  400. },
  401. signers: [data],
  402. });
  403. const account = await program.account.dataZeroCopy.fetch(data.publicKey);
  404. assert.ok(account.data === 10);
  405. assert.ok(account.bump === 2);
  406. });
  407. let mint = undefined;
  408. it("Can create a random mint account", async () => {
  409. mint = anchor.web3.Keypair.generate();
  410. await program.rpc.testInitMint({
  411. accounts: {
  412. mint: mint.publicKey,
  413. payer: program.provider.wallet.publicKey,
  414. systemProgram: anchor.web3.SystemProgram.programId,
  415. tokenProgram: TOKEN_PROGRAM_ID,
  416. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  417. },
  418. signers: [mint],
  419. });
  420. const client = new Token(
  421. program.provider.connection,
  422. mint.publicKey,
  423. TOKEN_PROGRAM_ID,
  424. program.provider.wallet.payer
  425. );
  426. const mintAccount = await client.getMintInfo();
  427. assert.ok(mintAccount.decimals === 6);
  428. assert.ok(
  429. mintAccount.mintAuthority.equals(program.provider.wallet.publicKey)
  430. );
  431. assert.ok(
  432. mintAccount.freezeAuthority.equals(program.provider.wallet.publicKey)
  433. );
  434. });
  435. it("Can create a random mint account prefunded", async () => {
  436. mint = anchor.web3.Keypair.generate();
  437. await program.rpc.testInitMint({
  438. accounts: {
  439. mint: mint.publicKey,
  440. payer: program.provider.wallet.publicKey,
  441. systemProgram: anchor.web3.SystemProgram.programId,
  442. tokenProgram: TOKEN_PROGRAM_ID,
  443. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  444. },
  445. signers: [mint],
  446. instructions: [
  447. anchor.web3.SystemProgram.transfer({
  448. fromPubkey: program.provider.wallet.publicKey,
  449. toPubkey: mint.publicKey,
  450. lamports: 4039280,
  451. }),
  452. ],
  453. });
  454. const client = new Token(
  455. program.provider.connection,
  456. mint.publicKey,
  457. TOKEN_PROGRAM_ID,
  458. program.provider.wallet.payer
  459. );
  460. const mintAccount = await client.getMintInfo();
  461. assert.ok(mintAccount.decimals === 6);
  462. assert.ok(
  463. mintAccount.mintAuthority.equals(program.provider.wallet.publicKey)
  464. );
  465. });
  466. it("Can create a random token account", async () => {
  467. const token = anchor.web3.Keypair.generate();
  468. await program.rpc.testInitToken({
  469. accounts: {
  470. token: token.publicKey,
  471. mint: mint.publicKey,
  472. payer: program.provider.wallet.publicKey,
  473. systemProgram: anchor.web3.SystemProgram.programId,
  474. tokenProgram: TOKEN_PROGRAM_ID,
  475. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  476. },
  477. signers: [token],
  478. });
  479. const client = new Token(
  480. program.provider.connection,
  481. mint.publicKey,
  482. TOKEN_PROGRAM_ID,
  483. program.provider.wallet.payer
  484. );
  485. const account = await client.getAccountInfo(token.publicKey);
  486. assert.ok(account.state === 1);
  487. assert.ok(account.amount.toNumber() === 0);
  488. assert.ok(account.isInitialized);
  489. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  490. assert.ok(account.mint.equals(mint.publicKey));
  491. });
  492. it("Can create a random token with prefunding", async () => {
  493. const token = anchor.web3.Keypair.generate();
  494. await program.rpc.testInitToken({
  495. accounts: {
  496. token: token.publicKey,
  497. mint: mint.publicKey,
  498. payer: program.provider.wallet.publicKey,
  499. systemProgram: anchor.web3.SystemProgram.programId,
  500. tokenProgram: TOKEN_PROGRAM_ID,
  501. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  502. },
  503. signers: [token],
  504. instructions: [
  505. anchor.web3.SystemProgram.transfer({
  506. fromPubkey: program.provider.wallet.publicKey,
  507. toPubkey: token.publicKey,
  508. lamports: 4039280,
  509. }),
  510. ],
  511. });
  512. const client = new Token(
  513. program.provider.connection,
  514. mint.publicKey,
  515. TOKEN_PROGRAM_ID,
  516. program.provider.wallet.payer
  517. );
  518. const account = await client.getAccountInfo(token.publicKey);
  519. assert.ok(account.state === 1);
  520. assert.ok(account.amount.toNumber() === 0);
  521. assert.ok(account.isInitialized);
  522. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  523. assert.ok(account.mint.equals(mint.publicKey));
  524. });
  525. it("Can create a random token with prefunding under the rent exemption", async () => {
  526. const token = anchor.web3.Keypair.generate();
  527. await program.rpc.testInitToken({
  528. accounts: {
  529. token: token.publicKey,
  530. mint: mint.publicKey,
  531. payer: program.provider.wallet.publicKey,
  532. systemProgram: anchor.web3.SystemProgram.programId,
  533. tokenProgram: TOKEN_PROGRAM_ID,
  534. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  535. },
  536. signers: [token],
  537. instructions: [
  538. anchor.web3.SystemProgram.transfer({
  539. fromPubkey: program.provider.wallet.publicKey,
  540. toPubkey: token.publicKey,
  541. lamports: 1,
  542. }),
  543. ],
  544. });
  545. const client = new Token(
  546. program.provider.connection,
  547. mint.publicKey,
  548. TOKEN_PROGRAM_ID,
  549. program.provider.wallet.payer
  550. );
  551. const account = await client.getAccountInfo(token.publicKey);
  552. assert.ok(account.state === 1);
  553. assert.ok(account.amount.toNumber() === 0);
  554. assert.ok(account.isInitialized);
  555. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  556. assert.ok(account.mint.equals(mint.publicKey));
  557. });
  558. it("Can initialize multiple accounts via a composite payer", async () => {
  559. const data1 = anchor.web3.Keypair.generate();
  560. const data2 = anchor.web3.Keypair.generate();
  561. const tx = await program.rpc.testCompositePayer({
  562. accounts: {
  563. composite: {
  564. data: data1.publicKey,
  565. payer: program.provider.wallet.publicKey,
  566. systemProgram: anchor.web3.SystemProgram.programId,
  567. },
  568. data: data2.publicKey,
  569. systemProgram: anchor.web3.SystemProgram.programId,
  570. },
  571. signers: [data1, data2],
  572. });
  573. const account1 = await program.account.dataI8.fetch(data1.publicKey);
  574. assert.equal(account1.data, 1);
  575. const account2 = await program.account.data.fetch(data2.publicKey);
  576. assert.equal(account2.udata, 2);
  577. assert.equal(account2.idata, 3);
  578. });
  579. describe("associated_token constraints", () => {
  580. let associatedToken = null;
  581. // apparently cannot await here so doing it in the 'it' statements
  582. let client = Token.createMint(
  583. program.provider.connection,
  584. program.provider.wallet.payer,
  585. program.provider.wallet.publicKey,
  586. program.provider.wallet.publicKey,
  587. 9,
  588. TOKEN_PROGRAM_ID
  589. );
  590. it("Can create an associated token account", async () => {
  591. const localClient = await client;
  592. associatedToken = await Token.getAssociatedTokenAddress(
  593. ASSOCIATED_TOKEN_PROGRAM_ID,
  594. TOKEN_PROGRAM_ID,
  595. localClient.publicKey,
  596. program.provider.wallet.publicKey
  597. );
  598. await program.rpc.testInitAssociatedToken({
  599. accounts: {
  600. token: associatedToken,
  601. mint: localClient.publicKey,
  602. payer: program.provider.wallet.publicKey,
  603. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  604. systemProgram: anchor.web3.SystemProgram.programId,
  605. tokenProgram: TOKEN_PROGRAM_ID,
  606. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  607. },
  608. });
  609. const account = await localClient.getAccountInfo(associatedToken);
  610. assert.ok(account.state === 1);
  611. assert.ok(account.amount.toNumber() === 0);
  612. assert.ok(account.isInitialized);
  613. assert.ok(account.owner.equals(program.provider.wallet.publicKey));
  614. assert.ok(account.mint.equals(localClient.publicKey));
  615. });
  616. it("Can validate associated_token constraints", async () => {
  617. const localClient = await client;
  618. await program.rpc.testValidateAssociatedToken({
  619. accounts: {
  620. token: associatedToken,
  621. mint: localClient.publicKey,
  622. wallet: program.provider.wallet.publicKey,
  623. },
  624. });
  625. let otherMint = await Token.createMint(
  626. program.provider.connection,
  627. program.provider.wallet.payer,
  628. program.provider.wallet.publicKey,
  629. program.provider.wallet.publicKey,
  630. 9,
  631. TOKEN_PROGRAM_ID
  632. );
  633. await assert.rejects(
  634. async () => {
  635. await program.rpc.testValidateAssociatedToken({
  636. accounts: {
  637. token: associatedToken,
  638. mint: otherMint.publicKey,
  639. wallet: program.provider.wallet.publicKey,
  640. },
  641. });
  642. },
  643. (err) => {
  644. assert.equal(err.code, 2009);
  645. return true;
  646. }
  647. );
  648. });
  649. it("associated_token constraints check do not allow authority change", async () => {
  650. const localClient = await client;
  651. await program.rpc.testValidateAssociatedToken({
  652. accounts: {
  653. token: associatedToken,
  654. mint: localClient.publicKey,
  655. wallet: program.provider.wallet.publicKey,
  656. },
  657. });
  658. await localClient.setAuthority(
  659. associatedToken,
  660. anchor.web3.Keypair.generate().publicKey,
  661. "AccountOwner",
  662. program.provider.wallet.payer,
  663. []
  664. );
  665. await assert.rejects(
  666. async () => {
  667. await program.rpc.testValidateAssociatedToken({
  668. accounts: {
  669. token: associatedToken,
  670. mint: localClient.publicKey,
  671. wallet: program.provider.wallet.publicKey,
  672. },
  673. });
  674. },
  675. (err) => {
  676. assert.equal(err.code, 2015);
  677. return true;
  678. }
  679. );
  680. });
  681. });
  682. it("Can fetch all accounts of a given type", async () => {
  683. // Initialize the accounts.
  684. const data1 = anchor.web3.Keypair.generate();
  685. const data2 = anchor.web3.Keypair.generate();
  686. const data3 = anchor.web3.Keypair.generate();
  687. const data4 = anchor.web3.Keypair.generate();
  688. // Initialize filterable data.
  689. const filterable1 = anchor.web3.Keypair.generate().publicKey;
  690. const filterable2 = anchor.web3.Keypair.generate().publicKey;
  691. // Set up a secondary wallet and program.
  692. const anotherProgram = new anchor.Program(
  693. miscIdl,
  694. program.programId,
  695. new anchor.Provider(
  696. program.provider.connection,
  697. new anchor.Wallet(anchor.web3.Keypair.generate()),
  698. { commitment: program.provider.connection.commitment }
  699. )
  700. );
  701. // Request airdrop for secondary wallet.
  702. const signature = await program.provider.connection.requestAirdrop(
  703. anotherProgram.provider.wallet.publicKey,
  704. anchor.web3.LAMPORTS_PER_SOL
  705. );
  706. await program.provider.connection.confirmTransaction(signature);
  707. // Create all the accounts.
  708. await Promise.all([
  709. program.rpc.testFetchAll(filterable1, {
  710. accounts: {
  711. data: data1.publicKey,
  712. authority: program.provider.wallet.publicKey,
  713. systemProgram: anchor.web3.SystemProgram.programId,
  714. },
  715. signers: [data1],
  716. }),
  717. program.rpc.testFetchAll(filterable1, {
  718. accounts: {
  719. data: data2.publicKey,
  720. authority: program.provider.wallet.publicKey,
  721. systemProgram: anchor.web3.SystemProgram.programId,
  722. },
  723. signers: [data2],
  724. }),
  725. program.rpc.testFetchAll(filterable2, {
  726. accounts: {
  727. data: data3.publicKey,
  728. authority: program.provider.wallet.publicKey,
  729. systemProgram: anchor.web3.SystemProgram.programId,
  730. },
  731. signers: [data3],
  732. }),
  733. anotherProgram.rpc.testFetchAll(filterable1, {
  734. accounts: {
  735. data: data4.publicKey,
  736. authority: anotherProgram.provider.wallet.publicKey,
  737. systemProgram: anchor.web3.SystemProgram.programId,
  738. },
  739. signers: [data4],
  740. }),
  741. ]);
  742. // Call for multiple kinds of .all.
  743. const allAccounts = await program.account.dataWithFilter.all();
  744. const allAccountsFilteredByBuffer =
  745. await program.account.dataWithFilter.all(
  746. program.provider.wallet.publicKey.toBuffer()
  747. );
  748. const allAccountsFilteredByProgramFilters1 =
  749. await program.account.dataWithFilter.all([
  750. {
  751. memcmp: {
  752. offset: 8,
  753. bytes: program.provider.wallet.publicKey.toBase58(),
  754. },
  755. },
  756. { memcmp: { offset: 40, bytes: filterable1.toBase58() } },
  757. ]);
  758. const allAccountsFilteredByProgramFilters2 =
  759. await program.account.dataWithFilter.all([
  760. {
  761. memcmp: {
  762. offset: 8,
  763. bytes: program.provider.wallet.publicKey.toBase58(),
  764. },
  765. },
  766. { memcmp: { offset: 40, bytes: filterable2.toBase58() } },
  767. ]);
  768. // Without filters there should be 4 accounts.
  769. assert.equal(allAccounts.length, 4);
  770. // Filtering by main wallet there should be 3 accounts.
  771. assert.equal(allAccountsFilteredByBuffer.length, 3);
  772. // Filtering all the main wallet accounts and matching the filterable1 value
  773. // results in a 2 accounts.
  774. assert.equal(allAccountsFilteredByProgramFilters1.length, 2);
  775. // Filtering all the main wallet accounts and matching the filterable2 value
  776. // results in 1 account.
  777. assert.equal(allAccountsFilteredByProgramFilters2.length, 1);
  778. });
  779. it("Can use pdas with empty seeds", async () => {
  780. const [pda, bump] = await PublicKey.findProgramAddress(
  781. [],
  782. program.programId
  783. );
  784. await program.rpc.testInitWithEmptySeeds({
  785. accounts: {
  786. pda: pda,
  787. authority: program.provider.wallet.publicKey,
  788. systemProgram: anchor.web3.SystemProgram.programId,
  789. },
  790. });
  791. await program.rpc.testEmptySeedsConstraint({
  792. accounts: {
  793. pda: pda,
  794. },
  795. });
  796. const [pda2, bump2] = await PublicKey.findProgramAddress(
  797. ["non-empty"],
  798. program.programId
  799. );
  800. await assert.rejects(
  801. program.rpc.testEmptySeedsConstraint({
  802. accounts: {
  803. pda: pda2,
  804. },
  805. }),
  806. (err) => {
  807. assert.equal(err.code, 2006);
  808. return true;
  809. }
  810. );
  811. });
  812. const ifNeededAcc = anchor.web3.Keypair.generate();
  813. it("Can init if needed a new account", async () => {
  814. await program.rpc.testInitIfNeeded(1, {
  815. accounts: {
  816. data: ifNeededAcc.publicKey,
  817. systemProgram: anchor.web3.SystemProgram.programId,
  818. payer: program.provider.wallet.publicKey,
  819. },
  820. signers: [ifNeededAcc],
  821. });
  822. const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
  823. assert.equal(account.data, 1);
  824. });
  825. it("Can init if needed a previously created account", async () => {
  826. await program.rpc.testInitIfNeeded(3, {
  827. accounts: {
  828. data: ifNeededAcc.publicKey,
  829. systemProgram: anchor.web3.SystemProgram.programId,
  830. payer: program.provider.wallet.publicKey,
  831. },
  832. signers: [ifNeededAcc],
  833. });
  834. const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
  835. assert.equal(account.data, 3);
  836. });
  837. it("Can use const for array size", async () => {
  838. const data = anchor.web3.Keypair.generate();
  839. const tx = await program.rpc.testConstArraySize(99, {
  840. accounts: {
  841. data: data.publicKey,
  842. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  843. },
  844. signers: [data],
  845. instructions: [
  846. await program.account.dataConstArraySize.createInstruction(data),
  847. ],
  848. });
  849. const dataAccount = await program.account.dataConstArraySize.fetch(
  850. data.publicKey
  851. );
  852. assert.deepStrictEqual(dataAccount.data, [99, ...new Array(9).fill(0)]);
  853. });
  854. it("Can use const for instruction data size", async () => {
  855. const data = anchor.web3.Keypair.generate();
  856. const dataArray = [99, ...new Array(9).fill(0)];
  857. const tx = await program.rpc.testConstIxDataSize(dataArray, {
  858. accounts: {
  859. data: data.publicKey,
  860. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  861. },
  862. signers: [data],
  863. instructions: [
  864. await program.account.dataConstArraySize.createInstruction(data),
  865. ],
  866. });
  867. const dataAccount = await program.account.dataConstArraySize.fetch(
  868. data.publicKey
  869. );
  870. assert.deepStrictEqual(dataAccount.data, dataArray);
  871. });
  872. it("Should include BASE const in IDL", async () => {
  873. assert(
  874. miscIdl.constants.find(
  875. (c) => c.name === "BASE" && c.type === "u128" && c.value === "1_000_000"
  876. ) !== undefined
  877. );
  878. });
  879. it("Should include DECIMALS const in IDL", async () => {
  880. assert(
  881. miscIdl.constants.find(
  882. (c) => c.name === "DECIMALS" && c.type === "u8" && c.value === "6"
  883. ) !== undefined
  884. );
  885. });
  886. it("Should not include NO_IDL const in IDL", async () => {
  887. assert.equal(
  888. miscIdl.constants.find((c) => c.name === "NO_IDL"),
  889. undefined
  890. );
  891. });
  892. it("init_if_needed throws if account exists but is not owned by the expected program", async () => {
  893. const newAcc = await anchor.web3.PublicKey.findProgramAddress(
  894. [utf8.encode("hello")],
  895. program.programId
  896. );
  897. await program.rpc.testInitIfNeededChecksOwner({
  898. accounts: {
  899. data: newAcc[0],
  900. systemProgram: anchor.web3.SystemProgram.programId,
  901. payer: program.provider.wallet.publicKey,
  902. owner: program.programId,
  903. },
  904. });
  905. try {
  906. await program.rpc.testInitIfNeededChecksOwner({
  907. accounts: {
  908. data: newAcc[0],
  909. systemProgram: anchor.web3.SystemProgram.programId,
  910. payer: program.provider.wallet.publicKey,
  911. owner: anchor.web3.Keypair.generate().publicKey,
  912. },
  913. });
  914. assert.ok(false);
  915. } catch (err) {
  916. assert.equal(err.code, 2004);
  917. }
  918. });
  919. it("init_if_needed throws if pda account exists but does not have the expected seeds", async () => {
  920. const newAcc = await anchor.web3.PublicKey.findProgramAddress(
  921. [utf8.encode("nothello")],
  922. program.programId
  923. );
  924. await program.rpc.testInitIfNeededChecksSeeds("nothello", {
  925. accounts: {
  926. data: newAcc[0],
  927. systemProgram: anchor.web3.SystemProgram.programId,
  928. payer: program.provider.wallet.publicKey,
  929. },
  930. });
  931. // this will throw if it is not a proper PDA
  932. // we need this so we know that the following tx failed
  933. // not because it couldn't create this pda
  934. // but because the two pdas were different
  935. anchor.web3.PublicKey.createProgramAddress(
  936. [utf8.encode("hello")],
  937. program.programId
  938. );
  939. try {
  940. await program.rpc.testInitIfNeededChecksSeeds("hello", {
  941. accounts: {
  942. data: newAcc[0],
  943. systemProgram: anchor.web3.SystemProgram.programId,
  944. payer: program.provider.wallet.publicKey,
  945. owner: anchor.web3.Keypair.generate().publicKey,
  946. },
  947. });
  948. assert.ok(false);
  949. } catch (err) {
  950. assert.equal(err.code, 2006);
  951. }
  952. });
  953. it("init_if_needed throws if account exists but is not the expected space", async () => {
  954. const newAcc = anchor.web3.Keypair.generate();
  955. const _irrelevantForTest = 3;
  956. await program.rpc.initWithSpace(_irrelevantForTest, {
  957. accounts: {
  958. data: newAcc.publicKey,
  959. systemProgram: anchor.web3.SystemProgram.programId,
  960. payer: program.provider.wallet.publicKey,
  961. },
  962. signers: [newAcc],
  963. });
  964. try {
  965. await program.rpc.testInitIfNeeded(_irrelevantForTest, {
  966. accounts: {
  967. data: newAcc.publicKey,
  968. systemProgram: anchor.web3.SystemProgram.programId,
  969. payer: program.provider.wallet.publicKey,
  970. },
  971. signers: [newAcc],
  972. });
  973. assert.ok(false);
  974. } catch (err) {
  975. assert.equal(err.code, 2019);
  976. }
  977. });
  978. it("init_if_needed throws if mint exists but has the wrong mint authority", async () => {
  979. const mint = anchor.web3.Keypair.generate();
  980. await program.rpc.testInitMint({
  981. accounts: {
  982. mint: mint.publicKey,
  983. payer: program.provider.wallet.publicKey,
  984. systemProgram: anchor.web3.SystemProgram.programId,
  985. tokenProgram: TOKEN_PROGRAM_ID,
  986. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  987. },
  988. signers: [mint],
  989. });
  990. try {
  991. await program.rpc.testInitMintIfNeeded(6, {
  992. accounts: {
  993. mint: mint.publicKey,
  994. payer: program.provider.wallet.publicKey,
  995. systemProgram: anchor.web3.SystemProgram.programId,
  996. tokenProgram: TOKEN_PROGRAM_ID,
  997. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  998. mintAuthority: anchor.web3.Keypair.generate().publicKey,
  999. freezeAuthority: program.provider.wallet.publicKey,
  1000. },
  1001. signers: [mint],
  1002. });
  1003. assert.ok(false);
  1004. } catch (err) {
  1005. assert.equal(err.code, 2016);
  1006. }
  1007. });
  1008. it("init_if_needed throws if mint exists but has the wrong freeze authority", async () => {
  1009. const mint = anchor.web3.Keypair.generate();
  1010. await program.rpc.testInitMint({
  1011. accounts: {
  1012. mint: mint.publicKey,
  1013. payer: program.provider.wallet.publicKey,
  1014. systemProgram: anchor.web3.SystemProgram.programId,
  1015. tokenProgram: TOKEN_PROGRAM_ID,
  1016. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1017. },
  1018. signers: [mint],
  1019. });
  1020. try {
  1021. await program.rpc.testInitMintIfNeeded(6, {
  1022. accounts: {
  1023. mint: mint.publicKey,
  1024. payer: program.provider.wallet.publicKey,
  1025. systemProgram: anchor.web3.SystemProgram.programId,
  1026. tokenProgram: TOKEN_PROGRAM_ID,
  1027. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1028. mintAuthority: program.provider.wallet.publicKey,
  1029. freezeAuthority: anchor.web3.Keypair.generate().publicKey,
  1030. },
  1031. signers: [mint],
  1032. });
  1033. assert.ok(false);
  1034. } catch (err) {
  1035. assert.equal(err.code, 2017);
  1036. }
  1037. });
  1038. it("init_if_needed throws if mint exists but has the wrong decimals", async () => {
  1039. const mint = anchor.web3.Keypair.generate();
  1040. await program.rpc.testInitMint({
  1041. accounts: {
  1042. mint: mint.publicKey,
  1043. payer: program.provider.wallet.publicKey,
  1044. systemProgram: anchor.web3.SystemProgram.programId,
  1045. tokenProgram: TOKEN_PROGRAM_ID,
  1046. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1047. },
  1048. signers: [mint],
  1049. });
  1050. try {
  1051. await program.rpc.testInitMintIfNeeded(9, {
  1052. accounts: {
  1053. mint: mint.publicKey,
  1054. payer: program.provider.wallet.publicKey,
  1055. systemProgram: anchor.web3.SystemProgram.programId,
  1056. tokenProgram: TOKEN_PROGRAM_ID,
  1057. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1058. mintAuthority: program.provider.wallet.publicKey,
  1059. freezeAuthority: program.provider.wallet.publicKey,
  1060. },
  1061. signers: [mint],
  1062. });
  1063. assert.ok(false);
  1064. } catch (err) {
  1065. assert.equal(err.code, 2018);
  1066. }
  1067. });
  1068. it("init_if_needed throws if token exists but has the wrong owner", async () => {
  1069. const mint = anchor.web3.Keypair.generate();
  1070. await program.rpc.testInitMint({
  1071. accounts: {
  1072. mint: mint.publicKey,
  1073. payer: program.provider.wallet.publicKey,
  1074. systemProgram: anchor.web3.SystemProgram.programId,
  1075. tokenProgram: TOKEN_PROGRAM_ID,
  1076. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1077. },
  1078. signers: [mint],
  1079. });
  1080. const token = anchor.web3.Keypair.generate();
  1081. await program.rpc.testInitToken({
  1082. accounts: {
  1083. token: token.publicKey,
  1084. mint: mint.publicKey,
  1085. payer: program.provider.wallet.publicKey,
  1086. systemProgram: anchor.web3.SystemProgram.programId,
  1087. tokenProgram: TOKEN_PROGRAM_ID,
  1088. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1089. },
  1090. signers: [token],
  1091. });
  1092. try {
  1093. await program.rpc.testInitTokenIfNeeded({
  1094. accounts: {
  1095. token: token.publicKey,
  1096. mint: mint.publicKey,
  1097. payer: program.provider.wallet.publicKey,
  1098. systemProgram: anchor.web3.SystemProgram.programId,
  1099. tokenProgram: TOKEN_PROGRAM_ID,
  1100. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1101. authority: anchor.web3.Keypair.generate().publicKey,
  1102. },
  1103. signers: [token],
  1104. });
  1105. assert.ok(false);
  1106. } catch (err) {
  1107. assert.equal(err.code, 2015);
  1108. }
  1109. });
  1110. it("init_if_needed throws if token exists but has the wrong mint", async () => {
  1111. const mint = anchor.web3.Keypair.generate();
  1112. await program.rpc.testInitMint({
  1113. accounts: {
  1114. mint: mint.publicKey,
  1115. payer: program.provider.wallet.publicKey,
  1116. systemProgram: anchor.web3.SystemProgram.programId,
  1117. tokenProgram: TOKEN_PROGRAM_ID,
  1118. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1119. },
  1120. signers: [mint],
  1121. });
  1122. const mint2 = anchor.web3.Keypair.generate();
  1123. await program.rpc.testInitMint({
  1124. accounts: {
  1125. mint: mint2.publicKey,
  1126. payer: program.provider.wallet.publicKey,
  1127. systemProgram: anchor.web3.SystemProgram.programId,
  1128. tokenProgram: TOKEN_PROGRAM_ID,
  1129. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1130. },
  1131. signers: [mint2],
  1132. });
  1133. const token = anchor.web3.Keypair.generate();
  1134. await program.rpc.testInitToken({
  1135. accounts: {
  1136. token: token.publicKey,
  1137. mint: mint.publicKey,
  1138. payer: program.provider.wallet.publicKey,
  1139. systemProgram: anchor.web3.SystemProgram.programId,
  1140. tokenProgram: TOKEN_PROGRAM_ID,
  1141. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1142. },
  1143. signers: [token],
  1144. });
  1145. try {
  1146. await program.rpc.testInitTokenIfNeeded({
  1147. accounts: {
  1148. token: token.publicKey,
  1149. mint: mint2.publicKey,
  1150. payer: program.provider.wallet.publicKey,
  1151. systemProgram: anchor.web3.SystemProgram.programId,
  1152. tokenProgram: TOKEN_PROGRAM_ID,
  1153. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1154. authority: program.provider.wallet.publicKey,
  1155. },
  1156. signers: [token],
  1157. });
  1158. assert.ok(false);
  1159. } catch (err) {
  1160. assert.equal(err.code, 2014);
  1161. }
  1162. });
  1163. it("init_if_needed throws if associated token exists but has the wrong owner", async () => {
  1164. const mint = Keypair.generate();
  1165. await program.rpc.testInitMint({
  1166. accounts: {
  1167. mint: mint.publicKey,
  1168. payer: program.provider.wallet.publicKey,
  1169. systemProgram: SystemProgram.programId,
  1170. tokenProgram: TOKEN_PROGRAM_ID,
  1171. rent: SYSVAR_RENT_PUBKEY,
  1172. },
  1173. signers: [mint],
  1174. });
  1175. const associatedToken = await Token.getAssociatedTokenAddress(
  1176. ASSOCIATED_TOKEN_PROGRAM_ID,
  1177. TOKEN_PROGRAM_ID,
  1178. mint.publicKey,
  1179. program.provider.wallet.publicKey
  1180. );
  1181. await program.rpc.testInitAssociatedToken({
  1182. accounts: {
  1183. token: associatedToken,
  1184. mint: mint.publicKey,
  1185. payer: program.provider.wallet.publicKey,
  1186. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1187. systemProgram: anchor.web3.SystemProgram.programId,
  1188. tokenProgram: TOKEN_PROGRAM_ID,
  1189. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1190. },
  1191. });
  1192. try {
  1193. await program.rpc.testInitAssociatedTokenIfNeeded({
  1194. accounts: {
  1195. token: associatedToken,
  1196. mint: mint.publicKey,
  1197. payer: program.provider.wallet.publicKey,
  1198. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1199. systemProgram: anchor.web3.SystemProgram.programId,
  1200. tokenProgram: TOKEN_PROGRAM_ID,
  1201. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1202. authority: anchor.web3.Keypair.generate().publicKey,
  1203. },
  1204. });
  1205. assert.ok(false);
  1206. } catch (err) {
  1207. assert.equal(err.code, 2015);
  1208. }
  1209. });
  1210. it("init_if_needed throws if associated token exists but has the wrong mint", async () => {
  1211. const mint = anchor.web3.Keypair.generate();
  1212. await program.rpc.testInitMint({
  1213. accounts: {
  1214. mint: mint.publicKey,
  1215. payer: program.provider.wallet.publicKey,
  1216. systemProgram: anchor.web3.SystemProgram.programId,
  1217. tokenProgram: TOKEN_PROGRAM_ID,
  1218. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1219. },
  1220. signers: [mint],
  1221. });
  1222. const mint2 = anchor.web3.Keypair.generate();
  1223. await program.rpc.testInitMint({
  1224. accounts: {
  1225. mint: mint2.publicKey,
  1226. payer: program.provider.wallet.publicKey,
  1227. systemProgram: anchor.web3.SystemProgram.programId,
  1228. tokenProgram: TOKEN_PROGRAM_ID,
  1229. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1230. },
  1231. signers: [mint2],
  1232. });
  1233. const associatedToken = await Token.getAssociatedTokenAddress(
  1234. ASSOCIATED_TOKEN_PROGRAM_ID,
  1235. TOKEN_PROGRAM_ID,
  1236. mint.publicKey,
  1237. program.provider.wallet.publicKey
  1238. );
  1239. await program.rpc.testInitAssociatedToken({
  1240. accounts: {
  1241. token: associatedToken,
  1242. mint: mint.publicKey,
  1243. payer: program.provider.wallet.publicKey,
  1244. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1245. systemProgram: anchor.web3.SystemProgram.programId,
  1246. tokenProgram: TOKEN_PROGRAM_ID,
  1247. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1248. },
  1249. });
  1250. try {
  1251. await program.rpc.testInitAssociatedTokenIfNeeded({
  1252. accounts: {
  1253. token: associatedToken,
  1254. mint: mint2.publicKey,
  1255. payer: program.provider.wallet.publicKey,
  1256. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1257. systemProgram: anchor.web3.SystemProgram.programId,
  1258. tokenProgram: TOKEN_PROGRAM_ID,
  1259. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1260. authority: program.provider.wallet.publicKey,
  1261. },
  1262. });
  1263. assert.ok(false);
  1264. } catch (err) {
  1265. assert.equal(err.code, 2014);
  1266. }
  1267. });
  1268. it("init_if_needed throws if token exists with correct owner and mint but is not the ATA", async () => {
  1269. const mint = anchor.web3.Keypair.generate();
  1270. await program.rpc.testInitMint({
  1271. accounts: {
  1272. mint: mint.publicKey,
  1273. payer: program.provider.wallet.publicKey,
  1274. systemProgram: anchor.web3.SystemProgram.programId,
  1275. tokenProgram: TOKEN_PROGRAM_ID,
  1276. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1277. },
  1278. signers: [mint],
  1279. });
  1280. const associatedToken = await Token.getAssociatedTokenAddress(
  1281. ASSOCIATED_TOKEN_PROGRAM_ID,
  1282. TOKEN_PROGRAM_ID,
  1283. mint.publicKey,
  1284. program.provider.wallet.publicKey
  1285. );
  1286. await program.rpc.testInitAssociatedToken({
  1287. accounts: {
  1288. token: associatedToken,
  1289. mint: mint.publicKey,
  1290. payer: program.provider.wallet.publicKey,
  1291. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1292. systemProgram: anchor.web3.SystemProgram.programId,
  1293. tokenProgram: TOKEN_PROGRAM_ID,
  1294. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1295. },
  1296. });
  1297. const token = anchor.web3.Keypair.generate();
  1298. await program.rpc.testInitToken({
  1299. accounts: {
  1300. token: token.publicKey,
  1301. mint: mint.publicKey,
  1302. payer: program.provider.wallet.publicKey,
  1303. systemProgram: anchor.web3.SystemProgram.programId,
  1304. tokenProgram: TOKEN_PROGRAM_ID,
  1305. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1306. },
  1307. signers: [token],
  1308. });
  1309. try {
  1310. await program.rpc.testInitAssociatedTokenIfNeeded({
  1311. accounts: {
  1312. token: token.publicKey,
  1313. mint: mint.publicKey,
  1314. payer: program.provider.wallet.publicKey,
  1315. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1316. systemProgram: anchor.web3.SystemProgram.programId,
  1317. tokenProgram: TOKEN_PROGRAM_ID,
  1318. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1319. authority: program.provider.wallet.publicKey,
  1320. },
  1321. });
  1322. assert.ok(false);
  1323. } catch (err) {
  1324. assert.equal(err.code, 3014);
  1325. }
  1326. });
  1327. it("init_if_needed checks rent_exemption if init is not needed", async () => {
  1328. const data = anchor.web3.Keypair.generate();
  1329. await program.rpc.initDecreaseLamports({
  1330. accounts: {
  1331. data: data.publicKey,
  1332. user: anchor.getProvider().wallet.publicKey,
  1333. systemProgram: SystemProgram.programId,
  1334. },
  1335. signers: [data],
  1336. });
  1337. try {
  1338. await program.rpc.initIfNeededChecksRentExemption({
  1339. accounts: {
  1340. data: data.publicKey,
  1341. user: anchor.getProvider().wallet.publicKey,
  1342. systemProgram: SystemProgram.programId,
  1343. },
  1344. signers: [data],
  1345. });
  1346. assert.ok(false);
  1347. } catch (err) {
  1348. assert.equal(err.code, 2005);
  1349. }
  1350. });
  1351. it("Can use multidimensional array", async () => {
  1352. const array2d = new Array(10).fill(new Array(10).fill(99));
  1353. const data = anchor.web3.Keypair.generate();
  1354. await program.rpc.testMultidimensionalArray(array2d, {
  1355. accounts: {
  1356. data: data.publicKey,
  1357. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1358. },
  1359. signers: [data],
  1360. instructions: [
  1361. await program.account.dataMultidimensionalArray.createInstruction(data),
  1362. ],
  1363. });
  1364. const dataAccount = await program.account.dataMultidimensionalArray.fetch(
  1365. data.publicKey
  1366. );
  1367. assert.deepStrictEqual(dataAccount.data, array2d);
  1368. });
  1369. it("Can use multidimensional array with const sizes", async () => {
  1370. const array2d = new Array(10).fill(new Array(11).fill(22));
  1371. const data = anchor.web3.Keypair.generate();
  1372. await program.rpc.testMultidimensionalArrayConstSizes(array2d, {
  1373. accounts: {
  1374. data: data.publicKey,
  1375. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1376. },
  1377. signers: [data],
  1378. instructions: [
  1379. await program.account.dataMultidimensionalArrayConstSizes.createInstruction(
  1380. data
  1381. ),
  1382. ],
  1383. });
  1384. const dataAccount =
  1385. await program.account.dataMultidimensionalArrayConstSizes.fetch(
  1386. data.publicKey
  1387. );
  1388. assert.deepStrictEqual(dataAccount.data, array2d);
  1389. });
  1390. it("allows non-rent exempt accounts", async () => {
  1391. const data = anchor.web3.Keypair.generate();
  1392. await program.rpc.initializeNoRentExempt({
  1393. accounts: {
  1394. data: data.publicKey,
  1395. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1396. },
  1397. signers: [data],
  1398. instructions: [
  1399. SystemProgram.createAccount({
  1400. programId: program.programId,
  1401. space: 8 + 16 + 16,
  1402. lamports:
  1403. await program.provider.connection.getMinimumBalanceForRentExemption(
  1404. 39
  1405. ),
  1406. fromPubkey: anchor.getProvider().wallet.publicKey,
  1407. newAccountPubkey: data.publicKey,
  1408. }),
  1409. ],
  1410. });
  1411. await program.rpc.testNoRentExempt({
  1412. accounts: {
  1413. data: data.publicKey,
  1414. },
  1415. });
  1416. });
  1417. it("allows rent exemption to be skipped", async () => {
  1418. const data = anchor.web3.Keypair.generate();
  1419. await program.rpc.initializeSkipRentExempt({
  1420. accounts: {
  1421. data: data.publicKey,
  1422. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1423. },
  1424. signers: [data],
  1425. instructions: [
  1426. SystemProgram.createAccount({
  1427. programId: program.programId,
  1428. space: 8 + 16 + 16,
  1429. lamports:
  1430. await program.provider.connection.getMinimumBalanceForRentExemption(
  1431. 39
  1432. ),
  1433. fromPubkey: anchor.getProvider().wallet.publicKey,
  1434. newAccountPubkey: data.publicKey,
  1435. }),
  1436. ],
  1437. });
  1438. });
  1439. it("can use rent_exempt to enforce rent exemption", async () => {
  1440. const data = anchor.web3.Keypair.generate();
  1441. await program.rpc.initializeSkipRentExempt({
  1442. accounts: {
  1443. data: data.publicKey,
  1444. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1445. },
  1446. signers: [data],
  1447. instructions: [
  1448. SystemProgram.createAccount({
  1449. programId: program.programId,
  1450. space: 8 + 16 + 16,
  1451. lamports:
  1452. await program.provider.connection.getMinimumBalanceForRentExemption(
  1453. 39
  1454. ),
  1455. fromPubkey: anchor.getProvider().wallet.publicKey,
  1456. newAccountPubkey: data.publicKey,
  1457. }),
  1458. ],
  1459. });
  1460. try {
  1461. await program.rpc.testEnforceRentExempt({
  1462. accounts: {
  1463. data: data.publicKey,
  1464. },
  1465. });
  1466. assert.ok(false);
  1467. } catch (err) {
  1468. assert.equal(2005, err.code);
  1469. assert.equal("A rent exempt constraint was violated", err.msg);
  1470. }
  1471. });
  1472. describe("Can validate PDAs derived from other program ids", () => {
  1473. it("With bumps using create_program_address", async () => {
  1474. const [firstPDA, firstBump] =
  1475. await anchor.web3.PublicKey.findProgramAddress(
  1476. [anchor.utils.bytes.utf8.encode("seed")],
  1477. ASSOCIATED_TOKEN_PROGRAM_ID
  1478. );
  1479. const [secondPDA, secondBump] =
  1480. await anchor.web3.PublicKey.findProgramAddress(
  1481. [anchor.utils.bytes.utf8.encode("seed")],
  1482. program.programId
  1483. );
  1484. // correct bump but wrong address
  1485. const wrongAddress = anchor.web3.Keypair.generate().publicKey;
  1486. try {
  1487. await program.rpc.testProgramIdConstraint(firstBump, secondBump, {
  1488. accounts: {
  1489. first: wrongAddress,
  1490. second: secondPDA,
  1491. },
  1492. });
  1493. assert.ok(false);
  1494. } catch (err) {
  1495. assert.equal(err.code, 2006);
  1496. }
  1497. // matching bump seed for wrong address but derived from wrong program
  1498. try {
  1499. await program.rpc.testProgramIdConstraint(secondBump, secondBump, {
  1500. accounts: {
  1501. first: secondPDA,
  1502. second: secondPDA,
  1503. },
  1504. });
  1505. assert.ok(false);
  1506. } catch (err) {
  1507. assert.equal(err.code, 2006);
  1508. }
  1509. // correct inputs should lead to successful tx
  1510. await program.rpc.testProgramIdConstraint(firstBump, secondBump, {
  1511. accounts: {
  1512. first: firstPDA,
  1513. second: secondPDA,
  1514. },
  1515. });
  1516. });
  1517. it("With bumps using find_program_address", async () => {
  1518. const firstPDA = (
  1519. await anchor.web3.PublicKey.findProgramAddress(
  1520. [anchor.utils.bytes.utf8.encode("seed")],
  1521. ASSOCIATED_TOKEN_PROGRAM_ID
  1522. )
  1523. )[0];
  1524. const secondPDA = (
  1525. await anchor.web3.PublicKey.findProgramAddress(
  1526. [anchor.utils.bytes.utf8.encode("seed")],
  1527. program.programId
  1528. )
  1529. )[0];
  1530. // random wrong address
  1531. const wrongAddress = anchor.web3.Keypair.generate().publicKey;
  1532. try {
  1533. await program.rpc.testProgramIdConstraintFindPda({
  1534. accounts: {
  1535. first: wrongAddress,
  1536. second: secondPDA,
  1537. },
  1538. });
  1539. assert.ok(false);
  1540. } catch (err) {
  1541. assert.equal(err.code, 2006);
  1542. }
  1543. // same seeds but derived from wrong program
  1544. try {
  1545. await program.rpc.testProgramIdConstraintFindPda({
  1546. accounts: {
  1547. first: secondPDA,
  1548. second: secondPDA,
  1549. },
  1550. });
  1551. assert.ok(false);
  1552. } catch (err) {
  1553. assert.equal(err.code, 2006);
  1554. }
  1555. // correct inputs should lead to successful tx
  1556. await program.rpc.testProgramIdConstraintFindPda({
  1557. accounts: {
  1558. first: firstPDA,
  1559. second: secondPDA,
  1560. },
  1561. });
  1562. });
  1563. });
  1564. });