UNPKG

@dethcrypto/eth-sdk

Version:

🛠 Generate type-safe, lightweight SDK for your Ethereum smart contracts

52 lines • 2.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.gatherABIs = exports.d = void 0; const debug_1 = __importDefault(require("debug")); const path_1 = require("path"); const traverse_1 = require("../config/traverse"); const fetchJson_1 = require("../peripherals/fetchJson"); const detectProxy_1 = require("./detectProxy"); const getAbiFromEtherscan_1 = require("./etherscan/getAbiFromEtherscan"); const getRpcProvider_1 = require("./getRpcProvider"); const getAbiFromSourcify_1 = require("./sourcify/getAbiFromSourcify"); exports.d = (0, debug_1.default)('@dethcrypto/eth-sdk:abi'); async function gatherABIs({ config, fs, cliArgs }, getAbi = makeGetAbi(config), getProvider = getRpcProvider_1.getRpcProvider) { await (0, traverse_1.traverseContractsMap)(config.contracts, async (network, key, address) => { const fullAbiPath = (0, path_1.join)(cliArgs.workingDirPath, 'abis', network, ...key) + '.json'; (0, exports.d)(`Getting ABI for ${key.join('.')}`); if (!fs.exists(fullAbiPath)) { (0, exports.d)('ABI doesnt exist already. Querying etherscan'); let abi = await getAbi(network, address); if (!config.noFollowProxies) { const rpcProvider = getProvider(config, network); if (rpcProvider) { const detectedProxy = await (0, detectProxy_1.detectProxy)(address, abi, rpcProvider); if (detectedProxy.isProxy) { // Implementation ABI will usually contain proxy ABI, // so just replacing is a good enough merging strategy. abi = await getAbi(network, detectedProxy.implAddress); } } else { console.warn(`\n\nNo RPC URL found for network ${network}. Please add it to "config.rpc.${network}" to enable fetching proxy implementation ABIs.\n\n`); } } await fs.ensureDir((0, path_1.dirname)(fullAbiPath)); await fs.write(fullAbiPath, JSON.stringify(abi)); } }); } exports.gatherABIs = gatherABIs; const makeGetAbi = (config) => { switch (config.abiSource) { case 'etherscan': return (network, address) => (0, getAbiFromEtherscan_1.getAbiFromEtherscan)(network, address, config, fetchJson_1.fetchJson); case 'sourcify': { return (network, address) => (0, getAbiFromSourcify_1.getAbiFromSourcify)(network, address, config.networkIds, fetchJson_1.fetchJson); } } }; //# sourceMappingURL=index.js.map