misc.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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("Should include BASE const in IDL", async () => {
  855. assert(
  856. miscIdl.constants.find(
  857. (c) => c.name === "BASE" && c.type === "u128" && c.value === "1_000_000"
  858. ) !== undefined
  859. );
  860. });
  861. it("Should include DECIMALS const in IDL", async () => {
  862. assert(
  863. miscIdl.constants.find(
  864. (c) => c.name === "DECIMALS" && c.type === "u8" && c.value === "6"
  865. ) !== undefined
  866. );
  867. });
  868. it("Should not include NO_IDL const in IDL", async () => {
  869. assert.equal(
  870. miscIdl.constants.find((c) => c.name === "NO_IDL"),
  871. undefined
  872. );
  873. });
  874. it("init_if_needed throws if account exists but is not owned by the expected program", async () => {
  875. const newAcc = await anchor.web3.PublicKey.findProgramAddress(
  876. [utf8.encode("hello")],
  877. program.programId
  878. );
  879. await program.rpc.testInitIfNeededChecksOwner({
  880. accounts: {
  881. data: newAcc[0],
  882. systemProgram: anchor.web3.SystemProgram.programId,
  883. payer: program.provider.wallet.publicKey,
  884. owner: program.programId,
  885. },
  886. });
  887. try {
  888. await program.rpc.testInitIfNeededChecksOwner({
  889. accounts: {
  890. data: newAcc[0],
  891. systemProgram: anchor.web3.SystemProgram.programId,
  892. payer: program.provider.wallet.publicKey,
  893. owner: anchor.web3.Keypair.generate().publicKey,
  894. },
  895. });
  896. assert.ok(false);
  897. } catch (err) {
  898. assert.equal(err.code, 2004);
  899. }
  900. });
  901. it("init_if_needed throws if pda account exists but does not have the expected seeds", async () => {
  902. const newAcc = await anchor.web3.PublicKey.findProgramAddress(
  903. [utf8.encode("nothello")],
  904. program.programId
  905. );
  906. await program.rpc.testInitIfNeededChecksSeeds("nothello", {
  907. accounts: {
  908. data: newAcc[0],
  909. systemProgram: anchor.web3.SystemProgram.programId,
  910. payer: program.provider.wallet.publicKey,
  911. },
  912. });
  913. // this will throw if it is not a proper PDA
  914. // we need this so we know that the following tx failed
  915. // not because it couldn't create this pda
  916. // but because the two pdas were different
  917. anchor.web3.PublicKey.createProgramAddress(
  918. [utf8.encode("hello")],
  919. program.programId
  920. );
  921. try {
  922. await program.rpc.testInitIfNeededChecksSeeds("hello", {
  923. accounts: {
  924. data: newAcc[0],
  925. systemProgram: anchor.web3.SystemProgram.programId,
  926. payer: program.provider.wallet.publicKey,
  927. owner: anchor.web3.Keypair.generate().publicKey,
  928. },
  929. });
  930. assert.ok(false);
  931. } catch (err) {
  932. assert.equal(err.code, 2006);
  933. }
  934. });
  935. it("init_if_needed throws if account exists but is not the expected space", async () => {
  936. const newAcc = anchor.web3.Keypair.generate();
  937. const _irrelevantForTest = 3;
  938. await program.rpc.initWithSpace(_irrelevantForTest, {
  939. accounts: {
  940. data: newAcc.publicKey,
  941. systemProgram: anchor.web3.SystemProgram.programId,
  942. payer: program.provider.wallet.publicKey,
  943. },
  944. signers: [newAcc],
  945. });
  946. try {
  947. await program.rpc.testInitIfNeeded(_irrelevantForTest, {
  948. accounts: {
  949. data: newAcc.publicKey,
  950. systemProgram: anchor.web3.SystemProgram.programId,
  951. payer: program.provider.wallet.publicKey,
  952. },
  953. signers: [newAcc],
  954. });
  955. assert.ok(false);
  956. } catch (err) {
  957. assert.equal(err.code, 2019);
  958. }
  959. });
  960. it("init_if_needed throws if mint exists but has the wrong mint authority", async () => {
  961. const mint = anchor.web3.Keypair.generate();
  962. await program.rpc.testInitMint({
  963. accounts: {
  964. mint: mint.publicKey,
  965. payer: program.provider.wallet.publicKey,
  966. systemProgram: anchor.web3.SystemProgram.programId,
  967. tokenProgram: TOKEN_PROGRAM_ID,
  968. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  969. },
  970. signers: [mint],
  971. });
  972. try {
  973. await program.rpc.testInitMintIfNeeded(6, {
  974. accounts: {
  975. mint: mint.publicKey,
  976. payer: program.provider.wallet.publicKey,
  977. systemProgram: anchor.web3.SystemProgram.programId,
  978. tokenProgram: TOKEN_PROGRAM_ID,
  979. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  980. mintAuthority: anchor.web3.Keypair.generate().publicKey,
  981. freezeAuthority: program.provider.wallet.publicKey,
  982. },
  983. signers: [mint],
  984. });
  985. assert.ok(false);
  986. } catch (err) {
  987. assert.equal(err.code, 2016);
  988. }
  989. });
  990. it("init_if_needed throws if mint exists but has the wrong freeze authority", async () => {
  991. const mint = anchor.web3.Keypair.generate();
  992. await program.rpc.testInitMint({
  993. accounts: {
  994. mint: mint.publicKey,
  995. payer: program.provider.wallet.publicKey,
  996. systemProgram: anchor.web3.SystemProgram.programId,
  997. tokenProgram: TOKEN_PROGRAM_ID,
  998. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  999. },
  1000. signers: [mint],
  1001. });
  1002. try {
  1003. await program.rpc.testInitMintIfNeeded(6, {
  1004. accounts: {
  1005. mint: mint.publicKey,
  1006. payer: program.provider.wallet.publicKey,
  1007. systemProgram: anchor.web3.SystemProgram.programId,
  1008. tokenProgram: TOKEN_PROGRAM_ID,
  1009. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1010. mintAuthority: program.provider.wallet.publicKey,
  1011. freezeAuthority: anchor.web3.Keypair.generate().publicKey,
  1012. },
  1013. signers: [mint],
  1014. });
  1015. assert.ok(false);
  1016. } catch (err) {
  1017. assert.equal(err.code, 2017);
  1018. }
  1019. });
  1020. it("init_if_needed throws if mint exists but has the wrong decimals", async () => {
  1021. const mint = anchor.web3.Keypair.generate();
  1022. await program.rpc.testInitMint({
  1023. accounts: {
  1024. mint: mint.publicKey,
  1025. payer: program.provider.wallet.publicKey,
  1026. systemProgram: anchor.web3.SystemProgram.programId,
  1027. tokenProgram: TOKEN_PROGRAM_ID,
  1028. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1029. },
  1030. signers: [mint],
  1031. });
  1032. try {
  1033. await program.rpc.testInitMintIfNeeded(9, {
  1034. accounts: {
  1035. mint: mint.publicKey,
  1036. payer: program.provider.wallet.publicKey,
  1037. systemProgram: anchor.web3.SystemProgram.programId,
  1038. tokenProgram: TOKEN_PROGRAM_ID,
  1039. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1040. mintAuthority: program.provider.wallet.publicKey,
  1041. freezeAuthority: program.provider.wallet.publicKey,
  1042. },
  1043. signers: [mint],
  1044. });
  1045. assert.ok(false);
  1046. } catch (err) {
  1047. assert.equal(err.code, 2018);
  1048. }
  1049. });
  1050. it("init_if_needed throws if token exists but has the wrong owner", async () => {
  1051. const mint = anchor.web3.Keypair.generate();
  1052. await program.rpc.testInitMint({
  1053. accounts: {
  1054. mint: mint.publicKey,
  1055. payer: program.provider.wallet.publicKey,
  1056. systemProgram: anchor.web3.SystemProgram.programId,
  1057. tokenProgram: TOKEN_PROGRAM_ID,
  1058. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1059. },
  1060. signers: [mint],
  1061. });
  1062. const token = anchor.web3.Keypair.generate();
  1063. await program.rpc.testInitToken({
  1064. accounts: {
  1065. token: token.publicKey,
  1066. mint: mint.publicKey,
  1067. payer: program.provider.wallet.publicKey,
  1068. systemProgram: anchor.web3.SystemProgram.programId,
  1069. tokenProgram: TOKEN_PROGRAM_ID,
  1070. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1071. },
  1072. signers: [token],
  1073. });
  1074. try {
  1075. await program.rpc.testInitTokenIfNeeded({
  1076. accounts: {
  1077. token: token.publicKey,
  1078. mint: mint.publicKey,
  1079. payer: program.provider.wallet.publicKey,
  1080. systemProgram: anchor.web3.SystemProgram.programId,
  1081. tokenProgram: TOKEN_PROGRAM_ID,
  1082. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1083. authority: anchor.web3.Keypair.generate().publicKey,
  1084. },
  1085. signers: [token],
  1086. });
  1087. assert.ok(false);
  1088. } catch (err) {
  1089. assert.equal(err.code, 2015);
  1090. }
  1091. });
  1092. it("init_if_needed throws if token exists but has the wrong mint", async () => {
  1093. const mint = anchor.web3.Keypair.generate();
  1094. await program.rpc.testInitMint({
  1095. accounts: {
  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. },
  1102. signers: [mint],
  1103. });
  1104. const mint2 = anchor.web3.Keypair.generate();
  1105. await program.rpc.testInitMint({
  1106. accounts: {
  1107. mint: mint2.publicKey,
  1108. payer: program.provider.wallet.publicKey,
  1109. systemProgram: anchor.web3.SystemProgram.programId,
  1110. tokenProgram: TOKEN_PROGRAM_ID,
  1111. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1112. },
  1113. signers: [mint2],
  1114. });
  1115. const token = anchor.web3.Keypair.generate();
  1116. await program.rpc.testInitToken({
  1117. accounts: {
  1118. token: token.publicKey,
  1119. mint: mint.publicKey,
  1120. payer: program.provider.wallet.publicKey,
  1121. systemProgram: anchor.web3.SystemProgram.programId,
  1122. tokenProgram: TOKEN_PROGRAM_ID,
  1123. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1124. },
  1125. signers: [token],
  1126. });
  1127. try {
  1128. await program.rpc.testInitTokenIfNeeded({
  1129. accounts: {
  1130. token: token.publicKey,
  1131. mint: mint2.publicKey,
  1132. payer: program.provider.wallet.publicKey,
  1133. systemProgram: anchor.web3.SystemProgram.programId,
  1134. tokenProgram: TOKEN_PROGRAM_ID,
  1135. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1136. authority: program.provider.wallet.publicKey,
  1137. },
  1138. signers: [token],
  1139. });
  1140. assert.ok(false);
  1141. } catch (err) {
  1142. assert.equal(err.code, 2014);
  1143. }
  1144. });
  1145. it("init_if_needed throws if associated token exists but has the wrong owner", async () => {
  1146. const mint = Keypair.generate();
  1147. await program.rpc.testInitMint({
  1148. accounts: {
  1149. mint: mint.publicKey,
  1150. payer: program.provider.wallet.publicKey,
  1151. systemProgram: SystemProgram.programId,
  1152. tokenProgram: TOKEN_PROGRAM_ID,
  1153. rent: SYSVAR_RENT_PUBKEY,
  1154. },
  1155. signers: [mint],
  1156. });
  1157. const associatedToken = await Token.getAssociatedTokenAddress(
  1158. ASSOCIATED_TOKEN_PROGRAM_ID,
  1159. TOKEN_PROGRAM_ID,
  1160. mint.publicKey,
  1161. program.provider.wallet.publicKey
  1162. );
  1163. await program.rpc.testInitAssociatedToken({
  1164. accounts: {
  1165. token: associatedToken,
  1166. mint: mint.publicKey,
  1167. payer: program.provider.wallet.publicKey,
  1168. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1169. systemProgram: anchor.web3.SystemProgram.programId,
  1170. tokenProgram: TOKEN_PROGRAM_ID,
  1171. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1172. },
  1173. });
  1174. try {
  1175. await program.rpc.testInitAssociatedTokenIfNeeded({
  1176. accounts: {
  1177. token: associatedToken,
  1178. mint: mint.publicKey,
  1179. payer: program.provider.wallet.publicKey,
  1180. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1181. systemProgram: anchor.web3.SystemProgram.programId,
  1182. tokenProgram: TOKEN_PROGRAM_ID,
  1183. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1184. authority: anchor.web3.Keypair.generate().publicKey,
  1185. },
  1186. });
  1187. assert.ok(false);
  1188. } catch (err) {
  1189. assert.equal(err.code, 2015);
  1190. }
  1191. });
  1192. it("init_if_needed throws if associated token exists but has the wrong mint", async () => {
  1193. const mint = anchor.web3.Keypair.generate();
  1194. await program.rpc.testInitMint({
  1195. accounts: {
  1196. mint: mint.publicKey,
  1197. payer: program.provider.wallet.publicKey,
  1198. systemProgram: anchor.web3.SystemProgram.programId,
  1199. tokenProgram: TOKEN_PROGRAM_ID,
  1200. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1201. },
  1202. signers: [mint],
  1203. });
  1204. const mint2 = anchor.web3.Keypair.generate();
  1205. await program.rpc.testInitMint({
  1206. accounts: {
  1207. mint: mint2.publicKey,
  1208. payer: program.provider.wallet.publicKey,
  1209. systemProgram: anchor.web3.SystemProgram.programId,
  1210. tokenProgram: TOKEN_PROGRAM_ID,
  1211. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1212. },
  1213. signers: [mint2],
  1214. });
  1215. const associatedToken = await Token.getAssociatedTokenAddress(
  1216. ASSOCIATED_TOKEN_PROGRAM_ID,
  1217. TOKEN_PROGRAM_ID,
  1218. mint.publicKey,
  1219. program.provider.wallet.publicKey
  1220. );
  1221. await program.rpc.testInitAssociatedToken({
  1222. accounts: {
  1223. token: associatedToken,
  1224. mint: mint.publicKey,
  1225. payer: program.provider.wallet.publicKey,
  1226. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1227. systemProgram: anchor.web3.SystemProgram.programId,
  1228. tokenProgram: TOKEN_PROGRAM_ID,
  1229. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1230. },
  1231. });
  1232. try {
  1233. await program.rpc.testInitAssociatedTokenIfNeeded({
  1234. accounts: {
  1235. token: associatedToken,
  1236. mint: mint2.publicKey,
  1237. payer: program.provider.wallet.publicKey,
  1238. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1239. systemProgram: anchor.web3.SystemProgram.programId,
  1240. tokenProgram: TOKEN_PROGRAM_ID,
  1241. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1242. authority: program.provider.wallet.publicKey,
  1243. },
  1244. });
  1245. assert.ok(false);
  1246. } catch (err) {
  1247. assert.equal(err.code, 2014);
  1248. }
  1249. });
  1250. it("init_if_needed throws if token exists with correct owner and mint but is not the ATA", async () => {
  1251. const mint = anchor.web3.Keypair.generate();
  1252. await program.rpc.testInitMint({
  1253. accounts: {
  1254. mint: mint.publicKey,
  1255. payer: program.provider.wallet.publicKey,
  1256. systemProgram: anchor.web3.SystemProgram.programId,
  1257. tokenProgram: TOKEN_PROGRAM_ID,
  1258. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1259. },
  1260. signers: [mint],
  1261. });
  1262. const associatedToken = await Token.getAssociatedTokenAddress(
  1263. ASSOCIATED_TOKEN_PROGRAM_ID,
  1264. TOKEN_PROGRAM_ID,
  1265. mint.publicKey,
  1266. program.provider.wallet.publicKey
  1267. );
  1268. await program.rpc.testInitAssociatedToken({
  1269. accounts: {
  1270. token: associatedToken,
  1271. mint: mint.publicKey,
  1272. payer: program.provider.wallet.publicKey,
  1273. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1274. systemProgram: anchor.web3.SystemProgram.programId,
  1275. tokenProgram: TOKEN_PROGRAM_ID,
  1276. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  1277. },
  1278. });
  1279. const token = anchor.web3.Keypair.generate();
  1280. await program.rpc.testInitToken({
  1281. accounts: {
  1282. token: token.publicKey,
  1283. mint: mint.publicKey,
  1284. payer: program.provider.wallet.publicKey,
  1285. systemProgram: anchor.web3.SystemProgram.programId,
  1286. tokenProgram: TOKEN_PROGRAM_ID,
  1287. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1288. },
  1289. signers: [token],
  1290. });
  1291. try {
  1292. await program.rpc.testInitAssociatedTokenIfNeeded({
  1293. accounts: {
  1294. token: token.publicKey,
  1295. mint: mint.publicKey,
  1296. payer: program.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. authority: program.provider.wallet.publicKey,
  1302. },
  1303. });
  1304. assert.ok(false);
  1305. } catch (err) {
  1306. assert.equal(err.code, 3014);
  1307. }
  1308. });
  1309. it("init_if_needed checks rent_exemption if init is not needed", async () => {
  1310. const data = anchor.web3.Keypair.generate();
  1311. await program.rpc.initDecreaseLamports({
  1312. accounts: {
  1313. data: data.publicKey,
  1314. user: anchor.getProvider().wallet.publicKey,
  1315. systemProgram: SystemProgram.programId,
  1316. },
  1317. signers: [data],
  1318. });
  1319. try {
  1320. await program.rpc.initIfNeededChecksRentExemption({
  1321. accounts: {
  1322. data: data.publicKey,
  1323. user: anchor.getProvider().wallet.publicKey,
  1324. systemProgram: SystemProgram.programId,
  1325. },
  1326. signers: [data],
  1327. });
  1328. assert.ok(false);
  1329. } catch (err) {
  1330. assert.equal(err.code, 2005);
  1331. }
  1332. });
  1333. it("Can use multidimensional array", async () => {
  1334. const array2d = new Array(10).fill(new Array(10).fill(99));
  1335. const data = anchor.web3.Keypair.generate();
  1336. await program.rpc.testMultidimensionalArray(array2d, {
  1337. accounts: {
  1338. data: data.publicKey,
  1339. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1340. },
  1341. signers: [data],
  1342. instructions: [
  1343. await program.account.dataMultidimensionalArray.createInstruction(data),
  1344. ],
  1345. });
  1346. const dataAccount = await program.account.dataMultidimensionalArray.fetch(
  1347. data.publicKey
  1348. );
  1349. assert.deepStrictEqual(dataAccount.data, array2d);
  1350. });
  1351. it("Can use multidimensional array with const sizes", async () => {
  1352. const array2d = new Array(10).fill(new Array(11).fill(22));
  1353. const data = anchor.web3.Keypair.generate();
  1354. await program.rpc.testMultidimensionalArrayConstSizes(array2d, {
  1355. accounts: {
  1356. data: data.publicKey,
  1357. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1358. },
  1359. signers: [data],
  1360. instructions: [
  1361. await program.account.dataMultidimensionalArrayConstSizes.createInstruction(
  1362. data
  1363. ),
  1364. ],
  1365. });
  1366. const dataAccount =
  1367. await program.account.dataMultidimensionalArrayConstSizes.fetch(
  1368. data.publicKey
  1369. );
  1370. assert.deepStrictEqual(dataAccount.data, array2d);
  1371. });
  1372. it("allows non-rent exempt accounts", async () => {
  1373. const data = anchor.web3.Keypair.generate();
  1374. await program.rpc.initializeNoRentExempt({
  1375. accounts: {
  1376. data: data.publicKey,
  1377. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1378. },
  1379. signers: [data],
  1380. instructions: [
  1381. SystemProgram.createAccount({
  1382. programId: program.programId,
  1383. space: 8 + 16 + 16,
  1384. lamports:
  1385. await program.provider.connection.getMinimumBalanceForRentExemption(
  1386. 39
  1387. ),
  1388. fromPubkey: anchor.getProvider().wallet.publicKey,
  1389. newAccountPubkey: data.publicKey,
  1390. }),
  1391. ],
  1392. });
  1393. await program.rpc.testNoRentExempt({
  1394. accounts: {
  1395. data: data.publicKey,
  1396. },
  1397. });
  1398. });
  1399. it("allows rent exemption to be skipped", async () => {
  1400. const data = anchor.web3.Keypair.generate();
  1401. await program.rpc.initializeSkipRentExempt({
  1402. accounts: {
  1403. data: data.publicKey,
  1404. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1405. },
  1406. signers: [data],
  1407. instructions: [
  1408. SystemProgram.createAccount({
  1409. programId: program.programId,
  1410. space: 8 + 16 + 16,
  1411. lamports:
  1412. await program.provider.connection.getMinimumBalanceForRentExemption(
  1413. 39
  1414. ),
  1415. fromPubkey: anchor.getProvider().wallet.publicKey,
  1416. newAccountPubkey: data.publicKey,
  1417. }),
  1418. ],
  1419. });
  1420. });
  1421. it("can use rent_exempt to enforce rent exemption", async () => {
  1422. const data = anchor.web3.Keypair.generate();
  1423. await program.rpc.initializeSkipRentExempt({
  1424. accounts: {
  1425. data: data.publicKey,
  1426. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  1427. },
  1428. signers: [data],
  1429. instructions: [
  1430. SystemProgram.createAccount({
  1431. programId: program.programId,
  1432. space: 8 + 16 + 16,
  1433. lamports:
  1434. await program.provider.connection.getMinimumBalanceForRentExemption(
  1435. 39
  1436. ),
  1437. fromPubkey: anchor.getProvider().wallet.publicKey,
  1438. newAccountPubkey: data.publicKey,
  1439. }),
  1440. ],
  1441. });
  1442. try {
  1443. await program.rpc.testEnforceRentExempt({
  1444. accounts: {
  1445. data: data.publicKey,
  1446. },
  1447. });
  1448. assert.ok(false);
  1449. } catch (err) {
  1450. assert.equal(2005, err.code);
  1451. assert.equal("A rent exempt constraint was violated", err.msg);
  1452. }
  1453. });
  1454. describe("Can validate PDAs derived from other program ids", () => {
  1455. it("With bumps using create_program_address", async () => {
  1456. const [firstPDA, firstBump] =
  1457. await anchor.web3.PublicKey.findProgramAddress(
  1458. [anchor.utils.bytes.utf8.encode("seed")],
  1459. ASSOCIATED_TOKEN_PROGRAM_ID
  1460. );
  1461. const [secondPDA, secondBump] =
  1462. await anchor.web3.PublicKey.findProgramAddress(
  1463. [anchor.utils.bytes.utf8.encode("seed")],
  1464. program.programId
  1465. );
  1466. // correct bump but wrong address
  1467. const wrongAddress = anchor.web3.Keypair.generate().publicKey;
  1468. try {
  1469. await program.rpc.testProgramIdConstraint(firstBump, secondBump, {
  1470. accounts: {
  1471. first: wrongAddress,
  1472. second: secondPDA,
  1473. },
  1474. });
  1475. assert.ok(false);
  1476. } catch (err) {
  1477. assert.equal(err.code, 2006);
  1478. }
  1479. // matching bump seed for wrong address but derived from wrong program
  1480. try {
  1481. await program.rpc.testProgramIdConstraint(secondBump, secondBump, {
  1482. accounts: {
  1483. first: secondPDA,
  1484. second: secondPDA,
  1485. },
  1486. });
  1487. assert.ok(false);
  1488. } catch (err) {
  1489. assert.equal(err.code, 2006);
  1490. }
  1491. // correct inputs should lead to successful tx
  1492. await program.rpc.testProgramIdConstraint(firstBump, secondBump, {
  1493. accounts: {
  1494. first: firstPDA,
  1495. second: secondPDA,
  1496. },
  1497. });
  1498. });
  1499. it("With bumps using find_program_address", async () => {
  1500. const firstPDA = (
  1501. await anchor.web3.PublicKey.findProgramAddress(
  1502. [anchor.utils.bytes.utf8.encode("seed")],
  1503. ASSOCIATED_TOKEN_PROGRAM_ID
  1504. )
  1505. )[0];
  1506. const secondPDA = (
  1507. await anchor.web3.PublicKey.findProgramAddress(
  1508. [anchor.utils.bytes.utf8.encode("seed")],
  1509. program.programId
  1510. )
  1511. )[0];
  1512. // random wrong address
  1513. const wrongAddress = anchor.web3.Keypair.generate().publicKey;
  1514. try {
  1515. await program.rpc.testProgramIdConstraintFindPda({
  1516. accounts: {
  1517. first: wrongAddress,
  1518. second: secondPDA,
  1519. },
  1520. });
  1521. assert.ok(false);
  1522. } catch (err) {
  1523. assert.equal(err.code, 2006);
  1524. }
  1525. // same seeds but derived from wrong program
  1526. try {
  1527. await program.rpc.testProgramIdConstraintFindPda({
  1528. accounts: {
  1529. first: secondPDA,
  1530. second: secondPDA,
  1531. },
  1532. });
  1533. assert.ok(false);
  1534. } catch (err) {
  1535. assert.equal(err.code, 2006);
  1536. }
  1537. // correct inputs should lead to successful tx
  1538. await program.rpc.testProgramIdConstraintFindPda({
  1539. accounts: {
  1540. first: firstPDA,
  1541. second: secondPDA,
  1542. },
  1543. });
  1544. });
  1545. });
  1546. });