UNPKG

chaingate

Version:

A complete TypeScript library for connecting to and making transactions on different blockchains

220 lines 8.56 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.UtxoCurrencyUtils = void 0; const CurrencyUtils_1 = require("../../CurrencyUtils"); const Client_1 = require("../../../../Client"); const Utils_1 = require("../../../../InternalUtils/Utils"); const NumberLike_1 = require("../../../../InternalUtils/NumberLike"); const btc = __importStar(require("@scure/btc-signer")); class UtxoCurrencyUtils extends CurrencyUtils_1.CurrencyUtils { network; networkParams; signHeader; addressToNative; constructor(context, currencyInfo, network, networkParams, signHeader, addressToNative) { super(context, currencyInfo); this.network = network; this.networkParams = networkParams; this.signHeader = signHeader; this.addressToNative = addressToNative; } async addressUtxos(address, page = 0) { const response = await (0, Client_1.utxoUtxosByAddress)({ client: this.context.client, path: { network: this.network }, query: { address, page }, }); return { page: response.data.page, utxos: response.data.utxos.map((utxo) => ({ txid: utxo.txid, script: (0, Utils_1.hexToBytes)(utxo.script), amount: this.buildAmount(utxo.amount), n: utxo.n, })), }; } async addressBalance(address) { const result = await (0, Client_1.utxoAddressBalance)({ client: this.context.client, path: { network: this.network }, query: { address }, }); return { confirmed: this.buildAmount(result.data.confirmed), unconfirmed: this.buildAmount(result.data.unconfirmed), }; } async addressHistory(address, page = 0) { const respose = await (0, Client_1.utxoAddressHistory)({ client: this.context.client, path: { network: this.network }, query: { address, page }, }); return { transactions: respose.data.transactions.map((t) => ({ amount: this.buildAmount(t.amount), addressBalance: this.buildAmount(t.addressBalance), txid: t.txid, height: t.height, })), page: respose.data.page, }; } async broadcastTransaction(transactionRaw) { const result = await (0, Client_1.utxoBroadcastTransaction)({ client: this.context.client, path: { network: this.network }, body: { transactionRaw: transactionRaw instanceof Uint8Array ? (0, Utils_1.bytesToHex)(transactionRaw, false) : transactionRaw, }, }); return { transactionId: result.data.txId }; } async transactionDetails(transactionId) { const result = await (0, Client_1.utxoTransactionDetails)({ client: this.context.client, path: { network: this.network }, query: { transactionId }, }); return { blockHeight: result.data.blockHeight ?? null, fee: this.buildAmount(result.data.fee), feePerKb: this.buildAmount(result.data.feePerKb), timestamp: result.data.timestamp, inputs: result.data.inputs.map((input) => ({ address: input.address, amount: this.buildAmount(input.amount), n: (0, NumberLike_1.toDecimal)(input.n), script: input.script ? (0, Utils_1.hexToBytes)(input.script) : null, scriptSig: input.scriptSig ? (0, Utils_1.hexToBytes)(input.scriptSig) : null, })), outputs: result.data.outputs.map((output) => ({ address: output.address, amount: this.buildAmount(output.amount), n: (0, NumberLike_1.toDecimal)(output.n), script: output.script ? (0, Utils_1.hexToBytes)(output.script) : null, })), rawTransaction: result.data.rawTransaction, }; } async getFeeRate() { const result = await (0, Client_1.utxoFeeRate)({ client: this.context.client, path: { network: this.network }, }); return { low: { feePerKb: this.buildAmount(result.data.low.feePerKb), confirmationTimeSecs: Number(result.data.low.confirmationTimeSecs), }, normal: { feePerKb: this.buildAmount(result.data.normal.feePerKb), confirmationTimeSecs: Number(result.data.normal.confirmationTimeSecs), }, high: { feePerKb: this.buildAmount(result.data.high.feePerKb), confirmationTimeSecs: Number(result.data.high.confirmationTimeSecs), }, maximum: { feePerKb: this.buildAmount(result.data.maximum.feePerKb), confirmationTimeSecs: Number(result.data.maximum.confirmationTimeSecs), }, }; } async latestBlock() { const result = await (0, Client_1.utxoLatestBlock)({ client: this.context.client, path: { network: this.network }, }); return result.data; } async mempoolTransactions() { const result = await (0, Client_1.utxoMempool)({ client: this.context.client, path: { network: this.network }, }); return result.data; } async blockByHeight(blockHeight) { const result = await (0, Client_1.utxoBlockByHeight)({ client: this.context.client, path: { network: this.network }, query: { blockHeight }, }); return result.data; } async blockByHash(blockHash) { const result = await (0, Client_1.utxoBlockByHash)({ client: this.context.client, path: { network: this.network }, query: { blockHash }, }); return result.data; } getUtxoCache() { const key = `${this.currencyInfo.id}/utxoCache`; let utxoCache = this.context.extra.get(key); if (!utxoCache) { utxoCache = []; this.context.extra.set(key, utxoCache); } return utxoCache; } getSpentTxoCache() { const key = `${this.currencyInfo.id}/spentTxoCache`; let spentTxoCache = this.context.extra.get(key); if (!spentTxoCache) { spentTxoCache = []; this.context.extra.set(key, spentTxoCache); } return spentTxoCache; } addressToScript(address) { if (this.addressToNative) address = this.addressToNative(address); const desc = btc.Address(this.networkParams).decode(address); return new Uint8Array(btc.OutScript.encode(desc)); } scriptToAddress(script) { const out = btc.OutScript.decode(script); return btc.Address(this.networkParams).encode(out); } } exports.UtxoCurrencyUtils = UtxoCurrencyUtils; //# sourceMappingURL=UtxoCurrencyUtils.js.map