Merge pull request #1351 from nymkappa/bugfix/hashrates-indexing-duplicates

Fix duplicate hashrate data points in "difficulty vs hashrate" chart
This commit is contained in:
wiz 2022-03-13 23:05:45 +00:00 committed by GitHub
commit 1052b19fae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 214 additions and 169 deletions

View File

@ -70,7 +70,7 @@ class BitcoinApi implements AbstractBitcoinApi {
} }
return this.bitcoindClient.getBlock(hash) return this.bitcoindClient.getBlock(hash)
.then((block: IBitcoinApi.Block) => this.convertBlock(block)); .then((block: IBitcoinApi.Block) => BitcoinApi.convertBlock(block));
} }
$getAddress(address: string): Promise<IEsploraApi.Address> { $getAddress(address: string): Promise<IEsploraApi.Address> {
@ -186,7 +186,7 @@ class BitcoinApi implements AbstractBitcoinApi {
return esploraTransaction; return esploraTransaction;
} }
private convertBlock(block: IBitcoinApi.Block): IEsploraApi.Block { static convertBlock(block: IBitcoinApi.Block): IEsploraApi.Block {
return { return {
id: block.hash, id: block.hash,
height: block.height, height: block.height,

View File

@ -11,6 +11,7 @@ import { IEsploraApi } from './bitcoin/esplora-api.interface';
import poolsRepository from '../repositories/PoolsRepository'; import poolsRepository from '../repositories/PoolsRepository';
import blocksRepository from '../repositories/BlocksRepository'; import blocksRepository from '../repositories/BlocksRepository';
import loadingIndicators from './loading-indicators'; import loadingIndicators from './loading-indicators';
import BitcoinApi from './bitcoin/bitcoin-api';
class Blocks { class Blocks {
private blocks: BlockExtended[] = []; private blocks: BlockExtended[] = [];
@ -103,8 +104,8 @@ class Blocks {
* @param transactions * @param transactions
* @returns BlockExtended * @returns BlockExtended
*/ */
private async $getBlockExtended(block: IEsploraApi.Block, transactions: TransactionExtended[]): Promise<BlockExtended> { private async $getBlockExtended(block: IEsploraApi.Block, transactions: TransactionExtended[]): Promise<BlockExtended> {
const blockExtended: BlockExtended = Object.assign({extras: {}}, block); const blockExtended: BlockExtended = Object.assign({ extras: {} }, block);
blockExtended.extras.reward = transactions[0].vout.reduce((acc, curr) => acc + curr.value, 0); blockExtended.extras.reward = transactions[0].vout.reduce((acc, curr) => acc + curr.value, 0);
blockExtended.extras.coinbaseTx = transactionUtils.stripCoinbaseTransaction(transactions[0]); blockExtended.extras.coinbaseTx = transactionUtils.stripCoinbaseTransaction(transactions[0]);
@ -112,19 +113,19 @@ class Blocks {
blockExtended.extras.coinbaseRaw = coinbaseRaw.hex; blockExtended.extras.coinbaseRaw = coinbaseRaw.hex;
if (block.height === 0) { if (block.height === 0) {
blockExtended.extras.medianFee = 0; // 50th percentiles blockExtended.extras.medianFee = 0; // 50th percentiles
blockExtended.extras.feeRange = [0, 0, 0, 0, 0, 0, 0]; blockExtended.extras.feeRange = [0, 0, 0, 0, 0, 0, 0];
blockExtended.extras.totalFees = 0; blockExtended.extras.totalFees = 0;
blockExtended.extras.avgFee = 0; blockExtended.extras.avgFee = 0;
blockExtended.extras.avgFeeRate = 0; blockExtended.extras.avgFeeRate = 0;
} else { } else {
const stats = await bitcoinClient.getBlockStats(block.id); const stats = await bitcoinClient.getBlockStats(block.id);
blockExtended.extras.medianFee = stats.feerate_percentiles[2]; // 50th percentiles blockExtended.extras.medianFee = stats.feerate_percentiles[2]; // 50th percentiles
blockExtended.extras.feeRange = [stats.minfeerate, stats.feerate_percentiles, stats.maxfeerate].flat(); blockExtended.extras.feeRange = [stats.minfeerate, stats.feerate_percentiles, stats.maxfeerate].flat();
blockExtended.extras.totalFees = stats.totalfee; blockExtended.extras.totalFees = stats.totalfee;
blockExtended.extras.avgFee = stats.avgfee; blockExtended.extras.avgFee = stats.avgfee;
blockExtended.extras.avgFeeRate = stats.avgfeerate; blockExtended.extras.avgFeeRate = stats.avgfeerate;
} }
if (Common.indexingEnabled()) { if (Common.indexingEnabled()) {
let pool: PoolTag; let pool: PoolTag;
@ -239,7 +240,7 @@ class Blocks {
indexedThisRun = 0; indexedThisRun = 0;
} }
const blockHash = await bitcoinApi.$getBlockHash(blockHeight); const blockHash = await bitcoinApi.$getBlockHash(blockHeight);
const block = await bitcoinApi.$getBlock(blockHash); const block = BitcoinApi.convertBlock(await bitcoinClient.getBlock(blockHash));
const transactions = await this.$getTransactionsExtended(blockHash, block.height, true, true); const transactions = await this.$getTransactionsExtended(blockHash, block.height, true, true);
const blockExtended = await this.$getBlockExtended(block, transactions); const blockExtended = await this.$getBlockExtended(block, transactions);
await blocksRepository.$saveBlockInDatabase(blockExtended); await blocksRepository.$saveBlockInDatabase(blockExtended);
@ -276,7 +277,7 @@ class Blocks {
if (blockchainInfo.blocks === blockchainInfo.headers) { if (blockchainInfo.blocks === blockchainInfo.headers) {
const heightDiff = blockHeightTip % 2016; const heightDiff = blockHeightTip % 2016;
const blockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff); const blockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff);
const block = await bitcoinApi.$getBlock(blockHash); const block = BitcoinApi.convertBlock(await bitcoinClient.getBlock(blockHash));
this.lastDifficultyAdjustmentTime = block.timestamp; this.lastDifficultyAdjustmentTime = block.timestamp;
this.currentDifficulty = block.difficulty; this.currentDifficulty = block.difficulty;
@ -300,7 +301,7 @@ class Blocks {
} }
const blockHash = await bitcoinApi.$getBlockHash(this.currentBlockHeight); const blockHash = await bitcoinApi.$getBlockHash(this.currentBlockHeight);
const block = await bitcoinApi.$getBlock(blockHash); const block = BitcoinApi.convertBlock(await bitcoinClient.getBlock(blockHash));
const txIds: string[] = await bitcoinApi.$getTxIdsForBlock(blockHash); const txIds: string[] = await bitcoinApi.$getTxIdsForBlock(blockHash);
const transactions = await this.$getTransactionsExtended(blockHash, block.height, false); const transactions = await this.$getTransactionsExtended(blockHash, block.height, false);
const blockExtended: BlockExtended = await this.$getBlockExtended(block, transactions); const blockExtended: BlockExtended = await this.$getBlockExtended(block, transactions);
@ -332,14 +333,14 @@ class Blocks {
/** /**
* Index a block if it's missing from the database. Returns the block after indexing * Index a block if it's missing from the database. Returns the block after indexing
*/ */
public async $indexBlock(height: number): Promise<BlockExtended> { public async $indexBlock(height: number): Promise<BlockExtended> {
const dbBlock = await blocksRepository.$getBlockByHeight(height); const dbBlock = await blocksRepository.$getBlockByHeight(height);
if (dbBlock != null) { if (dbBlock != null) {
return this.prepareBlock(dbBlock); return this.prepareBlock(dbBlock);
} }
const blockHash = await bitcoinApi.$getBlockHash(height); const blockHash = await bitcoinApi.$getBlockHash(height);
const block = await bitcoinApi.$getBlock(blockHash); const block = BitcoinApi.convertBlock(await bitcoinClient.getBlock(blockHash));
const transactions = await this.$getTransactionsExtended(blockHash, block.height, true); const transactions = await this.$getTransactionsExtended(blockHash, block.height, true);
const blockExtended = await this.$getBlockExtended(block, transactions); const blockExtended = await this.$getBlockExtended(block, transactions);

View File

@ -6,7 +6,7 @@ import logger from '../logger';
const sleep = (ms: number) => new Promise(res => setTimeout(res, ms)); const sleep = (ms: number) => new Promise(res => setTimeout(res, ms));
class DatabaseMigration { class DatabaseMigration {
private static currentVersion = 13; private static currentVersion = 14;
private queryTimeout = 120000; private queryTimeout = 120000;
private statisticsAddedIndexed = false; private statisticsAddedIndexed = false;
@ -77,7 +77,7 @@ class DatabaseMigration {
await this.$setStatisticsAddedIndexedFlag(databaseSchemaVersion); await this.$setStatisticsAddedIndexedFlag(databaseSchemaVersion);
const isBitcoin = ['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK); const isBitcoin = ['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
await this.$executeQuery(connection, this.getCreateElementsTableQuery(), await this.$checkIfTableExists('elements_pegs')); await this.$executeQuery(connection, this.getCreateElementsTableQuery(), await this.$checkIfTableExists('elements_pegs'));
await this.$executeQuery(connection, this.getCreateStatisticsQuery(), await this.$checkIfTableExists('statistics')); await this.$executeQuery(connection, this.getCreateStatisticsQuery(), await this.$checkIfTableExists('statistics'));
@ -168,6 +168,13 @@ class DatabaseMigration {
await this.$executeQuery(connection, 'ALTER TABLE blocks MODIFY `avg_fee_rate` BIGINT UNSIGNED NOT NULL DEFAULT "0"'); await this.$executeQuery(connection, 'ALTER TABLE blocks MODIFY `avg_fee_rate` BIGINT UNSIGNED NOT NULL DEFAULT "0"');
} }
if (databaseSchemaVersion < 14 && isBitcoin === true) {
logger.warn(`'hashrates' table has been truncated. Re-indexing from scratch.`);
await this.$executeQuery(connection, 'TRUNCATE hashrates;'); // Need to re-index
await this.$executeQuery(connection, 'ALTER TABLE `hashrates` DROP FOREIGN KEY `hashrates_ibfk_1`');
await this.$executeQuery(connection, 'ALTER TABLE `hashrates` MODIFY `pool_id` SMALLINT UNSIGNED NOT NULL DEFAULT "0"');
}
connection.release(); connection.release();
} catch (e) { } catch (e) {
connection.release(); connection.release();
@ -187,7 +194,7 @@ class DatabaseMigration {
return; return;
} }
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
// We don't use "CREATE INDEX IF NOT EXISTS" because it is not supported on old mariadb version 5.X // We don't use "CREATE INDEX IF NOT EXISTS" because it is not supported on old mariadb version 5.X
@ -225,7 +232,7 @@ class DatabaseMigration {
* Check if 'table' exists in the database * Check if 'table' exists in the database
*/ */
private async $checkIfTableExists(table: string): Promise<boolean> { private async $checkIfTableExists(table: string): Promise<boolean> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '${config.DATABASE.DATABASE}' AND TABLE_NAME = '${table}'`; const query = `SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '${config.DATABASE.DATABASE}' AND TABLE_NAME = '${table}'`;
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();
@ -236,7 +243,7 @@ class DatabaseMigration {
* Get current database version * Get current database version
*/ */
private async $getSchemaVersionFromDatabase(): Promise<number> { private async $getSchemaVersionFromDatabase(): Promise<number> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `SELECT number FROM state WHERE name = 'schema_version';`; const query = `SELECT number FROM state WHERE name = 'schema_version';`;
const [rows] = await this.$executeQuery(connection, query, true); const [rows] = await this.$executeQuery(connection, query, true);
connection.release(); connection.release();
@ -247,7 +254,7 @@ class DatabaseMigration {
* Create the `state` table * Create the `state` table
*/ */
private async $createMigrationStateTable(): Promise<void> { private async $createMigrationStateTable(): Promise<void> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const query = `CREATE TABLE IF NOT EXISTS state ( const query = `CREATE TABLE IF NOT EXISTS state (
@ -279,7 +286,7 @@ class DatabaseMigration {
} }
transactionQueries.push(this.getUpdateToLatestSchemaVersionQuery()); transactionQueries.push(this.getUpdateToLatestSchemaVersionQuery());
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
await this.$executeQuery(connection, 'START TRANSACTION;'); await this.$executeQuery(connection, 'START TRANSACTION;');
for (const query of transactionQueries) { for (const query of transactionQueries) {
@ -330,7 +337,7 @@ class DatabaseMigration {
* Print current database version * Print current database version
*/ */
private async $printDatabaseVersion() { private async $printDatabaseVersion() {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows] = await this.$executeQuery(connection, 'SELECT VERSION() as version;', true); const [rows] = await this.$executeQuery(connection, 'SELECT VERSION() as version;', true);
logger.debug(`MIGRATIONS: Database engine version '${rows[0].version}'`); logger.debug(`MIGRATIONS: Database engine version '${rows[0].version}'`);
@ -474,7 +481,7 @@ class DatabaseMigration {
public async $truncateIndexedData(tables: string[]) { public async $truncateIndexedData(tables: string[]) {
const allowedTables = ['blocks', 'hashrates']; const allowedTables = ['blocks', 'hashrates'];
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
for (const table of tables) { for (const table of tables) {
if (!allowedTables.includes(table)) { if (!allowedTables.includes(table)) {

View File

@ -33,7 +33,7 @@ class ElementsParser {
} }
public async $getPegDataByMonth(): Promise<any> { public async $getPegDataByMonth(): Promise<any> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `SELECT SUM(amount) AS amount, DATE_FORMAT(FROM_UNIXTIME(datetime), '%Y-%m-01') AS date FROM elements_pegs GROUP BY DATE_FORMAT(FROM_UNIXTIME(datetime), '%Y%m')`; const query = `SELECT SUM(amount) AS amount, DATE_FORMAT(FROM_UNIXTIME(datetime), '%Y-%m-01') AS date FROM elements_pegs GROUP BY DATE_FORMAT(FROM_UNIXTIME(datetime), '%Y%m')`;
const [rows] = await connection.query<any>(query); const [rows] = await connection.query<any>(query);
connection.release(); connection.release();
@ -79,7 +79,7 @@ class ElementsParser {
protected async $savePegToDatabase(height: number, blockTime: number, amount: number, txid: string, protected async $savePegToDatabase(height: number, blockTime: number, amount: number, txid: string,
txindex: number, bitcoinaddress: string, bitcointxid: string, bitcoinindex: number, final_tx: number): Promise<void> { txindex: number, bitcoinaddress: string, bitcointxid: string, bitcoinindex: number, final_tx: number): Promise<void> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `INSERT INTO elements_pegs( const query = `INSERT INTO elements_pegs(
block, datetime, amount, txid, txindex, bitcoinaddress, bitcointxid, bitcoinindex, final_tx block, datetime, amount, txid, txindex, bitcoinaddress, bitcointxid, bitcoinindex, final_tx
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`; ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
@ -93,7 +93,7 @@ class ElementsParser {
} }
protected async $getLatestBlockHeightFromDatabase(): Promise<number> { protected async $getLatestBlockHeightFromDatabase(): Promise<number> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `SELECT number FROM state WHERE name = 'last_elements_block'`; const query = `SELECT number FROM state WHERE name = 'last_elements_block'`;
const [rows] = await connection.query<any>(query); const [rows] = await connection.query<any>(query);
connection.release(); connection.release();
@ -101,7 +101,7 @@ class ElementsParser {
} }
protected async $saveLatestBlockToDatabase(blockHeight: number) { protected async $saveLatestBlockToDatabase(blockHeight: number) {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `UPDATE state SET number = ? WHERE name = 'last_elements_block'`; const query = `UPDATE state SET number = ? WHERE name = 'last_elements_block'`;
await connection.query<any>(query, [blockHeight]); await connection.query<any>(query, [blockHeight]);
connection.release(); connection.release();

View File

@ -80,8 +80,8 @@ class Mining {
// We only run this once a week // We only run this once a week
const latestTimestamp = await HashratesRepository.$getLatestRunTimestamp('last_weekly_hashrates_indexing'); const latestTimestamp = await HashratesRepository.$getLatestRunTimestamp('last_weekly_hashrates_indexing');
const now = new Date().getTime() / 1000; const now = new Date();
if (now - latestTimestamp < 604800) { if ((now.getTime() / 1000) - latestTimestamp < 604800) {
return; return;
} }
@ -94,7 +94,6 @@ class Mining {
const hashrates: any[] = []; const hashrates: any[] = [];
const genesisTimestamp = 1231006505; // bitcoin-cli getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f const genesisTimestamp = 1231006505; // bitcoin-cli getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
const now = new Date();
const lastMonday = new Date(now.setDate(now.getDate() - (now.getDay() + 6) % 7)); const lastMonday = new Date(now.setDate(now.getDate() - (now.getDay() + 6) % 7));
const lastMondayMidnight = this.getDateMidnight(lastMonday); const lastMondayMidnight = this.getDateMidnight(lastMonday);
let toTimestamp = Math.round((lastMondayMidnight.getTime() - 604800) / 1000); let toTimestamp = Math.round((lastMondayMidnight.getTime() - 604800) / 1000);
@ -108,7 +107,7 @@ class Mining {
const fromTimestamp = toTimestamp - 604800; const fromTimestamp = toTimestamp - 604800;
// Skip already indexed weeks // Skip already indexed weeks
if (indexedTimestamp.includes(toTimestamp + 1)) { if (indexedTimestamp.includes(toTimestamp)) {
toTimestamp -= 604800; toTimestamp -= 604800;
++totalIndexed; ++totalIndexed;
continue; continue;
@ -133,7 +132,7 @@ class Mining {
for (const pool of pools) { for (const pool of pools) {
hashrates.push({ hashrates.push({
hashrateTimestamp: toTimestamp + 1, hashrateTimestamp: toTimestamp,
avgHashrate: pool['hashrate'], avgHashrate: pool['hashrate'],
poolId: pool.poolId, poolId: pool.poolId,
share: pool['share'], share: pool['share'],
@ -202,7 +201,7 @@ class Mining {
const fromTimestamp = toTimestamp - 86400; const fromTimestamp = toTimestamp - 86400;
// Skip already indexed weeks // Skip already indexed weeks
if (indexedTimestamp.includes(fromTimestamp)) { if (indexedTimestamp.includes(toTimestamp)) {
toTimestamp -= 86400; toTimestamp -= 86400;
++totalIndexed; ++totalIndexed;
continue; continue;
@ -220,7 +219,7 @@ class Mining {
hashrates.push({ hashrates.push({
hashrateTimestamp: toTimestamp, hashrateTimestamp: toTimestamp,
avgHashrate: lastBlockHashrate, avgHashrate: lastBlockHashrate,
poolId: null, poolId: 0,
share: 1, share: 1,
type: 'daily', type: 'daily',
}); });

View File

@ -66,7 +66,7 @@ class PoolsParser {
logger.debug(`Found ${poolNames.length} unique mining pools`); logger.debug(`Found ${poolNames.length} unique mining pools`);
// Get existing pools from the db // Get existing pools from the db
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
let existingPools; let existingPools;
try { try {
[existingPools] = await connection.query<any>({ sql: 'SELECT * FROM pools;', timeout: 120000 }); [existingPools] = await connection.query<any>({ sql: 'SELECT * FROM pools;', timeout: 120000 });
@ -152,7 +152,7 @@ class PoolsParser {
* Manually add the 'unknown pool' * Manually add the 'unknown pool'
*/ */
private async insertUnknownPool() { private async insertUnknownPool() {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows]: any[] = await connection.query({ sql: 'SELECT name from pools where name="Unknown"', timeout: 120000 }); const [rows]: any[] = await connection.query({ sql: 'SELECT name from pools where name="Unknown"', timeout: 120000 });
if (rows.length === 0) { if (rows.length === 0) {

View File

@ -155,7 +155,7 @@ class Statistics {
} }
private async $createZeroedStatistic(): Promise<number | undefined> { private async $createZeroedStatistic(): Promise<number | undefined> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const query = `INSERT INTO statistics( const query = `INSERT INTO statistics(
added, added,
@ -216,7 +216,7 @@ class Statistics {
} }
private async $create(statistics: Statistic): Promise<number | undefined> { private async $create(statistics: Statistic): Promise<number | undefined> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const query = `INSERT INTO statistics( const query = `INSERT INTO statistics(
added, added,
@ -421,7 +421,7 @@ class Statistics {
private async $get(id: number): Promise<OptimizedStatistic | undefined> { private async $get(id: number): Promise<OptimizedStatistic | undefined> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `SELECT *, UNIX_TIMESTAMP(added) as added FROM statistics WHERE id = ?`; const query = `SELECT *, UNIX_TIMESTAMP(added) as added FROM statistics WHERE id = ?`;
const [rows] = await connection.query<any>(query, [id]); const [rows] = await connection.query<any>(query, [id]);
connection.release(); connection.release();
@ -435,7 +435,7 @@ class Statistics {
public async $list2H(): Promise<OptimizedStatistic[]> { public async $list2H(): Promise<OptimizedStatistic[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `SELECT *, UNIX_TIMESTAMP(added) as added FROM statistics ORDER BY statistics.added DESC LIMIT 120`; const query = `SELECT *, UNIX_TIMESTAMP(added) as added FROM statistics ORDER BY statistics.added DESC LIMIT 120`;
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();
@ -448,7 +448,7 @@ class Statistics {
public async $list24H(): Promise<OptimizedStatistic[]> { public async $list24H(): Promise<OptimizedStatistic[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `SELECT *, UNIX_TIMESTAMP(added) as added FROM statistics ORDER BY statistics.added DESC LIMIT 1440`; const query = `SELECT *, UNIX_TIMESTAMP(added) as added FROM statistics ORDER BY statistics.added DESC LIMIT 1440`;
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();
@ -461,7 +461,7 @@ class Statistics {
public async $list1W(): Promise<OptimizedStatistic[]> { public async $list1W(): Promise<OptimizedStatistic[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = this.getQueryForDaysAvg(300, '1 WEEK'); // 5m interval const query = this.getQueryForDaysAvg(300, '1 WEEK'); // 5m interval
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();
@ -474,7 +474,7 @@ class Statistics {
public async $list1M(): Promise<OptimizedStatistic[]> { public async $list1M(): Promise<OptimizedStatistic[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = this.getQueryForDaysAvg(1800, '1 MONTH'); // 30m interval const query = this.getQueryForDaysAvg(1800, '1 MONTH'); // 30m interval
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();
@ -487,7 +487,7 @@ class Statistics {
public async $list3M(): Promise<OptimizedStatistic[]> { public async $list3M(): Promise<OptimizedStatistic[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = this.getQueryForDaysAvg(7200, '3 MONTH'); // 2h interval const query = this.getQueryForDaysAvg(7200, '3 MONTH'); // 2h interval
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();
@ -500,7 +500,7 @@ class Statistics {
public async $list6M(): Promise<OptimizedStatistic[]> { public async $list6M(): Promise<OptimizedStatistic[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = this.getQueryForDaysAvg(10800, '6 MONTH'); // 3h interval const query = this.getQueryForDaysAvg(10800, '6 MONTH'); // 3h interval
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();
@ -513,7 +513,7 @@ class Statistics {
public async $list1Y(): Promise<OptimizedStatistic[]> { public async $list1Y(): Promise<OptimizedStatistic[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = this.getQueryForDays(28800, '1 YEAR'); // 8h interval const query = this.getQueryForDays(28800, '1 YEAR'); // 8h interval
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();
@ -526,7 +526,7 @@ class Statistics {
public async $list2Y(): Promise<OptimizedStatistic[]> { public async $list2Y(): Promise<OptimizedStatistic[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = this.getQueryForDays(28800, "2 YEAR"); // 8h interval const query = this.getQueryForDays(28800, "2 YEAR"); // 8h interval
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();
@ -539,7 +539,7 @@ class Statistics {
public async $list3Y(): Promise<OptimizedStatistic[]> { public async $list3Y(): Promise<OptimizedStatistic[]> {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = this.getQueryForDays(43200, "3 YEAR"); // 12h interval const query = this.getQueryForDays(43200, "3 YEAR"); // 12h interval
const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout }); const [rows] = await connection.query<any>({ sql: query, timeout: this.queryTimeout });
connection.release(); connection.release();

View File

@ -1,5 +1,5 @@
import config from './config'; import config from './config';
import { createPool } from 'mysql2/promise'; import { createPool, PoolConnection } from 'mysql2/promise';
import logger from './logger'; import logger from './logger';
export class DB { export class DB {
@ -11,13 +11,24 @@ export class DB {
password: config.DATABASE.PASSWORD, password: config.DATABASE.PASSWORD,
connectionLimit: 10, connectionLimit: 10,
supportBigNumbers: true, supportBigNumbers: true,
timezone: '+00:00',
}); });
static connectionsReady: number[] = [];
static async getConnection() {
const connection: PoolConnection = await DB.pool.getConnection();
const connectionId = connection['connection'].connectionId;
if (!DB.connectionsReady.includes(connectionId)) {
await connection.query(`SET time_zone='+00:00';`);
this.connectionsReady.push(connectionId);
}
return connection;
}
} }
export async function checkDbConnection() { export async function checkDbConnection() {
try { try {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
logger.info('Database connection established.'); logger.info('Database connection established.');
connection.release(); connection.release();
} catch (e) { } catch (e) {

View File

@ -5,7 +5,7 @@ import * as WebSocket from 'ws';
import * as cluster from 'cluster'; import * as cluster from 'cluster';
import axios from 'axios'; import axios from 'axios';
import { checkDbConnection } from './database'; import { checkDbConnection, DB } from './database';
import config from './config'; import config from './config';
import routes from './routes'; import routes from './routes';
import blocks from './api/blocks'; import blocks from './api/blocks';
@ -180,8 +180,8 @@ class Server {
try { try {
blocks.$generateBlockDatabase(); blocks.$generateBlockDatabase();
mining.$generateNetworkHashrateHistory(); await mining.$generateNetworkHashrateHistory();
mining.$generatePoolHashrateHistory(); await mining.$generatePoolHashrateHistory();
} catch (e) { } catch (e) {
logger.err(`Unable to run indexing right now, trying again later. ` + e); logger.err(`Unable to run indexing right now, trying again later. ` + e);
} }

View File

@ -8,7 +8,7 @@ class BlocksRepository {
* Save indexed block data in the database * Save indexed block data in the database
*/ */
public async $saveBlockInDatabase(block: BlockExtended) { public async $saveBlockInDatabase(block: BlockExtended) {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const query = `INSERT INTO blocks( const query = `INSERT INTO blocks(
@ -70,7 +70,7 @@ class BlocksRepository {
return []; return [];
} }
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows]: any[] = await connection.query(` const [rows]: any[] = await connection.query(`
SELECT height SELECT height
@ -116,7 +116,7 @@ class BlocksRepository {
query += ` GROUP by pools.id`; query += ` GROUP by pools.id`;
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows] = await connection.query(query, params); const [rows] = await connection.query(query, params);
connection.release(); connection.release();
@ -154,7 +154,7 @@ class BlocksRepository {
} }
// logger.debug(query); // logger.debug(query);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows] = await connection.query(query, params); const [rows] = await connection.query(query, params);
connection.release(); connection.release();
@ -194,7 +194,7 @@ class BlocksRepository {
query += ` blockTimestamp BETWEEN FROM_UNIXTIME('${from}') AND FROM_UNIXTIME('${to}')`; query += ` blockTimestamp BETWEEN FROM_UNIXTIME('${from}') AND FROM_UNIXTIME('${to}')`;
// logger.debug(query); // logger.debug(query);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows] = await connection.query(query, params); const [rows] = await connection.query(query, params);
connection.release(); connection.release();
@ -217,7 +217,7 @@ class BlocksRepository {
LIMIT 1;`; LIMIT 1;`;
// logger.debug(query); // logger.debug(query);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows]: any[] = await connection.query(query); const [rows]: any[] = await connection.query(query);
connection.release(); connection.release();
@ -253,7 +253,7 @@ class BlocksRepository {
LIMIT 10`; LIMIT 10`;
// logger.debug(query); // logger.debug(query);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows] = await connection.query(query, params); const [rows] = await connection.query(query, params);
connection.release(); connection.release();
@ -274,7 +274,7 @@ class BlocksRepository {
* Get one block by height * Get one block by height
*/ */
public async $getBlockByHeight(height: number): Promise<object | null> { public async $getBlockByHeight(height: number): Promise<object | null> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows]: any[] = await connection.query(` const [rows]: any[] = await connection.query(`
SELECT *, UNIX_TIMESTAMP(blocks.blockTimestamp) as blockTimestamp, SELECT *, UNIX_TIMESTAMP(blocks.blockTimestamp) as blockTimestamp,
@ -305,7 +305,7 @@ class BlocksRepository {
public async $getBlocksDifficulty(interval: string | null): Promise<object[]> { public async $getBlocksDifficulty(interval: string | null): Promise<object[]> {
interval = Common.getSqlInterval(interval); interval = Common.getSqlInterval(interval);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
// :D ... Yeah don't ask me about this one https://stackoverflow.com/a/40303162 // :D ... Yeah don't ask me about this one https://stackoverflow.com/a/40303162
// Basically, using temporary user defined fields, we are able to extract all // Basically, using temporary user defined fields, we are able to extract all
@ -356,7 +356,7 @@ class BlocksRepository {
} }
public async $getOldestIndexedBlockHeight(): Promise<number> { public async $getOldestIndexedBlockHeight(): Promise<number> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows]: any[] = await connection.query(`SELECT MIN(height) as minHeight FROM blocks`); const [rows]: any[] = await connection.query(`SELECT MIN(height) as minHeight FROM blocks`);
connection.release(); connection.release();

View File

@ -20,9 +20,9 @@ class HashratesRepository {
} }
query = query.slice(0, -1); query = query.slice(0, -1);
const connection = await DB.pool.getConnection(); let connection;
try { try {
// logger.debug(query); connection = await DB.getConnection();
await connection.query(query); await connection.query(query);
connection.release(); connection.release();
} catch (e: any) { } catch (e: any) {
@ -35,18 +35,16 @@ class HashratesRepository {
public async $getNetworkDailyHashrate(interval: string | null): Promise<any[]> { public async $getNetworkDailyHashrate(interval: string | null): Promise<any[]> {
interval = Common.getSqlInterval(interval); interval = Common.getSqlInterval(interval);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
let query = `SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp, avg_hashrate as avgHashrate let query = `SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp, avg_hashrate as avgHashrate
FROM hashrates`; FROM hashrates`;
if (interval) { if (interval) {
query += ` WHERE hashrate_timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW() query += ` WHERE hashrate_timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()
AND hashrates.type = 'daily' AND hashrates.type = 'daily'`;
AND pool_id IS NULL`;
} else { } else {
query += ` WHERE hashrates.type = 'daily' query += ` WHERE hashrates.type = 'daily'`;
AND pool_id IS NULL`;
} }
query += ` ORDER by hashrate_timestamp`; query += ` ORDER by hashrate_timestamp`;
@ -64,9 +62,12 @@ class HashratesRepository {
} }
public async $getWeeklyHashrateTimestamps(): Promise<number[]> { public async $getWeeklyHashrateTimestamps(): Promise<number[]> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp FROM hashrates where type = 'weekly' GROUP BY hashrate_timestamp`; const query = `SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp
FROM hashrates
WHERE type = 'weekly'
GROUP BY hashrate_timestamp`;
try { try {
const [rows]: any[] = await connection.query(query); const [rows]: any[] = await connection.query(query);
@ -86,7 +87,7 @@ class HashratesRepository {
public async $getPoolsWeeklyHashrate(interval: string | null): Promise<any[]> { public async $getPoolsWeeklyHashrate(interval: string | null): Promise<any[]> {
interval = Common.getSqlInterval(interval); interval = Common.getSqlInterval(interval);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const topPoolsId = (await PoolsRepository.$getPoolsInfo('1w')).map((pool) => pool.poolId); const topPoolsId = (await PoolsRepository.$getPoolsInfo('1w')).map((pool) => pool.poolId);
let query = `SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp, avg_hashrate as avgHashrate, share, pools.name as poolName let query = `SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp, avg_hashrate as avgHashrate, share, pools.name as poolName
@ -120,7 +121,7 @@ class HashratesRepository {
* Returns a pool hashrate history * Returns a pool hashrate history
*/ */
public async $getPoolWeeklyHashrate(poolId: number): Promise<any[]> { public async $getPoolWeeklyHashrate(poolId: number): Promise<any[]> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
// Find hashrate boundaries // Find hashrate boundaries
let query = `SELECT MIN(hashrate_timestamp) as firstTimestamp, MAX(hashrate_timestamp) as lastTimestamp let query = `SELECT MIN(hashrate_timestamp) as firstTimestamp, MAX(hashrate_timestamp) as lastTimestamp
@ -163,7 +164,7 @@ class HashratesRepository {
} }
public async $setLatestRunTimestamp(key: string, val: any = null) { public async $setLatestRunTimestamp(key: string, val: any = null) {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `UPDATE state SET number = ? WHERE name = ?`; const query = `UPDATE state SET number = ? WHERE name = ?`;
try { try {
@ -175,7 +176,7 @@ class HashratesRepository {
} }
public async $getLatestRunTimestamp(key: string): Promise<number> { public async $getLatestRunTimestamp(key: string): Promise<number> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const query = `SELECT number FROM state WHERE name = ?`; const query = `SELECT number FROM state WHERE name = ?`;
try { try {

View File

@ -8,7 +8,7 @@ class PoolsRepository {
* Get all pools tagging info * Get all pools tagging info
*/ */
public async $getPools(): Promise<PoolTag[]> { public async $getPools(): Promise<PoolTag[]> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const [rows] = await connection.query('SELECT id, name, addresses, regexes FROM pools;'); const [rows] = await connection.query('SELECT id, name, addresses, regexes FROM pools;');
connection.release(); connection.release();
return <PoolTag[]>rows; return <PoolTag[]>rows;
@ -18,7 +18,7 @@ class PoolsRepository {
* Get unknown pool tagging info * Get unknown pool tagging info
*/ */
public async $getUnknownPool(): Promise<PoolTag> { public async $getUnknownPool(): Promise<PoolTag> {
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
const [rows] = await connection.query('SELECT id, name FROM pools where name = "Unknown"'); const [rows] = await connection.query('SELECT id, name FROM pools where name = "Unknown"');
connection.release(); connection.release();
return <PoolTag>rows[0]; return <PoolTag>rows[0];
@ -42,7 +42,7 @@ class PoolsRepository {
ORDER BY COUNT(height) DESC`; ORDER BY COUNT(height) DESC`;
// logger.debug(query); // logger.debug(query);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows] = await connection.query(query); const [rows] = await connection.query(query);
connection.release(); connection.release();
@ -64,7 +64,7 @@ class PoolsRepository {
LEFT JOIN blocks on pools.id = blocks.pool_id AND blocks.blockTimestamp BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?) LEFT JOIN blocks on pools.id = blocks.pool_id AND blocks.blockTimestamp BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?)
GROUP BY pools.id`; GROUP BY pools.id`;
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows] = await connection.query(query, [from, to]); const [rows] = await connection.query(query, [from, to]);
connection.release(); connection.release();
@ -87,7 +87,7 @@ class PoolsRepository {
WHERE pools.id = ?`; WHERE pools.id = ?`;
// logger.debug(query); // logger.debug(query);
const connection = await DB.pool.getConnection(); const connection = await DB.getConnection();
try { try {
const [rows] = await connection.query(query, [poolId]); const [rows] = await connection.query(query, [poolId]);
connection.release(); connection.release();

View File

@ -17,7 +17,7 @@
</td> </td>
<td class="text-right">{{ diffChange.difficultyShorten }}</td> <td class="text-right">{{ diffChange.difficultyShorten }}</td>
<td class="text-right" [style]="diffChange.change >= 0 ? 'color: #42B747' : 'color: #B74242'"> <td class="text-right" [style]="diffChange.change >= 0 ? 'color: #42B747' : 'color: #B74242'">
{{ diffChange.change >= 0 ? '+' : '' }}{{ formatNumber(diffChange.change, locale, '1.2-2') }}% {{ diffChange.change >= 0 ? '+' : '' }}{{ diffChange.change | amountShortener }}%
</td> </td>
</tr> </tr>
</tbody> </tbody>

View File

@ -43,7 +43,7 @@ export class DifficultyAdjustmentsTable implements OnInit {
const change = (data.difficulty[i].difficulty / data.difficulty[i - 1].difficulty - 1) * 100; const change = (data.difficulty[i].difficulty / data.difficulty[i - 1].difficulty - 1) * 100;
tableData.push(Object.assign(data.difficulty[i], { tableData.push(Object.assign(data.difficulty[i], {
change: change, change: Math.round(change * 100) / 100,
difficultyShorten: formatNumber( difficultyShorten: formatNumber(
data.difficulty[i].difficulty / selectedPowerOfTen.divider, data.difficulty[i].difficulty / selectedPowerOfTen.divider,
this.locale, '1.2-2') + selectedPowerOfTen.unit this.locale, '1.2-2') + selectedPowerOfTen.unit

View File

@ -5,30 +5,32 @@
<!-- Temporary stuff here - Will be moved to a component once we have more useful data to show --> <!-- Temporary stuff here - Will be moved to a component once we have more useful data to show -->
<div class="col"> <div class="col">
<div class="main-title">Reward stats</div> <div class="main-title">Reward stats</div>
<div class="card" style="height: 123px"> <div class="card-wrapper">
<div class="card-body more-padding"> <div class="card" style="height: 123px">
<div class="fee-estimation-container" *ngIf="$rewardStats | async as rewardStats; else loadingReward"> <div class="card-body more-padding">
<div class="item"> <div class="reward-container" *ngIf="$rewardStats | async as rewardStats; else loadingReward">
<h5 class="card-title mb-1" i18n="mining.rewards">Miners Reward</h5> <div class="item">
<div class="card-text"> <h5 class="card-title" i18n="mining.rewards">Miners Reward</h5>
<app-amount [satoshis]="rewardStats.totalReward" digitsInfo="1.2-2" [noFiat]="true"></app-amount> <div class="card-text">
<div class="symbol">in the last 8 blocks</div> <app-amount [satoshis]="rewardStats.totalReward" digitsInfo="1.2-2" [noFiat]="true"></app-amount>
<div class="symbol">in the last 8 blocks</div>
</div>
</div> </div>
</div> <div class="item">
<div class="item"> <h5 class="card-title" i18n="mining.rewards-per-tx">Reward Per Tx</h5>
<h5 class="card-title mb-1" i18n="mining.rewards-per-tx">Reward Per Tx</h5> <div class="card-text">
<div class="card-text"> {{ rewardStats.rewardPerTx | amountShortener }}
{{ rewardStats.rewardPerTx | amountShortener }} <span class="symbol">sats/tx</span>
<span class="symbol">sats/tx</span> <div class="symbol">in the last 8 blocks</div>
<div class="symbol">in the last 8 blocks</div> </div>
</div> </div>
</div> <div class="item">
<div class="item"> <h5 class="card-title" i18n="mining.average-fee">Average Fee</h5>
<h5 class="card-title mb-1" i18n="mining.average-fee">Average Fee</h5> <div class="card-text">
<div class="card-text"> {{ rewardStats.feePerTx | amountShortener}}
{{ rewardStats.feePerTx | amountShortener}} <span class="symbol">sats/tx</span>
<span class="symbol">sats/tx</span> <div class="symbol">in the last 8 blocks</div>
<div class="symbol">in the last 8 blocks</div> </div>
</div> </div>
</div> </div>
</div> </div>
@ -36,24 +38,24 @@
</div> </div>
</div> </div>
<ng-template #loadingReward> <ng-template #loadingReward>
<div class="fee-estimation-container"> <div class="reward-container">
<div class="item"> <div class="item">
<h5 class="card-title" i18n="">Miners Reward</h5> <h5 class="card-title" i18n="mining.rewards">Miners Reward</h5>
<div class="card-text"> <div class="card-text skeleton">
<div class="skeleton-loader"></div> <div class="skeleton-loader"></div>
<div class="skeleton-loader"></div> <div class="skeleton-loader"></div>
</div> </div>
</div> </div>
<div class="item"> <div class="item">
<h5 class="card-title" i18n="">Reward Per Tx</h5> <h5 class="card-title" i18n="mining.rewards-per-tx">Reward Per Tx</h5>
<div class="card-text"> <div class="card-text skeleton">
<div class="skeleton-loader"></div> <div class="skeleton-loader"></div>
<div class="skeleton-loader"></div> <div class="skeleton-loader"></div>
</div> </div>
</div> </div>
<div class="item"> <div class="item">
<h5 class="card-title" i18n="">Average Fee</h5> <h5 class="card-title" i18n="mining.average-fee">Average Fee</h5>
<div class="card-text"> <div class="card-text skeleton">
<div class="skeleton-loader"></div> <div class="skeleton-loader"></div>
<div class="skeleton-loader"></div> <div class="skeleton-loader"></div>
</div> </div>
@ -136,4 +138,4 @@
</div> </div>
</div> </div>
</div> </div>

View File

@ -59,50 +59,57 @@
padding-bottom: 3px; padding-bottom: 3px;
} }
.fee-estimation-container { .reward-container {
display: flex; display: flex;
justify-content: space-between; flex-direction: row;
@media (min-width: 376px) { justify-content: space-around;
flex-direction: row; height: 76px;
.shared-block {
color: #ffffff66;
font-size: 12px;
} }
.item { .item {
max-width: 150px; display: table-cell;
margin: 0; padding: 0 5px;
width: -webkit-fill-available; width: 100%;
@media (min-width: 376px) { &:nth-child(1) {
margin: 0 auto 0px;
}
&:first-child{
display: none; display: none;
@media (min-width: 485px) { @media (min-width: 485px) {
display: block; display: table-cell;
} }
@media (min-width: 768px) { @media (min-width: 768px) {
display: none; display: none;
} }
@media (min-width: 992px) { @media (min-width: 992px) {
display: block; display: table-cell;
} }
} }
&:last-child { }
margin-bottom: 0; .card-text {
} font-size: 22px;
.card-text span { margin-top: -9px;
color: #ffffff66; position: relative;
font-size: 12px; }
top: 0px; .card-text.skeleton {
} margin-top: 0px;
.fee-text{ }
border-bottom: 1px solid #ffffff1c; }
width: fit-content;
margin: auto; .more-padding {
line-height: 1.45; padding: 18px;
padding: 0px 2px; }
}
.fiat { .card-wrapper {
display: block; .card {
font-size: 14px !important; height: auto !important;
} }
.card-body {
display: flex;
flex: inherit;
text-align: center;
flex-direction: column;
justify-content: space-around;
padding: 22px 20px;
} }
} }

View File

@ -36,8 +36,8 @@ export class MiningDashboardComponent implements OnInit {
return { return {
'totalReward': totalReward, 'totalReward': totalReward,
'rewardPerTx': totalReward / totalTx, 'rewardPerTx': Math.round(totalReward / totalTx),
'feePerTx': totalFee / totalTx, 'feePerTx': Math.round(totalFee / totalTx),
} }
}) })
); );

View File

@ -153,7 +153,7 @@ export class ApiService {
getBlocks$(from: number): Observable<BlockExtended[]> { getBlocks$(from: number): Observable<BlockExtended[]> {
return this.httpClient.get<BlockExtended[]>( return this.httpClient.get<BlockExtended[]>(
this.apiBasePath + this.apiBasePath + `/api/v1/blocks-extras` + this.apiBaseUrl + this.apiBasePath + `/api/v1/blocks-extras` +
(from !== undefined ? `/${from}` : ``) (from !== undefined ? `/${from}` : ``)
); );
} }

View File

@ -1,22 +1,39 @@
import { Pipe, PipeTransform } from '@angular/core'; import { Pipe, PipeTransform } from '@angular/core';
// https://medium.com/@thunderroid/angular-short-number-suffix-pipe-1k-2m-3b-dded4af82fb4
@Pipe({ @Pipe({
name: 'amountShortener' name: 'amountShortener'
}) })
export class AmountShortenerPipe implements PipeTransform { export class AmountShortenerPipe implements PipeTransform {
transform(num: number, ...args: number[]): unknown { transform(number: number, args?: any): any {
const digits = args[0] || 1; if (isNaN(number)) return null; // will only work value is a number
const lookup = [ if (number === null) return null;
{ value: 1, symbol: '' }, if (number === 0) return null;
{ value: 1e3, symbol: 'k' }, let abs = Math.abs(number);
{ value: 1e6, symbol: 'M' }, const rounder = Math.pow(10, 1);
{ value: 1e9, symbol: 'G' }, const isNegative = number < 0; // will also work for Negetive numbers
{ value: 1e12, symbol: 'T' }, let key = '';
{ value: 1e15, symbol: 'P' },
{ value: 1e18, symbol: 'E' } const powers = [
{ key: 'E', value: 10e18 },
{ key: 'P', value: 10e15 },
{ key: 'T', value: 10e12 },
{ key: 'B', value: 10e9 },
{ key: 'M', value: 10e6 },
{ key: 'K', value: 1000 }
]; ];
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
var item = lookup.slice().reverse().find((item) => num >= item.value); for (let i = 0; i < powers.length; i++) {
return item ? (num / item.value).toFixed(digits).replace(rx, '$1') + item.symbol : '0'; let reduced = abs / powers[i].value;
reduced = Math.round(reduced * rounder) / rounder;
if (reduced >= 1) {
abs = reduced;
key = powers[i].key;
break;
}
}
return (isNegative ? '-' : '') + abs + key;
} }
} }