Skip to content

Commit

Permalink
[IND-500]: Add logic to aggregate trading rewards (#952)
Browse files Browse the repository at this point in the history
* [IND-500]: Add logic to aggregate trading rewards

* nits

* nit
  • Loading branch information
Christopher-Li authored Jan 10, 2024
1 parent 51cdad7 commit 2bfcb31
Show file tree
Hide file tree
Showing 7 changed files with 605 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export const DENOM_COIN_SCALE: number = 18;
export function denomToHumanReadableConversion(denom: number): string {
return Big(denom).times(DENOM_TO_COIN_CONVERSION).toFixed(DENOM_COIN_SCALE);
}

export function convertToDenomScale(num: string): string {
return Big(num).toFixed(DENOM_COIN_SCALE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function uuid(
export async function findAll(
{
address,
addresses,
startedAtHeight,
period,
limit,
Expand All @@ -40,6 +41,7 @@ export async function findAll(
verifyAllRequiredFields(
{
address,
addresses,
startedAtHeight,
period,
limit,
Expand All @@ -57,6 +59,10 @@ export async function findAll(
baseQuery = baseQuery.where(TradingRewardAggregationColumns.address, address);
}

if (addresses) {
baseQuery = baseQuery.whereIn(TradingRewardAggregationColumns.address, addresses);
}

if (startedAtHeight) {
baseQuery = baseQuery.where(TradingRewardAggregationColumns.startedAtHeight, startedAtHeight);
}
Expand Down
12 changes: 12 additions & 0 deletions indexer/packages/postgres/src/stores/trading-reward-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export async function findAll(
address,
blockHeight,
blockTimeBeforeOrAt,
blockTimeAfterOrAt,
blockTimeBefore,
limit,
}: TradingRewardQueryConfig,
requiredFields: QueryableField[],
Expand All @@ -36,6 +38,8 @@ export async function findAll(
address,
blockHeight,
blockTimeBeforeOrAt,
blockTimeAfterOrAt,
blockTimeBefore,
limit,
} as QueryConfig,
requiredFields,
Expand All @@ -58,6 +62,14 @@ export async function findAll(
baseQuery = baseQuery.where(TradingRewardColumns.blockTime, '<=', blockTimeBeforeOrAt);
}

if (blockTimeAfterOrAt) {
baseQuery = baseQuery.where(TradingRewardColumns.blockTime, '>=', blockTimeAfterOrAt);
}

if (blockTimeBefore) {
baseQuery = baseQuery.where(TradingRewardColumns.blockTime, '<', blockTimeBefore);
}

if (options.orderBy !== undefined) {
for (const [column, order] of options.orderBy) {
baseQuery = baseQuery.orderBy(
Expand Down
8 changes: 7 additions & 1 deletion indexer/packages/postgres/src/types/query-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export enum QueryableField {
STARTED_AT_HEIGHT = 'startedAtHeight',
PERIOD = 'period',
STARTED_AT_HEIGHT_OR_AFTER = 'startedAtHeightOrAfter',
BLOCK_TIME_AFTER_OR_AT = 'blockTimeAfterOrAt',
BLOCK_TIME_BEFORE = 'blockTimeBefore',
ADDRESSES = 'addresses',
}

export interface QueryConfig {
Expand Down Expand Up @@ -149,7 +152,7 @@ export interface FillQueryConfig extends QueryConfig {

export interface BlockQueryConfig extends QueryConfig {
[QueryableField.BLOCK_HEIGHT]?: string[];
[QueryableField.CREATED_ON_OR_AFTER]?: string[];
[QueryableField.CREATED_ON_OR_AFTER]?: string;
}

export interface TendermintEventQueryConfig extends QueryConfig {
Expand Down Expand Up @@ -275,10 +278,13 @@ export interface TradingRewardQueryConfig extends QueryConfig {
[QueryableField.ADDRESS]?: string;
[QueryableField.BLOCK_HEIGHT]?: string;
[QueryableField.BLOCK_TIME_BEFORE_OR_AT]?: IsoString;
[QueryableField.BLOCK_TIME_AFTER_OR_AT]?: IsoString;
[QueryableField.BLOCK_TIME_BEFORE]?: IsoString;
}

export interface TradingRewardAggregationQueryConfig extends QueryConfig {
[QueryableField.ADDRESS]?: string;
[QueryableField.ADDRESSES]?: string[];
[QueryableField.STARTED_AT_HEIGHT]?: string;
[QueryableField.STARTED_AT_HEIGHT_OR_AFTER]?: string;
[QueryableField.PERIOD]?: TradingRewardAggregationPeriod;
Expand Down
Loading

0 comments on commit 2bfcb31

Please sign in to comment.