Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start processing aggregate trading rewards from first reward #959

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('aggregate-trading-rewards', () => {
TradingRewardAggregationPeriod.DAILY,
TradingRewardAggregationPeriod.WEEKLY,
TradingRewardAggregationPeriod.MONTHLY,
])('Successfully returns interval if cache is empty and no aggregations', async (
])('Successfully returns interval if cache is empty no aggregations, and no trading rewards', async (
period: TradingRewardAggregationPeriod,
) => {
const firstBlockTime: DateTime = DateTime.fromISO(
Expand All @@ -141,7 +141,7 @@ describe('aggregate-trading-rewards', () => {
});

it(
'Successfully returns interval when cache is empty and no',
'Successfully returns interval when cache is empty and no aggregation for the period and trading reward',
async () => {
await TradingRewardAggregationTable.create({
...defaultMonthlyTradingRewardAggregation,
Expand All @@ -166,6 +166,35 @@ describe('aggregate-trading-rewards', () => {
},
);

it(
'Successfully returns interval when cache is empty and no aggregation and trading reward exists',
async () => {
await Promise.all([
TradingRewardTable.create({
address: testConstants.defaultAddress,
blockTime: startedAt.plus({ minutes: 2, seconds: 20 }).toISO(), // random constants
blockHeight: '1',
amount: '10',
}),
createBlockWithTime(startedAt.plus({ hours: 10 })),
]);
const aggregateTradingReward: AggregateTradingReward = new AggregateTradingReward(
TradingRewardAggregationPeriod.MONTHLY,
);
const interval:
Interval = await aggregateTradingReward.getTradingRewardDataToProcessInterval();

expect(interval).toEqual(Interval.fromDateTimes(
startedAt.plus({ minutes: 2, seconds: 20 }),
startedAt.plus({
minutes: 2,
seconds: 20,
milliseconds: config.AGGREGATE_TRADING_REWARDS_MAX_INTERVAL_SIZE_MS,
}),
));
},
);

it(
'Successfully returns interval when cache is empty and a complete aggregations exist',
async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
BlockTable,
IsoString,
IsolationLevel,
Ordering,
TradingRewardAggregationColumns,
TradingRewardAggregationCreateObject,
TradingRewardAggregationFromDatabase,
Expand Down Expand Up @@ -156,7 +157,7 @@ export class AggregateTradingReward {
if (processedTime === null) {
logger.info({
at: 'aggregate-trading-rewards#getTradingRewardDataToProcessInterval',
message: 'Resetting AggregateTradingRewardsProcessedCache',
message: 'AggregateTradingRewardsProcessedCache is empty',
});
const nextStartTime: DateTime = await this.getNextIntervalStartWhenCacheEmpty();
await this.setProcessedTime(
Expand All @@ -172,9 +173,12 @@ export class AggregateTradingReward {

/**
* Returns the start time of the next interval to process if the
* AggregateTradingRewardProcessedCache is empty. If there is a most recent complete aggregation
* for this period, returns the end time of the most recent aggregation, otherwise returns the
* start time of the first block in the database.
* AggregateTradingRewardProcessedCache is empty.
* - If there is a most recent complete aggregation for this period,
* returns the end time of the most recent aggregation.
* - If there is a trading reward in the database,
* returns the block time of the trading reward.
* - Otherwise returns the start time of the first block in the database.
*/
private async getNextIntervalStartWhenCacheEmpty(): Promise<DateTime> {
const latestAggregation:
Expand All @@ -186,12 +190,23 @@ export class AggregateTradingReward {
return DateTime.fromISO(latestAggregation.endedAt!, UTC_OPTIONS);
}

// Since we were able to find the latest block, we assume we can find the first block
const firstBlock: BlockFromDatabase[] = await BlockTable.findAll({
blockHeight: ['1'],
const firstTradingReward: TradingRewardFromDatabase[] = await TradingRewardTable.findAll({
limit: 1,
}, []);
return DateTime.fromISO(firstBlock[0].time, UTC_OPTIONS);
}, [], { orderBy: [[TradingRewardColumns.blockTime, Ordering.ASC]] });
if (firstTradingReward.length === 0) {
logger.info({
at: 'aggregate-trading-rewards#getNextIntervalStartWhenCacheEmpty',
message: 'No trading rewards in database, relying on first block time to pick first start time',
});
// Since we were able to find the latest block, we assume we can find the first block
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Where in this function are we finding the latest block?

const firstBlock: BlockFromDatabase[] = await BlockTable.findAll({
blockHeight: ['1'],
limit: 1,
}, []);
return DateTime.fromISO(firstBlock[0].time, UTC_OPTIONS);
}

return DateTime.fromISO(firstTradingReward[0].blockTime, UTC_OPTIONS);
}

/**
Expand Down