Skip to content

Commit

Permalink
template object for hardhat.config
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 committed Dec 19, 2024
1 parent 7e32370 commit 095389d
Showing 1 changed file with 87 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,120 +1,102 @@
import { withDefaults } from "../../../../utils.js";
import { withDefaults, stringify } from "../../../../utils.js";

const contents = ({ imports, solidityVersion, networks, compilers }) => {
function deepMerge(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();

const massagedCompilers = compilers[0]?.[0] ? JSON.stringify(compilers[0]) : '';

const defaultCompilers = `[
{
version: "${solidityVersion[0]}",
settings: {
optimizer: {
enabled: true,
// https://docs.soliditylang.org/en/latest/using-the-compiler.html#optimizer-options
runs: 200,
},
},
},
]`;
if (source && typeof source === 'object') {
for (const key in source) {
if (source[key] && typeof source[key] === 'object') {
if (!target[key]) {
target[key] = Array.isArray(source[key]) ? [] : {};
}
deepMerge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
}

return `import * as dotenv from "dotenv";
dotenv.config();
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-ethers";
import "@nomicfoundation/hardhat-chai-matchers";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";
import "@nomicfoundation/hardhat-verify";
import "hardhat-deploy";
import "hardhat-deploy-ethers";
import { task } from "hardhat/config";
import generateTsAbis from "./scripts/generateTsAbis";
${imports.filter(Boolean).join("\n")}
// If not set, it uses ours Alchemy's default API key.
// You can get your own at https://dashboard.alchemyapi.io
const providerApiKey = process.env.ALCHEMY_API_KEY || "oKxs-03sij-U_N0iOlrSsZFr29-IqbuF";
// If not set, it uses the hardhat account 0 private key.
// You can generate a random account with \`yarn generate\` or \`yarn account:import\` to import your existing PK
const deployerPrivateKey =
process.env.__RUNTIME_DEPLOYER_PRIVATE_KEY ?? "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
// If not set, it uses our block explorers default API keys.
const etherscanApiKey = process.env.ETHERSCAN_MAINNET_API_KEY || "DNXJA8RX2Q3VZ4URQIWP7Z68CJXQZSC6AW";
const etherscanOptimisticApiKey = process.env.ETHERSCAN_OPTIMISTIC_API_KEY || "RM62RDISS1RH448ZY379NX625ASG1N633R";
const basescanApiKey = process.env.BASESCAN_API_KEY || "ZZZEIPMT1MNJ8526VV2Y744CA7TNZR64G6";
return deepMerge(target, ...sources);
}

const config: HardhatUserConfig = {
const defaultConfig = {
solidity: {
compilers: ${massagedCompilers || defaultCompilers}
compilers: [
{
version: "0.8.20",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
]
},
defaultNetwork: "localhost",
namedAccounts: {
deployer: {
// By default, it will take the first Hardhat account as the deployer
default: 0,
},
},
networks: {
${networks[0] && `${networks[0]},`}
// View the networks that are pre-configured.
// If the network you are looking for is not here you can add new network settings
hardhat: {
forking: {
url: \`https://eth-mainnet.alchemyapi.io/v2/\${providerApiKey}\`,
url: `https://eth-mainnet.alchemyapi.io/v2/\${providerApiKey}`,
enabled: process.env.MAINNET_FORKING_ENABLED === "true",
},
},
mainnet: {
url: \`https://eth-mainnet.alchemyapi.io/v2/\${providerApiKey}\`,
url: `https://eth-mainnet.alchemyapi.io/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
},
sepolia: {
url: \`https://eth-sepolia.g.alchemy.com/v2/\${providerApiKey}\`,
url: `https://eth-sepolia.g.alchemy.com/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
},
arbitrum: {
url: \`https://arb-mainnet.g.alchemy.com/v2/\${providerApiKey}\`,
url: `https://arb-mainnet.g.alchemy.com/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
},
arbitrumSepolia: {
url: \`https://arb-sepolia.g.alchemy.com/v2/\${providerApiKey}\`,
url: `https://arb-sepolia.g.alchemy.com/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
},
optimism: {
url: \`https://opt-mainnet.g.alchemy.com/v2/\${providerApiKey}\`,
url: `https://opt-mainnet.g.alchemy.com/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
verify: {
etherscan: {
apiUrl: "https://api-optimistic.etherscan.io",
apiKey: etherscanOptimisticApiKey,
apiKey: "etherscanOptimisticApiKey",
},
},
},
optimismSepolia: {
url: \`https://opt-sepolia.g.alchemy.com/v2/\${providerApiKey}\`,
url: `https://opt-sepolia.g.alchemy.com/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
verify: {
etherscan: {
apiUrl: "https://api-sepolia-optimistic.etherscan.io",
apiKey: etherscanOptimisticApiKey,
apiKey: "etherscanOptimisticApiKey",
},
},
},
polygon: {
url: \`https://polygon-mainnet.g.alchemy.com/v2/\${providerApiKey}\`,
url: `https://polygon-mainnet.g.alchemy.com/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
},
polygonMumbai: {
url: \`https://polygon-mumbai.g.alchemy.com/v2/\${providerApiKey}\`,
url: `https://polygon-mumbai.g.alchemy.com/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
},
polygonZkEvm: {
url: \`https://polygonzkevm-mainnet.g.alchemy.com/v2/\${providerApiKey}\`,
url: `https://polygonzkevm-mainnet.g.alchemy.com/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
},
polygonZkEvmTestnet: {
url: \`https://polygonzkevm-testnet.g.alchemy.com/v2/\${providerApiKey}\`,
url: `https://polygonzkevm-testnet.g.alchemy.com/v2/\${providerApiKey}`,
accounts: [deployerPrivateKey],
},
gnosis: {
Expand All @@ -131,7 +113,7 @@ const config: HardhatUserConfig = {
verify: {
etherscan: {
apiUrl: "https://api.basescan.org",
apiKey: basescanApiKey,
apiKey: "basescanApiKey",
},
},
},
Expand All @@ -141,7 +123,7 @@ const config: HardhatUserConfig = {
verify: {
etherscan: {
apiUrl: "https://api-sepolia.basescan.org",
apiKey: basescanApiKey,
apiKey: "basescanApiKey",
},
},
},
Expand Down Expand Up @@ -170,21 +152,56 @@ const config: HardhatUserConfig = {
accounts: [deployerPrivateKey],
},
},
// configuration for harhdat-verify plugin
etherscan: {
apiKey: \`\${etherscanApiKey}\`,
apiKey: `\${etherscanApiKey}`,
},
// configuration for etherscan-verify from hardhat-deploy plugin
verify: {
etherscan: {
apiKey: \`\${etherscanApiKey}\`,
apiKey: `\${etherscanApiKey}`,
},
},
sourcify: {
enabled: false,
},
};

const contents = ({ preConfigContent, configOverride }) => {
// Merge the default config with any overrides
const finalConfig = deepMerge({}, defaultConfig);

if (configOverride) {
deepMerge(finalConfig, configOverride);
}

return `import * as dotenv from "dotenv";
dotenv.config();
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-ethers";
import "@nomicfoundation/hardhat-chai-matchers";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";
import "@nomicfoundation/hardhat-verify";
import "hardhat-deploy";
import "hardhat-deploy-ethers";
import { task } from "hardhat/config";
import generateTsAbis from "./scripts/generateTsAbis";
${preConfigContent[0] || ''}
// If not set, it uses ours Alchemy's default API key.
// You can get your own at https://dashboard.alchemyapi.io
const providerApiKey = process.env.ALCHEMY_API_KEY || "oKxs-03sij-U_N0iOlrSsZFr29-IqbuF";
// If not set, it uses the hardhat account 0 private key.
// You can generate a random account with \`yarn generate\` or \`yarn account:import\` to import your existing PK
const deployerPrivateKey =
process.env.__RUNTIME_DEPLOYER_PRIVATE_KEY ?? "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
// If not set, it uses our block explorers default API keys.
const etherscanApiKey = process.env.ETHERSCAN_MAINNET_API_KEY || "DNXJA8RX2Q3VZ4URQIWP7Z68CJXQZSC6AW";
const etherscanOptimisticApiKey = process.env.ETHERSCAN_OPTIMISTIC_API_KEY || "RM62RDISS1RH448ZY379NX625ASG1N633R";
const basescanApiKey = process.env.BASESCAN_API_KEY || "ZZZEIPMT1MNJ8526VV2Y744CA7TNZR64G6";
const config: HardhatUserConfig = ${stringify(finalConfig)};
// Extend the deploy task
task("deploy").setAction(async (args, hre, runSuper) => {
// Run the original deploy task
Expand All @@ -193,14 +210,10 @@ task("deploy").setAction(async (args, hre, runSuper) => {
await generateTsAbis(hre);
});
export default config;`
export default config;`;
};

export default withDefaults(contents, {
imports: "",
solidityVersion: "0.8.20",
networks: "",
// set solidity compilers
// https://hardhat.org/hardhat-runner/docs/advanced/multiple-solidity-versions#multiple-solidity-versions
compilers: [],
preConfigContent: "",
configOverrides: {},
});

0 comments on commit 095389d

Please sign in to comment.