Explorer page with latest blocks. WIP

This commit is contained in:
Simon Lindh
2019-11-06 15:35:02 +08:00
parent 7344c518d3
commit 02d67e8406
22 changed files with 225 additions and 20 deletions

View File

@@ -7,4 +7,7 @@ export interface AbstractBitcoinApi {
getBlockCount(): Promise<number>;
getBlock(hash: string): Promise<IBlock>;
getBlockHash(height: number): Promise<string>;
getBlocks(): Promise<string>;
getBlocksFromHeight(height: number): Promise<string>;
}

View File

@@ -80,6 +80,14 @@ class BitcoindApi implements AbstractBitcoinApi {
});
});
}
getBlocks(): Promise<string> {
throw new Error('Method not implemented.');
}
getBlocksFromHeight(height: number): Promise<string> {
throw new Error('Method not implemented.');
}
}
export default BitcoindApi;

View File

@@ -94,6 +94,28 @@ class EsploraApi implements AbstractBitcoinApi {
}
});
}
getBlocks(): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
const response: AxiosResponse = await this.client.get('/blocks');
resolve(response.data);
} catch (error) {
reject(error);
}
});
}
getBlocksFromHeight(height: number): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
const response: AxiosResponse = await this.client.get('/blocks/' + height);
resolve(response.data);
} catch (error) {
reject(error);
}
});
}
}
export default EsploraApi;

View File

@@ -263,7 +263,15 @@ class MempoolSpace {
.get(config.API_ENDPOINT + 'statistics/3m', routes.get3MStatistics.bind(routes))
.get(config.API_ENDPOINT + 'statistics/6m', routes.get6MStatistics.bind(routes))
;
if (config.BACKEND_API === 'esplora') {
this.app
.get(config.API_ENDPOINT + 'explorer/blocks', routes.getBlocks)
.get(config.API_ENDPOINT + 'explorer/blocks/:height', routes.getBlocks)
;
}
}
}
}
const mempoolSpace = new MempoolSpace();

View File

@@ -1,6 +1,7 @@
import statistics from './api/statistics';
import feeApi from './api/fee-api';
import projectedBlocks from './api/projected-blocks';
import bitcoinApi from './api/bitcoin/bitcoin-api-factory';
class Routes {
private cache = {};
@@ -75,6 +76,20 @@ class Routes {
res.status(500).send(e.message);
}
}
public async getBlocks(req, res) {
try {
let result: string;
if (req.params.height) {
result = await bitcoinApi.getBlocksFromHeight(req.params.height);
} else {
result = await bitcoinApi.getBlocks();
}
res.send(result);
} catch (e) {
res.status(500).send(e.message);
}
}
}
export default new Routes();