misc.ts 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. import * as anchor from "@project-serum/anchor";
  2. import {
  3. Program,
  4. BN,
  5. IdlAccounts,
  6. AnchorError,
  7. Wallet,
  8. IdlTypes,
  9. IdlEvents,
  10. } from "@project-serum/anchor";
  11. import {
  12. PublicKey,
  13. Keypair,
  14. SystemProgram,
  15. SYSVAR_RENT_PUBKEY,
  16. Message,
  17. VersionedTransaction,
  18. } from "@solana/web3.js";
  19. import {
  20. TOKEN_PROGRAM_ID,
  21. Token,
  22. ASSOCIATED_TOKEN_PROGRAM_ID,
  23. } from "@solana/spl-token";
  24. import { Misc } from "../../target/types/misc";
  25. import { Misc2 } from "../../target/types/misc2";
  26. const utf8 = anchor.utils.bytes.utf8;
  27. const { assert, expect } = require("chai");
  28. const nativeAssert = require("assert");
  29. const miscIdl = require("../../target/idl/misc.json");
  30. describe("misc", () => {
  31. // Configure the client to use the local cluster.
  32. const provider = anchor.AnchorProvider.env();
  33. const wallet = provider.wallet as Wallet;
  34. anchor.setProvider(provider);
  35. const program = anchor.workspace.Misc as Program<Misc>;
  36. const misc2Program = anchor.workspace.Misc2 as Program<Misc2>;
  37. it("Can allocate extra space for a state constructor", async () => {
  38. // @ts-expect-error
  39. const tx = await program.state.rpc.new();
  40. const addr = await program.state.address();
  41. const state = await program.state.fetch();
  42. const accountInfo = await program.provider.connection.getAccountInfo(addr);
  43. assert.isTrue(state.v.equals(Buffer.from([])));
  44. assert.lengthOf(accountInfo.data, 99);
  45. });
  46. it("Can use remaining accounts for a state instruction", async () => {
  47. await program.state.rpc.remainingAccounts({
  48. remainingAccounts: [
  49. { pubkey: misc2Program.programId, isWritable: false, isSigner: false },
  50. ],
  51. });
  52. });
  53. const data = anchor.web3.Keypair.generate();
  54. it("Can use u128 and i128", async () => {
  55. const tx = await program.rpc.initialize(
  56. new anchor.BN(1234),
  57. new anchor.BN(22),
  58. {
  59. accounts: {
  60. data: data.publicKey,
  61. },
  62. signers: [data],
  63. instructions: [await program.account.data.createInstruction(data)],
  64. }
  65. );
  66. const dataAccount = await program.account.data.fetch(data.publicKey);
  67. assert.isTrue(dataAccount.udata.eq(new anchor.BN(1234)));
  68. assert.isTrue(dataAccount.idata.eq(new anchor.BN(22)));
  69. });
  70. it("Can use u16", async () => {
  71. const data = anchor.web3.Keypair.generate();
  72. const tx = await program.rpc.testU16(99, {
  73. accounts: {
  74. myAccount: data.publicKey,
  75. },
  76. signers: [data],
  77. instructions: [await program.account.dataU16.createInstruction(data)],
  78. });
  79. const dataAccount = await program.account.dataU16.fetch(data.publicKey);
  80. assert.strictEqual(dataAccount.data, 99);
  81. });
  82. it("Can embed programs into genesis from the Anchor.toml", async () => {
  83. const pid = new anchor.web3.PublicKey(
  84. "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
  85. );
  86. let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
  87. assert.isTrue(accInfo.executable);
  88. });
  89. it("Can use the owner constraint", async () => {
  90. await program.rpc.testOwner({
  91. accounts: {
  92. data: data.publicKey,
  93. misc: program.programId,
  94. },
  95. });
  96. await nativeAssert.rejects(
  97. async () => {
  98. await program.rpc.testOwner({
  99. accounts: {
  100. data: provider.wallet.publicKey,
  101. misc: program.programId,
  102. },
  103. });
  104. },
  105. (err) => {
  106. return true;
  107. }
  108. );
  109. });
  110. it("Can use the executable attribute", async () => {
  111. await program.rpc.testExecutable({
  112. accounts: {
  113. program: program.programId,
  114. },
  115. });
  116. await nativeAssert.rejects(
  117. async () => {
  118. await program.rpc.testExecutable({
  119. accounts: {
  120. program: provider.wallet.publicKey,
  121. },
  122. });
  123. },
  124. (err) => {
  125. return true;
  126. }
  127. );
  128. });
  129. it("Can CPI to state instructions", async () => {
  130. const oldData = new anchor.BN(0);
  131. await misc2Program.state.rpc.new({
  132. accounts: {
  133. authority: provider.wallet.publicKey,
  134. },
  135. });
  136. let stateAccount = await misc2Program.state.fetch();
  137. assert.isTrue(stateAccount.data.eq(oldData));
  138. assert.isTrue(stateAccount.auth.equals(provider.wallet.publicKey));
  139. const newData = new anchor.BN(2134);
  140. await program.rpc.testStateCpi(newData, {
  141. accounts: {
  142. authority: provider.wallet.publicKey,
  143. cpiState: await misc2Program.state.address(),
  144. misc2Program: misc2Program.programId,
  145. },
  146. });
  147. stateAccount = await misc2Program.state.fetch();
  148. assert.isTrue(stateAccount.data.eq(newData));
  149. assert.isTrue(stateAccount.auth.equals(provider.wallet.publicKey));
  150. });
  151. it("Can retrieve events when simulating a transaction", async () => {
  152. const resp = await program.methods.testSimulate(44).simulate();
  153. const expectedRaw = [
  154. "Program 3TEqcc8xhrhdspwbvoamUJe2borm4Nr72JxL66k6rgrh invoke [1]",
  155. "Program log: Instruction: TestSimulate",
  156. "Program data: NgyCA9omwbMsAAAA",
  157. "Program data: fPhuIELK/k7SBAAA",
  158. "Program data: jvbowsvlmkcJAAAA",
  159. "Program data: zxM5neEnS1kBAgMEBQYHCAkK",
  160. "Program data: g06Ei2GL1gIBAgMEBQYHCAkKCw==",
  161. ];
  162. assert.deepStrictEqual(expectedRaw, resp.raw.slice(0, -2));
  163. assert.strictEqual(resp.events[0].name, "E1");
  164. assert.strictEqual(resp.events[0].data.data, 44);
  165. assert.strictEqual(resp.events[1].name, "E2");
  166. assert.strictEqual(resp.events[1].data.data, 1234);
  167. assert.strictEqual(resp.events[2].name, "E3");
  168. assert.strictEqual(resp.events[2].data.data, 9);
  169. assert.strictEqual(resp.events[3].name, "E5");
  170. assert.deepStrictEqual(
  171. resp.events[3].data.data,
  172. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  173. );
  174. assert.strictEqual(resp.events[4].name, "E6");
  175. assert.deepStrictEqual(
  176. resp.events[4].data.data,
  177. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  178. );
  179. });
  180. it("Can use enum in idl", async () => {
  181. const resp1 = await program.methods.testInputEnum({ first: {} }).simulate();
  182. const event1 = resp1.events[0].data as IdlEvents<Misc>["E7"];
  183. assert.deepEqual(event1.data.first, {});
  184. const resp2 = await program.methods
  185. .testInputEnum({ second: { x: new BN(1), y: new BN(2) } })
  186. .simulate();
  187. const event2 = resp2.events[0].data as IdlEvents<Misc>["E7"];
  188. assert.isTrue(new BN(1).eq(event2.data.second.x));
  189. assert.isTrue(new BN(2).eq(event2.data.second.y));
  190. const resp3 = await program.methods
  191. .testInputEnum({
  192. tupleStructTest: [
  193. { data1: 1, data2: 11, data3: 111, data4: new BN(1111) },
  194. ],
  195. })
  196. .simulate();
  197. const event3 = resp3.events[0].data as IdlEvents<Misc>["E7"];
  198. assert.strictEqual(event3.data.tupleStructTest[0].data1, 1);
  199. assert.strictEqual(event3.data.tupleStructTest[0].data2, 11);
  200. assert.strictEqual(event3.data.tupleStructTest[0].data3, 111);
  201. assert.isTrue(event3.data.tupleStructTest[0].data4.eq(new BN(1111)));
  202. const resp4 = await program.methods
  203. .testInputEnum({ tupleTest: [1, 2, 3, 4] })
  204. .simulate();
  205. const event4 = resp4.events[0].data as IdlEvents<Misc>["E7"];
  206. assert.strictEqual(event4.data.tupleTest[0], 1);
  207. assert.strictEqual(event4.data.tupleTest[1], 2);
  208. assert.strictEqual(event4.data.tupleTest[2], 3);
  209. assert.strictEqual(event4.data.tupleTest[3], 4);
  210. });
  211. let dataI8;
  212. it("Can use i8 in the idl", async () => {
  213. dataI8 = anchor.web3.Keypair.generate();
  214. await program.rpc.testI8(-3, {
  215. accounts: {
  216. data: dataI8.publicKey,
  217. },
  218. instructions: [await program.account.dataI8.createInstruction(dataI8)],
  219. signers: [dataI8],
  220. });
  221. const dataAccount = await program.account.dataI8.fetch(dataI8.publicKey);
  222. assert.strictEqual(dataAccount.data, -3);
  223. });
  224. let dataPubkey;
  225. it("Can use i16 in the idl", async () => {
  226. const data = anchor.web3.Keypair.generate();
  227. await program.rpc.testI16(-2048, {
  228. accounts: {
  229. data: data.publicKey,
  230. },
  231. instructions: [await program.account.dataI16.createInstruction(data)],
  232. signers: [data],
  233. });
  234. const dataAccount = await program.account.dataI16.fetch(data.publicKey);
  235. assert.strictEqual(dataAccount.data, -2048);
  236. dataPubkey = data.publicKey;
  237. });
  238. it("Can use base58 strings to fetch an account", async () => {
  239. const dataAccount = await program.account.dataI16.fetch(
  240. dataPubkey.toString()
  241. );
  242. assert.strictEqual(dataAccount.data, -2048);
  243. });
  244. it("Should fail to close an account when sending lamports to itself", async () => {
  245. try {
  246. await program.rpc.testClose({
  247. accounts: {
  248. data: data.publicKey,
  249. solDest: data.publicKey,
  250. },
  251. });
  252. expect(false).to.be.true;
  253. } catch (err) {
  254. const errMsg = "A close constraint was violated";
  255. assert.strictEqual(err.error.errorMessage, errMsg);
  256. assert.strictEqual(err.error.errorCode.number, 2011);
  257. }
  258. });
  259. it("Can close an account", async () => {
  260. const openAccount = await program.provider.connection.getAccountInfo(
  261. data.publicKey
  262. );
  263. assert.isNotNull(openAccount);
  264. let beforeBalance = (
  265. await program.provider.connection.getAccountInfo(
  266. provider.wallet.publicKey
  267. )
  268. ).lamports;
  269. await program.rpc.testClose({
  270. accounts: {
  271. data: data.publicKey,
  272. solDest: provider.wallet.publicKey,
  273. },
  274. });
  275. let afterBalance = (
  276. await program.provider.connection.getAccountInfo(
  277. provider.wallet.publicKey
  278. )
  279. ).lamports;
  280. // Retrieved rent exemption sol.
  281. expect(afterBalance > beforeBalance).to.be.true;
  282. const closedAccount = await program.provider.connection.getAccountInfo(
  283. data.publicKey
  284. );
  285. assert.isNull(closedAccount);
  286. });
  287. it("Can use instruction data in accounts constraints", async () => {
  288. // b"my-seed"
  289. const seed = Buffer.from([109, 121, 45, 115, 101, 101, 100]);
  290. const [myPda, nonce] = await PublicKey.findProgramAddress(
  291. [seed, anchor.web3.SYSVAR_RENT_PUBKEY.toBuffer()],
  292. program.programId
  293. );
  294. await program.rpc.testInstructionConstraint(nonce, {
  295. accounts: {
  296. myPda,
  297. myAccount: anchor.web3.SYSVAR_RENT_PUBKEY,
  298. },
  299. });
  300. });
  301. it("Can create a PDA account with instruction data", async () => {
  302. const seed = Buffer.from([1, 2, 3, 4]);
  303. const domain = "my-domain";
  304. const foo = anchor.web3.SYSVAR_RENT_PUBKEY;
  305. const [myPda, nonce] = await PublicKey.findProgramAddress(
  306. [
  307. Buffer.from(anchor.utils.bytes.utf8.encode("my-seed")),
  308. Buffer.from(anchor.utils.bytes.utf8.encode(domain)),
  309. foo.toBuffer(),
  310. seed,
  311. ],
  312. program.programId
  313. );
  314. await program.rpc.testPdaInit(domain, seed, nonce, {
  315. accounts: {
  316. myPda,
  317. myPayer: provider.wallet.publicKey,
  318. foo,
  319. systemProgram: anchor.web3.SystemProgram.programId,
  320. },
  321. });
  322. const myPdaAccount = await program.account.dataU16.fetch(myPda);
  323. assert.strictEqual(myPdaAccount.data, 6);
  324. });
  325. it("Can create a zero copy PDA account", async () => {
  326. const [myPda, nonce] = await PublicKey.findProgramAddress(
  327. [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
  328. program.programId
  329. );
  330. await program.rpc.testPdaInitZeroCopy({
  331. accounts: {
  332. myPda,
  333. myPayer: provider.wallet.publicKey,
  334. systemProgram: anchor.web3.SystemProgram.programId,
  335. },
  336. });
  337. const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
  338. assert.strictEqual(myPdaAccount.data, 9);
  339. assert.strictEqual(myPdaAccount.bump, nonce);
  340. });
  341. it("Can write to a zero copy PDA account", async () => {
  342. const [myPda, bump] = await PublicKey.findProgramAddress(
  343. [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
  344. program.programId
  345. );
  346. await program.rpc.testPdaMutZeroCopy({
  347. accounts: {
  348. myPda,
  349. myPayer: provider.wallet.publicKey,
  350. },
  351. });
  352. const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
  353. assert.strictEqual(myPdaAccount.data, 1234);
  354. assert.strictEqual(myPdaAccount.bump, bump);
  355. });
  356. it("Can create a token account from seeds pda", async () => {
  357. const [mint, mint_bump] = await PublicKey.findProgramAddress(
  358. [Buffer.from(anchor.utils.bytes.utf8.encode("my-mint-seed"))],
  359. program.programId
  360. );
  361. const [myPda, token_bump] = await PublicKey.findProgramAddress(
  362. [Buffer.from(anchor.utils.bytes.utf8.encode("my-token-seed"))],
  363. program.programId
  364. );
  365. await program.rpc.testTokenSeedsInit({
  366. accounts: {
  367. myPda,
  368. mint,
  369. authority: provider.wallet.publicKey,
  370. systemProgram: anchor.web3.SystemProgram.programId,
  371. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  372. tokenProgram: TOKEN_PROGRAM_ID,
  373. },
  374. });
  375. const mintAccount = new Token(
  376. program.provider.connection,
  377. mint,
  378. TOKEN_PROGRAM_ID,
  379. wallet.payer
  380. );
  381. const account = await mintAccount.getAccountInfo(myPda);
  382. // @ts-expect-error
  383. assert.strictEqual(account.state, 1);
  384. assert.strictEqual(account.amount.toNumber(), 0);
  385. assert.isTrue(account.isInitialized);
  386. assert.isTrue(account.owner.equals(provider.wallet.publicKey));
  387. assert.isTrue(account.mint.equals(mint));
  388. });
  389. it("Can execute a fallback function", async () => {
  390. await nativeAssert.rejects(
  391. async () => {
  392. await anchor.utils.rpc.invoke(program.programId);
  393. },
  394. (err) => {
  395. assert.isTrue(err.toString().includes("custom program error: 0x4d2"));
  396. return true;
  397. }
  398. );
  399. });
  400. it("Can init a random account", async () => {
  401. const data = anchor.web3.Keypair.generate();
  402. await program.rpc.testInit({
  403. accounts: {
  404. data: data.publicKey,
  405. payer: provider.wallet.publicKey,
  406. systemProgram: anchor.web3.SystemProgram.programId,
  407. },
  408. signers: [data],
  409. });
  410. const account = await program.account.dataI8.fetch(data.publicKey);
  411. assert.strictEqual(account.data, 3);
  412. });
  413. it("Can init a random account prefunded", async () => {
  414. const data = anchor.web3.Keypair.generate();
  415. await program.rpc.testInit({
  416. accounts: {
  417. data: data.publicKey,
  418. payer: provider.wallet.publicKey,
  419. systemProgram: anchor.web3.SystemProgram.programId,
  420. },
  421. signers: [data],
  422. instructions: [
  423. anchor.web3.SystemProgram.transfer({
  424. fromPubkey: provider.wallet.publicKey,
  425. toPubkey: data.publicKey,
  426. lamports: 4039280,
  427. }),
  428. ],
  429. });
  430. const account = await program.account.dataI8.fetch(data.publicKey);
  431. assert.strictEqual(account.data, 3);
  432. });
  433. it("Can init a random zero copy account", async () => {
  434. const data = anchor.web3.Keypair.generate();
  435. await program.rpc.testInitZeroCopy({
  436. accounts: {
  437. data: data.publicKey,
  438. payer: provider.wallet.publicKey,
  439. systemProgram: anchor.web3.SystemProgram.programId,
  440. },
  441. signers: [data],
  442. });
  443. const account = await program.account.dataZeroCopy.fetch(data.publicKey);
  444. assert.strictEqual(account.data, 10);
  445. assert.strictEqual(account.bump, 2);
  446. });
  447. let mint = undefined;
  448. it("Can create a random mint account", async () => {
  449. mint = anchor.web3.Keypair.generate();
  450. await program.rpc.testInitMint({
  451. accounts: {
  452. mint: mint.publicKey,
  453. payer: provider.wallet.publicKey,
  454. systemProgram: anchor.web3.SystemProgram.programId,
  455. tokenProgram: TOKEN_PROGRAM_ID,
  456. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  457. },
  458. signers: [mint],
  459. });
  460. const client = new Token(
  461. program.provider.connection,
  462. mint.publicKey,
  463. TOKEN_PROGRAM_ID,
  464. wallet.payer
  465. );
  466. const mintAccount = await client.getMintInfo();
  467. assert.strictEqual(mintAccount.decimals, 6);
  468. assert.isTrue(mintAccount.mintAuthority.equals(provider.wallet.publicKey));
  469. assert.isTrue(
  470. mintAccount.freezeAuthority.equals(provider.wallet.publicKey)
  471. );
  472. });
  473. it("Can create a random mint account prefunded", async () => {
  474. mint = anchor.web3.Keypair.generate();
  475. await program.rpc.testInitMint({
  476. accounts: {
  477. mint: mint.publicKey,
  478. payer: provider.wallet.publicKey,
  479. systemProgram: anchor.web3.SystemProgram.programId,
  480. tokenProgram: TOKEN_PROGRAM_ID,
  481. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  482. },
  483. signers: [mint],
  484. instructions: [
  485. anchor.web3.SystemProgram.transfer({
  486. fromPubkey: provider.wallet.publicKey,
  487. toPubkey: mint.publicKey,
  488. lamports: 4039280,
  489. }),
  490. ],
  491. });
  492. const client = new Token(
  493. program.provider.connection,
  494. mint.publicKey,
  495. TOKEN_PROGRAM_ID,
  496. wallet.payer
  497. );
  498. const mintAccount = await client.getMintInfo();
  499. assert.strictEqual(mintAccount.decimals, 6);
  500. assert.isTrue(mintAccount.mintAuthority.equals(provider.wallet.publicKey));
  501. });
  502. it("Can create a random token account", async () => {
  503. const token = anchor.web3.Keypair.generate();
  504. await program.rpc.testInitToken({
  505. accounts: {
  506. token: token.publicKey,
  507. mint: mint.publicKey,
  508. payer: provider.wallet.publicKey,
  509. systemProgram: anchor.web3.SystemProgram.programId,
  510. tokenProgram: TOKEN_PROGRAM_ID,
  511. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  512. },
  513. signers: [token],
  514. });
  515. const client = new Token(
  516. program.provider.connection,
  517. mint.publicKey,
  518. TOKEN_PROGRAM_ID,
  519. wallet.payer
  520. );
  521. const account = await client.getAccountInfo(token.publicKey);
  522. // @ts-expect-error
  523. assert.strictEqual(account.state, 1);
  524. assert.strictEqual(account.amount.toNumber(), 0);
  525. assert.isTrue(account.isInitialized);
  526. assert.isTrue(account.owner.equals(provider.wallet.publicKey));
  527. assert.isTrue(account.mint.equals(mint.publicKey));
  528. });
  529. it("Can create a random token with prefunding", async () => {
  530. const token = anchor.web3.Keypair.generate();
  531. await program.rpc.testInitToken({
  532. accounts: {
  533. token: token.publicKey,
  534. mint: mint.publicKey,
  535. payer: provider.wallet.publicKey,
  536. systemProgram: anchor.web3.SystemProgram.programId,
  537. tokenProgram: TOKEN_PROGRAM_ID,
  538. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  539. },
  540. signers: [token],
  541. instructions: [
  542. anchor.web3.SystemProgram.transfer({
  543. fromPubkey: provider.wallet.publicKey,
  544. toPubkey: token.publicKey,
  545. lamports: 4039280,
  546. }),
  547. ],
  548. });
  549. const client = new Token(
  550. program.provider.connection,
  551. mint.publicKey,
  552. TOKEN_PROGRAM_ID,
  553. wallet.payer
  554. );
  555. const account = await client.getAccountInfo(token.publicKey);
  556. // @ts-expect-error
  557. assert.strictEqual(account.state, 1);
  558. assert.strictEqual(account.amount.toNumber(), 0);
  559. assert.isTrue(account.isInitialized);
  560. assert.isTrue(account.owner.equals(provider.wallet.publicKey));
  561. assert.isTrue(account.mint.equals(mint.publicKey));
  562. });
  563. it("Can create a random token with prefunding under the rent exemption", async () => {
  564. const token = anchor.web3.Keypair.generate();
  565. await program.rpc.testInitToken({
  566. accounts: {
  567. token: token.publicKey,
  568. mint: mint.publicKey,
  569. payer: provider.wallet.publicKey,
  570. systemProgram: anchor.web3.SystemProgram.programId,
  571. tokenProgram: TOKEN_PROGRAM_ID,
  572. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  573. },
  574. signers: [token],
  575. instructions: [
  576. anchor.web3.SystemProgram.transfer({
  577. fromPubkey: provider.wallet.publicKey,
  578. toPubkey: token.publicKey,
  579. lamports: 1,
  580. }),
  581. ],
  582. });
  583. const client = new Token(
  584. program.provider.connection,
  585. mint.publicKey,
  586. TOKEN_PROGRAM_ID,
  587. wallet.payer
  588. );
  589. const account = await client.getAccountInfo(token.publicKey);
  590. // @ts-expect-error
  591. assert.strictEqual(account.state, 1);
  592. assert.strictEqual(account.amount.toNumber(), 0);
  593. assert.isTrue(account.isInitialized);
  594. assert.isTrue(account.owner.equals(provider.wallet.publicKey));
  595. assert.isTrue(account.mint.equals(mint.publicKey));
  596. });
  597. it("Can initialize multiple accounts via a composite payer", async () => {
  598. const data1 = anchor.web3.Keypair.generate();
  599. const data2 = anchor.web3.Keypair.generate();
  600. const tx = await program.rpc.testCompositePayer({
  601. accounts: {
  602. composite: {
  603. data: data1.publicKey,
  604. payer: provider.wallet.publicKey,
  605. systemProgram: anchor.web3.SystemProgram.programId,
  606. },
  607. data: data2.publicKey,
  608. systemProgram: anchor.web3.SystemProgram.programId,
  609. },
  610. signers: [data1, data2],
  611. });
  612. const account1 = await program.account.dataI8.fetch(data1.publicKey);
  613. assert.strictEqual(account1.data, 1);
  614. const account2 = await program.account.data.fetch(data2.publicKey);
  615. assert.strictEqual(account2.udata.toNumber(), 2);
  616. assert.strictEqual(account2.idata.toNumber(), 3);
  617. });
  618. describe("associated_token constraints", () => {
  619. let associatedToken = null;
  620. // apparently cannot await here so doing it in the 'it' statements
  621. let client = Token.createMint(
  622. program.provider.connection,
  623. wallet.payer,
  624. provider.wallet.publicKey,
  625. provider.wallet.publicKey,
  626. 9,
  627. TOKEN_PROGRAM_ID
  628. );
  629. it("Can create an associated token account", async () => {
  630. const localClient = await client;
  631. associatedToken = await Token.getAssociatedTokenAddress(
  632. ASSOCIATED_TOKEN_PROGRAM_ID,
  633. TOKEN_PROGRAM_ID,
  634. localClient.publicKey,
  635. provider.wallet.publicKey
  636. );
  637. await program.rpc.testInitAssociatedToken({
  638. accounts: {
  639. token: associatedToken,
  640. mint: localClient.publicKey,
  641. payer: provider.wallet.publicKey,
  642. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  643. systemProgram: anchor.web3.SystemProgram.programId,
  644. tokenProgram: TOKEN_PROGRAM_ID,
  645. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  646. },
  647. });
  648. const account = await localClient.getAccountInfo(associatedToken);
  649. // @ts-expect-error
  650. assert.strictEqual(account.state, 1);
  651. assert.strictEqual(account.amount.toNumber(), 0);
  652. assert.isTrue(account.isInitialized);
  653. assert.isTrue(account.owner.equals(provider.wallet.publicKey));
  654. assert.isTrue(account.mint.equals(localClient.publicKey));
  655. });
  656. it("Can validate associated_token constraints", async () => {
  657. const localClient = await client;
  658. await program.rpc.testValidateAssociatedToken({
  659. accounts: {
  660. token: associatedToken,
  661. mint: localClient.publicKey,
  662. wallet: provider.wallet.publicKey,
  663. },
  664. });
  665. let otherMint = await Token.createMint(
  666. program.provider.connection,
  667. wallet.payer,
  668. provider.wallet.publicKey,
  669. provider.wallet.publicKey,
  670. 9,
  671. TOKEN_PROGRAM_ID
  672. );
  673. await nativeAssert.rejects(
  674. async () => {
  675. await program.rpc.testValidateAssociatedToken({
  676. accounts: {
  677. token: associatedToken,
  678. mint: otherMint.publicKey,
  679. wallet: provider.wallet.publicKey,
  680. },
  681. });
  682. },
  683. (err) => {
  684. assert.strictEqual(err.error.errorCode.number, 2009);
  685. return true;
  686. }
  687. );
  688. });
  689. it("associated_token constraints check do not allow authority change", async () => {
  690. const localClient = await client;
  691. await program.rpc.testValidateAssociatedToken({
  692. accounts: {
  693. token: associatedToken,
  694. mint: localClient.publicKey,
  695. wallet: provider.wallet.publicKey,
  696. },
  697. });
  698. await localClient.setAuthority(
  699. associatedToken,
  700. anchor.web3.Keypair.generate().publicKey,
  701. "AccountOwner",
  702. wallet.payer,
  703. []
  704. );
  705. await nativeAssert.rejects(
  706. async () => {
  707. await program.rpc.testValidateAssociatedToken({
  708. accounts: {
  709. token: associatedToken,
  710. mint: localClient.publicKey,
  711. wallet: provider.wallet.publicKey,
  712. },
  713. });
  714. },
  715. (err) => {
  716. assert.strictEqual(err.error.errorCode.number, 2015);
  717. return true;
  718. }
  719. );
  720. });
  721. });
  722. it("Can fetch all accounts of a given type", async () => {
  723. // Initialize the accounts.
  724. const data1 = anchor.web3.Keypair.generate();
  725. const data2 = anchor.web3.Keypair.generate();
  726. const data3 = anchor.web3.Keypair.generate();
  727. const data4 = anchor.web3.Keypair.generate();
  728. // Initialize filterable data.
  729. const filterable1 = anchor.web3.Keypair.generate().publicKey;
  730. const filterable2 = anchor.web3.Keypair.generate().publicKey;
  731. // Set up a secondary wallet and program.
  732. const anotherProvider = new anchor.AnchorProvider(
  733. program.provider.connection,
  734. new anchor.Wallet(anchor.web3.Keypair.generate()),
  735. { commitment: program.provider.connection.commitment }
  736. );
  737. const anotherProgram = new anchor.Program(
  738. miscIdl,
  739. program.programId,
  740. anotherProvider
  741. );
  742. // Request airdrop for secondary wallet.
  743. const signature = await program.provider.connection.requestAirdrop(
  744. anotherProvider.wallet.publicKey,
  745. anchor.web3.LAMPORTS_PER_SOL
  746. );
  747. await program.provider.connection.confirmTransaction(signature);
  748. // Create all the accounts.
  749. await Promise.all([
  750. program.rpc.testFetchAll(filterable1, {
  751. accounts: {
  752. data: data1.publicKey,
  753. authority: provider.wallet.publicKey,
  754. systemProgram: anchor.web3.SystemProgram.programId,
  755. },
  756. signers: [data1],
  757. }),
  758. program.rpc.testFetchAll(filterable1, {
  759. accounts: {
  760. data: data2.publicKey,
  761. authority: provider.wallet.publicKey,
  762. systemProgram: anchor.web3.SystemProgram.programId,
  763. },
  764. signers: [data2],
  765. }),
  766. program.rpc.testFetchAll(filterable2, {
  767. accounts: {
  768. data: data3.publicKey,
  769. authority: provider.wallet.publicKey,
  770. systemProgram: anchor.web3.SystemProgram.programId,
  771. },
  772. signers: [data3],
  773. }),
  774. anotherProgram.rpc.testFetchAll(filterable1, {
  775. accounts: {
  776. data: data4.publicKey,
  777. authority: anotherProvider.wallet.publicKey,
  778. systemProgram: anchor.web3.SystemProgram.programId,
  779. },
  780. signers: [data4],
  781. }),
  782. ]);
  783. // Call for multiple kinds of .all.
  784. const allAccounts = await program.account.dataWithFilter.all();
  785. const allAccountsFilteredByBuffer =
  786. await program.account.dataWithFilter.all(
  787. provider.wallet.publicKey.toBuffer()
  788. );
  789. const allAccountsFilteredByProgramFilters1 =
  790. await program.account.dataWithFilter.all([
  791. {
  792. memcmp: {
  793. offset: 8,
  794. bytes: provider.wallet.publicKey.toBase58(),
  795. },
  796. },
  797. { memcmp: { offset: 40, bytes: filterable1.toBase58() } },
  798. ]);
  799. const allAccountsFilteredByProgramFilters2 =
  800. await program.account.dataWithFilter.all([
  801. {
  802. memcmp: {
  803. offset: 8,
  804. bytes: provider.wallet.publicKey.toBase58(),
  805. },
  806. },
  807. { memcmp: { offset: 40, bytes: filterable2.toBase58() } },
  808. ]);
  809. // Without filters there should be 4 accounts.
  810. assert.lengthOf(allAccounts, 4);
  811. // Filtering by main wallet there should be 3 accounts.
  812. assert.lengthOf(allAccountsFilteredByBuffer, 3);
  813. // Filtering all the main wallet accounts and matching the filterable1 value
  814. // results in a 2 accounts.
  815. assert.lengthOf(allAccountsFilteredByProgramFilters1, 2);
  816. // Filtering all the main wallet accounts and matching the filterable2 value
  817. // results in 1 account.
  818. assert.lengthOf(allAccountsFilteredByProgramFilters2, 1);
  819. });
  820. it("Can use pdas with empty seeds", async () => {
  821. const [pda, bump] = await PublicKey.findProgramAddress(
  822. [],
  823. program.programId
  824. );
  825. await program.rpc.testInitWithEmptySeeds({
  826. accounts: {
  827. pda: pda,
  828. authority: provider.wallet.publicKey,
  829. systemProgram: anchor.web3.SystemProgram.programId,
  830. },
  831. });
  832. await program.rpc.testEmptySeedsConstraint({
  833. accounts: {
  834. pda: pda,
  835. },
  836. });
  837. const [pda2] = await PublicKey.findProgramAddress(
  838. [anchor.utils.bytes.utf8.encode("non-empty")],
  839. program.programId
  840. );
  841. await nativeAssert.rejects(
  842. program.rpc.testEmptySeedsConstraint({
  843. accounts: {
  844. pda: pda2,
  845. },
  846. }),
  847. (err) => {
  848. assert.equal(err.error.errorCode.number, 2006);
  849. return true;
  850. }
  851. );
  852. });
  853. const ifNeededAcc = anchor.web3.Keypair.generate();
  854. it("Can init if needed a new account", async () => {
  855. await program.rpc.testInitIfNeeded(1, {
  856. accounts: {
  857. data: ifNeededAcc.publicKey,
  858. systemProgram: anchor.web3.SystemProgram.programId,
  859. payer: provider.wallet.publicKey,
  860. },
  861. signers: [ifNeededAcc],
  862. });
  863. const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
  864. assert.strictEqual(account.data, 1);
  865. });
  866. it("Can init if needed a previously created account", async () => {
  867. await program.rpc.testInitIfNeeded(3, {
  868. accounts: {
  869. data: ifNeededAcc.publicKey,
  870. systemProgram: anchor.web3.SystemProgram.programId,
  871. payer: provider.wallet.publicKey,
  872. },
  873. signers: [ifNeededAcc],
  874. });
  875. const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
  876. assert.strictEqual(account.data, 3);
  877. });
  878. it("Can use const for array size", async () => {
  879. const data = anchor.web3.Keypair.generate();
  880. const tx = await program.rpc.testConstArraySize(99, {
  881. accounts: {
  882. data: data.publicKey,
  883. },
  884. signers: [data],
  885. instructions: [
  886. await program.account.dataConstArraySize.createInstruction(data),
  887. ],
  888. });
  889. const dataAccount = await program.account.dataConstArraySize.fetch(
  890. data.publicKey
  891. );
  892. assert.deepStrictEqual(dataAccount.data, [99, ...new Array(9).fill(0)]);
  893. });
  894. it("Can use const for instruction data size", async () => {
  895. const data = anchor.web3.Keypair.generate();
  896. const dataArray = [99, ...new Array(9).fill(0)];
  897. const tx = await program.rpc.testConstIxDataSize(dataArray, {
  898. accounts: {
  899. data: data.publicKey,
  900. },
  901. signers: [data],
  902. instructions: [
  903. await program.account.dataConstArraySize.createInstruction(data),
  904. ],
  905. });
  906. const dataAccount = await program.account.dataConstArraySize.fetch(
  907. data.publicKey
  908. );
  909. assert.deepStrictEqual(dataAccount.data, dataArray);
  910. });
  911. it("Should include BASE const in IDL", async () => {
  912. assert.isDefined(
  913. miscIdl.constants.find(
  914. (c) => c.name === "BASE" && c.type === "u128" && c.value === "1_000_000"
  915. )
  916. );
  917. });
  918. it("Should include DECIMALS const in IDL", async () => {
  919. assert.isDefined(
  920. miscIdl.constants.find(
  921. (c) => c.name === "DECIMALS" && c.type === "u8" && c.value === "6"
  922. )
  923. );
  924. });
  925. it("Should not include NO_IDL const in IDL", async () => {
  926. assert.isUndefined(miscIdl.constants.find((c) => c.name === "NO_IDL"));
  927. });
  928. it("init_if_needed throws if account exists but is not owned by the expected program", async () => {
  929. const newAcc = await anchor.web3.PublicKey.findProgramAddress(
  930. [utf8.encode("hello")],
  931. program.programId
  932. );
  933. await program.rpc.testInitIfNeededChecksOwner({
  934. accounts: {
  935. data: newAcc[0],
  936. systemProgram: anchor.web3.SystemProgram.programId,
  937. payer: provider.wallet.publicKey,
  938. owner: program.programId,
  939. },
  940. });
  941. try {
  942. await program.rpc.testInitIfNeededChecksOwner({
  943. accounts: {
  944. data: newAcc[0],
  945. systemProgram: anchor.web3.SystemProgram.programId,
  946. payer: provider.wallet.publicKey,
  947. owner: anchor.web3.Keypair.generate().publicKey,
  948. },
  949. });
  950. expect(false).to.be.true;
  951. } catch (_err) {
  952. assert.isTrue(_err instanceof AnchorError);
  953. const err: AnchorError = _err;
  954. assert.strictEqual(err.error.errorCode.number, 2004);
  955. }
  956. });
  957. it("init_if_needed throws if pda account exists but does not have the expected seeds", async () => {
  958. const newAcc = await anchor.web3.PublicKey.findProgramAddress(
  959. [utf8.encode("nothello")],
  960. program.programId
  961. );
  962. await program.rpc.testInitIfNeededChecksSeeds("nothello", {
  963. accounts: {
  964. data: newAcc[0],
  965. systemProgram: anchor.web3.SystemProgram.programId,
  966. payer: provider.wallet.publicKey,
  967. },
  968. });
  969. // this will throw if it is not a proper PDA
  970. // we need this so we know that the following tx failed
  971. // not because it couldn't create this pda
  972. // but because the two pdas were different
  973. anchor.web3.PublicKey.createProgramAddress(
  974. [utf8.encode("hello")],
  975. program.programId
  976. );
  977. try {
  978. await program.rpc.testInitIfNeededChecksSeeds("hello", {
  979. accounts: {
  980. data: newAcc[0],
  981. systemProgram: anchor.web3.SystemProgram.programId,
  982. payer: provider.wallet.publicKey,
  983. },
  984. });
  985. expect(false).to.be.true;
  986. } catch (_err) {
  987. assert.isTrue(_err instanceof AnchorError);
  988. const err: AnchorError = _err;
  989. assert.strictEqual(err.error.errorCode.number, 2006);
  990. }
  991. });
  992. it("init_if_needed throws if account exists but is not the expected space", async () => {
  993. const newAcc = anchor.web3.Keypair.generate();
  994. const _irrelevantForTest = 3;
  995. await program.rpc.initWithSpace(_irrelevantForTest, {
  996. accounts: {
  997. data: newAcc.publicKey,
  998. systemProgram: anchor.web3.SystemProgram.programId,
  999. payer: provider.wallet.publicKey,
  1000. },
  1001. signers: [newAcc],
  1002. });
  1003. try {
  1004. await program.rpc.testInitIfNeeded(_irrelevantForTest, {
  1005. accounts: {
  1006. data: newAcc.publicKey,
  1007. systemProgram: anchor.web3.SystemProgram.programId,
  1008. payer: provider.wallet.publicKey,
  1009. },
  1010. signers: [newAcc],
  1011. });
  1012. expect(false).to.be.true;
  1013. } catch (_err) {
  1014. assert.isTrue(_err instanceof AnchorError);
  1015. const err: AnchorError = _err;
  1016. assert.strictEqual(err.error.errorCode.number, 2019);
  1017. }
  1018. });
  1019. it("init_if_needed throws if mint exists but has the wrong mint authority", async () => {
  1020. const mint = anchor.web3.Keypair.generate();
  1021. await program.rpc.testInitMint({
  1022. accounts: {
  1023. mint: mint.publicKey,
  1024. payer: provider.wallet.publicKey,
  1025. systemProgram: anchor.web3.SystemProgram.programId,
  1026. tokenProgram: TOKEN_PROGRAM_ID,
  1027. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1028. },
  1029. signers: [mint],
  1030. });
  1031. try {
  1032. await program.rpc.testInitMintIfNeeded(6, {
  1033. accounts: {
  1034. mint: mint.publicKey,
  1035. payer: provider.wallet.publicKey,
  1036. systemProgram: anchor.web3.SystemProgram.programId,
  1037. tokenProgram: TOKEN_PROGRAM_ID,
  1038. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1039. mintAuthority: anchor.web3.Keypair.generate().publicKey,
  1040. freezeAuthority: provider.wallet.publicKey,
  1041. },
  1042. signers: [mint],
  1043. });
  1044. expect(false).to.be.true;
  1045. } catch (_err) {
  1046. assert.isTrue(_err instanceof AnchorError);
  1047. const err: AnchorError = _err;
  1048. assert.strictEqual(err.error.errorCode.number, 2016);
  1049. }
  1050. });
  1051. it("init_if_needed throws if mint exists but has the wrong freeze authority", async () => {
  1052. const mint = anchor.web3.Keypair.generate();
  1053. await program.rpc.testInitMint({
  1054. accounts: {
  1055. mint: mint.publicKey,
  1056. payer: provider.wallet.publicKey,
  1057. systemProgram: anchor.web3.SystemProgram.programId,
  1058. tokenProgram: TOKEN_PROGRAM_ID,
  1059. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1060. },
  1061. signers: [mint],
  1062. });
  1063. try {
  1064. await program.rpc.testInitMintIfNeeded(6, {
  1065. accounts: {
  1066. mint: mint.publicKey,
  1067. payer: provider.wallet.publicKey,
  1068. systemProgram: anchor.web3.SystemProgram.programId,
  1069. tokenProgram: TOKEN_PROGRAM_ID,
  1070. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1071. mintAuthority: provider.wallet.publicKey,
  1072. freezeAuthority: anchor.web3.Keypair.generate().publicKey,
  1073. },
  1074. signers: [mint],
  1075. });
  1076. expect(false).to.be.true;
  1077. } catch (_err) {
  1078. assert.isTrue(_err instanceof AnchorError);
  1079. const err: AnchorError = _err;
  1080. assert.strictEqual(err.error.errorCode.number, 2017);
  1081. }
  1082. });
  1083. it("init_if_needed throws if mint exists but has the wrong decimals", async () => {
  1084. const mint = anchor.web3.Keypair.generate();
  1085. await program.rpc.testInitMint({
  1086. accounts: {
  1087. mint: mint.publicKey,
  1088. payer: provider.wallet.publicKey,
  1089. systemProgram: anchor.web3.SystemProgram.programId,
  1090. tokenProgram: TOKEN_PROGRAM_ID,
  1091. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1092. },
  1093. signers: [mint],
  1094. });
  1095. try {
  1096. await program.rpc.testInitMintIfNeeded(9, {
  1097. accounts: {
  1098. mint: mint.publicKey,
  1099. payer: provider.wallet.publicKey,
  1100. systemProgram: anchor.web3.SystemProgram.programId,
  1101. tokenProgram: TOKEN_PROGRAM_ID,
  1102. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1103. mintAuthority: provider.wallet.publicKey,
  1104. freezeAuthority: provider.wallet.publicKey,
  1105. },
  1106. signers: [mint],
  1107. });
  1108. expect(false).to.be.true;
  1109. } catch (_err) {
  1110. assert.isTrue(_err instanceof AnchorError);
  1111. const err: AnchorError = _err;
  1112. assert.strictEqual(err.error.errorCode.number, 2018);
  1113. }
  1114. });
  1115. it("init_if_needed throws if token exists but has the wrong owner", async () => {
  1116. const mint = anchor.web3.Keypair.generate();
  1117. await program.rpc.testInitMint({
  1118. accounts: {
  1119. mint: mint.publicKey,
  1120. payer: provider.wallet.publicKey,
  1121. systemProgram: anchor.web3.SystemProgram.programId,
  1122. tokenProgram: TOKEN_PROGRAM_ID,
  1123. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1124. },
  1125. signers: [mint],
  1126. });
  1127. const token = anchor.web3.Keypair.generate();
  1128. await program.rpc.testInitToken({
  1129. accounts: {
  1130. token: token.publicKey,
  1131. mint: mint.publicKey,
  1132. payer: provider.wallet.publicKey,
  1133. systemProgram: anchor.web3.SystemProgram.programId,
  1134. tokenProgram: TOKEN_PROGRAM_ID,
  1135. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1136. },
  1137. signers: [token],
  1138. });
  1139. try {
  1140. await program.rpc.testInitTokenIfNeeded({
  1141. accounts: {
  1142. token: token.publicKey,
  1143. mint: mint.publicKey,
  1144. payer: provider.wallet.publicKey,
  1145. systemProgram: anchor.web3.SystemProgram.programId,
  1146. tokenProgram: TOKEN_PROGRAM_ID,
  1147. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1148. authority: anchor.web3.Keypair.generate().publicKey,
  1149. },
  1150. signers: [token],
  1151. });
  1152. expect(false).to.be.true;
  1153. } catch (_err) {
  1154. assert.isTrue(_err instanceof AnchorError);
  1155. const err: AnchorError = _err;
  1156. assert.strictEqual(err.error.errorCode.number, 2015);
  1157. }
  1158. });
  1159. it("init_if_needed throws if token exists but has the wrong mint", async () => {
  1160. const mint = anchor.web3.Keypair.generate();
  1161. await program.rpc.testInitMint({
  1162. accounts: {
  1163. mint: mint.publicKey,
  1164. payer: provider.wallet.publicKey,
  1165. systemProgram: anchor.web3.SystemProgram.programId,
  1166. tokenProgram: TOKEN_PROGRAM_ID,
  1167. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1168. },
  1169. signers: [mint],
  1170. });
  1171. const mint2 = anchor.web3.Keypair.generate();
  1172. await program.rpc.testInitMint({
  1173. accounts: {
  1174. mint: mint2.publicKey,
  1175. payer: provider.wallet.publicKey,
  1176. systemProgram: anchor.web3.SystemProgram.programId,
  1177. tokenProgram: TOKEN_PROGRAM_ID,
  1178. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1179. },
  1180. signers: [mint2],
  1181. });
  1182. const token = anchor.web3.Keypair.generate();
  1183. await program.rpc.testInitToken({
  1184. accounts: {
  1185. token: token.publicKey,
  1186. mint: mint.publicKey,
  1187. payer: provider.wallet.publicKey,
  1188. systemProgram: anchor.web3.SystemProgram.programId,
  1189. tokenProgram: TOKEN_PROGRAM_ID,
  1190. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1191. },
  1192. signers: [token],
  1193. });
  1194. try {
  1195. await program.rpc.testInitTokenIfNeeded({
  1196. accounts: {
  1197. token: token.publicKey,
  1198. mint: mint2.publicKey,
  1199. payer: provider.wallet.publicKey,
  1200. systemProgram: anchor.web3.SystemProgram.programId,
  1201. tokenProgram: TOKEN_PROGRAM_ID,
  1202. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1203. authority: provider.wallet.publicKey,
  1204. },
  1205. signers: [token],
  1206. });
  1207. expect(false).to.be.true;
  1208. } catch (_err) {
  1209. assert.isTrue(_err instanceof AnchorError);
  1210. const err: AnchorError = _err;
  1211. assert.strictEqual(err.error.errorCode.number, 2014);
  1212. }
  1213. });
  1214. it("init_if_needed throws if associated token exists but has the wrong owner", async () => {
  1215. const mint = Keypair.generate();
  1216. await program.rpc.testInitMint({
  1217. accounts: {
  1218. mint: mint.publicKey,
  1219. payer: provider.wallet.publicKey,
  1220. systemProgram: SystemProgram.programId,
  1221. tokenProgram: TOKEN_PROGRAM_ID,
  1222. rent: SYSVAR_RENT_PUBKEY,
  1223. },
  1224. signers: [mint],
  1225. });
  1226. const associatedToken = await Token.getAssociatedTokenAddress(
  1227. ASSOCIATED_TOKEN_PROGRAM_ID,
  1228. TOKEN_PROGRAM_ID,
  1229. mint.publicKey,
  1230. provider.wallet.publicKey
  1231. );
  1232. await program.rpc.testInitAssociatedToken({
  1233. accounts: {
  1234. token: associatedToken,
  1235. mint: mint.publicKey,
  1236. payer: provider.wallet.publicKey,
  1237. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1238. systemProgram: anchor.web3.SystemProgram.programId,
  1239. tokenProgram: TOKEN_PROGRAM_ID,
  1240. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1241. },
  1242. });
  1243. try {
  1244. await program.rpc.testInitAssociatedTokenIfNeeded({
  1245. accounts: {
  1246. token: associatedToken,
  1247. mint: mint.publicKey,
  1248. payer: provider.wallet.publicKey,
  1249. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1250. systemProgram: anchor.web3.SystemProgram.programId,
  1251. tokenProgram: TOKEN_PROGRAM_ID,
  1252. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1253. authority: anchor.web3.Keypair.generate().publicKey,
  1254. },
  1255. });
  1256. expect(false).to.be.true;
  1257. } catch (_err) {
  1258. assert.isTrue(_err instanceof AnchorError);
  1259. const err: AnchorError = _err;
  1260. assert.strictEqual(err.error.errorCode.number, 2015);
  1261. }
  1262. });
  1263. it("init_if_needed throws if associated token exists but has the wrong mint", async () => {
  1264. const mint = anchor.web3.Keypair.generate();
  1265. await program.rpc.testInitMint({
  1266. accounts: {
  1267. mint: mint.publicKey,
  1268. payer: provider.wallet.publicKey,
  1269. systemProgram: anchor.web3.SystemProgram.programId,
  1270. tokenProgram: TOKEN_PROGRAM_ID,
  1271. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1272. },
  1273. signers: [mint],
  1274. });
  1275. const mint2 = anchor.web3.Keypair.generate();
  1276. await program.rpc.testInitMint({
  1277. accounts: {
  1278. mint: mint2.publicKey,
  1279. payer: provider.wallet.publicKey,
  1280. systemProgram: anchor.web3.SystemProgram.programId,
  1281. tokenProgram: TOKEN_PROGRAM_ID,
  1282. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1283. },
  1284. signers: [mint2],
  1285. });
  1286. const associatedToken = await Token.getAssociatedTokenAddress(
  1287. ASSOCIATED_TOKEN_PROGRAM_ID,
  1288. TOKEN_PROGRAM_ID,
  1289. mint.publicKey,
  1290. provider.wallet.publicKey
  1291. );
  1292. await program.rpc.testInitAssociatedToken({
  1293. accounts: {
  1294. token: associatedToken,
  1295. mint: mint.publicKey,
  1296. payer: provider.wallet.publicKey,
  1297. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1298. systemProgram: anchor.web3.SystemProgram.programId,
  1299. tokenProgram: TOKEN_PROGRAM_ID,
  1300. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1301. },
  1302. });
  1303. try {
  1304. await program.rpc.testInitAssociatedTokenIfNeeded({
  1305. accounts: {
  1306. token: associatedToken,
  1307. mint: mint2.publicKey,
  1308. payer: provider.wallet.publicKey,
  1309. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1310. systemProgram: anchor.web3.SystemProgram.programId,
  1311. tokenProgram: TOKEN_PROGRAM_ID,
  1312. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1313. authority: provider.wallet.publicKey,
  1314. },
  1315. });
  1316. expect(false).to.be.true;
  1317. } catch (_err) {
  1318. assert.isTrue(_err instanceof AnchorError);
  1319. const err: AnchorError = _err;
  1320. assert.strictEqual(err.error.errorCode.number, 2014);
  1321. }
  1322. });
  1323. it("init_if_needed throws if token exists with correct owner and mint but is not the ATA", async () => {
  1324. const mint = anchor.web3.Keypair.generate();
  1325. await program.rpc.testInitMint({
  1326. accounts: {
  1327. mint: mint.publicKey,
  1328. payer: provider.wallet.publicKey,
  1329. systemProgram: anchor.web3.SystemProgram.programId,
  1330. tokenProgram: TOKEN_PROGRAM_ID,
  1331. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1332. },
  1333. signers: [mint],
  1334. });
  1335. const associatedToken = await Token.getAssociatedTokenAddress(
  1336. ASSOCIATED_TOKEN_PROGRAM_ID,
  1337. TOKEN_PROGRAM_ID,
  1338. mint.publicKey,
  1339. provider.wallet.publicKey
  1340. );
  1341. await program.rpc.testInitAssociatedToken({
  1342. accounts: {
  1343. token: associatedToken,
  1344. mint: mint.publicKey,
  1345. payer: provider.wallet.publicKey,
  1346. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1347. systemProgram: anchor.web3.SystemProgram.programId,
  1348. tokenProgram: TOKEN_PROGRAM_ID,
  1349. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1350. },
  1351. });
  1352. const token = anchor.web3.Keypair.generate();
  1353. await program.rpc.testInitToken({
  1354. accounts: {
  1355. token: token.publicKey,
  1356. mint: mint.publicKey,
  1357. payer: provider.wallet.publicKey,
  1358. systemProgram: anchor.web3.SystemProgram.programId,
  1359. tokenProgram: TOKEN_PROGRAM_ID,
  1360. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1361. },
  1362. signers: [token],
  1363. });
  1364. try {
  1365. await program.rpc.testInitAssociatedTokenIfNeeded({
  1366. accounts: {
  1367. token: token.publicKey,
  1368. mint: mint.publicKey,
  1369. payer: provider.wallet.publicKey,
  1370. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1371. systemProgram: anchor.web3.SystemProgram.programId,
  1372. tokenProgram: TOKEN_PROGRAM_ID,
  1373. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1374. authority: provider.wallet.publicKey,
  1375. },
  1376. });
  1377. expect(false).to.be.true;
  1378. } catch (_err) {
  1379. assert.isTrue(_err instanceof AnchorError);
  1380. const err: AnchorError = _err;
  1381. assert.strictEqual(err.error.errorCode.number, 3014);
  1382. }
  1383. });
  1384. it("Can use multidimensional array", async () => {
  1385. const array2d = new Array(10).fill(new Array(10).fill(99));
  1386. const data = anchor.web3.Keypair.generate();
  1387. await program.rpc.testMultidimensionalArray(array2d, {
  1388. accounts: {
  1389. data: data.publicKey,
  1390. },
  1391. signers: [data],
  1392. instructions: [
  1393. await program.account.dataMultidimensionalArray.createInstruction(data),
  1394. ],
  1395. });
  1396. const dataAccount = await program.account.dataMultidimensionalArray.fetch(
  1397. data.publicKey
  1398. );
  1399. assert.deepStrictEqual(dataAccount.data, array2d);
  1400. });
  1401. it("Can use multidimensional array with const sizes", async () => {
  1402. const array2d = new Array(10).fill(new Array(11).fill(22));
  1403. const data = anchor.web3.Keypair.generate();
  1404. await program.rpc.testMultidimensionalArrayConstSizes(array2d, {
  1405. accounts: {
  1406. data: data.publicKey,
  1407. },
  1408. signers: [data],
  1409. instructions: [
  1410. await program.account.dataMultidimensionalArrayConstSizes.createInstruction(
  1411. data
  1412. ),
  1413. ],
  1414. });
  1415. const dataAccount =
  1416. await program.account.dataMultidimensionalArrayConstSizes.fetch(
  1417. data.publicKey
  1418. );
  1419. assert.deepStrictEqual(dataAccount.data, array2d);
  1420. });
  1421. describe("Can validate PDAs derived from other program ids", () => {
  1422. it("With bumps using create_program_address", async () => {
  1423. const [firstPDA, firstBump] =
  1424. await anchor.web3.PublicKey.findProgramAddress(
  1425. [anchor.utils.bytes.utf8.encode("seed")],
  1426. ASSOCIATED_TOKEN_PROGRAM_ID
  1427. );
  1428. const [secondPDA, secondBump] =
  1429. await anchor.web3.PublicKey.findProgramAddress(
  1430. [anchor.utils.bytes.utf8.encode("seed")],
  1431. program.programId
  1432. );
  1433. // correct bump but wrong address
  1434. const wrongAddress = anchor.web3.Keypair.generate().publicKey;
  1435. try {
  1436. await program.rpc.testProgramIdConstraint(firstBump, secondBump, {
  1437. accounts: {
  1438. first: wrongAddress,
  1439. second: secondPDA,
  1440. },
  1441. });
  1442. expect(false).to.be.true;
  1443. } catch (_err) {
  1444. assert.isTrue(_err instanceof AnchorError);
  1445. const err: AnchorError = _err;
  1446. assert.strictEqual(err.error.errorCode.number, 2006);
  1447. }
  1448. // matching bump seed for wrong address but derived from wrong program
  1449. try {
  1450. await program.rpc.testProgramIdConstraint(secondBump, secondBump, {
  1451. accounts: {
  1452. first: secondPDA,
  1453. second: secondPDA,
  1454. },
  1455. });
  1456. expect(false).to.be.true;
  1457. } catch (_err) {
  1458. assert.isTrue(_err instanceof AnchorError);
  1459. const err: AnchorError = _err;
  1460. assert.strictEqual(err.error.errorCode.number, 2006);
  1461. }
  1462. // correct inputs should lead to successful tx
  1463. await program.rpc.testProgramIdConstraint(firstBump, secondBump, {
  1464. accounts: {
  1465. first: firstPDA,
  1466. second: secondPDA,
  1467. },
  1468. });
  1469. });
  1470. it("With bumps using find_program_address", async () => {
  1471. const firstPDA = (
  1472. await anchor.web3.PublicKey.findProgramAddress(
  1473. [anchor.utils.bytes.utf8.encode("seed")],
  1474. ASSOCIATED_TOKEN_PROGRAM_ID
  1475. )
  1476. )[0];
  1477. const secondPDA = (
  1478. await anchor.web3.PublicKey.findProgramAddress(
  1479. [anchor.utils.bytes.utf8.encode("seed")],
  1480. program.programId
  1481. )
  1482. )[0];
  1483. // random wrong address
  1484. const wrongAddress = anchor.web3.Keypair.generate().publicKey;
  1485. try {
  1486. await program.rpc.testProgramIdConstraintFindPda({
  1487. accounts: {
  1488. first: wrongAddress,
  1489. second: secondPDA,
  1490. },
  1491. });
  1492. expect(false).to.be.true;
  1493. } catch (_err) {
  1494. assert.isTrue(_err instanceof AnchorError);
  1495. const err: AnchorError = _err;
  1496. assert.strictEqual(err.error.errorCode.number, 2006);
  1497. }
  1498. // same seeds but derived from wrong program
  1499. try {
  1500. await program.rpc.testProgramIdConstraintFindPda({
  1501. accounts: {
  1502. first: secondPDA,
  1503. second: secondPDA,
  1504. },
  1505. });
  1506. expect(false).to.be.true;
  1507. } catch (_err) {
  1508. assert.isTrue(_err instanceof AnchorError);
  1509. const err: AnchorError = _err;
  1510. assert.strictEqual(err.error.errorCode.number, 2006);
  1511. }
  1512. // correct inputs should lead to successful tx
  1513. await program.rpc.testProgramIdConstraintFindPda({
  1514. accounts: {
  1515. first: firstPDA,
  1516. second: secondPDA,
  1517. },
  1518. });
  1519. });
  1520. });
  1521. describe("Token Constraint Test", () => {
  1522. it("Token Constraint Test(no init) - Can make token::mint and token::authority", async () => {
  1523. const mint = anchor.web3.Keypair.generate();
  1524. await program.rpc.testInitMint({
  1525. accounts: {
  1526. mint: mint.publicKey,
  1527. payer: provider.wallet.publicKey,
  1528. systemProgram: anchor.web3.SystemProgram.programId,
  1529. tokenProgram: TOKEN_PROGRAM_ID,
  1530. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1531. },
  1532. signers: [mint],
  1533. });
  1534. const token = anchor.web3.Keypair.generate();
  1535. await program.rpc.testInitToken({
  1536. accounts: {
  1537. token: token.publicKey,
  1538. mint: mint.publicKey,
  1539. payer: provider.wallet.publicKey,
  1540. systemProgram: anchor.web3.SystemProgram.programId,
  1541. tokenProgram: TOKEN_PROGRAM_ID,
  1542. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1543. },
  1544. signers: [token],
  1545. });
  1546. await program.rpc.testTokenConstraint({
  1547. accounts: {
  1548. token: token.publicKey,
  1549. mint: mint.publicKey,
  1550. payer: provider.wallet.publicKey,
  1551. },
  1552. });
  1553. const mintAccount = new Token(
  1554. program.provider.connection,
  1555. mint.publicKey,
  1556. TOKEN_PROGRAM_ID,
  1557. wallet.payer
  1558. );
  1559. const account = await mintAccount.getAccountInfo(token.publicKey);
  1560. assert.isTrue(account.owner.equals(provider.wallet.publicKey));
  1561. assert.isTrue(account.mint.equals(mint.publicKey));
  1562. });
  1563. it("Token Constraint Test(no init) - Can make only token::authority", async () => {
  1564. const mint = anchor.web3.Keypair.generate();
  1565. await program.rpc.testInitMint({
  1566. accounts: {
  1567. mint: mint.publicKey,
  1568. payer: provider.wallet.publicKey,
  1569. systemProgram: anchor.web3.SystemProgram.programId,
  1570. tokenProgram: TOKEN_PROGRAM_ID,
  1571. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1572. },
  1573. signers: [mint],
  1574. });
  1575. const token = anchor.web3.Keypair.generate();
  1576. await program.rpc.testInitToken({
  1577. accounts: {
  1578. token: token.publicKey,
  1579. mint: mint.publicKey,
  1580. payer: provider.wallet.publicKey,
  1581. systemProgram: anchor.web3.SystemProgram.programId,
  1582. tokenProgram: TOKEN_PROGRAM_ID,
  1583. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1584. },
  1585. signers: [token],
  1586. });
  1587. await program.rpc.testOnlyAuthConstraint({
  1588. accounts: {
  1589. token: token.publicKey,
  1590. mint: mint.publicKey,
  1591. payer: provider.wallet.publicKey,
  1592. },
  1593. });
  1594. const mintAccount = new Token(
  1595. program.provider.connection,
  1596. mint.publicKey,
  1597. TOKEN_PROGRAM_ID,
  1598. wallet.payer
  1599. );
  1600. const account = await mintAccount.getAccountInfo(token.publicKey);
  1601. assert.isTrue(account.owner.equals(provider.wallet.publicKey));
  1602. });
  1603. it("Token Constraint Test(no init) - Can make only token::mint", async () => {
  1604. const mint = anchor.web3.Keypair.generate();
  1605. await program.rpc.testInitMint({
  1606. accounts: {
  1607. mint: mint.publicKey,
  1608. payer: provider.wallet.publicKey,
  1609. systemProgram: anchor.web3.SystemProgram.programId,
  1610. tokenProgram: TOKEN_PROGRAM_ID,
  1611. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1612. },
  1613. signers: [mint],
  1614. });
  1615. const token = anchor.web3.Keypair.generate();
  1616. await program.rpc.testInitToken({
  1617. accounts: {
  1618. token: token.publicKey,
  1619. mint: mint.publicKey,
  1620. payer: provider.wallet.publicKey,
  1621. systemProgram: anchor.web3.SystemProgram.programId,
  1622. tokenProgram: TOKEN_PROGRAM_ID,
  1623. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1624. },
  1625. signers: [token],
  1626. });
  1627. await program.rpc.testOnlyMintConstraint({
  1628. accounts: {
  1629. token: token.publicKey,
  1630. mint: mint.publicKey,
  1631. },
  1632. });
  1633. const mintAccount = new Token(
  1634. program.provider.connection,
  1635. mint.publicKey,
  1636. TOKEN_PROGRAM_ID,
  1637. wallet.payer
  1638. );
  1639. const account = await mintAccount.getAccountInfo(token.publicKey);
  1640. assert.isTrue(account.mint.equals(mint.publicKey));
  1641. });
  1642. it("Token Constraint Test(no init) - throws if token::mint mismatch", async () => {
  1643. const mint = anchor.web3.Keypair.generate();
  1644. await program.rpc.testInitMint({
  1645. accounts: {
  1646. mint: mint.publicKey,
  1647. payer: provider.wallet.publicKey,
  1648. systemProgram: anchor.web3.SystemProgram.programId,
  1649. tokenProgram: TOKEN_PROGRAM_ID,
  1650. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1651. },
  1652. signers: [mint],
  1653. });
  1654. const mint1 = anchor.web3.Keypair.generate();
  1655. await program.rpc.testInitMint({
  1656. accounts: {
  1657. mint: mint1.publicKey,
  1658. payer: provider.wallet.publicKey,
  1659. systemProgram: anchor.web3.SystemProgram.programId,
  1660. tokenProgram: TOKEN_PROGRAM_ID,
  1661. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1662. },
  1663. signers: [mint1],
  1664. });
  1665. const token = anchor.web3.Keypair.generate();
  1666. await program.rpc.testInitToken({
  1667. accounts: {
  1668. token: token.publicKey,
  1669. mint: mint.publicKey,
  1670. payer: provider.wallet.publicKey,
  1671. systemProgram: anchor.web3.SystemProgram.programId,
  1672. tokenProgram: TOKEN_PROGRAM_ID,
  1673. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1674. },
  1675. signers: [token],
  1676. });
  1677. try {
  1678. await program.rpc.testTokenConstraint({
  1679. accounts: {
  1680. token: token.publicKey,
  1681. mint: mint1.publicKey,
  1682. payer: provider.wallet.publicKey,
  1683. },
  1684. });
  1685. assert.isTrue(false);
  1686. } catch (_err) {
  1687. assert.isTrue(_err instanceof AnchorError);
  1688. const err: AnchorError = _err;
  1689. assert.strictEqual(err.error.errorCode.number, 2014);
  1690. assert.strictEqual(err.error.errorCode.code, "ConstraintTokenMint");
  1691. }
  1692. });
  1693. it("Token Constraint Test(no init) - throws if token::authority mismatch", async () => {
  1694. const mint = anchor.web3.Keypair.generate();
  1695. await program.rpc.testInitMint({
  1696. accounts: {
  1697. mint: mint.publicKey,
  1698. payer: provider.wallet.publicKey,
  1699. systemProgram: anchor.web3.SystemProgram.programId,
  1700. tokenProgram: TOKEN_PROGRAM_ID,
  1701. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1702. },
  1703. signers: [mint],
  1704. });
  1705. const token = anchor.web3.Keypair.generate();
  1706. await program.rpc.testInitToken({
  1707. accounts: {
  1708. token: token.publicKey,
  1709. mint: mint.publicKey,
  1710. payer: provider.wallet.publicKey,
  1711. systemProgram: anchor.web3.SystemProgram.programId,
  1712. tokenProgram: TOKEN_PROGRAM_ID,
  1713. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1714. },
  1715. signers: [token],
  1716. });
  1717. const fakeAuthority = Keypair.generate();
  1718. try {
  1719. await program.rpc.testTokenAuthConstraint({
  1720. accounts: {
  1721. token: token.publicKey,
  1722. mint: mint.publicKey,
  1723. fakeAuthority: fakeAuthority.publicKey,
  1724. },
  1725. });
  1726. assert.isTrue(false);
  1727. } catch (_err) {
  1728. assert.isTrue(_err instanceof AnchorError);
  1729. const err: AnchorError = _err;
  1730. assert.strictEqual(err.error.errorCode.number, 2015);
  1731. assert.strictEqual(err.error.errorCode.code, "ConstraintTokenOwner");
  1732. }
  1733. });
  1734. it("Token Constraint Test(no init) - throws if both token::authority, token::mint mismatch", async () => {
  1735. const mint = anchor.web3.Keypair.generate();
  1736. await program.rpc.testInitMint({
  1737. accounts: {
  1738. mint: mint.publicKey,
  1739. payer: provider.wallet.publicKey,
  1740. systemProgram: anchor.web3.SystemProgram.programId,
  1741. tokenProgram: TOKEN_PROGRAM_ID,
  1742. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1743. },
  1744. signers: [mint],
  1745. });
  1746. const mint1 = anchor.web3.Keypair.generate();
  1747. await program.rpc.testInitMint({
  1748. accounts: {
  1749. mint: mint1.publicKey,
  1750. payer: provider.wallet.publicKey,
  1751. systemProgram: anchor.web3.SystemProgram.programId,
  1752. tokenProgram: TOKEN_PROGRAM_ID,
  1753. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1754. },
  1755. signers: [mint1],
  1756. });
  1757. const token = anchor.web3.Keypair.generate();
  1758. await program.rpc.testInitToken({
  1759. accounts: {
  1760. token: token.publicKey,
  1761. mint: mint.publicKey,
  1762. payer: provider.wallet.publicKey,
  1763. systemProgram: anchor.web3.SystemProgram.programId,
  1764. tokenProgram: TOKEN_PROGRAM_ID,
  1765. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1766. },
  1767. signers: [token],
  1768. });
  1769. const fakeAuthority = Keypair.generate();
  1770. try {
  1771. await program.rpc.testTokenAuthConstraint({
  1772. accounts: {
  1773. token: token.publicKey,
  1774. mint: mint1.publicKey,
  1775. fakeAuthority: fakeAuthority.publicKey,
  1776. },
  1777. });
  1778. assert.isTrue(false);
  1779. } catch (_err) {
  1780. assert.isTrue(_err instanceof AnchorError);
  1781. const err: AnchorError = _err;
  1782. assert.strictEqual(err.error.errorCode.number, 2015);
  1783. assert.strictEqual(err.error.errorCode.code, "ConstraintTokenOwner");
  1784. }
  1785. });
  1786. it("Mint Constraint Test(no init) - mint::decimals, mint::authority, mint::freeze_authority", async () => {
  1787. const mint = anchor.web3.Keypair.generate();
  1788. await program.rpc.testInitMint({
  1789. accounts: {
  1790. mint: mint.publicKey,
  1791. payer: provider.wallet.publicKey,
  1792. systemProgram: anchor.web3.SystemProgram.programId,
  1793. tokenProgram: TOKEN_PROGRAM_ID,
  1794. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1795. },
  1796. signers: [mint],
  1797. });
  1798. await program.rpc.testMintConstraint(6, {
  1799. accounts: {
  1800. mint: mint.publicKey,
  1801. mintAuthority: provider.wallet.publicKey,
  1802. freezeAuthority: provider.wallet.publicKey,
  1803. },
  1804. });
  1805. const client = new Token(
  1806. program.provider.connection,
  1807. mint.publicKey,
  1808. TOKEN_PROGRAM_ID,
  1809. wallet.payer
  1810. );
  1811. const mintAccount = await client.getMintInfo();
  1812. assert.strictEqual(mintAccount.decimals, 6);
  1813. assert.isTrue(
  1814. mintAccount.mintAuthority.equals(provider.wallet.publicKey)
  1815. );
  1816. assert.isTrue(
  1817. mintAccount.freezeAuthority.equals(provider.wallet.publicKey)
  1818. );
  1819. });
  1820. it("Mint Constraint Test(no init) - throws if mint::decimals mismatch", async () => {
  1821. const mint = anchor.web3.Keypair.generate();
  1822. await program.rpc.testInitMint({
  1823. accounts: {
  1824. mint: mint.publicKey,
  1825. payer: provider.wallet.publicKey,
  1826. systemProgram: anchor.web3.SystemProgram.programId,
  1827. tokenProgram: TOKEN_PROGRAM_ID,
  1828. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1829. },
  1830. signers: [mint],
  1831. });
  1832. const fakeDecimal = 5;
  1833. try {
  1834. await program.rpc.testMintConstraint(fakeDecimal, {
  1835. accounts: {
  1836. mint: mint.publicKey,
  1837. mintAuthority: provider.wallet.publicKey,
  1838. freezeAuthority: provider.wallet.publicKey,
  1839. },
  1840. });
  1841. assert.isTrue(false);
  1842. } catch (_err) {
  1843. assert.isTrue(_err instanceof AnchorError);
  1844. const err: AnchorError = _err;
  1845. assert.strictEqual(err.error.errorCode.number, 2018);
  1846. assert.strictEqual(err.error.errorCode.code, "ConstraintMintDecimals");
  1847. }
  1848. });
  1849. it("Mint Constraint Test(no init) - throws if mint::mint_authority mismatch", async () => {
  1850. const mint = anchor.web3.Keypair.generate();
  1851. await program.rpc.testInitMint({
  1852. accounts: {
  1853. mint: mint.publicKey,
  1854. payer: provider.wallet.publicKey,
  1855. systemProgram: anchor.web3.SystemProgram.programId,
  1856. tokenProgram: TOKEN_PROGRAM_ID,
  1857. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1858. },
  1859. signers: [mint],
  1860. });
  1861. const fakeAuthority = Keypair.generate();
  1862. try {
  1863. await program.rpc.testMintConstraint(6, {
  1864. accounts: {
  1865. mint: mint.publicKey,
  1866. mintAuthority: fakeAuthority.publicKey,
  1867. freezeAuthority: provider.wallet.publicKey,
  1868. },
  1869. });
  1870. assert.isTrue(false);
  1871. } catch (_err) {
  1872. assert.isTrue(_err instanceof AnchorError);
  1873. const err: AnchorError = _err;
  1874. assert.strictEqual(err.error.errorCode.number, 2016);
  1875. assert.strictEqual(
  1876. err.error.errorCode.code,
  1877. "ConstraintMintMintAuthority"
  1878. );
  1879. }
  1880. });
  1881. it("Mint Constraint Test(no init) - throws if mint::freeze_authority mismatch", async () => {
  1882. const mint = anchor.web3.Keypair.generate();
  1883. await program.rpc.testInitMint({
  1884. accounts: {
  1885. mint: mint.publicKey,
  1886. payer: provider.wallet.publicKey,
  1887. systemProgram: anchor.web3.SystemProgram.programId,
  1888. tokenProgram: TOKEN_PROGRAM_ID,
  1889. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1890. },
  1891. signers: [mint],
  1892. });
  1893. const fakeAuthority = Keypair.generate();
  1894. try {
  1895. await program.rpc.testMintConstraint(6, {
  1896. accounts: {
  1897. mint: mint.publicKey,
  1898. mintAuthority: provider.wallet.publicKey,
  1899. freezeAuthority: fakeAuthority.publicKey,
  1900. },
  1901. });
  1902. assert.isTrue(false);
  1903. } catch (_err) {
  1904. assert.isTrue(_err instanceof AnchorError);
  1905. const err: AnchorError = _err;
  1906. assert.strictEqual(err.error.errorCode.number, 2017);
  1907. assert.strictEqual(
  1908. err.error.errorCode.code,
  1909. "ConstraintMintFreezeAuthority"
  1910. );
  1911. }
  1912. });
  1913. it("Mint Constraint Test(no init) - can write only mint::decimals", async () => {
  1914. const mint = anchor.web3.Keypair.generate();
  1915. await program.rpc.testInitMint({
  1916. accounts: {
  1917. mint: mint.publicKey,
  1918. payer: provider.wallet.publicKey,
  1919. systemProgram: anchor.web3.SystemProgram.programId,
  1920. tokenProgram: TOKEN_PROGRAM_ID,
  1921. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1922. },
  1923. signers: [mint],
  1924. });
  1925. await program.rpc.testMintOnlyDecimalsConstraint(6, {
  1926. accounts: {
  1927. mint: mint.publicKey,
  1928. },
  1929. });
  1930. const client = new Token(
  1931. program.provider.connection,
  1932. mint.publicKey,
  1933. TOKEN_PROGRAM_ID,
  1934. wallet.payer
  1935. );
  1936. const mintAccount = await client.getMintInfo();
  1937. assert.strictEqual(mintAccount.decimals, 6);
  1938. });
  1939. it("Mint Constraint Test(no init) - can write only mint::authority and mint::freeze_authority", async () => {
  1940. const mint = anchor.web3.Keypair.generate();
  1941. await program.rpc.testInitMint({
  1942. accounts: {
  1943. mint: mint.publicKey,
  1944. payer: provider.wallet.publicKey,
  1945. systemProgram: anchor.web3.SystemProgram.programId,
  1946. tokenProgram: TOKEN_PROGRAM_ID,
  1947. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1948. },
  1949. signers: [mint],
  1950. });
  1951. await program.rpc.testMintOnlyAuthConstraint({
  1952. accounts: {
  1953. mint: mint.publicKey,
  1954. mintAuthority: provider.wallet.publicKey,
  1955. freezeAuthority: provider.wallet.publicKey,
  1956. },
  1957. });
  1958. const client = new Token(
  1959. program.provider.connection,
  1960. mint.publicKey,
  1961. TOKEN_PROGRAM_ID,
  1962. wallet.payer
  1963. );
  1964. const mintAccount = await client.getMintInfo();
  1965. assert.isTrue(
  1966. mintAccount.mintAuthority.equals(provider.wallet.publicKey)
  1967. );
  1968. assert.isTrue(
  1969. mintAccount.freezeAuthority.equals(provider.wallet.publicKey)
  1970. );
  1971. });
  1972. it("Mint Constraint Test(no init) - can write only mint::authority", async () => {
  1973. const mint = anchor.web3.Keypair.generate();
  1974. await program.rpc.testInitMint({
  1975. accounts: {
  1976. mint: mint.publicKey,
  1977. payer: provider.wallet.publicKey,
  1978. systemProgram: anchor.web3.SystemProgram.programId,
  1979. tokenProgram: TOKEN_PROGRAM_ID,
  1980. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1981. },
  1982. signers: [mint],
  1983. });
  1984. await program.rpc.testMintOnlyOneAuthConstraint({
  1985. accounts: {
  1986. mint: mint.publicKey,
  1987. mintAuthority: provider.wallet.publicKey,
  1988. },
  1989. });
  1990. const client = new Token(
  1991. program.provider.connection,
  1992. mint.publicKey,
  1993. TOKEN_PROGRAM_ID,
  1994. wallet.payer
  1995. );
  1996. const mintAccount = await client.getMintInfo();
  1997. assert.isTrue(
  1998. mintAccount.mintAuthority.equals(provider.wallet.publicKey)
  1999. );
  2000. });
  2001. it("Mint Constraint Test(no init) - can write only mint::decimals and mint::freeze_authority", async () => {
  2002. const mint = anchor.web3.Keypair.generate();
  2003. await program.rpc.testInitMint({
  2004. accounts: {
  2005. mint: mint.publicKey,
  2006. payer: provider.wallet.publicKey,
  2007. systemProgram: anchor.web3.SystemProgram.programId,
  2008. tokenProgram: TOKEN_PROGRAM_ID,
  2009. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  2010. },
  2011. signers: [mint],
  2012. });
  2013. await program.rpc.testMintMissMintAuthConstraint(6, {
  2014. accounts: {
  2015. mint: mint.publicKey,
  2016. freezeAuthority: provider.wallet.publicKey,
  2017. },
  2018. });
  2019. const client = new Token(
  2020. program.provider.connection,
  2021. mint.publicKey,
  2022. TOKEN_PROGRAM_ID,
  2023. wallet.payer
  2024. );
  2025. const mintAccount = await client.getMintInfo();
  2026. assert.strictEqual(mintAccount.decimals, 6);
  2027. assert.isTrue(
  2028. mintAccount.freezeAuthority.equals(provider.wallet.publicKey)
  2029. );
  2030. });
  2031. it("check versioned transaction is now available", async () => {
  2032. let thisTx = new VersionedTransaction(
  2033. new Message({
  2034. header: {
  2035. numReadonlySignedAccounts: 0,
  2036. numReadonlyUnsignedAccounts: 0,
  2037. numRequiredSignatures: 0,
  2038. },
  2039. accountKeys: [new PublicKey([0]).toString()],
  2040. instructions: [{ accounts: [0], data: "", programIdIndex: 0 }],
  2041. recentBlockhash: "",
  2042. })
  2043. );
  2044. assert.isDefined(thisTx);
  2045. });
  2046. });
  2047. });