Skip to content

Commit aea0024

Browse files
committed
Add project 6, NFTs
1 parent ecb355e commit aea0024

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
createNft,
3+
fetchDigitalAsset,
4+
mplTokenMetadata,
5+
} from "@metaplex-foundation/mpl-token-metadata";
6+
7+
import {
8+
airdropIfRequired,
9+
getExplorerLink,
10+
getKeypairFromFile,
11+
} from "@solana-developers/helpers";
12+
13+
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
14+
15+
import { Connection, LAMPORTS_PER_SOL, clusterApiUrl } from "@solana/web3.js";
16+
import {
17+
generateSigner,
18+
keypairIdentity,
19+
percentAmount,
20+
} from "@metaplex-foundation/umi";
21+
22+
const connection = new Connection(clusterApiUrl("devnet"));
23+
24+
const user = await getKeypairFromFile();
25+
26+
await airdropIfRequired(
27+
connection,
28+
user.publicKey,
29+
1 * LAMPORTS_PER_SOL,
30+
0.5 * LAMPORTS_PER_SOL
31+
);
32+
33+
console.log("Loaded user", user.publicKey.toBase58());
34+
35+
const umi = createUmi(connection.rpcEndpoint);
36+
umi.use(mplTokenMetadata());
37+
38+
const umiUser = umi.eddsa.createKeypairFromSecretKey(user.secretKey);
39+
umi.use(keypairIdentity(umiUser));
40+
41+
console.log("Set up Umi instance for user");
42+
43+
const collectionMint = generateSigner(umi);
44+
45+
const transaction = await createNft(umi, {
46+
mint: collectionMint,
47+
name: "My Collection",
48+
symbol: "MC",
49+
uri: "https://raw.githubusercontent.com/solana-developers/professional-education/main/labs/sample-nft-collection-offchain-data.json",
50+
sellerFeeBasisPoints: percentAmount(0),
51+
isCollection: true,
52+
});
53+
await transaction.sendAndConfirm(umi);
54+
55+
const createdCollectionNft = await fetchDigitalAsset(
56+
umi,
57+
collectionMint.publicKey
58+
);
59+
60+
console.log(
61+
`Created Collection 📦! Address is ${getExplorerLink(
62+
"address",
63+
createdCollectionNft.mint.publicKey,
64+
"devnet"
65+
)}`
66+
);

project-6-nfts/create-nft.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {
2+
createNft,
3+
fetchDigitalAsset,
4+
mplTokenMetadata,
5+
} from "@metaplex-foundation/mpl-token-metadata";
6+
7+
import {
8+
airdropIfRequired,
9+
getExplorerLink,
10+
getKeypairFromFile,
11+
} from "@solana-developers/helpers";
12+
13+
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
14+
15+
import { Connection, LAMPORTS_PER_SOL, clusterApiUrl } from "@solana/web3.js";
16+
import {
17+
generateSigner,
18+
keypairIdentity,
19+
percentAmount,
20+
publicKey,
21+
} from "@metaplex-foundation/umi";
22+
23+
const connection = new Connection(clusterApiUrl("devnet"));
24+
25+
const user = await getKeypairFromFile();
26+
27+
await airdropIfRequired(
28+
connection,
29+
user.publicKey,
30+
1 * LAMPORTS_PER_SOL,
31+
0.5 * LAMPORTS_PER_SOL
32+
);
33+
34+
console.log("Loaded user", user.publicKey.toBase58());
35+
36+
const umi = createUmi(connection.rpcEndpoint);
37+
umi.use(mplTokenMetadata());
38+
39+
const umiUser = umi.eddsa.createKeypairFromSecretKey(user.secretKey);
40+
umi.use(keypairIdentity(umiUser));
41+
42+
console.log("Set up Umi instance for user");
43+
44+
const collectionAddress = publicKey(
45+
"ChfGtd2wT12c2u82PHNpe4PdQ5PMqJnVECfaNbQ2uaVw"
46+
);
47+
48+
console.log(`Creating NFT...`);
49+
50+
const mint = generateSigner(umi);
51+
52+
const transaction = await createNft(umi, {
53+
mint,
54+
name: "My NFT",
55+
uri: "https://raw.githubusercontent.com/solana-developers/professional-education/main/labs/sample-nft-offchain-data.json",
56+
sellerFeeBasisPoints: percentAmount(0),
57+
collection: {
58+
key: collectionAddress,
59+
verified: false,
60+
},
61+
});
62+
63+
await transaction.sendAndConfirm(umi);
64+
65+
const createdNft = await fetchDigitalAsset(umi, mint.publicKey);
66+
67+
console.log(
68+
`🖼️ Created NFT! Address is ${getExplorerLink(
69+
"address",
70+
createdNft.mint.publicKey,
71+
"devnet"
72+
)}`
73+
);

project-6-nfts/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "new-nft",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"keywords": [],
9+
"author": "",
10+
"license": "ISC",
11+
"description": "",
12+
"dependencies": {
13+
"@metaplex-foundation/mpl-token-metadata": "^3.2.1",
14+
"@metaplex-foundation/umi-bundle-defaults": "^0.9.2",
15+
"@solana-developers/helpers": "^2.5.2",
16+
"esrun": "^3.2.26"
17+
}
18+
}

project-6-nfts/verify-nft.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {
2+
findMetadataPda,
3+
mplTokenMetadata,
4+
verifyCollectionV1,
5+
} from "@metaplex-foundation/mpl-token-metadata";
6+
7+
import {
8+
airdropIfRequired,
9+
getExplorerLink,
10+
getKeypairFromFile,
11+
} from "@solana-developers/helpers";
12+
13+
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
14+
15+
import { Connection, LAMPORTS_PER_SOL, clusterApiUrl } from "@solana/web3.js";
16+
import { keypairIdentity, publicKey } from "@metaplex-foundation/umi";
17+
18+
const connection = new Connection(clusterApiUrl("devnet"));
19+
20+
const user = await getKeypairFromFile();
21+
22+
await airdropIfRequired(
23+
connection,
24+
user.publicKey,
25+
1 * LAMPORTS_PER_SOL,
26+
0.5 * LAMPORTS_PER_SOL
27+
);
28+
29+
console.log("Loaded user", user.publicKey.toBase58());
30+
31+
const umi = createUmi(connection.rpcEndpoint);
32+
umi.use(mplTokenMetadata());
33+
34+
const umiUser = umi.eddsa.createKeypairFromSecretKey(user.secretKey);
35+
umi.use(keypairIdentity(umiUser));
36+
37+
console.log("Set up Umi instance for user");
38+
39+
// We could also dp
40+
const collectionAddress = publicKey(
41+
"ChfGtd2wT12c2u82PHNpe4PdQ5PMqJnVECfaNbQ2uaVw"
42+
);
43+
44+
const nftAddress = publicKey("84uZmnRpzfa77w3KtyagjWXA8g1yE6vP8ReuHyN5HZaN");
45+
46+
const transaction = await verifyCollectionV1(umi, {
47+
metadata: findMetadataPda(umi, { mint: nftAddress }),
48+
collectionMint: collectionAddress,
49+
authority: umi.identity,
50+
});
51+
52+
transaction.sendAndConfirm(umi);
53+
54+
console.log(
55+
`✅ NFT ${nftAddress} verified as member of collection ${collectionAddress}! See Explorer at ${getExplorerLink(
56+
"address",
57+
nftAddress,
58+
"devnet"
59+
)}`
60+
);

0 commit comments

Comments
 (0)