Skip to content
Open
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
153 changes: 68 additions & 85 deletions scripts/check-abi.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import process from "node:process";
import { formatAbi } from "abitype";
import {
createPublicClient,
http,
getFunctionSelector,
getAddress,
defineChain,
} from "viem";
Expand All @@ -13,10 +13,17 @@ import LocalSSVNetworkViewsABIJson from "../src/lib/abi/mainnet/v4/getter.json"
type: "json",
};

const RPC_BY_CHAIN_ID = {
1: "https://ethereum-rpc.publicnode.com/d8a2cc6e7483872e917d7899f9403d738b001c80e37d66834f4e40e9efb54a27",
560048:
"https://ethereum-hoodi-rpc.publicnode.com/d8a2cc6e7483872e917d7899f9403d738b001c80e37d66834f4e40e9efb54a27",
const CHAINS = {
1: {
name: "Ethereum",
rpc: "https://ethereum-rpc.publicnode.com/d8a2cc6e7483872e917d7899f9403d738b001c80e37d66834f4e40e9efb54a27",
etherscanApi: "https://api.etherscan.io/v2/api",
},
560048: {
name: "Hoodi",
rpc: "https://ethereum-hoodi-rpc.publicnode.com/d8a2cc6e7483872e917d7899f9403d738b001c80e37d66834f4e40e9efb54a27",
etherscanApi: "https://api.etherscan.io/v2/api",
},
};

const EIP1967_IMPLEMENTATION_SLOT =
Expand All @@ -25,6 +32,7 @@ const EIP1967_IMPLEMENTATION_SLOT =
const setterAddress = process.env.SSV_NETWORK_CONTRACT_ADDRESS;
const getterAddress = process.env.SSV_NETWORK_VIEWS_CONTRACT_ADDRESS;
const chainId = Number(process.env.SSV_NETWORK_CHAIN_ID ?? 1);
const apiKey = process.env.ETHERSCAN_API_KEY || "7D5ZWV94V4N8G13GXI2PWGM8PU8G7NY366";

if (!setterAddress || !getterAddress) {
console.error(
Expand All @@ -33,127 +41,102 @@ if (!setterAddress || !getterAddress) {
process.exit(1);
}

const rpcUrl = RPC_BY_CHAIN_ID[chainId];
if (!rpcUrl) {
const chainConfig = CHAINS[chainId];
if (!chainConfig) {
console.error(`Unknown chain ID: ${chainId}. Supported: 1, 560048`);
process.exit(1);
}

const chain = defineChain({
id: chainId,
name: chainId === 1 ? "Ethereum" : "Hoodi",
name: chainConfig.name,
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
rpcUrls: { default: { http: [rpcUrl] } },
rpcUrls: { default: { http: [chainConfig.rpc] } },
});

const client = createPublicClient({
chain,
transport: http(rpcUrl),
transport: http(chainConfig.rpc),
});

const EIP1167_MINIMAL_PROXY_PREFIX = "363d3d373d3d3d363d73";

function getImplementationFromEIP1167(bytecode) {
const hex = bytecode.startsWith("0x") ? bytecode.slice(2) : bytecode;
const idx = hex.indexOf(EIP1167_MINIMAL_PROXY_PREFIX);
if (idx === -1) return null;
const addrStart = idx + EIP1167_MINIMAL_PROXY_PREFIX.length;
const addrHex = hex.slice(addrStart, addrStart + 40);
if (addrHex.length !== 40) return null;
return getAddress("0x" + addrHex);
}

function extractSelectors(bytecode) {
const hex = (bytecode.startsWith("0x") ? bytecode.slice(2) : bytecode).toLowerCase();
const selectors = new Set();
for (let i = 0; i < hex.length - 9; i += 2) {
const op = hex[i] + hex[i + 1];
if (op === "63") {
// PUSH4 — full 4-byte selector
selectors.add("0x" + hex.slice(i + 2, i + 10));
i += 8;
} else if (op === "62") {
// PUSH3 — optimized for selectors whose first byte is 0x00
selectors.add("0x00" + hex.slice(i + 2, i + 8));
i += 6;
}
}
return selectors;
}

async function getImplementationAddress(proxyAddress) {
async function getImplementation(proxyAddress) {
const slot = await client.getStorageAt({
address: proxyAddress,
slot: EIP1967_IMPLEMENTATION_SLOT,
});
if (slot && slot !== "0x" + "0".repeat(64)) {
return getAddress("0x" + slot.slice(-40));
if (!slot || slot === "0x" + "0".repeat(64)) {
return proxyAddress;
}
return null;
return getAddress("0x" + slot.slice(-40));
}

async function getBytecodeForCheck(address) {
const code = await client.getBytecode({ address });
if (!code) return code;

const implFromStorage = await getImplementationAddress(address);
if (implFromStorage) {
return client.getBytecode({ address: implFromStorage });
async function fetchAbi(address) {
const url = `${chainConfig.etherscanApi}?apikey=${apiKey}&chainid=${chainId}&module=contract&action=getabi&address=${address}`;
const res = await fetch(url);
const json = await res.json();
if (json.status !== "1") {
throw new Error(`Etherscan error: ${json.message} — ${json.result}`);
}

const implFromEIP1167 = getImplementationFromEIP1167(code);
if (implFromEIP1167) {
return client.getBytecode({ address: implFromEIP1167 });
}

return code;
return JSON.parse(json.result);
}

const ABIS = [
{ name: "SSVNetwork", abi: LocalSSVNetworkABIJson, address: setterAddress },
{ name: "SSVNetwork", localAbi: LocalSSVNetworkABIJson, proxyAddress: setterAddress },
{
name: "SSVNetworkViews",
abi: LocalSSVNetworkViewsABIJson,
address: getterAddress,
localAbi: LocalSSVNetworkViewsABIJson,
proxyAddress: getterAddress,
},
];

let hasFailures = false;

for (const { name, abi, address } of ABIS) {
const fnItems = abi.filter((x) => x.type === "function");
const passed = [];
const failed = [];
for (const { name, localAbi, proxyAddress } of ABIS) {
console.log(`\n${name}:`);

let code;
let impl;
try {
code = await getBytecodeForCheck(address);
impl = await getImplementation(proxyAddress);
console.log(` Proxy: ${proxyAddress}`);
console.log(` Implementation: ${impl}`);
} catch (e) {
console.error(`Failed to fetch bytecode for ${name}: ${e.message}`);
console.error(` Failed to get implementation: ${e.message}`);
hasFailures = true;
continue;
}

const push4Selectors = extractSelectors(code ?? "");

for (const item of fnItems) {
const selector = getFunctionSelector(item);
if (push4Selectors.has(selector)) {
passed.push(item.name);
} else {
failed.push({ name: item.name, selector });
}
let etherscanAbi;
try {
etherscanAbi = await fetchAbi(impl);
} catch (e) {
console.error(` Failed to fetch ABI from Etherscan: ${e.message}`);
hasFailures = true;
continue;
}

console.log(`\n${name}:`);
if (failed.length) {
hasFailures = true;
console.log(
` ✗ Missing on-chain (${failed.length}):`,
failed.map((f) => `${f.name} (${f.selector})`).join(", "),
);
const localSigs = formatAbi(localAbi).sort();
const etherscanSigs = formatAbi(etherscanAbi).sort();

if (JSON.stringify(localSigs) === JSON.stringify(etherscanSigs)) {
console.log(` ✓ ABI matches (${localSigs.length} items)`);
} else {
console.log(` ✓ All ${passed.length} functions verified`);
hasFailures = true;
const localSet = new Set(localSigs);
const etherscanSet = new Set(etherscanSigs);
const onlyInLocal = localSigs.filter((s) => !etherscanSet.has(s));
const onlyInEtherscan = etherscanSigs.filter((s) => !localSet.has(s));
if (onlyInLocal.length) {
console.log(
` ✗ In local but not in Etherscan (${onlyInLocal.length}):`,
onlyInLocal.join(", "),
);
}
if (onlyInEtherscan.length) {
console.log(
` ✗ In Etherscan but not in local (${onlyInEtherscan.length}):`,
onlyInEtherscan.join(", "),
);
}
}
}

Expand Down