-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-nft.ts
120 lines (99 loc) · 2.69 KB
/
create-nft.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { Connection, clusterApiUrl } from "@solana/web3.js";
import {
Metaplex,
keypairIdentity,
bundlrStorage,
NftWithToken,
toMetaplexFile,
PublicKey,
} from "@metaplex-foundation/js";
import "dotenv/config";
import {
getExplorerLink,
getKeypairFromEnvironment,
} from "@solana-developers/helpers";
import * as fs from "fs";
interface NftData {
name: string;
symbol: string;
description: string;
sellerFeeBasisPoints: number;
imageFile: string;
}
const nftData = {
name: "New Solana Meme NFT",
symbol: "NSM",
description: "First Solana Meme in Romania :)",
sellerFeeBasisPoints: 0,
imageFile: "first_nft.png",
};
const updateNftData = {
name: "Update",
symbol: "UPDATE",
description: "Update Description",
sellerFeeBasisPoints: 100,
imageFile: "success.png",
};
async function uploadMetadata(
metaplex: Metaplex,
nftData: NftData,
): Promise<string> {
console.log("🚀 Uploading metadata...");
const buffer = fs.readFileSync("./" + nftData.imageFile);
const file = toMetaplexFile(buffer, nftData.imageFile);
const imageUri = await metaplex.storage().upload(file);
console.log("image uri:", imageUri);
const { uri } = await metaplex.nfts().uploadMetadata({
name: nftData.name,
symbol: nftData.symbol,
description: nftData.description,
uri: imageUri
})
console.log("Done ✅! Metadata uri:", uri);
return uri;
}
async function createNft(
metaplex: Metaplex,
uri: string,
nftData: NftData,
): Promise<NftWithToken> {
console.log("🚀 Creating NFT...");
const { nft } = await metaplex.nfts().create({
uri: uri,
name: nftData.name,
sellerFeeBasisPoints: nftData.sellerFeeBasisPoints,
symbol: nftData.symbol
}, {
commitment: "confirmed"
});
const link = getExplorerLink("address", nft.address.toString(), "devnet");
console.log(`✅ Token Mint: ${link}`);
return nft;
}
async function main() {
const connection = new Connection(clusterApiUrl("devnet"));
const user = getKeypairFromEnvironment("SECRET_KEY");
console.log(
`🔑 We've loaded our keypair securely, using an env file! Our public key is: ${user.publicKey.toBase58()}`,
);
const metaplex = new Metaplex(connection)
.use(keypairIdentity(user))
.use(
bundlrStorage({
address: "https://devnet.bundlr.network",
providerUrl: "https://api.devnet.solana.com"
})
)
const uri = await uploadMetadata(metaplex, nftData);
const nft = await createNft(metaplex, uri, nftData);
const updatedUri = await uploadMetadata(metaplex, updateNftData);
}
main()
.then(() => {
console.log("Finished successfully");
process.exit(0);
})
.catch((error) => {
console.log(error);
process.exit(1);
});