Skip to content
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

fix: use viem as most explorer urls are already there #18

Open
wants to merge 2 commits 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
3 changes: 2 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"noDelete": "off"
},
"style": {
"useTemplate": "off"
"useTemplate": "off",
"noNonNullAssertion": "off"
}
}
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.
85 changes: 44 additions & 41 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
{
"name": "catapulta-verify",
"version": "1.0.4",
"author": "@kartojal <[email protected]> (https://catapulta.sh/)",
"repository": {
"type": "git",
"url": "git+https://github.com/catapulta-sh/catapulta-verify.git"
},
"main": "./out/index.mjs",
"private": false,
"publishConfig": {
"access": "public"
},
"files": [
"out/index.mjs"
],
"devDependencies": {
"@biomejs/biome": "1.4.1",
"@types/chalk": "^2.2.0",
"bun-types": "latest",
"chalk": "^5.3.0",
"dotenv": "^16.3.1",
"ts-command-line-args": "^2.5.1"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"bin": {
"catapulta-verify": "out/index.mjs"
},
"bugs": {
"url": "https://github.com/catapulta-sh/catapulta-verify/issues",
"email": "[email protected]"
},
"homepage": "https://github.com/catapulta-sh/catapulta-verify#readme",
"license": "MIT",
"scripts": {
"build": "bun build ./src/index.ts --outfile=out/index.mjs --target node",
"postbuild": "bunx rexreplace '^(#!.+\\n)?' '$1import { createRequire as createImportMetaRequire } from \"module\"; import.meta.require ||= (id) => createImportMetaRequire(import.meta.url)(id);\\n' -GM out/index.mjs",
"lint:fix": "bunx @biomejs/biome check ./ --apply",
"lint": "bunx @biomejs/biome check ./"
}
"name": "catapulta-verify",
"version": "1.0.4",
"author": "@kartojal <[email protected]> (https://catapulta.sh/)",
"repository": {
"type": "git",
"url": "git+https://github.com/catapulta-sh/catapulta-verify.git"
},
"main": "./out/index.mjs",
"private": false,
"publishConfig": {
"access": "public"
},
"files": [
"out/index.mjs"
],
"devDependencies": {
"@biomejs/biome": "1.4.1",
"@types/chalk": "^2.2.0",
"bun-types": "latest",
"chalk": "^5.3.0",
"dotenv": "^16.3.1",
"ts-command-line-args": "^2.5.1"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"bin": {
"catapulta-verify": "out/index.mjs"
},
"bugs": {
"url": "https://github.com/catapulta-sh/catapulta-verify/issues",
"email": "[email protected]"
},
"homepage": "https://github.com/catapulta-sh/catapulta-verify#readme",
"license": "MIT",
"scripts": {
"build": "bun build ./src/index.ts --outfile=out/index.mjs --target node",
"postbuild": "bunx rexreplace '^(#!.+\\n)?' '$1import { createRequire as createImportMetaRequire } from \"module\"; import.meta.require ||= (id) => createImportMetaRequire(import.meta.url)(id);\\n' -GM out/index.mjs",
"lint:fix": "bunx @biomejs/biome check ./ --apply",
"lint": "bunx @biomejs/biome check ./"
},
"dependencies": {
"viem": "^2.7.3"
}
}
89 changes: 63 additions & 26 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
import { http, Chain, Client, Transport, createClient } from "viem";
import {
arbitrum,
avalanche,
base,
bsc,
bscTestnet,
gnosis,
goerli,
mainnet,
optimism,
polygon,
polygonZkEvm,
scroll,
sepolia,
} from "viem/chains";
import { Networks } from "./types";

const packageJson = require("../package.json");
Expand Down Expand Up @@ -26,37 +42,58 @@ export const ETHERSCAN_API_KEYS: {
export const ETHERSCAN_API_URL: {
[key: string]: string;
} = {
[Networks.polygon]: "https://api.polygonscan.com/api",
[Networks.main]: "https://api.etherscan.io/api",
[Networks.arbitrum]: "https://api.arbiscan.io/api",
[Networks.avalanche]: "https://api.routescan.io/v2/network/mainnet/evm/43114/etherscan",
[Networks.avalancheFuji]: "https://api.routescan.io/v2/network/testnet/evm/43113/etherscan",
[Networks.optimism]: "https://api-optimistic.etherscan.io/api",
[Networks.goerli]: "https://api-goerli.etherscan.io/api",
[Networks.sepolia]: "https://api-sepolia.etherscan.io/api",
[Networks.bnb]: "https://api.bscscan.com/api",
[Networks.bnbTestnet]: "https://api-testnet.bscscan.com/api",
[Networks.base]: "https://api.basescan.org/api",
[Networks.metis]: "https://api.routescan.io/v2/network/mainnet/evm/1088/etherscan",
[Networks.gnosis]: "https://api.gnosisscan.io/api",
[Networks.scroll]: "https://api.scrollscan.com/api",
[Networks.zkevm]: "https://api-zkevm.polygonscan.com/api",
};

export const DEFAULT_RPC_URLS: {
[key: string]: string;
[key: string]: string | undefined;
} = {
[Networks.polygon]: process.env.RPC_POLYGON || "",
[Networks.main]: process.env.RPC_MAINNET || "",
[Networks.arbitrum]: process.env.RPC_ARBITRUM || "",
[Networks.avalanche]: process.env.RPC_AVALANCHE || "",
[Networks.optimism]: process.env.RPC_OPTIMISM || "",
[Networks.goerli]: process.env.RPC_GOERLI || "",
[Networks.sepolia]: process.env.RPC_SEPOLIA || "",
[Networks.bnb]: process.env.RPC_BNB || "",
[Networks.bnbTestnet]: process.env.RPC_BNB_TESTNET || "",
[Networks.base]: process.env.RPC_BASE || "",
[Networks.gnosis]: process.env.RPC_GNOSIS || "",
[Networks.scroll]: process.env.RPC_SCROLL || "",
[Networks.zkevm]: process.env.RPC_ZKEVM || "",
[Networks.polygon]: process.env.RPC_POLYGON,
[Networks.main]: process.env.RPC_MAINNET,
[Networks.arbitrum]: process.env.RPC_ARBITRUM,
[Networks.avalanche]: process.env.RPC_AVALANCHE,
[Networks.optimism]: process.env.RPC_OPTIMISM,
[Networks.goerli]: process.env.RPC_GOERLI,
[Networks.sepolia]: process.env.RPC_SEPOLIA,
[Networks.bnb]: process.env.RPC_BNB,
[Networks.bnbTestnet]: process.env.RPC_BNB_TESTNET,
[Networks.base]: process.env.RPC_BASE,
[Networks.gnosis]: process.env.RPC_GNOSIS,
[Networks.scroll]: process.env.RPC_SCROLL,
[Networks.zkevm]: process.env.RPC_ZKEVM,
};

export const CHAINS: {
[key: string]: Chain;
} = {
[Networks.polygon]: polygon,
[Networks.main]: mainnet,
[Networks.arbitrum]: arbitrum,
[Networks.avalanche]: avalanche,
[Networks.optimism]: optimism,
[Networks.goerli]: goerli,
[Networks.sepolia]: sepolia,
[Networks.bnb]: bsc,
[Networks.bnbTestnet]: bscTestnet,
[Networks.base]: base,
[Networks.gnosis]: gnosis,
[Networks.scroll]: scroll,
[Networks.zkevm]: polygonZkEvm,
};

export function getClient({ chainId, rpcUrl, apiUrl }: { chainId: string; rpcUrl?: string; apiUrl?: string }) {
return createClient({
transport: http(rpcUrl || DEFAULT_RPC_URLS[chainId]),
chain: {
...CHAINS[chainId],
blockExplorers: {
default: {
...(CHAINS[chainId].blockExplorers?.default as any),
apiUrl: apiUrl || ETHERSCAN_API_URL[chainId] || CHAINS[chainId].blockExplorers?.default.apiUrl,
},
},
},
});
}
22 changes: 8 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { existsSync } from "fs";
import chalk from "chalk";
import "dotenv/config";
import { exit } from "process";
import { getChainId } from "viem/actions";
import { args } from "./cli-args";
import { DEFAULT_RPC_URLS, VERIFY_VERSION } from "./config";
import { VERIFY_VERSION, getClient } from "./config";
import { callTraceVerifier } from "./utils/calltrace-verifier";
import { loadArtifacts, loadBuildInfo } from "./utils/foundry-ffi";
import { loadJson } from "./utils/json";
import { getChainId, getTxInternalCalls } from "./utils/rpc";
import { getTxInternalCalls } from "./utils/rpc";

// prevent dotenv env to affect child_process.execSync environment
delete process.env.FOUNDRY_LIBRARIES;
Expand All @@ -32,14 +33,14 @@ const main = async () => {

greets();

const rpc = args.rpcUrl || DEFAULT_RPC_URLS[parsedRun.chain];
const chainId = await getChainId(rpc);
const client = getClient({ chainId: parsedRun.chain, rpcUrl: args.rpcUrl, apiUrl: args.etherscanUrl });
const chainId = await getChainId(client);

try {
console.log("Chain Id:", chainId);
console.log();
} catch (err) {
console.log("Could not connect to RPC endpoint", rpc);
console.log("Could not connect to RPC endpoint", client.transport.url);
process.exit(2);
}

Expand All @@ -48,17 +49,10 @@ const main = async () => {

console.log("\nAnalyzing deployment transactions...\n");
for (const tx of parsedRun.transactions) {
const trace = await getTxInternalCalls(tx.hash, rpc);
const trace = await getTxInternalCalls(tx.hash, client.transport.url as string);

try {
await callTraceVerifier(
trace.result,
chainId,
artifacts,
buildInfos,
args.etherscanUrl,
args.etherscanApiKey,
);
await callTraceVerifier(trace.result, chainId, artifacts, buildInfos, client, args.etherscanApiKey);
} catch (error) {
console.error("[Verification Error]", error);
}
Expand Down
21 changes: 9 additions & 12 deletions src/utils/calltrace-verifier.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Client } from "viem";
import { checkIfVerified, checkVerificationStatus, submitVerification, waitTillVisible } from "./explorer-api";
import { getSettingsByArtifact } from "./foundry-ffi";
import { delay } from "./misc";
Expand All @@ -7,26 +8,26 @@ export const callTraceVerifier = async (
chainId: number,
artifacts: any[],
buildInfos: any[],
etherscanUrl?: string,
client: Client,
etherscanApiKey?: string,
) => {
const deployOpcodes = ["CREATE", "CREATE2"];

// Perform nested call tracing verification in each internal call
if (call.calls) {
for (const c of call.calls) {
await callTraceVerifier(c, chainId, artifacts, buildInfos, etherscanUrl, etherscanApiKey);
await callTraceVerifier(c, chainId, artifacts, buildInfos, client, etherscanApiKey);
}
}

if (!deployOpcodes.includes(call.type)) return;

await waitTillVisible(call.to, Number(chainId), etherscanUrl, etherscanApiKey);
await waitTillVisible(call.to, Number(chainId), client.chain?.blockExplorers?.default.apiUrl, etherscanApiKey);

const verified = await checkIfVerified(call.to, Number(chainId), etherscanUrl, etherscanApiKey);
const verified = await checkIfVerified(call.to, client, etherscanApiKey);

if (verified) {
console.log(`(${call.to}) is already verified, skipping.`);
console.log(`(${client.chain?.blockExplorers?.default}/address/${call.to}) is already verified, skipping.`);
return;
}

Expand All @@ -46,22 +47,18 @@ export const callTraceVerifier = async (
return;
}

const {
result: guid,
message,
status,
}: any = await submitVerification(verificationReq, Number(chainId), etherscanUrl);
const { result: guid, message, status }: any = await submitVerification(verificationReq, client);

if (!status || guid.includes("Max rate limit reached")) {
console.log(`Couldn't verify ${call.to} `, guid);
return;
}

console.log(`Verifying contract ${call.to}, with guid: ${guid}`);
console.log(`Verifying contract ${client.chain?.blockExplorers?.default}/address/${call.to}, with guid: ${guid}`);

for (let i = 0; i < 30; i++) {
await delay(1000);
const { status, message } = await checkVerificationStatus(guid, chainId, etherscanUrl, etherscanApiKey);
const { status, message } = await checkVerificationStatus(guid, client, etherscanApiKey);

if (status !== 2) {
console.log(message);
Expand Down
Loading