Skip to content

Commit

Permalink
Use Umi for NFTs, metaplex-foundation/js is deprecated and no longer …
Browse files Browse the repository at this point in the history
…functional
  • Loading branch information
mikemaccana committed Aug 22, 2024
1 parent 242cedc commit 7fa1424
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 665 deletions.
71 changes: 71 additions & 0 deletions labs/create-metaplex-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// See https://developers.metaplex.com/token-metadata/collections
import {
createNft,
fetchDigitalAsset,
mplTokenMetadata,
} from "@metaplex-foundation/mpl-token-metadata";
import {
airdropIfRequired,
getExplorerLink,
getKeypairFromFile,
} from "@solana-developers/helpers";
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
import {
generateSigner,
keypairIdentity,
percentAmount,
} from "@metaplex-foundation/umi";
import { Connection, LAMPORTS_PER_SOL, clusterApiUrl } from "@solana/web3.js";

// create a new connection to the cluster's API
const connection = new Connection(clusterApiUrl("devnet"));

// initialize a keypair for the user
const user = await getKeypairFromFile();

await airdropIfRequired(
connection,
user.publicKey,
1 * LAMPORTS_PER_SOL,
0.1 * LAMPORTS_PER_SOL
);

console.log("Loaded user:", user.publicKey.toBase58());

// Create Umi Instance, using the same endpoint as our connection,
// and using our user to sign transactions
const umi = createUmi(connection.rpcEndpoint);
umi.use(mplTokenMetadata());
const umiKeypair = umi.eddsa.createKeypairFromSecretKey(user.secretKey);
umi.use(keypairIdentity(umiKeypair));

console.log(`Creating collection...`);
// This mint is like a factory for creating NFTs
// Except it only makes one NFT, and it's a collection!
const collectionMint = generateSigner(umi);
const transaction = await createNft(umi, {
mint: collectionMint,
name: "My Collection",
symbol: "MC",
// https://developers.metaplex.com/token-metadata/token-standard#the-non-fungible-standard
uri: "https://raw.githubusercontent.com/solana-developers/professional-education/main/labs/sample-nft-collection-offchain-data.json",
sellerFeeBasisPoints: percentAmount(0),
isCollection: true,
});

await transaction.sendAndConfirm(umi);

const createdCollectionNft = await fetchDigitalAsset(
umi,
collectionMint.publicKey
);

console.log(
`Created collection 📦! Address is: ${getExplorerLink(
"address",
createdCollectionNft.mint.publicKey,
"devnet"
)}`
);

console.log("✅ Finished successfully!");
91 changes: 0 additions & 91 deletions labs/create-metaplex-nft-collection.ts

This file was deleted.

121 changes: 48 additions & 73 deletions labs/create-metaplex-nft.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
// Adapted from https://github.com/Unboxed-Software/solana-metaplex/blob/solution/src/index.ts
// and updated to work with the latest version of the Metaplex SDK

// See https://developers.metaplex.com/token-metadata
// and https://developers.metaplex.com/token-metadata/collections#associating-nfts-to-collection-nfts
import {
Connection,
clusterApiUrl,
PublicKey,
LAMPORTS_PER_SOL,
} from "@solana/web3.js";
createNft,
fetchDigitalAsset,
mplTokenMetadata,
} from "@metaplex-foundation/mpl-token-metadata";
import {
getKeypairFromFile,
airdropIfRequired,
getExplorerLink,
getKeypairFromFile,
} from "@solana-developers/helpers";
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
import {
Metaplex,
generateSigner,
keypairIdentity,
irysStorage,
toMetaplexFile,
} from "@metaplex-foundation/js";
import { readFileSync } from "fs";
percentAmount,
publicKey,
} from "@metaplex-foundation/umi";
import {
Connection,
LAMPORTS_PER_SOL,
PublicKey,
clusterApiUrl,
} from "@solana/web3.js";

// create a new connection to the cluster's API
const connection = new Connection(clusterApiUrl("devnet"));
Expand All @@ -34,72 +39,42 @@ await airdropIfRequired(

console.log("Loaded user:", user.publicKey.toBase58());

// metaplex set up
const metaplex = Metaplex.make(connection)
.use(keypairIdentity(user))
.use(
irysStorage({
address: "https://devnet.irys.xyz",
providerUrl: "https://api.devnet.solana.com",
timeout: 60000,
})
);

// Substitute in your collection NFT address from create-metaplex-nft-collection.ts
const collectionNftAddress = new PublicKey("YOUR_COLLECTION_NFT_ADDRESS_HERE");
// Create Umi Instance, using the same endpoint as our connection,
// and using our user to sign transactions
const umi = createUmi(connection.rpcEndpoint).use(mplTokenMetadata());
const umiKeypair = umi.eddsa.createKeypairFromSecretKey(user.secretKey);
umi.use(keypairIdentity(umiKeypair));

// example data for a new NFT
const nftData = {
name: "Name",
symbol: "SYMBOL",
description: "Description",
sellerFeeBasisPoints: 0,
imageFile: "nft.png",
};

// Load the file into Metaplex
const buffer = readFileSync(nftData.imageFile);
const file = toMetaplexFile(buffer, nftData.imageFile);

// upload image and get image uri
const imageUri = await metaplex.storage().upload(file);
console.log("image uri:", imageUri);
const collectionAddress = new PublicKey(
"GyddqwoWKffNjgLZZwENHvjaegQqdy3wmiEuumGeiEvn"
);

// upload metadata and get metadata uri (off chain metadata)
const uploadMetadataOutput = await metaplex.nfts().uploadMetadata({
name: nftData.name,
symbol: nftData.symbol,
description: nftData.description,
image: imageUri,
// Generate an NFT
console.log(`Creating NFT...`);
const mint = generateSigner(umi);
const transaction = await createNft(umi, {
mint,
name: "My NFT",
// https://developers.metaplex.com/token-metadata/token-standard#the-non-fungible-standard
uri: "https://raw.githubusercontent.com/solana-developers/professional-education/main/labs/sample-nft-offchain-data.json",
sellerFeeBasisPoints: percentAmount(0),
collection: {
// See https://developers.metaplex.com/umi/public-keys-and-signers
key: publicKey(collectionAddress),
verified: false,
},
});

const metadataUri = uploadMetadataOutput.uri;
await transaction.sendAndConfirm(umi);

// create an NFT using the URI from the metadata
const createNftOutput = await metaplex.nfts().create(
{
uri: metadataUri, // metadata URI
name: nftData.name,
sellerFeeBasisPoints: nftData.sellerFeeBasisPoints,
symbol: nftData.symbol,
collection: collectionNftAddress,
},
{ commitment: "finalized" }
);
const nft = createNftOutput.nft;
const createdNft = await fetchDigitalAsset(umi, mint.publicKey);

console.log(
`Token Mint: https://explorer.solana.com/address/${nft.address.toString()}?cluster=devnet`
`✨🖼️ Created NFT! Address is: ${getExplorerLink(
"address",
createdNft.mint.publicKey,
"devnet"
)}`
);

await metaplex.nfts().verifyCollection({
// Verify our collection as a Certified Collection
// See https://developers.metaplex.com/token-metadata/collections
mintAddress: nft.mint.address,
collectionMintAddress: collectionNftAddress,
isSizedCollection: true,
});

console.log(`Created NFT address is`, nft.address.toString());

console.log("✅ Finished successfully!");
Loading

0 comments on commit 7fa1424

Please sign in to comment.