Skip to content

Commit

Permalink
Merge pull request #1275 from input-output-hk/refactor/minor-query-im…
Browse files Browse the repository at this point in the history
…provement

Refactor/minor query improvement
  • Loading branch information
mirceahasegan committed May 15, 2024
2 parents 388fb3a + ad9aa07 commit 723c6f1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import { Pool, QueryResult } from 'pg';
import { findLastEpoch } from '../../util';
import {
mapAddressOwner,
mapBlockfrostPoolMetrics,
Expand All @@ -43,7 +44,6 @@ import Queries, {
addSentenceToQuery,
blockfrostQuery,
buildOrQueryFromClauses,
findLastEpoch,
findPoolStats,
getIdentifierFullJoinClause,
getIdentifierWhereClause,
Expand Down Expand Up @@ -183,7 +183,10 @@ export class StakePoolBuilder {

public async getLastEpoch() {
this.#logger.debug('About to query last epoch');
const result: QueryResult<EpochModel> = await this.#db.query(Queries.findLastEpoch);
const result: QueryResult<EpochModel> = await this.#db.query({
name: 'current_epoch',
text: findLastEpoch
});
const lastEpoch = result.rows[0];
if (!lastEpoch) throw new ProviderError(ProviderFailure.Unknown, null, "Couldn't find last epoch");
return lastEpoch.no;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
/* eslint-disable sonarjs/no-nested-template-literals */
import { Cardano, MultipleChoiceSearchFilter, QueryStakePoolsArgs } from '@cardano-sdk/core';
import { OrderByOptions, SubQuery } from './types';
import { findLastEpoch } from '../../util';
import { getStakePoolSortType } from './util';

export const findLastEpoch = `
SELECT
"no"
FROM epoch
ORDER BY no DESC
LIMIT 1
`;

export const findLastEpochWithData = `
SELECT
epoch."no",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export class DbSyncEpochPollService implements EpochMonitor {
* @returns {number} epoch number
*/
async #queryLastEpoch() {
const result: QueryResult<EpochModel> = await this.#db.query(findLastEpoch);
const result: QueryResult<EpochModel> = await this.#db.query({
name: 'current_epoch',
text: findLastEpoch
});
return Cardano.EpochNo(result.rows[0].no);
}

Expand Down

0 comments on commit 723c6f1

Please sign in to comment.