Skip to content

Commit

Permalink
Fix Aggregate Historical Trading rewards and always use generated id …
Browse files Browse the repository at this point in the history
…in tables (#1022) (#1024)
  • Loading branch information
Christopher-Li authored Jan 30, 2024
1 parent f3202cc commit 82627db
Show file tree
Hide file tree
Showing 15 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/stores/candle-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export async function create(
return CandleModel.query(
Transaction.get(options.txId),
).insert({
id: uuid(candle.startedAt, candle.ticker, candle.resolution),
...candle,
id: uuid(candle.startedAt, candle.ticker, candle.resolution),
}).returning('*');
}

Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/stores/fill-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ export async function create(
return FillModel.query(
Transaction.get(options.txId),
).insert({
id: uuid(fillToCreate.eventId, fillToCreate.liquidity),
...fillToCreate,
id: uuid(fillToCreate.eventId, fillToCreate.liquidity),
}).returning('*');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ export async function create(
return FundingIndexUpdatesModel.query(
Transaction.get(options.txId),
).insert({
...fundingIndexUpdateToCreate,
id: uuid(
fundingIndexUpdateToCreate.effectiveAtHeight,
fundingIndexUpdateToCreate.eventId,
fundingIndexUpdateToCreate.perpetualId,
),
...fundingIndexUpdateToCreate,
}).returning('*');
}

Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/stores/oracle-price-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export async function create(
return OraclePriceModel.query(
Transaction.get(options.txId),
).insert({
id: uuid(oraclePriceToCreate.marketId, oraclePriceToCreate.effectiveAtHeight),
...oraclePriceToCreate,
id: uuid(oraclePriceToCreate.marketId, oraclePriceToCreate.effectiveAtHeight),
}).returning('*');
}

Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/stores/order-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ export async function create(
return OrderModel.query(
Transaction.get(options.txId),
).insert({
...orderToCreate,
id: uuid(
orderToCreate.subaccountId,
orderToCreate.clientId,
orderToCreate.clobPairId,
orderToCreate.orderFlags,
),
...orderToCreate,
}).returning('*');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export async function create(
return PerpetualPositionModel.query(
Transaction.get(options.txId),
).insert({
id: uuid(perpetualPositionToCreate.subaccountId, perpetualPositionToCreate.openEventId),
...perpetualPositionToCreate,
id: uuid(perpetualPositionToCreate.subaccountId, perpetualPositionToCreate.openEventId),
}).returning('*');
}

Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/stores/pnl-ticks-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export async function create(
return PnlTicksModel.query(
Transaction.get(options.txId),
).insert({
id: uuid(pnlTicksToCreate.subaccountId, pnlTicksToCreate.createdAt),
...pnlTicksToCreate,
id: uuid(pnlTicksToCreate.subaccountId, pnlTicksToCreate.createdAt),
}).returning('*');
}

Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/stores/subaccount-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export async function create(
return SubaccountModel.query(
Transaction.get(options.txId),
).insert({
id: uuid(subaccountToCreate.address, subaccountToCreate.subaccountNumber),
...subaccountToCreate,
id: uuid(subaccountToCreate.address, subaccountToCreate.subaccountNumber),
}).returning('*');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ export async function create(
return TendermintEventModel.query(
Transaction.get(options.txId),
).insert({
...tendermintEventToCreate,
id: createEventId(
tendermintEventToCreate.blockHeight,
tendermintEventToCreate.transactionIndex,
tendermintEventToCreate.eventIndex,
),
...tendermintEventToCreate,
}).returning('*');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ export async function create(
return TradingRewardAggregationModel.query(
Transaction.get(options.txId),
).insert({
...aggregationToCreate,
id: uuid(
aggregationToCreate.address,
aggregationToCreate.period,
aggregationToCreate.startedAtHeight,
),
...aggregationToCreate,
}).returning('*');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export async function create(
return TradingRewardModel.query(
Transaction.get(options.txId),
).insert({
id: uuid(tradingRewardToCreate.address, tradingRewardToCreate.blockHeight),
...tradingRewardToCreate,
id: uuid(tradingRewardToCreate.address, tradingRewardToCreate.blockHeight),
}).returning('*');
}

Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/stores/transaction-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export async function create(
return TransactionModel.query(
Transaction.get(options.txId),
).insert({
id: uuid(transactionToCreate.blockHeight, transactionToCreate.transactionIndex),
...transactionToCreate,
id: uuid(transactionToCreate.blockHeight, transactionToCreate.transactionIndex),
}).returning('*');
}

Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/stores/transfer-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export async function create(
return TransferModel.query(
Transaction.get(options.txId),
).insert({
...transferToCreate,
id: uuid(
transferToCreate.eventId,
transferToCreate.assetId,
Expand All @@ -414,7 +415,6 @@ export async function create(
transferToCreate.senderWalletAddress,
transferToCreate.recipientWalletAddress,
),
...transferToCreate,
}).returning('*');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ describe('aggregate-trading-rewards', () => {
intervalToBeProcessed.start.toISO(),
redisClient,
),
TradingRewardAggregationTable.create({
...defaultCreatedTradingRewardAggregation,
startedAt: intervalToBeProcessed.start.minus({ days: 1 }).toISO(),
startedAtHeight: '1',
}),
]);

const aggregateTradingReward: AggregateTradingReward = new AggregateTradingReward(
Expand All @@ -396,7 +401,7 @@ describe('aggregate-trading-rewards', () => {
TradingRewardAggregationPeriod.DAILY,
intervalToBeProcessed.end.toISO(),
);
await validateNumberOfAggregations(1);
await validateNumberOfAggregations(2);
await validateAggregationWithExpectedValue(defaultCreatedTradingRewardAggregation);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,14 @@ export class AggregateTradingReward {
): Promise<AggregationUpdateAndCreateObjects> {
const tradingRewardAddresses: string[] = Object.keys(intervalTradingRewardsByAddress);

const startedAt: string = this.getStartedAt(interval);
const startedAtHeight: string = await this.getNextBlock(startedAt);
const existingAggregateTradingRewards:
TradingRewardAggregationFromDatabase[] = await runFuncWithTimingStat(
TradingRewardAggregationTable.findAll({
addresses: tradingRewardAddresses,
period: this.period,
startedAtHeight,
}, []),
this.generateTimingStatsOptions('findAllExistingAggregations'),
);
Expand All @@ -343,8 +346,6 @@ export class AggregateTradingReward {
TradingRewardAggregationColumns.address,
);

const startedAt: string = this.getStartedAt(interval);
const startedAtHeight: string = await this.getNextBlock(startedAt);
const aggregateTradingRewardsToUpdate: TradingRewardAggregationUpdateObject[] = _.intersection(
tradingRewardAddresses,
Object.keys(existingAggregateTradingRewardsMap),
Expand Down

0 comments on commit 82627db

Please sign in to comment.