@evmexplorer/blockscout
Version:
EVM Explorer TypeScript BlockScout V2 sdk
230 lines (220 loc) • 8.56 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
fetchAddressCounters: () => fetchAddressCounters,
fetchAddressInfo: () => fetchAddressInfo,
fetchAddressTransactions: () => fetchAddressTransactions,
fetchBlockInfoBlockscout: () => fetchBlockInfoBlockscout,
fetchBlockTransactionsBlockscout: () => fetchBlockTransactionsBlockscout,
fetchInternalTransactionsBlockscout: () => fetchInternalTransactionsBlockscout,
fetchNFTIdInfo: () => fetchNFTIdInfo,
fetchSearchBlockscout: () => fetchSearchBlockscout,
fetchSmartContractBlockscout: () => fetchSmartContractBlockscout,
fetchStatsBlockscout: () => fetchStatsBlockscout,
fetchTokenInfo: () => fetchTokenInfo,
fetchTokenTransfersBlockscout: () => fetchTokenTransfersBlockscout,
fetchTokensAddress: () => fetchTokensAddress,
fetchTransactionBlockscout: () => fetchTransactionBlockscout,
getChainProviderBlockscout: () => getChainProviderBlockscout
});
module.exports = __toCommonJS(index_exports);
// src/utils.ts
function getChainProviderBlockscout(chainId) {
switch (chainId) {
case 1:
return "eth.blockscout.com";
case 10:
return "explorer.optimism.io";
case 137:
return "polygon.blockscout.com";
case 314:
return "filecoin.blockscout.com";
case 690:
return "explorer.redstone.xyz";
case 8453:
return "base.blockscout.com";
case 34443:
return "explorer-mode-mainnet-0.t.conduit.xyz";
case 42161:
return "arbitrum.blockscout.com";
case 7777777:
return "explorer.zora.energy";
}
return "eth.blockscout.com";
}
// src/address/index.ts
async function fetchAddressCounters(address, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/addresses/${address}/counters`;
const response = await fetch(query);
if (response.status === 200) {
const body = await response.json();
if (body.gas_usage_count === "0" && body.token_transfers_count === "0" && body.transactions_count === "0" && body.validations_count === "0") {
throw new Error("BlockScout Contract Counters response empty");
}
if (body.transactions_count !== "0" && body.gas_usage_count === "0") {
throw new Error("BlockScout Contract Counters response faulty");
}
if (body.gas_usage_count === "0" && body.token_transfers_count !== "0") {
throw new Error("BlockScout Contract Counters response faulty");
}
return body;
}
throw new Error("BlockScout Contract Counters response undefined");
}
async function fetchAddressTransactions(address, parameters, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
let query = "";
if (parameters) {
query = `https://${chainProvider}/api/v2/addresses/${address}/transactions?${parameters}`;
}
if (!parameters) {
query = `https://${chainProvider}/api/v2/addresses/${address}/transactions`;
}
const response = await fetch(query);
const body = await response.json();
if (body) return body;
throw new Error("no transactions");
}
async function fetchAddressInfo(address, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/addresses/${address}`;
const response = await fetch(query, { mode: "cors" });
const body = await response.json();
if (!body.hash) throw new Error("no address info data");
return body;
}
async function fetchInternalTransactionsBlockscout(address, parameters, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
let query = "";
if (parameters) {
query = `https://${chainProvider}/api/v2/addresses/${address}/internal-transactions?${parameters}`;
}
if (!parameters) {
query = `https://${chainProvider}/api/v2/addresses/${address}/internal-transactions`;
}
const response = await fetch(query);
const body = await response.json();
if (body) return body;
throw new Error("no transactions");
}
async function fetchTokenTransfersBlockscout(address, parameters, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
let query = "";
if (parameters) {
query = `https://${chainProvider}/api/v2/addresses/${address}/token-transfers?${parameters}`;
}
if (!parameters) {
query = `https://${chainProvider}/api/v2/addresses/${address}/token-transfers`;
}
const response = await fetch(query);
const body = await response.json();
if (body) return body;
throw new Error("no transactions");
}
async function fetchTokensAddress(address, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/addresses/${address}/token-balances`;
const response = await fetch(query);
const body = await response.json();
if (body) return body;
throw new Error("no tokens");
}
// src/block/index.ts
async function fetchBlockInfoBlockscout(block, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/blocks/${block}`;
const response = await fetch(query);
const body = await response.json();
return body;
}
async function fetchBlockTransactionsBlockscout(block, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/blocks/${block}/transactions`;
const response = await fetch(query);
const body = await response.json();
return body;
}
// src/contract/index.ts
async function fetchSmartContractBlockscout(address, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/smart-contracts/${address}`;
const response = await fetch(query);
const body = await response.json();
return body;
}
// src/search/index.ts
async function fetchSearchBlockscout(search, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/search?q=${search}`;
const response = await fetch(query);
const body = await response.json();
return body;
}
// src/stats/index.ts
async function fetchStatsBlockscout(chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/stats`;
const response = await fetch(query);
const body = await response.json();
return body;
}
// src/tokens/index.ts
async function fetchTokenInfo(address, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/tokens/${address}`;
const response = await fetch(query);
const body = await response.json();
return body;
}
async function fetchNFTIdInfo(contract, id, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/tokens/${contract}/instances/${id}`;
const response = await fetch(query);
const body = await response.json();
return body;
}
// src/transaction/index.ts
async function fetchTransactionBlockscout(hash, chainId) {
const chainProvider = getChainProviderBlockscout(chainId);
const query = `https://${chainProvider}/api/v2/transactions/${hash}`;
const response = await fetch(query);
const body = await response.json();
return body;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
fetchAddressCounters,
fetchAddressInfo,
fetchAddressTransactions,
fetchBlockInfoBlockscout,
fetchBlockTransactionsBlockscout,
fetchInternalTransactionsBlockscout,
fetchNFTIdInfo,
fetchSearchBlockscout,
fetchSmartContractBlockscout,
fetchStatsBlockscout,
fetchTokenInfo,
fetchTokenTransfersBlockscout,
fetchTokensAddress,
fetchTransactionBlockscout,
getChainProviderBlockscout
});