Skip to content

Commit

Permalink
Give default to tokenRecord for PNFTs in mintV1
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Mar 16, 2023
1 parent 64b0ddb commit 1749edc
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 10 deletions.
32 changes: 22 additions & 10 deletions clients/js/src/instructions/mintV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
publicKey,
TransactionBuilder,
} from '@metaplex-foundation/umi';
import { isFungible } from '../digitalAsset';
import { findMasterEditionPda, TokenStandard } from '../generated';
import { isFungible, isProgrammable } from '../digitalAsset';
import {
findMasterEditionPda,
findTokenRecordPda,
TokenStandard,
} from '../generated';
import {
getMintV1InstructionDataSerializer,
mintV1 as baseMintV1,
Expand Down Expand Up @@ -38,19 +42,27 @@ export const mintV1 = (
const defaultMasterEdition = isFungible(input.tokenStandard)
? undefined
: findMasterEditionPda(context, { mint: publicKey(input.mint) });
const masterEdition = input.masterEdition ?? defaultMasterEdition;
const defaultTokenOwner = input.token
? undefined
: context.identity.publicKey;
const tokenOwner = input.tokenOwner ?? defaultTokenOwner;
const token =
input.token ??
findAssociatedTokenPda(context, {
mint: publicKey(input.mint),
owner: publicKey(input.tokenOwner ?? (defaultTokenOwner as PublicKey)),
});
const defaultTokenRecord = isProgrammable(input.tokenStandard)
? findTokenRecordPda(context, { mint: publicKey(input.mint), token })
: undefined;
const tokenRecord = input.tokenRecord ?? defaultTokenRecord;

return baseMintV1(context, {
masterEdition: input.masterEdition ?? defaultMasterEdition,
...input,
tokenOwner: input.tokenOwner ?? defaultTokenOwner,
token:
input.token ??
findAssociatedTokenPda(context, {
mint: publicKey(input.mint),
owner: publicKey(input.tokenOwner ?? (defaultTokenOwner as PublicKey)),
}),
masterEdition,
tokenOwner,
token,
tokenRecord,
});
};
226 changes: 226 additions & 0 deletions clients/js/test/createHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import { fetchMint, Mint } from '@metaplex-foundation/mpl-essentials';
import {
generateSigner,
none,
percentAmount,
publicKey,
some,
} from '@metaplex-foundation/umi';
import test from 'ava';
import {
createFungible,
createFungibleAsset,
createNft,
createProgrammableNft,
fetchMasterEdition,
fetchMetadata,
findMasterEditionPda,
findMetadataPda,
MasterEdition,
Metadata,
programmableConfig,
TokenStandard,
} from '../src';
import { createUmi } from './_setup';

test('it can create a new NonFungible', async (t) => {
// Given a new mint Signer.
const umi = await createUmi();
const mint = generateSigner(umi);
const masterEdition = findMasterEditionPda(umi, { mint: mint.publicKey });

// When we create a new NonFungible at this address.
await createNft(umi, {
mint,
name: 'My NFT',
uri: 'https://example.com/my-nft.json',
sellerFeeBasisPoints: percentAmount(5.5),
}).sendAndConfirm(umi);

// Then a Mint account was created with 1 supply.
const mintAccount = await fetchMint(umi, mint.publicKey);
t.like(mintAccount, <Mint>{
publicKey: publicKey(mint),
supply: 1n,
decimals: 0,
mintAuthority: some(publicKey(masterEdition)),
freezeAuthority: some(publicKey(masterEdition)),
});

// And a Metadata account was created.
const metadata = findMetadataPda(umi, { mint: mint.publicKey });
const metadataAccount = await fetchMetadata(umi, metadata);
t.like(metadataAccount, <Metadata>{
publicKey: publicKey(metadata),
updateAuthority: publicKey(umi.identity),
mint: publicKey(mint),
tokenStandard: some(TokenStandard.NonFungible),
name: 'My NFT',
uri: 'https://example.com/my-nft.json',
sellerFeeBasisPoints: 550,
primarySaleHappened: false,
isMutable: true,
creators: some([
{ address: publicKey(umi.identity), verified: true, share: 100 },
]),
collection: none(),
uses: none(),
collectionDetails: none(),
programmableConfig: none(),
});

// And a MasterEdition account was created.
const masterEditionAccount = await fetchMasterEdition(umi, masterEdition);
t.like(masterEditionAccount, <MasterEdition>{
publicKey: publicKey(masterEdition),
supply: 0n,
maxSupply: some(0n),
});
});

test('it can create a new ProgrammableNonFungible', async (t) => {
// Given a new mint Signer.
const umi = await createUmi();
const mint = generateSigner(umi);

// When we create a new ProgrammableNonFungible at this address.
await createProgrammableNft(umi, {
mint,
name: 'My Programmable NFT',
uri: 'https://example.com/my-programmable-nft.json',
sellerFeeBasisPoints: percentAmount(5.5),
}).sendAndConfirm(umi);

// Then a Mint account was created with 1 supply.
const mintAccount = await fetchMint(umi, mint.publicKey);
const masterEdition = findMasterEditionPda(umi, { mint: mint.publicKey });
t.like(mintAccount, <Mint>{
publicKey: publicKey(mint),
supply: 1n,
decimals: 0,
mintAuthority: some(publicKey(masterEdition)),
freezeAuthority: some(publicKey(masterEdition)),
});

// And a Metadata account was created.
const metadata = findMetadataPda(umi, { mint: mint.publicKey });
const metadataAccount = await fetchMetadata(umi, metadata);
t.like(metadataAccount, <Metadata>{
publicKey: publicKey(metadata),
updateAuthority: publicKey(umi.identity),
mint: publicKey(mint),
tokenStandard: some(TokenStandard.ProgrammableNonFungible),
name: 'My Programmable NFT',
uri: 'https://example.com/my-programmable-nft.json',
sellerFeeBasisPoints: 550,
primarySaleHappened: false,
isMutable: true,
creators: some([
{ address: publicKey(umi.identity), verified: true, share: 100 },
]),
collection: none(),
uses: none(),
collectionDetails: none(),
programmableConfig: some(programmableConfig('V1', { ruleSet: none() })),
});

// And a MasterEdition account was created.
const masterEditionAccount = await fetchMasterEdition(umi, masterEdition);
t.like(masterEditionAccount, <MasterEdition>{
publicKey: publicKey(masterEdition),
supply: 0n,
maxSupply: some(0n),
});
});

test('it can create a new Fungible', async (t) => {
// Given a new mint Signer.
const umi = await createUmi();
const mint = generateSigner(umi);

// When we create a new Fungible at this address.
await createFungible(umi, {
mint,
name: 'My Fungible',
uri: 'https://example.com/my-fungible.json',
sellerFeeBasisPoints: percentAmount(5.5),
}).sendAndConfirm(umi);

// Then a Mint account was created with no supply.
const mintAccount = await fetchMint(umi, mint.publicKey);
t.like(mintAccount, <Mint>{
publicKey: publicKey(mint),
supply: 0n,
decimals: 0,
mintAuthority: some(publicKey(umi.identity)),
freezeAuthority: some(publicKey(umi.identity)),
});

// And a Metadata account was created.
const metadata = findMetadataPda(umi, { mint: mint.publicKey });
const metadataAccount = await fetchMetadata(umi, metadata);
t.like(metadataAccount, <Metadata>{
publicKey: publicKey(metadata),
updateAuthority: publicKey(umi.identity),
mint: publicKey(mint),
tokenStandard: some(TokenStandard.Fungible),
name: 'My Fungible',
uri: 'https://example.com/my-fungible.json',
sellerFeeBasisPoints: 550,
primarySaleHappened: false,
isMutable: true,
creators: some([
{ address: publicKey(umi.identity), verified: true, share: 100 },
]),
collection: none(),
uses: none(),
collectionDetails: none(),
programmableConfig: none(),
});
});

test('it can create a new FungibleAsset', async (t) => {
// Given a new mint Signer.
const umi = await createUmi();
const mint = generateSigner(umi);

// When we create a new FungibleAsset at this address.
await createFungibleAsset(umi, {
mint,
name: 'My Fungible Asset',
uri: 'https://example.com/my-fungible-asset.json',
sellerFeeBasisPoints: percentAmount(5.5),
}).sendAndConfirm(umi);

// Then a Mint account was created with no supply.
const mintAccount = await fetchMint(umi, mint.publicKey);
t.like(mintAccount, <Mint>{
publicKey: publicKey(mint),
supply: 0n,
decimals: 0,
mintAuthority: some(publicKey(umi.identity)),
freezeAuthority: some(publicKey(umi.identity)),
});

// And a Metadata account was created.
const metadata = findMetadataPda(umi, { mint: mint.publicKey });
const metadataAccount = await fetchMetadata(umi, metadata);
t.like(metadataAccount, <Metadata>{
publicKey: publicKey(metadata),
updateAuthority: publicKey(umi.identity),
mint: publicKey(mint),
tokenStandard: some(TokenStandard.FungibleAsset),
name: 'My Fungible Asset',
uri: 'https://example.com/my-fungible-asset.json',
sellerFeeBasisPoints: 550,
primarySaleHappened: false,
isMutable: true,
creators: some([
{ address: publicKey(umi.identity), verified: true, share: 100 },
]),
collection: none(),
uses: none(),
collectionDetails: none(),
programmableConfig: none(),
});
});

0 comments on commit 1749edc

Please sign in to comment.