-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-mock-nfts.ts
107 lines (90 loc) · 3.31 KB
/
create-mock-nfts.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
import beer from "beer-names";
import superb from "superb";
import fs from "fs";
import { NftMetadata } from "./interfaces/metadata-interface";
import { IpfsManager } from "./interfaces/ipfs-manager";
import { ApiPromise, WsProvider, Keyring } from "@polkadot/api";
import { NftCreator } from "./nft-creator";
import { createDirSync } from "./utils";
import { CID } from "multiformats/cid";
import * as json from 'multiformats/codecs/json'
import { sha256 } from 'multiformats/hashes/sha2'
const config = require("./mock-nft-config.json");
// Color trait to randomly choose for mock nfts
const colors = [
"Red",
"Blue",
"Green",
"Yellow",
"Orange",
"Purple",
"Pink",
"Brown",
"Gray",
"White",
"Black",
"Cyan",
"Magenta",
"Turquoise"
];
// Generates mock NFT metadata files and saves them to `outputDir`
function generateMockNfts(amount: number, outputDir: string, cleanDir: boolean = true) {
// remove trailing slashes
outputDir = outputDir.replace(/\/$/, "");
createDirSync(outputDir, cleanDir);
for (let i = 1; i <= amount; i++) {
const randomName = beer.random();
// get the last word, which is the beer type (ale, lager, etc.)
const beerType: string = randomName.split(" ").slice(-1).join("");
// add superb word + beer type
let randomDescription = superb.random() + " " + beerType;
const randomColor = colors[Math.floor(Math.random() * colors.length)];
let metadata: NftMetadata = {
attributes: [
{
trait_type: "type",
value: beerType
},
{
trait_type: "color",
value: randomColor
}
],
description: randomDescription,
image: "",
name: randomName,
itemId: i
};
// pad id with 0's for file name
const fileId = i.toString().padStart(amount.toString().length, "0");
const fileName = outputDir + "/beer_nft_" + fileId + ".json";
fs.writeFileSync(fileName, JSON.stringify(metadata, null, 2));
}
}
class MockIpfs implements IpfsManager {
// Mock implementation. There is no actual uploading
async uploadContent(content: string): Promise<CID> {
const bytes = json.encode(content);
const hash = await sha256.digest(bytes);
return CID.create(1, json.code, hash);
}
async uploadFile(filePath: string): Promise<CID> {
// simple implementation, just read the file and get CID
return this.uploadContent(fs.readFileSync(filePath, 'utf8'));
}
}
async function main() {
const wsProvider = new WsProvider(config.substrateEndpoint);
const dotApi = await ApiPromise.create({ provider: wsProvider });
const keyring = new Keyring({ type: 'sr25519' });
const signer = keyring.addFromUri('//Alice');
// generate metadata for `numNfts`. Save the metadata files to `metadataDir`
generateMockNfts(config.numNfts, config.out.metadataDir);
let nftCreator = new NftCreator(config, dotApi, signer, new MockIpfs());
console.log("Creating NFT collection");
await nftCreator.createNftCollection();
console.log("Creating NFTs...");
await nftCreator.bulkCreateNfts(config.numNfts);
console.log("Done!");
}
main();