Move reward stats to component - Add /api/v1/mining/reward-stats/{blockCount}

This commit is contained in:
nymkappa
2022-03-22 12:34:29 +09:00
parent 5c629dfe98
commit 2644f2fb07
15 changed files with 202 additions and 110 deletions

View File

@@ -354,6 +354,9 @@ class BlocksRepository {
}
}
/**
* Return oldest blocks height
*/
public async $getOldestIndexedBlockHeight(): Promise<number> {
const connection = await DB.getConnection();
try {
@@ -367,6 +370,29 @@ class BlocksRepository {
throw e;
}
}
/**
* Get general block stats
*/
public async $getBlockStats(blockCount: number): Promise<any> {
let connection;
try {
connection = await DB.getConnection();
// We need to use a subquery
const query = `SELECT SUM(reward) as totalReward, SUM(fees) as totalFee, SUM(tx_count) as totalTx
FROM (SELECT reward, fees, tx_count FROM blocks ORDER by height DESC LIMIT ${blockCount}) as sub`;
const [rows]: any = await connection.query(query);
connection.release();
return rows[0];
} catch (e) {
connection.release();
logger.err('$getBlockStats() error: ' + (e instanceof Error ? e.message : e));
throw e;
}
}
}
export default new BlocksRepository();