12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195 |
- import * as anchor from "@project-serum/anchor";
- import {
- Program,
- BN,
- IdlAccounts,
- AnchorError,
- Wallet,
- IdlTypes,
- IdlEvents,
- } from "@project-serum/anchor";
- import {
- PublicKey,
- Keypair,
- SystemProgram,
- SYSVAR_RENT_PUBKEY,
- Message,
- VersionedTransaction,
- } from "@solana/web3.js";
- import {
- TOKEN_PROGRAM_ID,
- Token,
- ASSOCIATED_TOKEN_PROGRAM_ID,
- } from "@solana/spl-token";
- import { Misc } from "../../target/types/misc";
- import { Misc2 } from "../../target/types/misc2";
- const utf8 = anchor.utils.bytes.utf8;
- const { assert, expect } = require("chai");
- const nativeAssert = require("assert");
- const miscIdl = require("../../target/idl/misc.json");
- describe("misc", () => {
- // Configure the client to use the local cluster.
- const provider = anchor.AnchorProvider.env();
- const wallet = provider.wallet as Wallet;
- anchor.setProvider(provider);
- const program = anchor.workspace.Misc as Program<Misc>;
- const misc2Program = anchor.workspace.Misc2 as Program<Misc2>;
- it("Can allocate extra space for a state constructor", async () => {
- // @ts-expect-error
- const tx = await program.state.rpc.new();
- const addr = await program.state.address();
- const state = await program.state.fetch();
- const accountInfo = await program.provider.connection.getAccountInfo(addr);
- assert.isTrue(state.v.equals(Buffer.from([])));
- assert.lengthOf(accountInfo.data, 99);
- });
- it("Can use remaining accounts for a state instruction", async () => {
- await program.state.rpc.remainingAccounts({
- remainingAccounts: [
- { pubkey: misc2Program.programId, isWritable: false, isSigner: false },
- ],
- });
- });
- const data = anchor.web3.Keypair.generate();
- it("Can use u128 and i128", async () => {
- const tx = await program.rpc.initialize(
- new anchor.BN(1234),
- new anchor.BN(22),
- {
- accounts: {
- data: data.publicKey,
- },
- signers: [data],
- instructions: [await program.account.data.createInstruction(data)],
- }
- );
- const dataAccount = await program.account.data.fetch(data.publicKey);
- assert.isTrue(dataAccount.udata.eq(new anchor.BN(1234)));
- assert.isTrue(dataAccount.idata.eq(new anchor.BN(22)));
- });
- it("Can use u16", async () => {
- const data = anchor.web3.Keypair.generate();
- const tx = await program.rpc.testU16(99, {
- accounts: {
- myAccount: data.publicKey,
- },
- signers: [data],
- instructions: [await program.account.dataU16.createInstruction(data)],
- });
- const dataAccount = await program.account.dataU16.fetch(data.publicKey);
- assert.strictEqual(dataAccount.data, 99);
- });
- it("Can embed programs into genesis from the Anchor.toml", async () => {
- const pid = new anchor.web3.PublicKey(
- "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
- );
- let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
- assert.isTrue(accInfo.executable);
- });
- it("Can use the owner constraint", async () => {
- await program.rpc.testOwner({
- accounts: {
- data: data.publicKey,
- misc: program.programId,
- },
- });
- await nativeAssert.rejects(
- async () => {
- await program.rpc.testOwner({
- accounts: {
- data: provider.wallet.publicKey,
- misc: program.programId,
- },
- });
- },
- (err) => {
- return true;
- }
- );
- });
- it("Can use the executable attribute", async () => {
- await program.rpc.testExecutable({
- accounts: {
- program: program.programId,
- },
- });
- await nativeAssert.rejects(
- async () => {
- await program.rpc.testExecutable({
- accounts: {
- program: provider.wallet.publicKey,
- },
- });
- },
- (err) => {
- return true;
- }
- );
- });
- it("Can CPI to state instructions", async () => {
- const oldData = new anchor.BN(0);
- await misc2Program.state.rpc.new({
- accounts: {
- authority: provider.wallet.publicKey,
- },
- });
- let stateAccount = await misc2Program.state.fetch();
- assert.isTrue(stateAccount.data.eq(oldData));
- assert.isTrue(stateAccount.auth.equals(provider.wallet.publicKey));
- const newData = new anchor.BN(2134);
- await program.rpc.testStateCpi(newData, {
- accounts: {
- authority: provider.wallet.publicKey,
- cpiState: await misc2Program.state.address(),
- misc2Program: misc2Program.programId,
- },
- });
- stateAccount = await misc2Program.state.fetch();
- assert.isTrue(stateAccount.data.eq(newData));
- assert.isTrue(stateAccount.auth.equals(provider.wallet.publicKey));
- });
- it("Can retrieve events when simulating a transaction", async () => {
- const resp = await program.methods.testSimulate(44).simulate();
- const expectedRaw = [
- "Program 3TEqcc8xhrhdspwbvoamUJe2borm4Nr72JxL66k6rgrh invoke [1]",
- "Program log: Instruction: TestSimulate",
- "Program data: NgyCA9omwbMsAAAA",
- "Program data: fPhuIELK/k7SBAAA",
- "Program data: jvbowsvlmkcJAAAA",
- "Program data: zxM5neEnS1kBAgMEBQYHCAkK",
- "Program data: g06Ei2GL1gIBAgMEBQYHCAkKCw==",
- ];
- assert.deepStrictEqual(expectedRaw, resp.raw.slice(0, -2));
- assert.strictEqual(resp.events[0].name, "E1");
- assert.strictEqual(resp.events[0].data.data, 44);
- assert.strictEqual(resp.events[1].name, "E2");
- assert.strictEqual(resp.events[1].data.data, 1234);
- assert.strictEqual(resp.events[2].name, "E3");
- assert.strictEqual(resp.events[2].data.data, 9);
- assert.strictEqual(resp.events[3].name, "E5");
- assert.deepStrictEqual(
- resp.events[3].data.data,
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- );
- assert.strictEqual(resp.events[4].name, "E6");
- assert.deepStrictEqual(
- resp.events[4].data.data,
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
- );
- });
- it("Can use enum in idl", async () => {
- const resp1 = await program.methods.testInputEnum({ first: {} }).simulate();
- const event1 = resp1.events[0].data as IdlEvents<Misc>["E7"];
- assert.deepEqual(event1.data.first, {});
- const resp2 = await program.methods
- .testInputEnum({ second: { x: new BN(1), y: new BN(2) } })
- .simulate();
- const event2 = resp2.events[0].data as IdlEvents<Misc>["E7"];
- assert.isTrue(new BN(1).eq(event2.data.second.x));
- assert.isTrue(new BN(2).eq(event2.data.second.y));
- const resp3 = await program.methods
- .testInputEnum({
- tupleStructTest: [
- { data1: 1, data2: 11, data3: 111, data4: new BN(1111) },
- ],
- })
- .simulate();
- const event3 = resp3.events[0].data as IdlEvents<Misc>["E7"];
- assert.strictEqual(event3.data.tupleStructTest[0].data1, 1);
- assert.strictEqual(event3.data.tupleStructTest[0].data2, 11);
- assert.strictEqual(event3.data.tupleStructTest[0].data3, 111);
- assert.isTrue(event3.data.tupleStructTest[0].data4.eq(new BN(1111)));
- const resp4 = await program.methods
- .testInputEnum({ tupleTest: [1, 2, 3, 4] })
- .simulate();
- const event4 = resp4.events[0].data as IdlEvents<Misc>["E7"];
- assert.strictEqual(event4.data.tupleTest[0], 1);
- assert.strictEqual(event4.data.tupleTest[1], 2);
- assert.strictEqual(event4.data.tupleTest[2], 3);
- assert.strictEqual(event4.data.tupleTest[3], 4);
- });
- let dataI8;
- it("Can use i8 in the idl", async () => {
- dataI8 = anchor.web3.Keypair.generate();
- await program.rpc.testI8(-3, {
- accounts: {
- data: dataI8.publicKey,
- },
- instructions: [await program.account.dataI8.createInstruction(dataI8)],
- signers: [dataI8],
- });
- const dataAccount = await program.account.dataI8.fetch(dataI8.publicKey);
- assert.strictEqual(dataAccount.data, -3);
- });
- let dataPubkey;
- it("Can use i16 in the idl", async () => {
- const data = anchor.web3.Keypair.generate();
- await program.rpc.testI16(-2048, {
- accounts: {
- data: data.publicKey,
- },
- instructions: [await program.account.dataI16.createInstruction(data)],
- signers: [data],
- });
- const dataAccount = await program.account.dataI16.fetch(data.publicKey);
- assert.strictEqual(dataAccount.data, -2048);
- dataPubkey = data.publicKey;
- });
- it("Can use base58 strings to fetch an account", async () => {
- const dataAccount = await program.account.dataI16.fetch(
- dataPubkey.toString()
- );
- assert.strictEqual(dataAccount.data, -2048);
- });
- it("Should fail to close an account when sending lamports to itself", async () => {
- try {
- await program.rpc.testClose({
- accounts: {
- data: data.publicKey,
- solDest: data.publicKey,
- },
- });
- expect(false).to.be.true;
- } catch (err) {
- const errMsg = "A close constraint was violated";
- assert.strictEqual(err.error.errorMessage, errMsg);
- assert.strictEqual(err.error.errorCode.number, 2011);
- }
- });
- it("Can close an account", async () => {
- const openAccount = await program.provider.connection.getAccountInfo(
- data.publicKey
- );
- assert.isNotNull(openAccount);
- let beforeBalance = (
- await program.provider.connection.getAccountInfo(
- provider.wallet.publicKey
- )
- ).lamports;
- await program.rpc.testClose({
- accounts: {
- data: data.publicKey,
- solDest: provider.wallet.publicKey,
- },
- });
- let afterBalance = (
- await program.provider.connection.getAccountInfo(
- provider.wallet.publicKey
- )
- ).lamports;
- // Retrieved rent exemption sol.
- expect(afterBalance > beforeBalance).to.be.true;
- const closedAccount = await program.provider.connection.getAccountInfo(
- data.publicKey
- );
- assert.isNull(closedAccount);
- });
- it("Can use instruction data in accounts constraints", async () => {
- // b"my-seed"
- const seed = Buffer.from([109, 121, 45, 115, 101, 101, 100]);
- const [myPda, nonce] = await PublicKey.findProgramAddress(
- [seed, anchor.web3.SYSVAR_RENT_PUBKEY.toBuffer()],
- program.programId
- );
- await program.rpc.testInstructionConstraint(nonce, {
- accounts: {
- myPda,
- myAccount: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- });
- });
- it("Can create a PDA account with instruction data", async () => {
- const seed = Buffer.from([1, 2, 3, 4]);
- const domain = "my-domain";
- const foo = anchor.web3.SYSVAR_RENT_PUBKEY;
- const [myPda, nonce] = await PublicKey.findProgramAddress(
- [
- Buffer.from(anchor.utils.bytes.utf8.encode("my-seed")),
- Buffer.from(anchor.utils.bytes.utf8.encode(domain)),
- foo.toBuffer(),
- seed,
- ],
- program.programId
- );
- await program.rpc.testPdaInit(domain, seed, nonce, {
- accounts: {
- myPda,
- myPayer: provider.wallet.publicKey,
- foo,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- });
- const myPdaAccount = await program.account.dataU16.fetch(myPda);
- assert.strictEqual(myPdaAccount.data, 6);
- });
- it("Can create a zero copy PDA account", async () => {
- const [myPda, nonce] = await PublicKey.findProgramAddress(
- [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
- program.programId
- );
- await program.rpc.testPdaInitZeroCopy({
- accounts: {
- myPda,
- myPayer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- });
- const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
- assert.strictEqual(myPdaAccount.data, 9);
- assert.strictEqual(myPdaAccount.bump, nonce);
- });
- it("Can write to a zero copy PDA account", async () => {
- const [myPda, bump] = await PublicKey.findProgramAddress(
- [Buffer.from(anchor.utils.bytes.utf8.encode("my-seed"))],
- program.programId
- );
- await program.rpc.testPdaMutZeroCopy({
- accounts: {
- myPda,
- myPayer: provider.wallet.publicKey,
- },
- });
- const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
- assert.strictEqual(myPdaAccount.data, 1234);
- assert.strictEqual(myPdaAccount.bump, bump);
- });
- it("Can create a token account from seeds pda", async () => {
- const [mint, mint_bump] = await PublicKey.findProgramAddress(
- [Buffer.from(anchor.utils.bytes.utf8.encode("my-mint-seed"))],
- program.programId
- );
- const [myPda, token_bump] = await PublicKey.findProgramAddress(
- [Buffer.from(anchor.utils.bytes.utf8.encode("my-token-seed"))],
- program.programId
- );
- await program.rpc.testTokenSeedsInit({
- accounts: {
- myPda,
- mint,
- authority: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- tokenProgram: TOKEN_PROGRAM_ID,
- },
- });
- const mintAccount = new Token(
- program.provider.connection,
- mint,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const account = await mintAccount.getAccountInfo(myPda);
- // @ts-expect-error
- assert.strictEqual(account.state, 1);
- assert.strictEqual(account.amount.toNumber(), 0);
- assert.isTrue(account.isInitialized);
- assert.isTrue(account.owner.equals(provider.wallet.publicKey));
- assert.isTrue(account.mint.equals(mint));
- });
- it("Can execute a fallback function", async () => {
- await nativeAssert.rejects(
- async () => {
- await anchor.utils.rpc.invoke(program.programId);
- },
- (err) => {
- assert.isTrue(err.toString().includes("custom program error: 0x4d2"));
- return true;
- }
- );
- });
- it("Can init a random account", async () => {
- const data = anchor.web3.Keypair.generate();
- await program.rpc.testInit({
- accounts: {
- data: data.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data],
- });
- const account = await program.account.dataI8.fetch(data.publicKey);
- assert.strictEqual(account.data, 3);
- });
- it("Can init a random account prefunded", async () => {
- const data = anchor.web3.Keypair.generate();
- await program.rpc.testInit({
- accounts: {
- data: data.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data],
- instructions: [
- anchor.web3.SystemProgram.transfer({
- fromPubkey: provider.wallet.publicKey,
- toPubkey: data.publicKey,
- lamports: 4039280,
- }),
- ],
- });
- const account = await program.account.dataI8.fetch(data.publicKey);
- assert.strictEqual(account.data, 3);
- });
- it("Can init a random zero copy account", async () => {
- const data = anchor.web3.Keypair.generate();
- await program.rpc.testInitZeroCopy({
- accounts: {
- data: data.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data],
- });
- const account = await program.account.dataZeroCopy.fetch(data.publicKey);
- assert.strictEqual(account.data, 10);
- assert.strictEqual(account.bump, 2);
- });
- let mint = undefined;
- it("Can create a random mint account", async () => {
- mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const mintAccount = await client.getMintInfo();
- assert.strictEqual(mintAccount.decimals, 6);
- assert.isTrue(mintAccount.mintAuthority.equals(provider.wallet.publicKey));
- assert.isTrue(
- mintAccount.freezeAuthority.equals(provider.wallet.publicKey)
- );
- });
- it("Can create a random mint account prefunded", async () => {
- mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- instructions: [
- anchor.web3.SystemProgram.transfer({
- fromPubkey: provider.wallet.publicKey,
- toPubkey: mint.publicKey,
- lamports: 4039280,
- }),
- ],
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const mintAccount = await client.getMintInfo();
- assert.strictEqual(mintAccount.decimals, 6);
- assert.isTrue(mintAccount.mintAuthority.equals(provider.wallet.publicKey));
- });
- it("Can create a random token account", async () => {
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const account = await client.getAccountInfo(token.publicKey);
- // @ts-expect-error
- assert.strictEqual(account.state, 1);
- assert.strictEqual(account.amount.toNumber(), 0);
- assert.isTrue(account.isInitialized);
- assert.isTrue(account.owner.equals(provider.wallet.publicKey));
- assert.isTrue(account.mint.equals(mint.publicKey));
- });
- it("Can create a random token with prefunding", async () => {
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- instructions: [
- anchor.web3.SystemProgram.transfer({
- fromPubkey: provider.wallet.publicKey,
- toPubkey: token.publicKey,
- lamports: 4039280,
- }),
- ],
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const account = await client.getAccountInfo(token.publicKey);
- // @ts-expect-error
- assert.strictEqual(account.state, 1);
- assert.strictEqual(account.amount.toNumber(), 0);
- assert.isTrue(account.isInitialized);
- assert.isTrue(account.owner.equals(provider.wallet.publicKey));
- assert.isTrue(account.mint.equals(mint.publicKey));
- });
- it("Can create a random token with prefunding under the rent exemption", async () => {
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- instructions: [
- anchor.web3.SystemProgram.transfer({
- fromPubkey: provider.wallet.publicKey,
- toPubkey: token.publicKey,
- lamports: 1,
- }),
- ],
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const account = await client.getAccountInfo(token.publicKey);
- // @ts-expect-error
- assert.strictEqual(account.state, 1);
- assert.strictEqual(account.amount.toNumber(), 0);
- assert.isTrue(account.isInitialized);
- assert.isTrue(account.owner.equals(provider.wallet.publicKey));
- assert.isTrue(account.mint.equals(mint.publicKey));
- });
- it("Can initialize multiple accounts via a composite payer", async () => {
- const data1 = anchor.web3.Keypair.generate();
- const data2 = anchor.web3.Keypair.generate();
- const tx = await program.rpc.testCompositePayer({
- accounts: {
- composite: {
- data: data1.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- data: data2.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data1, data2],
- });
- const account1 = await program.account.dataI8.fetch(data1.publicKey);
- assert.strictEqual(account1.data, 1);
- const account2 = await program.account.data.fetch(data2.publicKey);
- assert.strictEqual(account2.udata.toNumber(), 2);
- assert.strictEqual(account2.idata.toNumber(), 3);
- });
- describe("associated_token constraints", () => {
- let associatedToken = null;
- // apparently cannot await here so doing it in the 'it' statements
- let client = Token.createMint(
- program.provider.connection,
- wallet.payer,
- provider.wallet.publicKey,
- provider.wallet.publicKey,
- 9,
- TOKEN_PROGRAM_ID
- );
- it("Can create an associated token account", async () => {
- const localClient = await client;
- associatedToken = await Token.getAssociatedTokenAddress(
- ASSOCIATED_TOKEN_PROGRAM_ID,
- TOKEN_PROGRAM_ID,
- localClient.publicKey,
- provider.wallet.publicKey
- );
- await program.rpc.testInitAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: localClient.publicKey,
- payer: provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
- },
- });
- const account = await localClient.getAccountInfo(associatedToken);
- // @ts-expect-error
- assert.strictEqual(account.state, 1);
- assert.strictEqual(account.amount.toNumber(), 0);
- assert.isTrue(account.isInitialized);
- assert.isTrue(account.owner.equals(provider.wallet.publicKey));
- assert.isTrue(account.mint.equals(localClient.publicKey));
- });
- it("Can validate associated_token constraints", async () => {
- const localClient = await client;
- await program.rpc.testValidateAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: localClient.publicKey,
- wallet: provider.wallet.publicKey,
- },
- });
- let otherMint = await Token.createMint(
- program.provider.connection,
- wallet.payer,
- provider.wallet.publicKey,
- provider.wallet.publicKey,
- 9,
- TOKEN_PROGRAM_ID
- );
- await nativeAssert.rejects(
- async () => {
- await program.rpc.testValidateAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: otherMint.publicKey,
- wallet: provider.wallet.publicKey,
- },
- });
- },
- (err) => {
- assert.strictEqual(err.error.errorCode.number, 2009);
- return true;
- }
- );
- });
- it("associated_token constraints check do not allow authority change", async () => {
- const localClient = await client;
- await program.rpc.testValidateAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: localClient.publicKey,
- wallet: provider.wallet.publicKey,
- },
- });
- await localClient.setAuthority(
- associatedToken,
- anchor.web3.Keypair.generate().publicKey,
- "AccountOwner",
- wallet.payer,
- []
- );
- await nativeAssert.rejects(
- async () => {
- await program.rpc.testValidateAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: localClient.publicKey,
- wallet: provider.wallet.publicKey,
- },
- });
- },
- (err) => {
- assert.strictEqual(err.error.errorCode.number, 2015);
- return true;
- }
- );
- });
- });
- it("Can fetch all accounts of a given type", async () => {
- // Initialize the accounts.
- const data1 = anchor.web3.Keypair.generate();
- const data2 = anchor.web3.Keypair.generate();
- const data3 = anchor.web3.Keypair.generate();
- const data4 = anchor.web3.Keypair.generate();
- // Initialize filterable data.
- const filterable1 = anchor.web3.Keypair.generate().publicKey;
- const filterable2 = anchor.web3.Keypair.generate().publicKey;
- // Set up a secondary wallet and program.
- const anotherProvider = new anchor.AnchorProvider(
- program.provider.connection,
- new anchor.Wallet(anchor.web3.Keypair.generate()),
- { commitment: program.provider.connection.commitment }
- );
- const anotherProgram = new anchor.Program(
- miscIdl,
- program.programId,
- anotherProvider
- );
- // Request airdrop for secondary wallet.
- const signature = await program.provider.connection.requestAirdrop(
- anotherProvider.wallet.publicKey,
- anchor.web3.LAMPORTS_PER_SOL
- );
- await program.provider.connection.confirmTransaction(signature);
- // Create all the accounts.
- await Promise.all([
- program.rpc.testFetchAll(filterable1, {
- accounts: {
- data: data1.publicKey,
- authority: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data1],
- }),
- program.rpc.testFetchAll(filterable1, {
- accounts: {
- data: data2.publicKey,
- authority: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data2],
- }),
- program.rpc.testFetchAll(filterable2, {
- accounts: {
- data: data3.publicKey,
- authority: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data3],
- }),
- anotherProgram.rpc.testFetchAll(filterable1, {
- accounts: {
- data: data4.publicKey,
- authority: anotherProvider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data4],
- }),
- ]);
- // Call for multiple kinds of .all.
- const allAccounts = await program.account.dataWithFilter.all();
- const allAccountsFilteredByBuffer =
- await program.account.dataWithFilter.all(
- provider.wallet.publicKey.toBuffer()
- );
- const allAccountsFilteredByProgramFilters1 =
- await program.account.dataWithFilter.all([
- {
- memcmp: {
- offset: 8,
- bytes: provider.wallet.publicKey.toBase58(),
- },
- },
- { memcmp: { offset: 40, bytes: filterable1.toBase58() } },
- ]);
- const allAccountsFilteredByProgramFilters2 =
- await program.account.dataWithFilter.all([
- {
- memcmp: {
- offset: 8,
- bytes: provider.wallet.publicKey.toBase58(),
- },
- },
- { memcmp: { offset: 40, bytes: filterable2.toBase58() } },
- ]);
- // Without filters there should be 4 accounts.
- assert.lengthOf(allAccounts, 4);
- // Filtering by main wallet there should be 3 accounts.
- assert.lengthOf(allAccountsFilteredByBuffer, 3);
- // Filtering all the main wallet accounts and matching the filterable1 value
- // results in a 2 accounts.
- assert.lengthOf(allAccountsFilteredByProgramFilters1, 2);
- // Filtering all the main wallet accounts and matching the filterable2 value
- // results in 1 account.
- assert.lengthOf(allAccountsFilteredByProgramFilters2, 1);
- });
- it("Can use pdas with empty seeds", async () => {
- const [pda, bump] = await PublicKey.findProgramAddress(
- [],
- program.programId
- );
- await program.rpc.testInitWithEmptySeeds({
- accounts: {
- pda: pda,
- authority: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- });
- await program.rpc.testEmptySeedsConstraint({
- accounts: {
- pda: pda,
- },
- });
- const [pda2] = await PublicKey.findProgramAddress(
- [anchor.utils.bytes.utf8.encode("non-empty")],
- program.programId
- );
- await nativeAssert.rejects(
- program.rpc.testEmptySeedsConstraint({
- accounts: {
- pda: pda2,
- },
- }),
- (err) => {
- assert.equal(err.error.errorCode.number, 2006);
- return true;
- }
- );
- });
- const ifNeededAcc = anchor.web3.Keypair.generate();
- it("Can init if needed a new account", async () => {
- await program.rpc.testInitIfNeeded(1, {
- accounts: {
- data: ifNeededAcc.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: provider.wallet.publicKey,
- },
- signers: [ifNeededAcc],
- });
- const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
- assert.strictEqual(account.data, 1);
- });
- it("Can init if needed a previously created account", async () => {
- await program.rpc.testInitIfNeeded(3, {
- accounts: {
- data: ifNeededAcc.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: provider.wallet.publicKey,
- },
- signers: [ifNeededAcc],
- });
- const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
- assert.strictEqual(account.data, 3);
- });
- it("Can use const for array size", async () => {
- const data = anchor.web3.Keypair.generate();
- const tx = await program.rpc.testConstArraySize(99, {
- accounts: {
- data: data.publicKey,
- },
- signers: [data],
- instructions: [
- await program.account.dataConstArraySize.createInstruction(data),
- ],
- });
- const dataAccount = await program.account.dataConstArraySize.fetch(
- data.publicKey
- );
- assert.deepStrictEqual(dataAccount.data, [99, ...new Array(9).fill(0)]);
- });
- it("Can use const for instruction data size", async () => {
- const data = anchor.web3.Keypair.generate();
- const dataArray = [99, ...new Array(9).fill(0)];
- const tx = await program.rpc.testConstIxDataSize(dataArray, {
- accounts: {
- data: data.publicKey,
- },
- signers: [data],
- instructions: [
- await program.account.dataConstArraySize.createInstruction(data),
- ],
- });
- const dataAccount = await program.account.dataConstArraySize.fetch(
- data.publicKey
- );
- assert.deepStrictEqual(dataAccount.data, dataArray);
- });
- it("Should include BASE const in IDL", async () => {
- assert.isDefined(
- miscIdl.constants.find(
- (c) => c.name === "BASE" && c.type === "u128" && c.value === "1_000_000"
- )
- );
- });
- it("Should include DECIMALS const in IDL", async () => {
- assert.isDefined(
- miscIdl.constants.find(
- (c) => c.name === "DECIMALS" && c.type === "u8" && c.value === "6"
- )
- );
- });
- it("Should not include NO_IDL const in IDL", async () => {
- assert.isUndefined(miscIdl.constants.find((c) => c.name === "NO_IDL"));
- });
- it("init_if_needed throws if account exists but is not owned by the expected program", async () => {
- const newAcc = await anchor.web3.PublicKey.findProgramAddress(
- [utf8.encode("hello")],
- program.programId
- );
- await program.rpc.testInitIfNeededChecksOwner({
- accounts: {
- data: newAcc[0],
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: provider.wallet.publicKey,
- owner: program.programId,
- },
- });
- try {
- await program.rpc.testInitIfNeededChecksOwner({
- accounts: {
- data: newAcc[0],
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: provider.wallet.publicKey,
- owner: anchor.web3.Keypair.generate().publicKey,
- },
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2004);
- }
- });
- it("init_if_needed throws if pda account exists but does not have the expected seeds", async () => {
- const newAcc = await anchor.web3.PublicKey.findProgramAddress(
- [utf8.encode("nothello")],
- program.programId
- );
- await program.rpc.testInitIfNeededChecksSeeds("nothello", {
- accounts: {
- data: newAcc[0],
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: provider.wallet.publicKey,
- },
- });
- // this will throw if it is not a proper PDA
- // we need this so we know that the following tx failed
- // not because it couldn't create this pda
- // but because the two pdas were different
- anchor.web3.PublicKey.createProgramAddress(
- [utf8.encode("hello")],
- program.programId
- );
- try {
- await program.rpc.testInitIfNeededChecksSeeds("hello", {
- accounts: {
- data: newAcc[0],
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: provider.wallet.publicKey,
- },
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2006);
- }
- });
- it("init_if_needed throws if account exists but is not the expected space", async () => {
- const newAcc = anchor.web3.Keypair.generate();
- const _irrelevantForTest = 3;
- await program.rpc.initWithSpace(_irrelevantForTest, {
- accounts: {
- data: newAcc.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: provider.wallet.publicKey,
- },
- signers: [newAcc],
- });
- try {
- await program.rpc.testInitIfNeeded(_irrelevantForTest, {
- accounts: {
- data: newAcc.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: provider.wallet.publicKey,
- },
- signers: [newAcc],
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2019);
- }
- });
- it("init_if_needed throws if mint exists but has the wrong mint authority", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- try {
- await program.rpc.testInitMintIfNeeded(6, {
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- mintAuthority: anchor.web3.Keypair.generate().publicKey,
- freezeAuthority: provider.wallet.publicKey,
- },
- signers: [mint],
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2016);
- }
- });
- it("init_if_needed throws if mint exists but has the wrong freeze authority", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- try {
- await program.rpc.testInitMintIfNeeded(6, {
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- mintAuthority: provider.wallet.publicKey,
- freezeAuthority: anchor.web3.Keypair.generate().publicKey,
- },
- signers: [mint],
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2017);
- }
- });
- it("init_if_needed throws if mint exists but has the wrong decimals", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- try {
- await program.rpc.testInitMintIfNeeded(9, {
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- mintAuthority: provider.wallet.publicKey,
- freezeAuthority: provider.wallet.publicKey,
- },
- signers: [mint],
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2018);
- }
- });
- it("init_if_needed throws if token exists but has the wrong owner", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- try {
- await program.rpc.testInitTokenIfNeeded({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- authority: anchor.web3.Keypair.generate().publicKey,
- },
- signers: [token],
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2015);
- }
- });
- it("init_if_needed throws if token exists but has the wrong mint", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const mint2 = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint2.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint2],
- });
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- try {
- await program.rpc.testInitTokenIfNeeded({
- accounts: {
- token: token.publicKey,
- mint: mint2.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- authority: provider.wallet.publicKey,
- },
- signers: [token],
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2014);
- }
- });
- it("init_if_needed throws if associated token exists but has the wrong owner", async () => {
- const mint = Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const associatedToken = await Token.getAssociatedTokenAddress(
- ASSOCIATED_TOKEN_PROGRAM_ID,
- TOKEN_PROGRAM_ID,
- mint.publicKey,
- provider.wallet.publicKey
- );
- await program.rpc.testInitAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
- },
- });
- try {
- await program.rpc.testInitAssociatedTokenIfNeeded({
- accounts: {
- token: associatedToken,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
- authority: anchor.web3.Keypair.generate().publicKey,
- },
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2015);
- }
- });
- it("init_if_needed throws if associated token exists but has the wrong mint", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const mint2 = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint2.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint2],
- });
- const associatedToken = await Token.getAssociatedTokenAddress(
- ASSOCIATED_TOKEN_PROGRAM_ID,
- TOKEN_PROGRAM_ID,
- mint.publicKey,
- provider.wallet.publicKey
- );
- await program.rpc.testInitAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
- },
- });
- try {
- await program.rpc.testInitAssociatedTokenIfNeeded({
- accounts: {
- token: associatedToken,
- mint: mint2.publicKey,
- payer: provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
- authority: provider.wallet.publicKey,
- },
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2014);
- }
- });
- it("init_if_needed throws if token exists with correct owner and mint but is not the ATA", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const associatedToken = await Token.getAssociatedTokenAddress(
- ASSOCIATED_TOKEN_PROGRAM_ID,
- TOKEN_PROGRAM_ID,
- mint.publicKey,
- provider.wallet.publicKey
- );
- await program.rpc.testInitAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
- },
- });
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- try {
- await program.rpc.testInitAssociatedTokenIfNeeded({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
- authority: provider.wallet.publicKey,
- },
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 3014);
- }
- });
- it("Can use multidimensional array", async () => {
- const array2d = new Array(10).fill(new Array(10).fill(99));
- const data = anchor.web3.Keypair.generate();
- await program.rpc.testMultidimensionalArray(array2d, {
- accounts: {
- data: data.publicKey,
- },
- signers: [data],
- instructions: [
- await program.account.dataMultidimensionalArray.createInstruction(data),
- ],
- });
- const dataAccount = await program.account.dataMultidimensionalArray.fetch(
- data.publicKey
- );
- assert.deepStrictEqual(dataAccount.data, array2d);
- });
- it("Can use multidimensional array with const sizes", async () => {
- const array2d = new Array(10).fill(new Array(11).fill(22));
- const data = anchor.web3.Keypair.generate();
- await program.rpc.testMultidimensionalArrayConstSizes(array2d, {
- accounts: {
- data: data.publicKey,
- },
- signers: [data],
- instructions: [
- await program.account.dataMultidimensionalArrayConstSizes.createInstruction(
- data
- ),
- ],
- });
- const dataAccount =
- await program.account.dataMultidimensionalArrayConstSizes.fetch(
- data.publicKey
- );
- assert.deepStrictEqual(dataAccount.data, array2d);
- });
- describe("Can validate PDAs derived from other program ids", () => {
- it("With bumps using create_program_address", async () => {
- const [firstPDA, firstBump] =
- await anchor.web3.PublicKey.findProgramAddress(
- [anchor.utils.bytes.utf8.encode("seed")],
- ASSOCIATED_TOKEN_PROGRAM_ID
- );
- const [secondPDA, secondBump] =
- await anchor.web3.PublicKey.findProgramAddress(
- [anchor.utils.bytes.utf8.encode("seed")],
- program.programId
- );
- // correct bump but wrong address
- const wrongAddress = anchor.web3.Keypair.generate().publicKey;
- try {
- await program.rpc.testProgramIdConstraint(firstBump, secondBump, {
- accounts: {
- first: wrongAddress,
- second: secondPDA,
- },
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2006);
- }
- // matching bump seed for wrong address but derived from wrong program
- try {
- await program.rpc.testProgramIdConstraint(secondBump, secondBump, {
- accounts: {
- first: secondPDA,
- second: secondPDA,
- },
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2006);
- }
- // correct inputs should lead to successful tx
- await program.rpc.testProgramIdConstraint(firstBump, secondBump, {
- accounts: {
- first: firstPDA,
- second: secondPDA,
- },
- });
- });
- it("With bumps using find_program_address", async () => {
- const firstPDA = (
- await anchor.web3.PublicKey.findProgramAddress(
- [anchor.utils.bytes.utf8.encode("seed")],
- ASSOCIATED_TOKEN_PROGRAM_ID
- )
- )[0];
- const secondPDA = (
- await anchor.web3.PublicKey.findProgramAddress(
- [anchor.utils.bytes.utf8.encode("seed")],
- program.programId
- )
- )[0];
- // random wrong address
- const wrongAddress = anchor.web3.Keypair.generate().publicKey;
- try {
- await program.rpc.testProgramIdConstraintFindPda({
- accounts: {
- first: wrongAddress,
- second: secondPDA,
- },
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2006);
- }
- // same seeds but derived from wrong program
- try {
- await program.rpc.testProgramIdConstraintFindPda({
- accounts: {
- first: secondPDA,
- second: secondPDA,
- },
- });
- expect(false).to.be.true;
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2006);
- }
- // correct inputs should lead to successful tx
- await program.rpc.testProgramIdConstraintFindPda({
- accounts: {
- first: firstPDA,
- second: secondPDA,
- },
- });
- });
- });
- describe("Token Constraint Test", () => {
- it("Token Constraint Test(no init) - Can make token::mint and token::authority", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- await program.rpc.testTokenConstraint({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- },
- });
- const mintAccount = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const account = await mintAccount.getAccountInfo(token.publicKey);
- assert.isTrue(account.owner.equals(provider.wallet.publicKey));
- assert.isTrue(account.mint.equals(mint.publicKey));
- });
- it("Token Constraint Test(no init) - Can make only token::authority", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- await program.rpc.testOnlyAuthConstraint({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- },
- });
- const mintAccount = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const account = await mintAccount.getAccountInfo(token.publicKey);
- assert.isTrue(account.owner.equals(provider.wallet.publicKey));
- });
- it("Token Constraint Test(no init) - Can make only token::mint", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- await program.rpc.testOnlyMintConstraint({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- },
- });
- const mintAccount = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const account = await mintAccount.getAccountInfo(token.publicKey);
- assert.isTrue(account.mint.equals(mint.publicKey));
- });
- it("Token Constraint Test(no init) - throws if token::mint mismatch", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const mint1 = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint1.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint1],
- });
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- try {
- await program.rpc.testTokenConstraint({
- accounts: {
- token: token.publicKey,
- mint: mint1.publicKey,
- payer: provider.wallet.publicKey,
- },
- });
- assert.isTrue(false);
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2014);
- assert.strictEqual(err.error.errorCode.code, "ConstraintTokenMint");
- }
- });
- it("Token Constraint Test(no init) - throws if token::authority mismatch", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- const fakeAuthority = Keypair.generate();
- try {
- await program.rpc.testTokenAuthConstraint({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- fakeAuthority: fakeAuthority.publicKey,
- },
- });
- assert.isTrue(false);
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2015);
- assert.strictEqual(err.error.errorCode.code, "ConstraintTokenOwner");
- }
- });
- it("Token Constraint Test(no init) - throws if both token::authority, token::mint mismatch", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const mint1 = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint1.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint1],
- });
- const token = anchor.web3.Keypair.generate();
- await program.rpc.testInitToken({
- accounts: {
- token: token.publicKey,
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [token],
- });
- const fakeAuthority = Keypair.generate();
- try {
- await program.rpc.testTokenAuthConstraint({
- accounts: {
- token: token.publicKey,
- mint: mint1.publicKey,
- fakeAuthority: fakeAuthority.publicKey,
- },
- });
- assert.isTrue(false);
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2015);
- assert.strictEqual(err.error.errorCode.code, "ConstraintTokenOwner");
- }
- });
- it("Mint Constraint Test(no init) - mint::decimals, mint::authority, mint::freeze_authority", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- await program.rpc.testMintConstraint(6, {
- accounts: {
- mint: mint.publicKey,
- mintAuthority: provider.wallet.publicKey,
- freezeAuthority: provider.wallet.publicKey,
- },
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const mintAccount = await client.getMintInfo();
- assert.strictEqual(mintAccount.decimals, 6);
- assert.isTrue(
- mintAccount.mintAuthority.equals(provider.wallet.publicKey)
- );
- assert.isTrue(
- mintAccount.freezeAuthority.equals(provider.wallet.publicKey)
- );
- });
- it("Mint Constraint Test(no init) - throws if mint::decimals mismatch", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const fakeDecimal = 5;
- try {
- await program.rpc.testMintConstraint(fakeDecimal, {
- accounts: {
- mint: mint.publicKey,
- mintAuthority: provider.wallet.publicKey,
- freezeAuthority: provider.wallet.publicKey,
- },
- });
- assert.isTrue(false);
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2018);
- assert.strictEqual(err.error.errorCode.code, "ConstraintMintDecimals");
- }
- });
- it("Mint Constraint Test(no init) - throws if mint::mint_authority mismatch", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const fakeAuthority = Keypair.generate();
- try {
- await program.rpc.testMintConstraint(6, {
- accounts: {
- mint: mint.publicKey,
- mintAuthority: fakeAuthority.publicKey,
- freezeAuthority: provider.wallet.publicKey,
- },
- });
- assert.isTrue(false);
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2016);
- assert.strictEqual(
- err.error.errorCode.code,
- "ConstraintMintMintAuthority"
- );
- }
- });
- it("Mint Constraint Test(no init) - throws if mint::freeze_authority mismatch", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- const fakeAuthority = Keypair.generate();
- try {
- await program.rpc.testMintConstraint(6, {
- accounts: {
- mint: mint.publicKey,
- mintAuthority: provider.wallet.publicKey,
- freezeAuthority: fakeAuthority.publicKey,
- },
- });
- assert.isTrue(false);
- } catch (_err) {
- assert.isTrue(_err instanceof AnchorError);
- const err: AnchorError = _err;
- assert.strictEqual(err.error.errorCode.number, 2017);
- assert.strictEqual(
- err.error.errorCode.code,
- "ConstraintMintFreezeAuthority"
- );
- }
- });
- it("Mint Constraint Test(no init) - can write only mint::decimals", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- await program.rpc.testMintOnlyDecimalsConstraint(6, {
- accounts: {
- mint: mint.publicKey,
- },
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const mintAccount = await client.getMintInfo();
- assert.strictEqual(mintAccount.decimals, 6);
- });
- it("Mint Constraint Test(no init) - can write only mint::authority and mint::freeze_authority", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- await program.rpc.testMintOnlyAuthConstraint({
- accounts: {
- mint: mint.publicKey,
- mintAuthority: provider.wallet.publicKey,
- freezeAuthority: provider.wallet.publicKey,
- },
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const mintAccount = await client.getMintInfo();
- assert.isTrue(
- mintAccount.mintAuthority.equals(provider.wallet.publicKey)
- );
- assert.isTrue(
- mintAccount.freezeAuthority.equals(provider.wallet.publicKey)
- );
- });
- it("Mint Constraint Test(no init) - can write only mint::authority", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- await program.rpc.testMintOnlyOneAuthConstraint({
- accounts: {
- mint: mint.publicKey,
- mintAuthority: provider.wallet.publicKey,
- },
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const mintAccount = await client.getMintInfo();
- assert.isTrue(
- mintAccount.mintAuthority.equals(provider.wallet.publicKey)
- );
- });
- it("Mint Constraint Test(no init) - can write only mint::decimals and mint::freeze_authority", async () => {
- const mint = anchor.web3.Keypair.generate();
- await program.rpc.testInitMint({
- accounts: {
- mint: mint.publicKey,
- payer: provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [mint],
- });
- await program.rpc.testMintMissMintAuthConstraint(6, {
- accounts: {
- mint: mint.publicKey,
- freezeAuthority: provider.wallet.publicKey,
- },
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- wallet.payer
- );
- const mintAccount = await client.getMintInfo();
- assert.strictEqual(mintAccount.decimals, 6);
- assert.isTrue(
- mintAccount.freezeAuthority.equals(provider.wallet.publicKey)
- );
- });
- it("check versioned transaction is now available", async () => {
- let thisTx = new VersionedTransaction(
- new Message({
- header: {
- numReadonlySignedAccounts: 0,
- numReadonlyUnsignedAccounts: 0,
- numRequiredSignatures: 0,
- },
- accountKeys: [new PublicKey([0]).toString()],
- instructions: [{ accounts: [0], data: "", programIdIndex: 0 }],
- recentBlockhash: "",
- })
- );
- assert.isDefined(thisTx);
- });
- });
- });
|