electrs backend request and error handling improvements

This commit is contained in:
Simon Lindh 2019-12-09 19:14:54 +07:00
parent d1fce86adb
commit 67fb17f242
2 changed files with 109 additions and 64 deletions

View File

@ -9,173 +9,218 @@ class ElectrsApi implements AbstractBitcoinApi {
}
getMempoolInfo(): Promise<IMempoolInfo> {
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<ITransaction['txid'][]> {
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<ITransaction> {
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<number> {
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<IBlock> {
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<string> {
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<string> {
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<string> {
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<IBlock> {
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<IBlock> {
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<IBlock> {
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<IBlock> {
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<IBlock> {
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<IBlock> {
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);
});
});
}

View File

@ -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"