UNPKG

@pgchain/blockchain-libs

Version:
125 lines 4.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BlockBook = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const exceptions_1 = require("../../../basic/request/exceptions"); const restful_1 = require("../../../basic/request/restful"); const provider_1 = require("../../../types/provider"); const abc_1 = require("../../abc"); const MIN_SAT_PER_BYTE = 1; const BTC_PER_KBYTES__TO__SAT_PER_BYTE = Math.pow(10, 5); class BlockBook extends abc_1.SimpleClient { constructor(url) { super(); this.restful = new restful_1.RestfulRequest(url); } async getInfo() { const resp = await this.restful.get('/api/v2').then((i) => i.json()); const bestBlockNumber = Number(resp.backend.blocks); return { bestBlockNumber, isReady: Number.isFinite(bestBlockNumber) && bestBlockNumber > 0, }; } getAccount(xpub, params) { return this.restful .get(`/api/v2/xpub/${xpub}`, params) .then((i) => i.json()); } async getAddress(address) { const resp = await this.restful .get(`/api/v2/address/${address}`, { details: 'basic', }) .then((i) => i.json()); const unconfirmedBalance = Number(resp.unconfirmedBalance || 0); return { balance: new bignumber_js_1.default(Number(resp.balance || 0) + unconfirmedBalance), existing: Number(resp.txs || 0) > 0, }; } async estimateFee(waitingBlock) { const resp = await this.restful .get(`/api/v2/estimatefee/${waitingBlock}`) .then((i) => i.json()) .catch((reason) => { console.debug('Error when estimating fee', reason); return { result: '0' }; }); return Number(resp.result || 0) * BTC_PER_KBYTES__TO__SAT_PER_BYTE; } async getFeePricePerUnit() { const [normalResp, fastResp, slowResp] = await Promise.all([ this.estimateFee(5), this.estimateFee(1), this.estimateFee(20), ]); const isFulfilledFee = (fee) => Number.isFinite(fee) && fee >= MIN_SAT_PER_BYTE; const normal = isFulfilledFee(normalResp) ? normalResp : MIN_SAT_PER_BYTE; const fast = isFulfilledFee(fastResp) ? fastResp : Math.max(MIN_SAT_PER_BYTE, normal * 1.6); const slow = isFulfilledFee(slowResp) ? slowResp : Math.max(MIN_SAT_PER_BYTE, normal * 0.6); return { normal: { price: new bignumber_js_1.default(normal), waitingBlock: 5 }, others: [ { price: new bignumber_js_1.default(slow), waitingBlock: 20 }, { price: new bignumber_js_1.default(fast), waitingBlock: 1 }, ], }; } async getTransactionStatus(txid) { var _a; try { const resp = await this.restful .get(`/api/v2/tx/${txid}`) .then((i) => i.json()); const confirmations = Number(resp.confirmations); return Number.isFinite(confirmations) && confirmations > 0 ? provider_1.TransactionStatus.CONFIRM_AND_SUCCESS : provider_1.TransactionStatus.PENDING; } catch (e) { if (e instanceof exceptions_1.ResponseError && e.response) { const error = await e.response.json(); if ((_a = error.error) === null || _a === void 0 ? void 0 : _a.includes('not found')) { return provider_1.TransactionStatus.NOT_FOUND; } } throw e; } } async getRawTransaction(txid) { const resp = await this.restful .get(`/api/v2/tx/${txid}`) .then((i) => i.json()); return resp.hex; } async broadcastTransaction(rawTx) { try { const resp = await this.restful .get(`/api/v2/sendtx/${rawTx}`) .then((i) => i.json()); return resp.result; } catch (e) { if (e instanceof exceptions_1.ResponseError && e.response) { const error = await e.response.json(); if (typeof error.error === 'string' && error.error.includes('Transaction already in block chain')) { throw new Error('Transaction already in block'); } } throw e; } } } exports.BlockBook = BlockBook; //# sourceMappingURL=blockbook.js.map