-
Notifications
You must be signed in to change notification settings - Fork 3
/
helper-functions.js
129 lines (117 loc) · 3.66 KB
/
helper-functions.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
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
121
122
123
124
125
126
127
128
129
const hre = require("hardhat");
const fs = require("fs");
const path = require("path");
const absoluteDirPath = path.join(__dirname);
const relativeFilePath = "./map.json";
const absoluteFilePath = path.join(__dirname, relativeFilePath);
function writeContractAddressFile(addresses) {
fs.writeFile(
absoluteFilePath,
`${JSON.stringify(addresses, undefined, 2)}\n`, // \n adds end of life, so prettier is happy
(err) => {
if (err)
console.error(`Error writing the file ${absoluteFilePath}:`, err);
}
);
}
function createAndWritePersistentContractFiles(chainId, contractName, address) {
fs.readFile(absoluteFilePath, "utf8", (err, data) => {
if (err == null) {
// File exists
const addresses = JSON.parse(data);
addresses[chainId] = {
...addresses[chainId],
[contractName]: address,
};
writeContractAddressFile(addresses);
} else if (err.code === "ENOENT") {
// File does not exist
const addresses = {
[chainId]: {
[contractName]: address,
},
};
writeContractAddressFile(addresses);
} else {
console.error(`Error return information about ${absoluteFilePath}:`, err);
}
});
}
function createContractDirectory(chainId, contractName, address) {
fs.mkdir(absoluteDirPath, (err) => {
if (err) {
console.error(`Error creating directory ${absoluteFilePath}:`, err);
} else {
createAndWritePersistentContractFiles(chainId, contractName, address);
}
});
}
exports.storeContractAddress = async (contract, contractName) => {
const { address, deployTransaction } = contract;
const { chainId } = deployTransaction;
fs.stat(absoluteDirPath, (err) => {
if (err == null) {
// Directory exists
createAndWritePersistentContractFiles(chainId, contractName, address);
} else if (err.code === "ENOENT") {
// Directory does not exist
createContractDirectory(chainId, contractName, address);
} else {
console.error(
`Error returning information about directory ${absoluteDirPath}:`,
err
);
}
});
return contract.address;
};
exports.printEtherscanLink = (address, chainId, type = "address") => {
let link;
switch (chainId) {
case 1:
link = "https://etherscan.io";
break;
case 4:
link = "https://rinkeby.etherscan.io";
break;
case 137:
link = "https://polygonscan.com";
break;
case 80001:
link = "https://mumbai.polygonscan.com";
break;
case 1337:
return;
default:
return;
}
console.log(`Inspect deployed contract at ${link}/${type}/${address}`);
};
const isLocalNetwork = () => hre.network.config.chainId === 31337;
exports.isLocalNetwork = isLocalNetwork;
exports.logMarketplaceURL = (contract, id) => {
console.log(
`View your NFT at https://testnets.opensea.io/assets/${contract.address}/${id}
or at https://rinkeby.rarible.com/token/${contract.address}:${id}`
);
};
exports.verifyContract = async (contract, args) => {
const { address } = contract;
if (isLocalNetwork() || !hre.config.etherscan.apiKey) {
return; // contract is deployed on local network or no apiKey is configured
}
console.log("Waiting 5 block confirmations...");
await contract.deployTransaction.wait(5); // needed if verifyContract() is called immediately after deployment
try {
console.log("Verifying contract...");
await hre.run("verify:verify", {
address: address,
constructorArguments: args,
});
} catch (err) {
console.error("hallo", err);
if (err.message.includes("Reason: Already Verified")) {
console.log("Contract is already verified!");
}
}
};