-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-deploy-random-ipfs-nft.js
94 lines (81 loc) · 3.45 KB
/
03-deploy-random-ipfs-nft.js
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
const { network } = require("hardhat")
const { networkConfig, developmentChains } = require("../helper-hardhat-config")
const { verify } = require("../utils/verify")
const { storeImages, storeTokenUriMetadata } = require("../utils/uploadToPinata")
const FUND_AMOUNT = "1000000000000000000000"
const imagesLocation = "./images/randomNft/"
let tokenUris = [
"ipfs://QmaVkBn2tKmjbhphU7eyztbvSQU5EXDdqRyXZtRhSGgJGo",
"ipfs://QmYQC5aGZu2PTH8XzbJrbDnvhj3gVs7ya33H9mqUNvST3d",
"ipfs://QmZYmH5iDbD6v3U2ixoVAjioSzvWJszDzYdbeCLquGSpVm",
]
const metadataTemplate = {
name: "",
description: "",
image: "",
attributes: [
{
trait_type: "Cuteness",
value: 100,
},
],
}
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy, log } = deployments
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
let vrfCoordinatorV2Address, subscriptionId, vrfCoordinatorV2Mock
if (process.env.UPLOAD_TO_PINATA == "true") {
tokenUris = await handleTokenUris()
}
if (chainId == 31337) {
vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock")
vrfCoordinatorV2Address = vrfCoordinatorV2Mock.address
const transactionResponse = await vrfCoordinatorV2Mock.createSubscription()
const transactionReceipt = await transactionResponse.wait()
subscriptionId = transactionReceipt.events[0].args.subId
await vrfCoordinatorV2Mock.fundSubscription(subscriptionId, FUND_AMOUNT)
} else {
vrfCoordinatorV2Address = networkConfig[chainId].vrfCoordinatorV2
subscriptionId = networkConfig[chainId].subscriptionId
}
log("----------------------------------------------------")
arguments = [
vrfCoordinatorV2Address,
subscriptionId,
networkConfig[chainId]["gasLane"],
networkConfig[chainId]["mintFee"],
networkConfig[chainId]["callbackGasLimit"],
tokenUris,
]
const randomIpfsNft = await deploy("RandomIpfsNft", {
from: deployer,
args: arguments,
log: true,
waitConfirmations: network.config.blockConfirmations || 1,
})
if (chainId == 31337) {
await vrfCoordinatorV2Mock.addConsumer(subscriptionId, randomIpfsNft.address)
}
if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
log("Verifying...")
await verify(randomIpfsNft.address, arguments)
}
}
async function handleTokenUris() {
tokenUris = []
const { responses: imageUploadResponses, files } = await storeImages(imagesLocation)
for (imageUploadResponseIndex in imageUploadResponses) {
let tokenUriMetadata = { ...metadataTemplate }
tokenUriMetadata.name = files[imageUploadResponseIndex].replace(".png", "")
tokenUriMetadata.description = `An adorable ${tokenUriMetadata.name} pup!`
tokenUriMetadata.image = `ipfs://${imageUploadResponses[imageUploadResponseIndex].IpfsHash}`
console.log(`Uploading ${tokenUriMetadata.name}...`)
const metadataUploadResponse = await storeTokenUriMetadata(tokenUriMetadata)
tokenUris.push(`ipfs://${metadataUploadResponse.IpfsHash}`)
}
console.log("Token URIs uploaded! They are:")
console.log(tokenUris)
return tokenUris
}
module.exports.tags = ["all", "randomipfs", "main"]