Skip to content

Commit

Permalink
Add optional env variable overrides for network RPC URLs (#53)
Browse files Browse the repository at this point in the history
* Add optional env variable overrides for network RPC URLs

* Update README

* Typo
  • Loading branch information
andreogle authored Jul 17, 2023
1 parent db59bd3 commit 3fede54
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ console.log(CHAINS);

Returns an object where the key is each chain's alias and the value is an object that can be used as the `networks` field of [`hardhat.config.js`](https://hardhat.org/hardhat-runner/docs/config).

The default `url` values can be overridden with chain specific environment variables. These environment variables take the form of `HARDHAT_HTTP_RPC_URL_${toUpperSnakeCase(chain.alias)}`. e.g. `HARDHAT_HTTP_RPC_URL_ARBITRUM_GOERLI_TESTNET`.

```ts
import { hardhatConfig } from '@api3/chains';
console.log(hardhatConfig.networks());
Expand Down Expand Up @@ -109,7 +111,7 @@ console.log(hardhatConfig.etherscan());

Returns an array of expected environment variable names for chains that have an API key required for the explorer. The array also includes a single `MNEMONIC` variable that can be used to configure all networks.

NOTE: Each `ETHERSCAN_API_KEY_` environment variable has the chain alias as a suffix, where the alias has been converted to upper snake case.
NOTE: Each `ETHERSCAN_API_KEY_` and `HARDHAT_HTTP_RPC_URL_` environment variable has the chain alias as a suffix, where the alias has been converted to upper snake case.

```ts
import { hardhatConfig } from '@api3/chains';
Expand All @@ -119,6 +121,8 @@ console.log(hardhatConfig.getEnvVariableNames());
'MNEMONIC',
'ETHERSCAN_API_KEY_ARBITRUM_GOERLI_TESTNET',
...
'HARDHAT_HTTP_RPC_URL_ARBITRUM_GOERLI_TESTNET',
...
]
*/
```
Expand Down
14 changes: 9 additions & 5 deletions src/hardhat-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ import { toUpperSnakeCase } from './utils/strings';
import { Chain, HardhatEtherscanConfig, HardhatNetworksConfig } from './types';

export function getEnvVariableNames(): string[] {
const hardhatApiKeyEnvNames = CHAINS.filter((chain) => chain.explorer?.api?.key?.required).map((chain) =>
etherscanApiKeyName(chain)
);
const apiKeyEnvNames = CHAINS.filter((chain) => chain.explorer?.api?.key?.required).map((chain) => etherscanApiKeyName(chain));

const networkRpcUrlNames = CHAINS.map((chain) => chain.providerUrl);

return ['MNEMONIC', ...hardhatApiKeyEnvNames];
return ['MNEMONIC', ...apiKeyEnvNames, ...networkRpcUrlNames];
}

export function etherscanApiKeyName(chain: Chain): string {
return `ETHERSCAN_API_KEY_${toUpperSnakeCase(chain.alias)}`;
}

export function networkHttpRpcUrlName(chain: Chain): string {
return `HARDHAT_HTTP_RPC_URL_${toUpperSnakeCase(chain.alias)}`;
}

// https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify#multiple-api-keys-and-alternative-block-explorers
export function etherscan(): HardhatEtherscanConfig {
if (typeof window !== 'undefined') {
Expand Down Expand Up @@ -62,7 +66,7 @@ export function networks(): HardhatNetworksConfig {
networks[chain.alias] = {
accounts: { mnemonic: process.env.MNEMONIC || '' },
chainId: Number(chain.id),
url: chain.providerUrl,
url: process.env[networkHttpRpcUrlName(chain)] || chain.providerUrl,
};
return networks;
}, {} as HardhatNetworksConfig);
Expand Down

0 comments on commit 3fede54

Please sign in to comment.