UNPKG

@dethcrypto/eth-sdk

Version:

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

34 lines • 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAbiFromEtherscan = void 0; const networks_1 = require("../networks"); const explorerEndpoints_1 = require("./explorerEndpoints"); async function getAbiFromEtherscan(networkSymbol, address, config, fetch) { const url = createAPIUrl(networkSymbol, address, config); // @todo error handling for incorrect api keys const response = await fetch(url); if (response.status !== '1' && response.status !== 1) { throw new Error(`Can't find ${networkSymbol} ABI for ${address}. Msg: ${JSON.stringify(response, null, 2)}`); } const abi = JSON.parse(response.result); return abi; } exports.getAbiFromEtherscan = getAbiFromEtherscan; function createAPIUrl(networkSymbol, address, { etherscanKeys, etherscanURLs, etherscanKey }) { var _a; const apiKey = etherscanKey || etherscanKeys[networkSymbol] || ((_a = explorerEndpoints_1.predefinedExplorerEndpoints[networkSymbol]) === null || _a === void 0 ? void 0 : _a.apiKey); const apiUrl = getEtherscanLinkFromNetworkSymbol(networkSymbol, etherscanURLs); if (!apiUrl) { throw new Error(`Can't find network info for ${networkSymbol}`); } return `${apiUrl}?module=contract&action=getabi&address=${address}${apiKey ? `&apikey=${apiKey}` : ''}`; } function getEtherscanLinkFromNetworkSymbol(networkSymbol, userNetworks) { if ((0, networks_1.isUserProvidedNetwork)(networkSymbol, userNetworks)) { return userNetworks[networkSymbol]; } return explorerEndpoints_1.predefinedExplorerEndpoints[networkSymbol].url; } //# sourceMappingURL=getAbiFromEtherscan.js.map