diff --git a/backend/src/api/bitcoin/electrs-api.ts b/backend/src/api/bitcoin/electrs-api.ts index a378bdef4..12489b299 100644 --- a/backend/src/api/bitcoin/electrs-api.ts +++ b/backend/src/api/bitcoin/electrs-api.ts @@ -9,173 +9,218 @@ class ElectrsApi implements AbstractBitcoinApi { } getMempoolInfo(): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/mempool', { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/mempool', { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve({ + size: response.count, + bytes: response.vsize, + }); } - resolve({ - size: response.count, - bytes: response.vsize, - }); }); }); } getRawMempool(): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/mempool/txids', { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/mempool/txids', { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getRawTransaction(txId: string): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/tx/' + txId, { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/tx/' + txId, { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + response.vsize = Math.round(response.weight / 4); + response.fee = response.fee / 100000000; + response.blockhash = response.status.block_hash; + resolve(response); } - response.vsize = Math.round(response.weight / 4); - response.fee = response.fee / 100000000; - response.blockhash = response.status.block_hash; - - resolve(response); }); }); } getBlockCount(): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/blocks/tip/height', { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/blocks/tip/height', { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getBlockAndTransactions(hash: string): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/block/' + hash, { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/block/' + hash, { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); - } - request(config.ELECTRS_API_URL + '/block/' + hash + '/txids', { json: true }, (err2, res2, response2) => { - if (err2) { - reject(err2); - } - const block = response; - block.hash = hash; - block.nTx = block.tx_count; - block.time = block.timestamp; - block.tx = response2; + } else if (res.statusCode !== 200) { + reject(response); + } else { + request(config.ELECTRS_API_URL + '/block/' + hash + '/txids', { json: true, timeout: 10000 }, (err2, res2, response2) => { + if (err2) { + reject(err2); + } else if (res.statusCode !== 200) { + reject(response); + } else { + const block = response; + block.hash = hash; + block.nTx = block.tx_count; + block.time = block.timestamp; + block.tx = response2; - resolve(block); - }); + resolve(block); + } + }); + } }); }); } getBlockHash(height: number): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/block-height/' + height, { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/block-height/' + height, { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getBlocks(): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/blocks', { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/blocks', { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getBlocksFromHeight(height: number): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/blocks/' + height, { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/blocks/' + height, { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getBlock(hash: string): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/block/' + hash, { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/block/' + hash, { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getBlockTransactions(hash: string): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/block/' + hash + '/txs', { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/block/' + hash + '/txs', { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getBlockTransactionsFromIndex(hash: string, index: number): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/block/' + hash + '/txs/' + index, { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/block/' + hash + '/txs/' + index, { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getAddress(address: string): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/address/' + address, { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/address/' + address, { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getAddressTransactions(address: string): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/address/' + address + '/txs', { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/address/' + address + '/txs', { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } getAddressTransactionsFromLastSeenTxid(address: string, lastSeenTxid: string): Promise { - return new Promise(async (resolve, reject) => { - request(config.ELECTRS_API_URL + '/address/' + address + '/txs/chain/' + lastSeenTxid, { json: true }, (err, res, response) => { + return new Promise((resolve, reject) => { + request(config.ELECTRS_API_URL + '/address/' + address + '/txs/chain/' + lastSeenTxid, + { json: true, timeout: 10000 }, (err, res, response) => { if (err) { reject(err); + } else if (res.statusCode !== 200) { + reject(response); + } else { + resolve(response); } - resolve(response); }); }); } diff --git a/frontend/yarn.lock b/frontend/yarn.lock index f065f7bba..94e462db7 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -1619,10 +1619,10 @@ core-js@^2.4.0: resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.10.tgz#8a5b8391f8cc7013da703411ce5b585706300d7f" integrity sha512-I39t74+4t+zau64EN1fE5v2W31Adtc/REhzWN+gWRRXg6WH5qAsZm62DHpQ1+Yhe4047T55jvzz7MUqF/dBBlA== -core-js@^3.3.3: - version "3.4.1" - resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.4.1.tgz#76dd6828412900ab27c8ce0b22e6114d7ce21b18" - integrity sha512-KX/dnuY/J8FtEwbnrzmAjUYgLqtk+cxM86hfG60LGiW3MmltIc2yAmDgBgEkfm0blZhUrdr1Zd84J2Y14mLxzg== +core-js@^3.4.1: + version "3.4.7" + resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.4.7.tgz#57c35937da80fe494fbc3adcf9cf3dc00eb86b34" + integrity sha512-qaPVGw30J1wQ0GR3GvoPqlGf9GZfKKF4kFC7kiHlcsPTqH3txrs9crCp3ZiMAXuSenhz89Jnl4GZs/67S5VOSg== core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2"