Use mining pool slug in urls

This commit is contained in:
nymkappa
2022-03-25 14:22:22 +09:00
parent 8114ffe1c8
commit 352ea950a2
18 changed files with 76 additions and 55 deletions

View File

@@ -3,6 +3,7 @@ import { DB } from '../database';
import logger from '../logger';
import { Common } from '../api/common';
import { prepareBlock } from '../utils/blocks-utils';
import PoolsRepository from './PoolsRepository';
class BlocksRepository {
/**
@@ -235,13 +236,18 @@ class BlocksRepository {
/**
* Get blocks mined by a specific mining pool
*/
public async $getBlocksByPool(poolId: number, startHeight: number | undefined = undefined): Promise<object[]> {
public async $getBlocksByPool(slug: string, startHeight: number | undefined = undefined): Promise<object[]> {
const pool = await PoolsRepository.$getPool(slug);
if (!pool) {
throw new Error(`This mining pool does not exist`);
}
const params: any[] = [];
let query = ` SELECT *, UNIX_TIMESTAMP(blocks.blockTimestamp) as blockTimestamp,
previous_block_hash as previousblockhash
FROM blocks
WHERE pool_id = ?`;
params.push(poolId);
params.push(pool.id);
if (startHeight !== undefined) {
query += ` AND height < ?`;
@@ -277,7 +283,7 @@ class BlocksRepository {
try {
const [rows]: any[] = await connection.query(`
SELECT *, UNIX_TIMESTAMP(blocks.blockTimestamp) as blockTimestamp,
pools.id as pool_id, pools.name as pool_name, pools.link as pool_link,
pools.id as pool_id, pools.name as pool_name, pools.link as pool_link, pools.slug as pool_slug,
pools.addresses as pool_addresses, pools.regexes as pool_regexes,
previous_block_hash as previousblockhash
FROM blocks

View File

@@ -120,8 +120,11 @@ class HashratesRepository {
/**
* Returns a pool hashrate history
*/
public async $getPoolWeeklyHashrate(poolId: number): Promise<any[]> {
const connection = await DB.getConnection();
public async $getPoolWeeklyHashrate(slug: string): Promise<any[]> {
const pool = await PoolsRepository.$getPool(slug);
if (!pool) {
throw new Error(`This mining pool does not exist`);
}
// Find hashrate boundaries
let query = `SELECT MIN(hashrate_timestamp) as firstTimestamp, MAX(hashrate_timestamp) as lastTimestamp
@@ -134,8 +137,11 @@ class HashratesRepository {
firstTimestamp: '1970-01-01',
lastTimestamp: '9999-01-01'
};
let connection;
try {
const [rows]: any[] = await connection.query(query, [poolId]);
connection = await DB.getConnection();
const [rows]: any[] = await connection.query(query, [pool.id]);
boundaries = rows[0];
connection.release();
} catch (e) {
@@ -152,7 +158,7 @@ class HashratesRepository {
ORDER by hashrate_timestamp`;
try {
const [rows]: any[] = await connection.query(query, [boundaries.firstTimestamp, boundaries.lastTimestamp, poolId]);
const [rows]: any[] = await connection.query(query, [boundaries.firstTimestamp, boundaries.lastTimestamp, pool.id]);
connection.release();
return rows;

View File

@@ -9,7 +9,7 @@ class PoolsRepository {
*/
public async $getPools(): Promise<PoolTag[]> {
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, slug FROM pools;');
connection.release();
return <PoolTag[]>rows;
}
@@ -19,7 +19,7 @@ class PoolsRepository {
*/
public async $getUnknownPool(): Promise<PoolTag> {
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, slug FROM pools where name = "Unknown"');
connection.release();
return <PoolTag>rows[0];
}
@@ -30,7 +30,7 @@ class PoolsRepository {
public async $getPoolsInfo(interval: string | null = null): Promise<PoolInfo[]> {
interval = Common.getSqlInterval(interval);
let query = `SELECT COUNT(height) as blockCount, pool_id as poolId, pools.name as name, pools.link as link
let query = `SELECT COUNT(height) as blockCount, pool_id as poolId, pools.name as name, pools.link as link, slug
FROM blocks
JOIN pools on pools.id = pool_id`;
@@ -80,16 +80,17 @@ class PoolsRepository {
/**
* Get mining pool statistics for one pool
*/
public async $getPool(poolId: any): Promise<object> {
public async $getPool(slug: string): Promise<PoolTag> {
const query = `
SELECT *
FROM pools
WHERE pools.id = ?`;
WHERE pools.slug = ?`;
// logger.debug(query);
const connection = await DB.getConnection();
let connection;
try {
const [rows] = await connection.query(query, [poolId]);
connection = await DB.getConnection();
const [rows] = await connection.query(query, [slug]);
connection.release();
rows[0].regexes = JSON.parse(rows[0].regexes);