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
221 lines (220 loc) • 7.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UtxoExplorer = void 0;
const Client_1 = require("../Client");
const Amount_1 = require("../utils/Amount");
const networks_1 = require("../ChainGate/networks");
const UTXO_DECIMALS = 8;
class UtxoExplorer {
constructor(client, network, baseUrl, apiKey, global) {
this.client = client;
this.network = network;
this.baseUrl = baseUrl;
this.apiKey = apiKey;
this.global = global;
}
/**
* Creates an Amount from a raw satoshi bigint value.
* @internal
*/
amountFromSat(sat) {
return new Amount_1.Amount(sat, UTXO_DECIMALS, this.nativeData(), this.global.marketsCache);
}
/** 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 a UTXO address.
*
* The `confirmed` and `unconfirmed` fields are returned as `Amount` instances.
*/
async getAddressBalance(address) {
const { data } = await (0, Client_1.getUtxoNetworkAddressBalance)({
client: this.client,
path: { network: this.network },
query: { address },
throwOnError: true,
});
const meta = this.nativeData();
const cache = this.global.marketsCache;
return {
address: data.address,
confirmed: new Amount_1.Amount(BigInt(data.confirmedSat), UTXO_DECIMALS, meta, cache),
unconfirmed: Amount_1.Amount.fromDecimal(data.unconfirmed, UTXO_DECIMALS, meta, cache),
};
}
/**
* Returns paginated transaction history for a UTXO address.
*
* Each transaction's `amount` and `addressBalance` are returned as `Amount` instances.
*/
async getAddressHistory(address, page) {
const { data } = await (0, Client_1.getUtxoNetworkAddressHistory)({
client: this.client,
path: { network: this.network },
query: { address, page },
throwOnError: true,
});
const meta = this.nativeData();
const cache = this.global.marketsCache;
return {
page: data.page,
transactions: data.transactions.map((tx) => ({
height: tx.height,
txid: tx.txid,
amount: new Amount_1.Amount(BigInt(tx.amount), UTXO_DECIMALS, meta, cache),
addressBalance: new Amount_1.Amount(BigInt(tx.addressBalance), UTXO_DECIMALS, meta, cache),
})),
};
}
/**
* Returns paginated unspent transaction outputs (UTXOs) for a UTXO address.
*
* Each UTXO's `amount` is returned as an `Amount` instance.
*/
async getUtxosByAddress(address, page) {
const { data } = await (0, Client_1.getUtxoNetworkUtxosByAddress)({
client: this.client,
path: { network: this.network },
query: { address, page },
throwOnError: true,
});
const meta = this.nativeData();
const cache = this.global.marketsCache;
return {
address: data.address,
page: data.page,
utxos: data.utxos.map((utxo) => ({
txid: utxo.txid,
n: utxo.n,
amount: Amount_1.Amount.fromDecimal(utxo.amount, UTXO_DECIMALS, meta, cache),
height: utxo.height,
script: utxo.script,
})),
};
}
/**
* Returns detailed information about a transaction by its ID.
*
* The `fee`, `feePerKb`, and all input/output `amount` fields are returned as `Amount` instances.
*/
async getTransactionDetails(transactionId) {
const { data } = await (0, Client_1.getUtxoNetworkTransactionDetails)({
client: this.client,
path: { network: this.network },
query: { transactionId },
throwOnError: true,
});
const meta = this.nativeData();
const cache = this.global.marketsCache;
return {
...data,
fee: Amount_1.Amount.fromDecimal(data.fee, UTXO_DECIMALS, meta, cache),
feePerKb: Amount_1.Amount.fromDecimal(data.feePerKb, UTXO_DECIMALS, meta, cache),
inputs: data.inputs.map((input) => ({
...input,
amount: Amount_1.Amount.fromDecimal(input.amount, UTXO_DECIMALS, meta, cache),
})),
outputs: data.outputs.map((output) => ({
...output,
amount: Amount_1.Amount.fromDecimal(output.amount, UTXO_DECIMALS, meta, cache),
})),
};
}
/**
* Returns block details for the given block hash.
*/
async getBlockByHash(blockHash) {
const { data } = await (0, Client_1.getUtxoNetworkBlockByHash)({
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.getUtxoNetworkBlockByHeight)({
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.getUtxoNetworkLatestBlock)({
client: this.client,
path: { network: this.network },
throwOnError: true,
});
return data;
}
/**
* Returns fee rate estimates for 4 priority tiers in satoshis per KB.
*/
async getFeeRate() {
const { data } = await (0, Client_1.getUtxoNetworkFeeRate)({
client: this.client,
path: { network: this.network },
throwOnError: true,
});
return data;
}
/**
* Returns paginated mempool transaction IDs.
*/
async getMempool(page) {
const { data } = await (0, Client_1.getUtxoNetworkMempool)({
client: this.client,
path: { network: this.network },
query: { page },
throwOnError: true,
});
return data;
}
/**
* Returns the URL endpoint for the SVG logo of this UTXO network.
*/
getLogoUrl() {
const suffix = this.apiKey ? `?api_key=${encodeURIComponent(this.apiKey)}` : '';
return `${this.baseUrl}/utxo/${this.network}/logo${suffix}`;
}
/**
* Broadcasts a signed raw transaction to the UTXO network.
*/
async broadcastTransaction(transactionRaw) {
const { data } = await (0, Client_1.postUtxoNetworkBroadcastTransaction)({
client: this.client,
path: { network: this.network },
body: { transactionRaw },
throwOnError: true,
});
return data;
}
/**
* Estimates the byte size of a transaction given inputs and outputs.
*/
async estimateTransactionSize(body) {
const { data } = await (0, Client_1.postUtxoNetworkEstimateTransactionSize)({
client: this.client,
path: { network: this.network },
body,
throwOnError: true,
});
return data;
}
}
exports.UtxoExplorer = UtxoExplorer;