UNPKG

chaingate

Version:

Multi-chain cryptocurrency SDK for TypeScript — unified API for Bitcoin, Ethereum, Litecoin, Dogecoin, Bitcoin Cash, Polygon, Arbitrum, and any EVM-compatible chain. Create wallets, query balances, send transactions, and manage tokens and NFTs across UTXO

282 lines (281 loc) 9.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EvmExplorer = void 0; const Client_1 = require("../Client"); const Amount_1 = require("../utils/Amount"); const networks_1 = require("../ChainGate/networks"); const EVM_DECIMALS = 18; class EvmExplorer { constructor(client, network, baseUrl, apiKey, global) { this.client = client; this.network = network; this.baseUrl = baseUrl; this.apiKey = apiKey; this.global = global; } /** Returns the native coin data for this network. */ nativeData() { const info = networks_1.NETWORKS_INFO[this.network]; return { symbol: info.nativeToken.symbol, name: info.nativeToken.name, network: this.network, }; } /** * Returns the confirmed and unconfirmed balance for an EVM address. * * The `confirmed` and `unconfirmed` fields are returned as `Amount` instances * with native token metadata (symbol, name, network). */ async getAddressBalance(address) { const { data } = await (0, Client_1.getEvmNetworkAddressBalance)({ client: this.client, path: { network: this.network }, query: { address }, throwOnError: true, }); const ad = this.nativeData(); const cache = this.global.marketsCache; return { address: data.address, confirmed: new Amount_1.Amount(BigInt(data.confirmedWei), EVM_DECIMALS, ad, cache), unconfirmed: Amount_1.Amount.fromDecimal(data.unconfirmed, EVM_DECIMALS, ad, cache), }; } /** * Returns paginated transaction history for an EVM address, including decoded * contract events (transfers, mints, approvals, etc.). */ async getAddressHistory(address, page) { const { data } = await (0, Client_1.getEvmNetworkAddressHistory)({ client: this.client, path: { network: this.network }, query: { address, page }, throwOnError: true, }); return data; } /** * Returns all ERC-20/ERC-721/ERC-1155 token balances for an EVM address. * * Each balance is returned as an {@link Amount} instance. Use `isToken` and * `isNFT` to distinguish token types, and access `contractAddress` or * `ownedTokens` accordingly. */ async getAddressTokenBalances(address) { const { data } = await (0, Client_1.getEvmNetworkAddressTokenBalances)({ client: this.client, path: { network: this.network }, query: { address }, throwOnError: true, }); return data.tokens.map((token) => { const decimals = token.token.decimals ?? 0; const ad = { symbol: token.token.symbol ?? '', name: token.token.name ?? '', network: this.network, contractAddress: token.contractAddress, ownedTokens: token.ownedTokens?.map((nft) => ({ id: nft.id, uri: nft.uri })), }; return new Amount_1.Amount(BigInt(token.balance), decimals, ad, this.global.marketsCache); }); } /** * Returns the nonce (transaction count) for an EVM address. */ async getAddressTransactionCount(address) { const { data } = await (0, Client_1.getEvmNetworkAddressTransactionCount)({ client: this.client, path: { network: this.network }, query: { address }, throwOnError: true, }); return data; } /** * Returns the next nonce to use when sending a transaction from an EVM address. */ async getNonce(address) { const { data } = await (0, Client_1.getEvmNetworkNonce)({ client: this.client, path: { network: this.network }, query: { address }, throwOnError: true, }); return data; } /** * Returns detailed information about a transaction by its hash. * * The `amount` field is returned as an `Amount` instance with native token metadata. */ async getTransactionDetails(transactionId) { const { data } = await (0, Client_1.getEvmNetworkTransactionDetails)({ client: this.client, path: { network: this.network }, query: { transactionId }, throwOnError: true, }); return { ...data, amount: Amount_1.Amount.fromDecimal(data.amount, EVM_DECIMALS, this.nativeData(), this.global.marketsCache), }; } /** * Returns block details for the given block hash. */ async getBlockByHash(blockHash) { const { data } = await (0, Client_1.getEvmNetworkBlockByHash)({ client: this.client, path: { network: this.network }, query: { blockHash }, throwOnError: true, }); return data; } /** * Returns block details for the given block height. */ async getBlockByHeight(blockHeight) { const { data } = await (0, Client_1.getEvmNetworkBlockByHeight)({ client: this.client, path: { network: this.network }, query: { blockHeight }, throwOnError: true, }); return data; } /** * Returns the latest block number and hash. */ async getLatestBlock() { const { data } = await (0, Client_1.getEvmNetworkLatestBlock)({ client: this.client, path: { network: this.network }, throwOnError: true, }); return data; } /** * Estimates the gas required for a transaction. */ async estimateGas(params) { const { data } = await (0, Client_1.getEvmNetworkEstimateGas)({ client: this.client, path: { network: this.network }, query: params, throwOnError: true, }); return data; } /** * Returns the current fee rate recommendations (low, normal, high, maximum). * * Each tier contains EIP-1559 fee parameters (`maxFeePerGasGwei`, * `maxPriorityFeePerGasGwei`) and an estimated confirmation time. */ async getFeeRate() { const { data } = await (0, Client_1.getEvmNetworkFeeRate)({ client: this.client, path: { network: this.network }, throwOnError: true, }); return data; } /** * Returns current network status including block time, gas usage, and fee predictions. */ async getNetworkStatus() { const { data } = await (0, Client_1.getEvmNetworkNetworkStatus)({ client: this.client, path: { network: this.network }, throwOnError: true, }); return data; } /** * Returns the URL endpoint for the SVG logo of this EVM network. */ getLogoUrl() { const suffix = this.apiKey ? `?api_key=${encodeURIComponent(this.apiKey)}` : ''; return `${this.baseUrl}/evm/${this.network}/logo${suffix}`; } /** * Returns metadata and on-chain information for a token contract. */ async getTokenData(contractAddress) { const { data } = await (0, Client_1.getEvmNetworkTokenData)({ client: this.client, path: { network: this.network }, query: { contractAddress }, throwOnError: true, }); return data; } /** * Returns the URL endpoint for a token contract logo (PNG or SVG). */ getTokenLogoUrl(address) { const params = new URLSearchParams({ address }); if (this.apiKey) params.set('api_key', this.apiKey); return `${this.baseUrl}/evm/${this.network}/tokenLogo?${params.toString()}`; } /** * Returns the full decoded metadata for a specific NFT token. */ async getNftMetadata(contractAddress, tokenId) { const { data } = await (0, Client_1.getEvmNetworkNftMetadata)({ client: this.client, path: { network: this.network }, query: { contractAddress, tokenId }, throwOnError: true, }); return data; } /** * Returns the URL endpoint for a specific NFT token image. */ getNftImageUrl(contractAddress, tokenId) { const params = new URLSearchParams({ contractAddress, tokenId }); if (this.apiKey) params.set('api_key', this.apiKey); return `${this.baseUrl}/evm/${this.network}/nft/metadata/image?${params.toString()}`; } /** * Returns the URL endpoint for a specific NFT token animation/video. */ getNftAnimationUrl(contractAddress, tokenId) { const params = new URLSearchParams({ contractAddress, tokenId }); if (this.apiKey) params.set('api_key', this.apiKey); return `${this.baseUrl}/evm/${this.network}/nft/metadata/animation?${params.toString()}`; } /** * Broadcasts a signed raw transaction to the EVM network. */ async broadcastTransaction(transactionRaw) { const { data } = await (0, Client_1.postEvmNetworkBroadcastTransaction)({ client: this.client, path: { network: this.network }, body: { transactionRaw }, throwOnError: true, }); return data; } /** * Executes a read-only (eth_call) call against a smart contract. */ async callSmartContract(body) { const { data } = await (0, Client_1.postEvmNetworkCallSmartContractFunction)({ client: this.client, path: { network: this.network }, body, throwOnError: true, }); return data; } } exports.EvmExplorer = EvmExplorer;