UNPKG

@thorwallet/xchain-bitcoincash

Version:

Custom bitcoincash client and utilities used by XChainJS clients

161 lines 5.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSuggestedFee = exports.getUnspentTransactions = exports.getTransactions = exports.getRawTransaction = exports.getTransaction = exports.getAccount = void 0; const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const utils_1 = require("./utils"); /** * Check error response. * * @param {any} response The api resonse. * @returns {boolean} */ // eslint-disable-next-line @typescript-eslint/no-explicit-any const isErrorResponse = (response) => { return !!response.error; }; /** * Get account from address. * * @param {string} haskoinUrl The haskoin API url. * @param {string} address The BCH address. * @returns {AddressBalance} * * @throws {"failed to query account by a given address"} thrown if failed to query account by a given address */ const getAccount = ({ haskoinUrl, address }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const result = yield axios_1.default .get(`${haskoinUrl}/address/${address}/balance`) .then((response) => (isErrorResponse(response.data) ? null : response.data)); if (!result) { throw new Error('failed to query account by a given address'); } return result; } catch (error) { return Promise.reject(error); } }); exports.getAccount = getAccount; /** * Get transaction by hash. * * @param {string} haskoinUrl The haskoin API url. * @param {string} txId The transaction id. * @returns {Transaction} * * @throws {"failed to query transaction by a given hash"} thrown if failed to query transaction by a given hash */ const getTransaction = ({ haskoinUrl, txId }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const result = yield axios_1.default .get(`${haskoinUrl}/transaction/${txId}`) .then((response) => (isErrorResponse(response.data) ? null : response.data)); if (!result) { throw new Error('failed to query transaction by a given hash'); } return result; } catch (error) { return Promise.reject(error); } }); exports.getTransaction = getTransaction; /** * Get raw transaction by hash. * * @param {string} haskoinUrl The haskoin API url. * @param {string} txId The transaction id. * @returns {Transaction} * * @throws {"failed to query transaction by a given hash"} thrown if failed to query raw transaction by a given hash */ const getRawTransaction = ({ haskoinUrl, txId }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const result = yield axios_1.default .get(`${haskoinUrl}/transaction/${txId}/raw`) .then((response) => (isErrorResponse(response.data) ? null : response.data)); if (!result) { throw new Error('failed to query transaction by a given hash'); } return result.result; } catch (error) { return Promise.reject(error); } }); exports.getRawTransaction = getRawTransaction; /** * Get transaction history. * * @param {string} haskoinUrl The haskoin API url. * @param {string} address The BCH address. * @param {TransactionsQueryParam} params The API query parameters. * @returns {Array<Transaction>} * * @throws {"failed to query transactions"} thrown if failed to query transactions */ const getTransactions = ({ haskoinUrl, address, params, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { const result = yield axios_1.default .get(`${haskoinUrl}/address/${address}/transactions/full`, { params, }) .then((response) => (isErrorResponse(response.data) ? null : response.data)); if (!result) { throw new Error('failed to query transactions'); } return result; } catch (error) { return Promise.reject(error); } }); exports.getTransactions = getTransactions; /** * Get unspent transactions. * * @param {string} haskoinUrl The haskoin API url. * @param {string} address The BCH address. * @returns {Array<TxUnspent>} * * @throws {"failed to query unspent transactions"} thrown if failed to query unspent transactions */ const getUnspentTransactions = ({ haskoinUrl, address }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { try { // Get transacton count for a given address. const account = yield exports.getAccount({ haskoinUrl, address }); // Set limit to the transaction count to be all the utxos. const result = yield axios_1.default .get(`${haskoinUrl}/address/${address}/unspent?limit=${account === null || account === void 0 ? void 0 : account.utxo}`) .then((response) => (isErrorResponse(response.data) ? null : response.data)); if (!result) { throw new Error('failed to query unspent transactions'); } return result; } catch (error) { return Promise.reject(error); } }); exports.getUnspentTransactions = getUnspentTransactions; /** * Get suggested fee amount for Bitcoin cash. (fee per byte) * * @returns {number} The Bitcoin cash stats. */ const getSuggestedFee = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { //Note: Haskcoin does not provide fee rate related data //So use Bitgo API for fee estimation //Refer: https://app.bitgo.com/docs/#operation/v2.tx.getfeeestimate try { const response = yield axios_1.default.get('https://app.bitgo.com/api/v2/bch/tx/fee'); return response.data.feePerKb / 1000; // feePerKb to feePerByte } catch (error) { return utils_1.DEFAULT_SUGGESTED_TRANSACTION_FEE; } }); exports.getSuggestedFee = getSuggestedFee; //# sourceMappingURL=haskoin-api.js.map