Skip to content

[SC-1504] Make .env loading optional with autoLoadEnv constructor flag #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions hardhat-setup/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import dotenv from 'dotenv';
import { ChainConfig } from '@nomicfoundation/hardhat-verify/src/types';
import { HardhatNetworkAccountsUserConfig, Network, NetworksUserConfig } from 'hardhat/types';

/**
* @category Hardhat-Setup
* Loads environment variables into process.env using the dotenv package.
* By default, loads variables from a `.env` file in the project root.
* You can provide custom options (e.g. a different path or encoding) via the `options` parameter.
* @param options Optional configuration object for dotenv (e.g. `{ path: '.env.local' }`).
* @see https://github.com/motdotla/dotenv#config
*/
export function loadEnv(options?: dotenv.DotenvConfigOptions): void {
dotenv.config(options);
}

/**
* @category Hardhat-Setup
* Configuration type for managing Etherscan integration in Hardhat setups.
Expand Down Expand Up @@ -72,8 +84,16 @@ export class Networks {
networks: NetworksUserConfig = {};
etherscan: Etherscan = { apiKey: {}, customChains: [] };

constructor(useHardhat: boolean = true, forkingNetworkName?: string, saveHardhatDeployments: boolean = false, forkingAccounts?: HardhatNetworkAccountsUserConfig) {
dotenv.config();
constructor(
useHardhat: boolean = true,
forkingNetworkName?: string,
saveHardhatDeployments: boolean = false,
forkingAccounts?: HardhatNetworkAccountsUserConfig,
autoLoadEnv: boolean = true
) {
if (autoLoadEnv) {
loadEnv();
}

if (useHardhat || forkingNetworkName) {
this.networks.hardhat = {
Expand All @@ -88,7 +108,12 @@ export class Networks {
}

if (forkingNetworkName) {
const { url, authKeyHttpHeader } = parseRpcEnv(process.env[`${forkingNetworkName.toUpperCase()}_RPC_URL`] || '');
const forkRpcKey = `${forkingNetworkName.toUpperCase()}_RPC_URL`;
const forkRpcEnv = process.env[forkRpcKey];
if (!forkRpcEnv) {
throw new Error(`Missing required environment variable '${forkRpcKey}'. Did you forget to call loadEnv() or set autoLoadEnv to true?`);
}
const { url, authKeyHttpHeader } = parseRpcEnv(forkRpcEnv || '');
this.networks.hardhat!.forking = {
url,
httpHeaders: authKeyHttpHeader ? { 'auth-key': authKeyHttpHeader } : undefined,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1inch/solidity-utils",
"version": "6.6.0",
"version": "6.6.1",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"exports": {
Expand Down
Loading