Skip to content
Closed
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
1 change: 1 addition & 0 deletions packages/lib-sourcify/src/SourcifyChain/SourcifyChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class SourcifyChain {
readonly etherscanApi?: {
supported: boolean;
apiKeyEnvName?: string;
url?: string;
};

private static rpcTimeout: number = 10 * 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type SourcifyChainExtension = {
etherscanApi?: {
supported: boolean;
apiKeyEnvName?: string;
url?: string; // Custom base URL for Etherscan-compatible APIs (e.g. "https://block-explorer-api.testnet.battlechain.com")
};
fetchContractCreationTxUsing?: FetchContractCreationTxMethods;
rpc?: Array<string | BaseRPC | APIKeyRPC | FetchRequestRPC>;
Expand Down
5 changes: 4 additions & 1 deletion packages/lib-sourcify/src/utils/etherscan/etherscan-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ export const fetchFromEtherscan = async (
chainId: number | string,
address: string,
apiKey: string,
customBaseUrl?: string,
): Promise<EtherscanResult> => {
const url = `https://api.etherscan.io/v2/api?chainid=${chainId}&module=contract&action=getsourcecode&address=${address}&apikey=`;
const url = customBaseUrl
? `${customBaseUrl}/api?module=contract&action=getsourcecode&address=${address}&apikey=`
: `https://api.etherscan.io/v2/api?chainid=${chainId}&module=contract&action=getsourcecode&address=${address}&apikey=`;
const secretUrl = url + apiKey;
const maskedUrl = url + (apiKey ? apiKey.slice(0, 6) + '...' : '');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,20 @@ function getBlockscoutScrapeContractCreatorFetcher(
function getEtherscanApiContractCreatorFetcher(
apiKey: string,
chainId: number,
customBaseUrl?: string,
): ContractCreationFetcher {
const baseUrl = customBaseUrl
? `${customBaseUrl}/api?module=contract&action=getcontractcreation&contractaddresses=\${ADDRESS}&apikey=`
: ETHERSCAN_API.replace("${CHAIN_ID}", chainId.toString());
return getApiContractCreationFetcher(
ETHERSCAN_API.replace("${CHAIN_ID}", chainId.toString()) + apiKey,
baseUrl + apiKey,
(response: any) => {
if (response?.message === "NOTOK")
throw new Error(`Etherscan API error: ${response?.result}`);
if (response?.result?.[0]?.txHash)
return response?.result?.[0]?.txHash as string;
},
ETHERSCAN_API.replace("${CHAIN_ID}", chainId.toString()),
baseUrl,
);
}

Expand Down Expand Up @@ -355,6 +359,7 @@ export const getCreatorTx = async (
const fetcher = getEtherscanApiContractCreatorFetcher(
apiKey,
sourcifyChain.chainId,
sourcifyChain.etherscanApi?.url,
);
const result = await getCreatorTxUsingFetcher(fetcher, contractAddress);
if (result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const fetchFromEtherscanOrThrowError = async (
sourcifyChain.chainId,
address,
apiKey,
sourcifyChain.etherscanApi?.url,
);
} catch (err) {
return mapLibError(err, throwV2Errors);
Expand Down