WIP: Bisq DAO support. Transactions list and details.

This commit is contained in:
softsimon
2020-07-03 23:45:19 +07:00
parent 94ccd98d0a
commit d39b4a5c92
59 changed files with 926 additions and 38 deletions

78
backend/src/api/bisq.ts Normal file
View File

@@ -0,0 +1,78 @@
import * as fs from 'fs';
import { BisqBlocks, BisqBlock, BisqTransaction } from '../interfaces';
class Bisq {
static FILE_NAME = './blocks.json';
private latestBlockHeight = 0;
private blocks: BisqBlock[] = [];
private transactions: BisqTransaction[] = [];
private transactionsIndex: { [txId: string]: BisqTransaction } = {};
private blocksIndex: { [hash: string]: BisqBlock } = {};
constructor() {}
startBisqService(): void {
this.loadBisqDumpFile();
}
async loadBisqDumpFile(): Promise<void> {
await this.loadBisqBlocksDump();
this.buildIndex();
}
getTransaction(txId: string): BisqTransaction | undefined {
return this.transactionsIndex[txId];
}
getTransactions(start: number, length: number): [BisqTransaction[], number] {
return [this.transactions.slice(start, length + start), this.transactions.length];
}
getBlock(hash: string): BisqBlock | undefined {
return this.blocksIndex[hash];
}
private buildIndex() {
this.blocks.forEach((block) => {
this.blocksIndex[block.hash] = block;
block.txs.forEach((tx) => {
this.transactions.push(tx);
this.transactionsIndex[tx.id] = tx;
});
});
this.blocks.reverse();
this.transactions.reverse();
console.log('Bisq data index rebuilt');
}
private async loadBisqBlocksDump() {
const start = new Date().getTime();
const cacheData = await this.loadData();
if (cacheData) {
console.log('Parsing Bisq data from dump file');
const data: BisqBlocks = JSON.parse(cacheData);
if (data.blocks) {
this.blocks = data.blocks;
this.latestBlockHeight = data.chainHeight;
const end = new Date().getTime();
const time = end - start;
console.log('Loaded bisq dump in ' + time + ' ms');
} else {
throw new Error(`Bisq dump didn't contain any blocks`);
}
}
}
private loadData(): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(Bisq.FILE_NAME, 'utf8', (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});
}
}
export default new Bisq();

View File

@@ -73,10 +73,10 @@ class Blocks {
console.log(`${found} of ${txIds.length} found in mempool. ${notFound} not found.`);
block.reward = transactions[0].vout.reduce((acc, curr) => acc + curr.value, 0);
block.coinbaseTx = this.stripCoinbaseTransaction(transactions[0]);
transactions.sort((a, b) => b.feePerVsize - a.feePerVsize);
block.medianFee = transactions.length > 1 ? Common.median(transactions.map((tx) => tx.feePerVsize)) : 0;
block.feeRange = transactions.length > 1 ? Common.getFeesInRange(transactions, 8, 1) : [0, 0];
block.coinbaseTx = this.stripCoinbaseTransaction(transactions[0]);
this.blocks.push(block);
if (this.blocks.length > config.KEEP_BLOCK_AMOUNT) {

View File

@@ -35,6 +35,9 @@ class Mempool {
public setMempool(mempoolData: { [txId: string]: TransactionExtended }) {
this.mempoolCache = mempoolData;
if (this.mempoolChangedCallback) {
this.mempoolChangedCallback(this.mempoolCache, [], []);
}
}
public async updateMemPoolInfo() {