Skip to content

Commit 03a4abb

Browse files
committed
implement hardhat.config.js generator
1 parent baf8ef8 commit 03a4abb

File tree

7 files changed

+107
-13
lines changed

7 files changed

+107
-13
lines changed

helpers/cleanUpFiles.ts

+17
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,21 @@ export const cleanUpFiles = () => {
2929
recursive: true,
3030
force: true,
3131
});
32+
fs.rmSync(path.join(process.cwd(), "index.ts"), {
33+
force: true,
34+
});
35+
36+
fs.rmSync(path.join(process.cwd(), "interfaces"), {
37+
recursive: true,
38+
force: true,
39+
});
40+
41+
fs.rmSync(path.join(process.cwd(), "tsconfig.json"), {
42+
force: true,
43+
});
44+
45+
fs.rmSync(path.join(process.cwd(), "images"), {
46+
recursive: true,
47+
force: true,
48+
});
3249
};

helpers/createEnv.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import fs from "fs";
22
import { APIKeys } from "../interfaces/dappInfo";
33
import path from "path";
44

5-
export const createEnv = (apiKeys: APIKeys, projectPath = "./") => {
5+
export const createEnv = (apiKeys: APIKeys, projectPath = "./", exposed = true) => {
66

77
console.log(apiKeys)
88

99
if (Object.keys(apiKeys).length) {
1010
const writeStream = fs.createWriteStream(path.join(projectPath, ".env"));
1111
for (const [key, value] of Object.entries(apiKeys)) {
12-
writeStream.write(`NEXT_PUBLIC_${key.toUpperCase()}= ${value}`);
12+
writeStream.write(exposed ? `NEXT_PUBLIC_${key.toUpperCase()}= ${value}\n` : `${key.toUpperCase()}= ${value}\n`);
1313
}
1414
writeStream.end();
1515
}

helpers/createPackage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "fs";
22
import { execSync } from "child_process";
33
import chalk from "chalk";
44
import cliProgress from "cli-progress";
5-
import { selfDestroy } from "./selfDestroy";
5+
import { selfDestroy } from "./selfDestroy.js";
66

77
interface packageData{
88
isEVM: boolean,
@@ -55,7 +55,7 @@ export const createPackageJson = async ( projectName: string, {isEVM, useBackend
5555
}
5656

5757
if (useBackend) {
58-
58+
5959
switch (backendProvider) {
6060
case "hardhat":
6161
packageJson["devDependencies"]["@nomicfoundation/hardhat-toolbox"] = "^1.0.2";

helpers/generateAlchemyUrl.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export const generateAlchemyURL = (chain, apiKey): string => {
2+
let rpcUrl = "";
3+
4+
switch (chain) {
5+
case "ethereum":
6+
rpcUrl =
7+
"`https://eth-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
8+
break;
9+
case "goerli":
10+
rpcUrl =
11+
"`https://eth-goerli.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
12+
break;
13+
case "polygon":
14+
rpcUrl =
15+
"`https://polygon-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
16+
break;
17+
case "mumbai":
18+
rpcUrl =
19+
"`https://polygon-mumbai.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
20+
break;
21+
case "arbitrum":
22+
rpcUrl =
23+
"`https://arb-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
24+
break;
25+
case "arbitrum-goerli":
26+
rpcUrl =
27+
"`https://arb-goerli.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
28+
break;
29+
case "optimism":
30+
rpcUrl =
31+
"`https://opt-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
32+
break;
33+
case "optimism-goerli":
34+
rpcUrl =
35+
"`https://opt-goerli.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
36+
break;
37+
case "solana":
38+
rpcUrl =
39+
"`https://solana-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
40+
break;
41+
case "solana-devnet":
42+
rpcUrl =
43+
"`https://solana-devnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`";
44+
break;
45+
}
46+
47+
return rpcUrl;
48+
};

helpers/setupHardhat.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import path from "path";
44
import { createEnv } from "./createEnv.js";
55
import fse from "fs-extra";
66
import { dappInfo } from "../interfaces/dappInfo.js";
7+
import { createWriteStream } from "fs";
8+
import { generateAlchemyURL } from "./generateAlchemyUrl.js";
79

810
export const setUpHardhat = (dappInfo: dappInfo) => {
911
const bar2 = new cliProgress.SingleBar(
@@ -18,9 +20,40 @@ export const setUpHardhat = (dappInfo: dappInfo) => {
1820
fse.copySync(hardhatTemplate, path.join(process.cwd(), "backend"));
1921

2022
if (dappInfo.apiKeys) {
21-
createEnv(dappInfo.apiKeys, path.join(process.cwd(), "backend"));
23+
createEnv(dappInfo.apiKeys, path.join(process.cwd(), "backend"), false);
2224
}
2325

26+
const writeStream = createWriteStream(path.join(process.cwd(), "backend", "hardhat.config.js"))
27+
28+
writeStream.write("require('@nomicfoundation/hardhat-toolbox');\n");
29+
writeStream.write("require('dotenv').config()\n");
30+
31+
writeStream.write("// Remove the quotes to get started \n");
32+
const modules = {
33+
solidity: "0.8.9",
34+
networks: {
35+
hardhat: {},
36+
[dappInfo.chain]: {
37+
accounts: "[`0x${process.env.PRIVATE_KEY}`]",
38+
url: generateAlchemyURL(dappInfo.chain, dappInfo.apiKeys.alchemy_api_key)
39+
}
40+
},
41+
42+
}
43+
44+
if (dappInfo.isTestnet) {
45+
modules.networks[dappInfo.testnet!] = {
46+
accounts: "[`0x${process.env.PRIVATE_KEY}`]",
47+
url: generateAlchemyURL(dappInfo.testnet, dappInfo.apiKeys.alchemy_api_key)
48+
}
49+
}
50+
51+
52+
53+
writeStream.write(`module.exports = ${JSON.stringify(modules, null, "\t").replace(/"([^"]+)":/g, '$1:')}`)
54+
writeStream.close()
55+
56+
2457
bar2.update(100);
2558
bar2.stop();
2659
console.log(chalk.green("Smart Contract Development Environment copied ✅"));

index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ async function run() {
9393
isEVM: true,
9494
isTestnet: false,
9595
useBackend: false,
96+
backendProvider: "",
9697
wantsTemplateFiles: false,
9798
apiKeys: {}
9899
};
@@ -184,7 +185,7 @@ async function run() {
184185
message: "Choose a Blockchain development environment:",
185186
choices: [
186187
{ title: "Hardhat", value: "hardhat" },
187-
{ title: "Foundry", value: "foundry" },
188+
{ title: "Foundry (not yet supported)", value: "foundry" },
188189
],
189190
initial: 0,
190191
}).then((data) => (dappInfo.backendProvider = data.backendType));
@@ -198,7 +199,8 @@ async function run() {
198199
initial: "demo",
199200
}).then((data) => data.apiKey);
200201

201-
dappInfo.apiKeys["alchemy_api_key"]= alchemyAPIKey
202+
dappInfo.apiKeys["alchemy_api_key"] = alchemyAPIKey
203+
dappInfo.apiKeys["private_key"] = "none"
202204

203205
mkdir(resolvedProjectPath);
204206
cloneRepo(resolvedProjectPath, dappInfo);

templates/hardhat/hardhat.config.js

-6
This file was deleted.

0 commit comments

Comments
 (0)