Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
fix(env): drop support for most env variables messing up
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Sep 9, 2022
1 parent 0a20bde commit 9fe33de
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .env
@@ -1,3 +1,3 @@
# Local env vars for debugging
TS_NODE_IGNORE="false"
TS_NODE_FILES="true"
TS_NODE_FILES="true"
1 change: 0 additions & 1 deletion src/cli.ts
Expand Up @@ -3,7 +3,6 @@ import colors from "colors/safe";
import commandLineArgs from "command-line-args";
import commandLineUsage from "command-line-usage";
import dotenv from "dotenv";
import { ethers } from "ethers";
import fs from "fs";
import path from "path";

Expand Down
14 changes: 5 additions & 9 deletions src/etherscan.ts
@@ -1,6 +1,4 @@
export const getEtherscanBaseUrl = (chainId: number) => {
if (process.env.ETHERSCAN_BASE_URL) return process.env.ETHERSCAN_BASE_URL;

switch (chainId) {
case 1:
return "https://api.etherscan.io";
Expand Down Expand Up @@ -51,18 +49,16 @@ export const getEtherscanBaseUrl = (chainId: number) => {
export const getEtherscanUrl = (
chainId: number,
module: string,
params: Record<string, string>,
apiKey?: string
params: Record<string, string | undefined | null>
) => {
apiKey ??= process.env.ETHERSCAN_API_KEY || "";
params["apiKey"] ??= process.env.ETHERSCAN_API_KEY || "";

const query = Object.keys(params).reduce((accum, key) => {
const value = params[key];
if (value != null) {
accum += `&${key}=${value}`;
}
if (value != null) accum += `&${key}=${value}`;

return accum;
}, "");

return `${getEtherscanBaseUrl(chainId)}/api?module=${module}${query}${apiKey}`;
return `${getEtherscanBaseUrl(chainId)}/api?module=${module}${query}`;
};
24 changes: 9 additions & 15 deletions src/fetch.ts
Expand Up @@ -13,23 +13,21 @@ import { Config, EtherscanSourceCodeResponse, isEtherscanError } from "./types";

export const fetchAbiAt = async (
address: string,
{ rpcUrl, network, apiKey, provider }: Config
{ provider, rpcUrl, network, apiKey }: Config
) => {
let chainId: number = 1;

if (!provider && !rpcUrl) {
network ??= process.env.NETWORK || "mainnet";
network ??= "mainnet";

chainId = ethers.utils.isHexString(network)
? parseInt(network, 16)
: /^\d+$/.test(network)
? Number(network)
: chainIds[network.toLowerCase()];

rpcUrl ??=
process.env.RPC_URL ||
// @ts-ignore
rpcs[chainId.toString()]?.rpcs[0];
// @ts-ignore
rpcUrl ??= rpcs[chainId.toString()]?.rpcs[0];
}

if (provider || rpcUrl) {
Expand All @@ -45,15 +43,11 @@ export const fetchAbiAt = async (
}

const { data } = await axios.get<EtherscanSourceCodeResponse>(
getEtherscanUrl(
chainId,
"contract",
{
action: "getsourcecode",
address,
},
apiKey
)
getEtherscanUrl(chainId, "contract", {
action: "getsourcecode",
address,
apiKey,
})
);
if (isEtherscanError(data)) throw new Error(data.result);

Expand Down

0 comments on commit 9fe33de

Please sign in to comment.