UNPKG

@evmexplorer/blockscout

Version:
223 lines (215 loc) 7.99 kB
// src/utils.ts function getChainProviderBlockscout(chainId) { switch (chainId) { case 1: return "eth.blockscout.com"; case 10: return "optimism.blockscout.com"; 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.network"; 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; } async function fetchSmartContractReadMethodsBlockscout(address, chainId) { const chainProvider = getChainProviderBlockscout(chainId); const query = `https://${chainProvider}/api/v2/smart-contracts/${address}/methods-read`; const response = await fetch(query); const body = await response.json(); return body; } async function fetchSmartContractWriteMethodsBlockscout(address, chainId) { const chainProvider = getChainProviderBlockscout(chainId); const query = `https://${chainProvider}/api/v2/smart-contracts/${address}/methods-write`; const response = await fetch(query); const body = await response.json(); return body; } async function fetchSmartContractQueryReadBlockscout(address, method_id, args = [], chainId) { const chainProvider = getChainProviderBlockscout(chainId); const query = `https://${chainProvider}/api/v2/smart-contracts/${address}/query-read-method`; const response = await fetch(query, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ args, method_id, contract_type: "regular" }) }); 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; } export { fetchAddressCounters, fetchAddressInfo, fetchAddressTransactions, fetchBlockInfoBlockscout, fetchBlockTransactionsBlockscout, fetchInternalTransactionsBlockscout, fetchNFTIdInfo, fetchSearchBlockscout, fetchSmartContractBlockscout, fetchSmartContractQueryReadBlockscout, fetchSmartContractReadMethodsBlockscout, fetchSmartContractWriteMethodsBlockscout, fetchStatsBlockscout, fetchTokenInfo, fetchTokenTransfersBlockscout, fetchTokensAddress, fetchTransactionBlockscout, getChainProviderBlockscout };