Skip to content

Commit

Permalink
Fixing wrong check and adding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Jan 19, 2024
1 parent 4f1f016 commit efd6173
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 5 deletions.
5 changes: 4 additions & 1 deletion clients/js/test/initialize.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-await-in-loop */
import { generateSigner } from '@metaplex-foundation/umi';
import { generateSigner, none } from '@metaplex-foundation/umi';
import test from 'ava';
import {
AssociatedInscription,
Expand Down Expand Up @@ -59,6 +59,7 @@ test('it can initialize an Inscription account', async (t) => {
shardDataBefore.count * BigInt(32) + BigInt(shardDataBefore.shardNumber),
updateAuthorities: [umi.identity.publicKey],
associatedInscriptions: [] as AssociatedInscription[],
mint: none(),
});

const jsonData = await umi.rpc.getAccount(inscriptionAccount.publicKey);
Expand Down Expand Up @@ -120,6 +121,7 @@ test('it can initialize multiple Inscription accounts', async (t) => {
BigInt(shardDataBefore.shardNumber),
updateAuthorities: [umi.identity.publicKey],
associatedInscriptions: [] as AssociatedInscription[],
mint: none(),
});

const jsonData = await umi.rpc.getAccount(inscriptionAccount[i].publicKey);
Expand Down Expand Up @@ -178,6 +180,7 @@ test('it can initialize an Inscription account with separate authority', async (
shardDataBefore.count * BigInt(32) + BigInt(shardDataBefore.shardNumber),
updateAuthorities: [authority.publicKey],
associatedInscriptions: [] as AssociatedInscription[],
mint: none(),
});

const jsonData = await umi.rpc.getAccount(inscriptionAccount.publicKey);
Expand Down
4 changes: 3 additions & 1 deletion clients/js/test/initializeFromMint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateSigner, percentAmount } from '@metaplex-foundation/umi';
import { generateSigner, percentAmount, some } from '@metaplex-foundation/umi';
import test from 'ava';
import {
TokenStandard,
Expand Down Expand Up @@ -84,6 +84,7 @@ test('it can initialize a Mint Inscription account', async (t) => {
shardDataBefore.count * BigInt(32) + BigInt(shardDataBefore.shardNumber),
updateAuthorities: [umi.identity.publicKey],
associatedInscriptions: [] as AssociatedInscription[],
mint: some(mint.publicKey),
});

const jsonData = await umi.rpc.getAccount(inscriptionAccount[0]);
Expand Down Expand Up @@ -208,6 +209,7 @@ test('it can initialize a Mint Inscription account with separate authority', asy
shardDataBefore.count * BigInt(32) + BigInt(shardDataBefore.shardNumber),
updateAuthorities: [authority.publicKey],
associatedInscriptions: [] as AssociatedInscription[],
mint: some(mint.publicKey),
});

const jsonData = await umi.rpc.getAccount(inscriptionAccount[0]);
Expand Down
188 changes: 188 additions & 0 deletions clients/js/test/setMint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { TransactionBuilder, generateSigner, percentAmount, some } from '@metaplex-foundation/umi';
import test from 'ava';
import {
TokenStandard,
createV1,
mintV1,
mplTokenMetadata,
} from '@metaplex-foundation/mpl-token-metadata';
import {
AssociatedInscription,
DataType,
InscriptionMetadata,
Key,
fetchInscriptionMetadata,
findInscriptionMetadataPda,
findMintInscriptionPda,
initialize,
initializeFromMint,
setMint,
} from '../src';
import { createUmi } from './_setup';

test('it can set the mint on a Mint Inscription account', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
umi.use(mplTokenMetadata());

const mint = generateSigner(umi);
await createV1(umi, {
mint,
name: 'My NFT',
uri: 'https://arweave.net/LcjCf-NDr5bhCJ0YMKGlc8m8qT_J6TDWtIuW8lbu0-A',
sellerFeeBasisPoints: percentAmount(5.5),
tokenStandard: TokenStandard.NonFungible,
}).sendAndConfirm(umi);

await mintV1(umi, {
mint: mint.publicKey,
tokenStandard: TokenStandard.NonFungible,
}).sendAndConfirm(umi);

const inscriptionAccount = await findMintInscriptionPda(umi, {
mint: mint.publicKey,
});
const inscriptionMetadataAccount = await findInscriptionMetadataPda(umi, {
inscriptionAccount: inscriptionAccount[0],
});

// const asset = await fetchDigitalAsset(umi, mint.publicKey);

let builder = new TransactionBuilder();

// When we create a new account.
builder = builder.append(initializeFromMint(umi, {
mintAccount: mint.publicKey,
}));

// Set the mint on the account.
builder = builder.append(setMint(umi, {
mintInscriptionAccount: inscriptionAccount,
inscriptionMetadataAccount,
mintAccount: mint.publicKey
}));

await builder.sendAndConfirm(umi);

// Then an account was created with the correct data.
const inscriptionMetadata = await fetchInscriptionMetadata(
umi,
inscriptionMetadataAccount
);

t.like(inscriptionMetadata, <InscriptionMetadata>{
key: Key.MintInscriptionMetadataAccount,
inscriptionAccount: inscriptionAccount[0],
bump: inscriptionMetadataAccount[1],
dataType: DataType.Uninitialized,
updateAuthorities: [umi.identity.publicKey],
associatedInscriptions: [] as AssociatedInscription[],
mint: some(mint.publicKey),
});
});

