1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690 |
- const anchor = require("@project-serum/anchor");
- const assert = require("assert");
- const {
- ASSOCIATED_TOKEN_PROGRAM_ID,
- TOKEN_PROGRAM_ID,
- Token,
- } = require("@solana/spl-token");
- const miscIdl = require("../target/idl/misc.json");
- const {
- SystemProgram,
- Keypair,
- PublicKey,
- SYSVAR_RENT_PUBKEY,
- } = require("@solana/web3.js");
- const utf8 = anchor.utils.bytes.utf8;
- describe("misc", () => {
- // Configure the client to use the local cluster.
- anchor.setProvider(anchor.Provider.env());
- const program = anchor.workspace.Misc;
- const misc2Program = anchor.workspace.Misc2;
- it("Can allocate extra space for a state constructor", async () => {
- 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.ok(state.v.equals(Buffer.from([])));
- assert.ok(accountInfo.data.length === 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,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [data],
- instructions: [await program.account.data.createInstruction(data)],
- }
- );
- const dataAccount = await program.account.data.fetch(data.publicKey);
- assert.ok(dataAccount.udata.eq(new anchor.BN(1234)));
- assert.ok(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,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [data],
- instructions: [await program.account.dataU16.createInstruction(data)],
- });
- const dataAccount = await program.account.dataU16.fetch(data.publicKey);
- assert.ok(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.ok(accInfo.executable);
- });
- it("Can use the owner constraint", async () => {
- await program.rpc.testOwner({
- accounts: {
- data: data.publicKey,
- misc: program.programId,
- },
- });
- await assert.rejects(
- async () => {
- await program.rpc.testOwner({
- accounts: {
- data: program.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 assert.rejects(
- async () => {
- await program.rpc.testExecutable({
- accounts: {
- program: 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: program.provider.wallet.publicKey,
- },
- });
- let stateAccount = await misc2Program.state.fetch();
- assert.ok(stateAccount.data.eq(oldData));
- assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
- const newData = new anchor.BN(2134);
- await program.rpc.testStateCpi(newData, {
- accounts: {
- authority: program.provider.wallet.publicKey,
- cpiState: await misc2Program.state.address(),
- misc2Program: misc2Program.programId,
- },
- });
- stateAccount = await misc2Program.state.fetch();
- assert.ok(stateAccount.data.eq(newData));
- assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
- });
- it("Can retrieve events when simulating a transaction", async () => {
- const resp = await program.simulate.testSimulate(44);
- const expectedRaw = [
- "Program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS invoke [1]",
- "Program log: Instruction: TestSimulate",
- "Program data: NgyCA9omwbMsAAAA",
- "Program data: fPhuIELK/k7SBAAA",
- "Program data: jvbowsvlmkcJAAAA",
- "Program data: zxM5neEnS1kBAgMEBQYHCAkK",
- "Program data: g06Ei2GL1gIBAgMEBQYHCAkKCw==",
- "Program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS consumed 5320 of 200000 compute units",
- "Program Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS success",
- ];
- assert.deepStrictEqual(expectedRaw, resp.raw);
- assert.ok(resp.events[0].name === "E1");
- assert.ok(resp.events[0].data.data === 44);
- assert.ok(resp.events[1].name === "E2");
- assert.ok(resp.events[1].data.data === 1234);
- assert.ok(resp.events[2].name === "E3");
- assert.ok(resp.events[2].data.data === 9);
- assert.ok(resp.events[3].name === "E5");
- assert.deepStrictEqual(
- resp.events[3].data.data,
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- );
- assert.ok(resp.events[4].name === "E6");
- assert.deepStrictEqual(
- resp.events[4].data.data,
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
- );
- });
- let dataI8;
- it("Can use i8 in the idl", async () => {
- dataI8 = anchor.web3.Keypair.generate();
- await program.rpc.testI8(-3, {
- accounts: {
- data: dataI8.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- instructions: [await program.account.dataI8.createInstruction(dataI8)],
- signers: [dataI8],
- });
- const dataAccount = await program.account.dataI8.fetch(dataI8.publicKey);
- assert.ok(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,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- instructions: [await program.account.dataI16.createInstruction(data)],
- signers: [data],
- });
- const dataAccount = await program.account.dataI16.fetch(data.publicKey);
- assert.ok(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.ok(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,
- },
- });
- assert.ok(false);
- } catch (err) {
- const errMsg = "A close constraint was violated";
- assert.equal(err.toString(), errMsg);
- assert.equal(err.msg, errMsg);
- assert.equal(err.code, 2011);
- }
- });
- it("Can close an account", async () => {
- const openAccount = await program.provider.connection.getAccountInfo(
- data.publicKey
- );
- assert.ok(openAccount !== null);
- let beforeBalance = (
- await program.provider.connection.getAccountInfo(
- program.provider.wallet.publicKey
- )
- ).lamports;
- await program.rpc.testClose({
- accounts: {
- data: data.publicKey,
- solDest: program.provider.wallet.publicKey,
- },
- });
- let afterBalance = (
- await program.provider.connection.getAccountInfo(
- program.provider.wallet.publicKey
- )
- ).lamports;
- // Retrieved rent exemption sol.
- assert.ok(afterBalance > beforeBalance);
- const closedAccount = await program.provider.connection.getAccountInfo(
- data.publicKey
- );
- assert.ok(closedAccount === null);
- });
- 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: program.provider.wallet.publicKey,
- foo,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- });
- const myPdaAccount = await program.account.dataU16.fetch(myPda);
- assert.ok(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: program.provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- });
- const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
- assert.ok(myPdaAccount.data === 9);
- assert.ok(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: program.provider.wallet.publicKey,
- },
- });
- const myPdaAccount = await program.account.dataZeroCopy.fetch(myPda);
- assert.ok(myPdaAccount.data === 1234);
- assert.ok(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: program.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,
- program.provider.wallet.payer
- );
- const account = await mintAccount.getAccountInfo(myPda);
- assert.ok(account.state === 1);
- assert.ok(account.amount.toNumber() === 0);
- assert.ok(account.isInitialized);
- assert.ok(account.owner.equals(program.provider.wallet.publicKey));
- assert.ok(account.mint.equals(mint));
- });
- it("Can execute a fallback function", async () => {
- await assert.rejects(
- async () => {
- await anchor.utils.rpc.invoke(program.programId);
- },
- (err) => {
- assert.ok(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: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data],
- });
- const account = await program.account.dataI8.fetch(data.publicKey);
- assert.ok(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: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data],
- instructions: [
- anchor.web3.SystemProgram.transfer({
- fromPubkey: program.provider.wallet.publicKey,
- toPubkey: data.publicKey,
- lamports: 4039280,
- }),
- ],
- });
- const account = await program.account.dataI8.fetch(data.publicKey);
- assert.ok(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: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data],
- });
- const account = await program.account.dataZeroCopy.fetch(data.publicKey);
- assert.ok(account.data === 10);
- assert.ok(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: program.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,
- program.provider.wallet.payer
- );
- const mintAccount = await client.getMintInfo();
- assert.ok(mintAccount.decimals === 6);
- assert.ok(
- mintAccount.mintAuthority.equals(program.provider.wallet.publicKey)
- );
- assert.ok(
- mintAccount.freezeAuthority.equals(program.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: program.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: program.provider.wallet.publicKey,
- toPubkey: mint.publicKey,
- lamports: 4039280,
- }),
- ],
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- program.provider.wallet.payer
- );
- const mintAccount = await client.getMintInfo();
- assert.ok(mintAccount.decimals === 6);
- assert.ok(
- mintAccount.mintAuthority.equals(program.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: program.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,
- program.provider.wallet.payer
- );
- const account = await client.getAccountInfo(token.publicKey);
- assert.ok(account.state === 1);
- assert.ok(account.amount.toNumber() === 0);
- assert.ok(account.isInitialized);
- assert.ok(account.owner.equals(program.provider.wallet.publicKey));
- assert.ok(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: program.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: program.provider.wallet.publicKey,
- toPubkey: token.publicKey,
- lamports: 4039280,
- }),
- ],
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- program.provider.wallet.payer
- );
- const account = await client.getAccountInfo(token.publicKey);
- assert.ok(account.state === 1);
- assert.ok(account.amount.toNumber() === 0);
- assert.ok(account.isInitialized);
- assert.ok(account.owner.equals(program.provider.wallet.publicKey));
- assert.ok(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: program.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: program.provider.wallet.publicKey,
- toPubkey: token.publicKey,
- lamports: 1,
- }),
- ],
- });
- const client = new Token(
- program.provider.connection,
- mint.publicKey,
- TOKEN_PROGRAM_ID,
- program.provider.wallet.payer
- );
- const account = await client.getAccountInfo(token.publicKey);
- assert.ok(account.state === 1);
- assert.ok(account.amount.toNumber() === 0);
- assert.ok(account.isInitialized);
- assert.ok(account.owner.equals(program.provider.wallet.publicKey));
- assert.ok(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: program.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.equal(account1.data, 1);
- const account2 = await program.account.data.fetch(data2.publicKey);
- assert.equal(account2.udata, 2);
- assert.equal(account2.idata, 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,
- program.provider.wallet.payer,
- program.provider.wallet.publicKey,
- program.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,
- program.provider.wallet.publicKey
- );
- await program.rpc.testInitAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: localClient.publicKey,
- payer: program.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);
- assert.ok(account.state === 1);
- assert.ok(account.amount.toNumber() === 0);
- assert.ok(account.isInitialized);
- assert.ok(account.owner.equals(program.provider.wallet.publicKey));
- assert.ok(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: program.provider.wallet.publicKey,
- },
- });
- let otherMint = await Token.createMint(
- program.provider.connection,
- program.provider.wallet.payer,
- program.provider.wallet.publicKey,
- program.provider.wallet.publicKey,
- 9,
- TOKEN_PROGRAM_ID
- );
- await assert.rejects(
- async () => {
- await program.rpc.testValidateAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: otherMint.publicKey,
- wallet: program.provider.wallet.publicKey,
- },
- });
- },
- (err) => {
- assert.equal(err.code, 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: program.provider.wallet.publicKey,
- },
- });
- await localClient.setAuthority(
- associatedToken,
- anchor.web3.Keypair.generate().publicKey,
- "AccountOwner",
- program.provider.wallet.payer,
- []
- );
- await assert.rejects(
- async () => {
- await program.rpc.testValidateAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: localClient.publicKey,
- wallet: program.provider.wallet.publicKey,
- },
- });
- },
- (err) => {
- assert.equal(err.code, 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 anotherProgram = new anchor.Program(
- miscIdl,
- program.programId,
- new anchor.Provider(
- program.provider.connection,
- new anchor.Wallet(anchor.web3.Keypair.generate()),
- { commitment: program.provider.connection.commitment }
- )
- );
- // Request airdrop for secondary wallet.
- const signature = await program.provider.connection.requestAirdrop(
- anotherProgram.provider.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: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data1],
- }),
- program.rpc.testFetchAll(filterable1, {
- accounts: {
- data: data2.publicKey,
- authority: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data2],
- }),
- program.rpc.testFetchAll(filterable2, {
- accounts: {
- data: data3.publicKey,
- authority: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- signers: [data3],
- }),
- anotherProgram.rpc.testFetchAll(filterable1, {
- accounts: {
- data: data4.publicKey,
- authority: anotherProgram.provider.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(
- program.provider.wallet.publicKey.toBuffer()
- );
- const allAccountsFilteredByProgramFilters1 =
- await program.account.dataWithFilter.all([
- {
- memcmp: {
- offset: 8,
- bytes: program.provider.wallet.publicKey.toBase58(),
- },
- },
- { memcmp: { offset: 40, bytes: filterable1.toBase58() } },
- ]);
- const allAccountsFilteredByProgramFilters2 =
- await program.account.dataWithFilter.all([
- {
- memcmp: {
- offset: 8,
- bytes: program.provider.wallet.publicKey.toBase58(),
- },
- },
- { memcmp: { offset: 40, bytes: filterable2.toBase58() } },
- ]);
- // Without filters there should be 4 accounts.
- assert.equal(allAccounts.length, 4);
- // Filtering by main wallet there should be 3 accounts.
- assert.equal(allAccountsFilteredByBuffer.length, 3);
- // Filtering all the main wallet accounts and matching the filterable1 value
- // results in a 2 accounts.
- assert.equal(allAccountsFilteredByProgramFilters1.length, 2);
- // Filtering all the main wallet accounts and matching the filterable2 value
- // results in 1 account.
- assert.equal(allAccountsFilteredByProgramFilters2.length, 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: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- },
- });
- await program.rpc.testEmptySeedsConstraint({
- accounts: {
- pda: pda,
- },
- });
- const [pda2, bump2] = await PublicKey.findProgramAddress(
- ["non-empty"],
- program.programId
- );
- await assert.rejects(
- program.rpc.testEmptySeedsConstraint({
- accounts: {
- pda: pda2,
- },
- }),
- (err) => {
- assert.equal(err.code, 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: program.provider.wallet.publicKey,
- },
- signers: [ifNeededAcc],
- });
- const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
- assert.equal(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: program.provider.wallet.publicKey,
- },
- signers: [ifNeededAcc],
- });
- const account = await program.account.dataU16.fetch(ifNeededAcc.publicKey);
- assert.equal(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,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- 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,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- 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(
- miscIdl.constants.find(
- (c) => c.name === "BASE" && c.type === "u128" && c.value === "1_000_000"
- ) !== undefined
- );
- });
- it("Should include DECIMALS const in IDL", async () => {
- assert(
- miscIdl.constants.find(
- (c) => c.name === "DECIMALS" && c.type === "u8" && c.value === "6"
- ) !== undefined
- );
- });
- it("Should not include NO_IDL const in IDL", async () => {
- assert.equal(
- miscIdl.constants.find((c) => c.name === "NO_IDL"),
- undefined
- );
- });
- 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: program.provider.wallet.publicKey,
- owner: program.programId,
- },
- });
- try {
- await program.rpc.testInitIfNeededChecksOwner({
- accounts: {
- data: newAcc[0],
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: program.provider.wallet.publicKey,
- owner: anchor.web3.Keypair.generate().publicKey,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.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: program.provider.wallet.publicKey,
- owner: anchor.web3.Keypair.generate().publicKey,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.provider.wallet.publicKey,
- },
- signers: [newAcc],
- });
- try {
- await program.rpc.testInitIfNeeded(_irrelevantForTest, {
- accounts: {
- data: newAcc.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- payer: program.provider.wallet.publicKey,
- },
- signers: [newAcc],
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.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: program.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: program.provider.wallet.publicKey,
- },
- signers: [mint],
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.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: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- mintAuthority: program.provider.wallet.publicKey,
- freezeAuthority: anchor.web3.Keypair.generate().publicKey,
- },
- signers: [mint],
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.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: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- mintAuthority: program.provider.wallet.publicKey,
- freezeAuthority: program.provider.wallet.publicKey,
- },
- signers: [mint],
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.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: program.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: program.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],
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.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: program.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: program.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: program.provider.wallet.publicKey,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- authority: program.provider.wallet.publicKey,
- },
- signers: [token],
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.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,
- program.provider.wallet.publicKey
- );
- await program.rpc.testInitAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: mint.publicKey,
- payer: program.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: program.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,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.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: program.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,
- program.provider.wallet.publicKey
- );
- await program.rpc.testInitAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: mint.publicKey,
- payer: program.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: program.provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
- authority: program.provider.wallet.publicKey,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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: program.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,
- program.provider.wallet.publicKey
- );
- await program.rpc.testInitAssociatedToken({
- accounts: {
- token: associatedToken,
- mint: mint.publicKey,
- payer: program.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: program.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: program.provider.wallet.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
- authority: program.provider.wallet.publicKey,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 3014);
- }
- });
- it("init_if_needed checks rent_exemption if init is not needed", async () => {
- const data = anchor.web3.Keypair.generate();
- await program.rpc.initDecreaseLamports({
- accounts: {
- data: data.publicKey,
- user: anchor.getProvider().wallet.publicKey,
- systemProgram: SystemProgram.programId,
- },
- signers: [data],
- });
- try {
- await program.rpc.initIfNeededChecksRentExemption({
- accounts: {
- data: data.publicKey,
- user: anchor.getProvider().wallet.publicKey,
- systemProgram: SystemProgram.programId,
- },
- signers: [data],
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 2005);
- }
- });
- 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,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- 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,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [data],
- instructions: [
- await program.account.dataMultidimensionalArrayConstSizes.createInstruction(
- data
- ),
- ],
- });
- const dataAccount =
- await program.account.dataMultidimensionalArrayConstSizes.fetch(
- data.publicKey
- );
- assert.deepStrictEqual(dataAccount.data, array2d);
- });
- it("allows non-rent exempt accounts", async () => {
- const data = anchor.web3.Keypair.generate();
- await program.rpc.initializeNoRentExempt({
- accounts: {
- data: data.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [data],
- instructions: [
- SystemProgram.createAccount({
- programId: program.programId,
- space: 8 + 16 + 16,
- lamports:
- await program.provider.connection.getMinimumBalanceForRentExemption(
- 39
- ),
- fromPubkey: anchor.getProvider().wallet.publicKey,
- newAccountPubkey: data.publicKey,
- }),
- ],
- });
- await program.rpc.testNoRentExempt({
- accounts: {
- data: data.publicKey,
- },
- });
- });
- it("allows rent exemption to be skipped", async () => {
- const data = anchor.web3.Keypair.generate();
- await program.rpc.initializeSkipRentExempt({
- accounts: {
- data: data.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [data],
- instructions: [
- SystemProgram.createAccount({
- programId: program.programId,
- space: 8 + 16 + 16,
- lamports:
- await program.provider.connection.getMinimumBalanceForRentExemption(
- 39
- ),
- fromPubkey: anchor.getProvider().wallet.publicKey,
- newAccountPubkey: data.publicKey,
- }),
- ],
- });
- });
- it("can use rent_exempt to enforce rent exemption", async () => {
- const data = anchor.web3.Keypair.generate();
- await program.rpc.initializeSkipRentExempt({
- accounts: {
- data: data.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- },
- signers: [data],
- instructions: [
- SystemProgram.createAccount({
- programId: program.programId,
- space: 8 + 16 + 16,
- lamports:
- await program.provider.connection.getMinimumBalanceForRentExemption(
- 39
- ),
- fromPubkey: anchor.getProvider().wallet.publicKey,
- newAccountPubkey: data.publicKey,
- }),
- ],
- });
- try {
- await program.rpc.testEnforceRentExempt({
- accounts: {
- data: data.publicKey,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(2005, err.code);
- assert.equal("A rent exempt constraint was violated", err.msg);
- }
- });
- 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,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 2006);
- }
- // matching bump seed for wrong address but derived from wrong program
- try {
- await program.rpc.testProgramIdConstraint(secondBump, secondBump, {
- accounts: {
- first: secondPDA,
- second: secondPDA,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 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,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 2006);
- }
- // same seeds but derived from wrong program
- try {
- await program.rpc.testProgramIdConstraintFindPda({
- accounts: {
- first: secondPDA,
- second: secondPDA,
- },
- });
- assert.ok(false);
- } catch (err) {
- assert.equal(err.code, 2006);
- }
- // correct inputs should lead to successful tx
- await program.rpc.testProgramIdConstraintFindPda({
- accounts: {
- first: firstPDA,
- second: secondPDA,
- },
- });
- });
- });
- });
|