Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
andreogle committed Jul 5, 2023
1 parent cf2def5 commit b6e1b55
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
66 changes: 34 additions & 32 deletions src/hardhat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,60 @@ import { CHAINS } from './generated/chains';
import { toUpperSnakeCase } from './utils/strings';
import { Chain, HardhatEtherscanConfig, HardhatNetworksConfig } from './types';

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

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

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

// https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify#multiple-api-keys-and-alternative-block-explorers
export function buildEtherscanConfig(): HardhatEtherscanConfig {
// Not usable outside of a Node.js environment
if (typeof window !== 'undefined') {
return { apiKey: {}, customChains: [] };
throw new Error('Cannot be run outside of a Node.js environment');
}

return CHAINS.reduce((etherscan, chain) => {
if (!chain.explorer || !chain.explorer.api) {
return etherscan;
}
return CHAINS.reduce(
(etherscan, chain) => {
if (!chain.explorer || !chain.explorer.api) {
return etherscan;
}

const explorer = chain.explorer;
const apiKey = chain.explorer.api.key;
const apiKey = chain.explorer.api.key;

const apiKeyEnvName = buildEtherscanApiKeyName(chain);
const apiKeyValue = apiKey.required ? chain.alias : process.env[apiKeyEnvName];
const apiKeyEnvName = buildEtherscanApiKeyName(chain);
const apiKeyValue = apiKey.required ? process.env[apiKeyEnvName] || 'NOT_FOUND' : 'DUMMY_VALUE';

if (apiKey.hardhatEtherscanAlias) {
etherscan.apiKey[apiKey.hardhatEtherscanAlias] = apiKeyValue || '';
return etherscan;
}
if (apiKey.hardhatEtherscanAlias) {
etherscan.apiKey[apiKey.hardhatEtherscanAlias] = apiKeyValue;
return etherscan;
}

etherscan.customChains.push({
network: chain.alias,
chainId: Number(chain.id),
urls: {
apiURL: explorer.api?.url || '',
browserURL: explorer.browserUrl,
},
});
etherscan.customChains.push({
network: chain.alias,
chainId: Number(chain.id),
urls: {
apiURL: chain.explorer.api.url,
browserURL: chain.explorer.browserUrl,
},
});

etherscan.apiKey[chain.alias] = apiKeyValue || '';
etherscan.apiKey[chain.alias] = apiKeyValue || '';

return etherscan;
}, { apiKey: {}, customChains: [] } as HardhatEtherscanConfig);
return etherscan;
},
{ apiKey: {}, customChains: [] } as HardhatEtherscanConfig
);
}

export function buildNetworksConfig(): HardhatNetworksConfig {
// Not usable outside of a Node.js environment
if (typeof window !== 'undefined') {
return {};
throw new Error('Cannot be called outside of a Node.js environment');
}

return CHAINS.reduce((networks, chain) => {
Expand Down
9 changes: 0 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import * as hardhat from './hardhat';

// NOTE: the following file is generated with the generate-chains.ts script
export { CHAINS } from './generated/chains';

export * as hardhat from './hardhat';
export * from './types';

export function getEnvVariables(): string[] {
const hardhatEnvVariables = hardhat.buildEnvVariables();

return ['MNEMONIC', ...hardhatEnvVariables];
}

0 comments on commit b6e1b55

Please sign in to comment.