test('it cannot set the mint on an Inscription account', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
umi.use(mplTokenMetadata());

const mint = generateSigner(umi);
await createV1(umi, {
mint,
name: 'My NFT',
uri: 'https://arweave.net/LcjCf-NDr5bhCJ0YMKGlc8m8qT_J6TDWtIuW8lbu0-A',
sellerFeeBasisPoints: percentAmount(5.5),
tokenStandard: TokenStandard.NonFungible,
}).sendAndConfirm(umi);

await mintV1(umi, {
mint: mint.publicKey,
tokenStandard: TokenStandard.NonFungible,
}).sendAndConfirm(umi);

const inscriptionAccount = generateSigner(umi);

const inscriptionMetadataAccount = await findInscriptionMetadataPda(umi, {
inscriptionAccount: inscriptionAccount.publicKey,
});

let builder = new TransactionBuilder();

// When we create a new account.
builder = builder.append(initialize(umi, {
inscriptionAccount,
}));

// Set the mint on the account.
builder = builder.append(setMint(umi, {
mintInscriptionAccount: inscriptionAccount.publicKey,
inscriptionMetadataAccount,
mintAccount: mint.publicKey,
}));

const promise = builder.sendAndConfirm(umi);
// Then an error is thrown.
await t.throwsAsync(promise, { name: 'DerivedKeyInvalid' });
});

test('it cannot set the wrong mint on a Mint Inscription account', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
umi.use(mplTokenMetadata());

const mint = generateSigner(umi);
await createV1(umi, {
mint,
name: 'My NFT',
uri: 'https://arweave.net/LcjCf-NDr5bhCJ0YMKGlc8m8qT_J6TDWtIuW8lbu0-A',
sellerFeeBasisPoints: percentAmount(5.5),
tokenStandard: TokenStandard.NonFungible,
}).sendAndConfirm(umi);

await mintV1(umi, {
mint: mint.publicKey,
tokenStandard: TokenStandard.NonFungible,
}).sendAndConfirm(umi);

const wrongMint = generateSigner(umi);
await createV1(umi, {
mint: wrongMint,
name: 'My NFT',
uri: 'https://arweave.net/LcjCf-NDr5bhCJ0YMKGlc8m8qT_J6TDWtIuW8lbu0-A',
sellerFeeBasisPoints: percentAmount(5.5),
tokenStandard: TokenStandard.NonFungible,
}).sendAndConfirm(umi);

await mintV1(umi, {
mint: wrongMint.publicKey,
tokenStandard: TokenStandard.NonFungible,
}).sendAndConfirm(umi);

const inscriptionAccount = await findMintInscriptionPda(umi, {
mint: mint.publicKey,
});
const inscriptionMetadataAccount = await findInscriptionMetadataPda(umi, {
inscriptionAccount: inscriptionAccount[0],
});

// const asset = await fetchDigitalAsset(umi, mint.publicKey);

let builder = new TransactionBuilder();

// When we create a new account.
builder = builder.append(initializeFromMint(umi, {
mintAccount: mint.publicKey,
}));

// Set the mint on the account.
builder = builder.append(setMint(umi, {
mintInscriptionAccount: inscriptionAccount,
inscriptionMetadataAccount,
mintAccount: wrongMint.publicKey
}));

const promise = builder.sendAndConfirm(umi);

// Then an error is thrown.
await t.throwsAsync(promise, { name: 'DerivedKeyInvalid' });
});
4 changes: 1 addition & 3 deletions programs/mpl-inscription/src/processor/set_mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ pub(crate) fn process_set_mint<'a>(accounts: &'a [AccountInfo<'a>]) -> ProgramRe
let ctx = &SetMintAccounts::context(accounts)?;

// Check that the account is already initialized.
if (ctx.accounts.mint_inscription_account.owner != &crate::ID)
|| ctx.accounts.mint_inscription_account.data_is_empty()
{
if ctx.accounts.mint_inscription_account.owner != &crate::ID {
return Err(MplInscriptionError::NotInitialized.into());
}

Expand Down

0 comments on commit efd6173

Please sign in to comment.