Skip to content

Commit

Permalink
use separate table for preprocessed l2 transaction statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
torztomasz committed Jul 19, 2023
1 parent 7c5680d commit 5f5745f
Show file tree
Hide file tree
Showing 17 changed files with 697 additions and 412 deletions.
16 changes: 14 additions & 2 deletions packages/backend/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { PerpetualHistoryPreprocessor } from './core/preprocessing/PerpetualHist
import { Preprocessor } from './core/preprocessing/Preprocessor'
import { SpotHistoryPreprocessor } from './core/preprocessing/SpotHistoryPreprocessor'
import { StateDetailsPreprocessor } from './core/preprocessing/StateDetailsPreprocessor'
import { UserL2TransactionsPreprocessor } from './core/preprocessing/UserL2TransactionsPreprocessor'
import { UserStatisticsPreprocessor } from './core/preprocessing/UserStatisticsPreprocessor'
import { SpotValidiumSyncService } from './core/SpotValidiumSyncService'
import { SpotValidiumUpdater } from './core/SpotValidiumUpdater'
Expand All @@ -70,6 +71,7 @@ import { PositionRepository } from './peripherals/database/PositionRepository'
import { PreprocessedAssetHistoryRepository } from './peripherals/database/PreprocessedAssetHistoryRepository'
import { PreprocessedStateDetailsRepository } from './peripherals/database/PreprocessedStateDetailsRepository'
import { PreprocessedStateUpdateRepository } from './peripherals/database/PreprocessedStateUpdateRepository'
import { PreprocessedUserL2TransactionsRepository } from './peripherals/database/PreprocessedUserL2TransactionsRepository'
import { PreprocessedUserStatisticsRepository } from './peripherals/database/PreprocessedUserStatisticsRepository'
import { Database } from './peripherals/database/shared/Database'
import { StateTransitionRepository } from './peripherals/database/StateTransitionRepository'
Expand Down Expand Up @@ -413,6 +415,15 @@ export class Application {
const preprocessedUserStatisticsRepository =
new PreprocessedUserStatisticsRepository(database, logger)

const preprocessedUserL2TransactionsRepository =
new PreprocessedUserL2TransactionsRepository(database, logger)

const userL2TransactionsPreprocessor = new UserL2TransactionsPreprocessor(
preprocessedUserL2TransactionsRepository,
l2TransactionRepository,
logger
)

let preprocessor: Preprocessor<AssetHash> | Preprocessor<AssetId>
const isPreprocessorEnabled = config.enablePreprocessing

Expand Down Expand Up @@ -450,7 +461,6 @@ export class Application {
preprocessedUserStatisticsRepository,
preprocessedAssetHistoryRepository,
stateUpdateRepository,
l2TransactionRepository,
kvStore,
logger
)
Expand All @@ -462,6 +472,7 @@ export class Application {
perpetualHistoryPreprocessor,
stateDetailsPreprocessor,
userStatisticsPreprocessor,
userL2TransactionsPreprocessor,
l2TransactionRepository,
logger,
isPreprocessorEnabled
Expand All @@ -488,7 +499,6 @@ export class Application {
preprocessedUserStatisticsRepository,
preprocessedAssetHistoryRepository,
stateUpdateRepository,
l2TransactionRepository,
kvStore,
logger
)
Expand All @@ -500,6 +510,7 @@ export class Application {
spotHistoryPreprocessor,
stateDetailsPreprocessor,
userStatisticsPreprocessor,
userL2TransactionsPreprocessor,
l2TransactionRepository,
logger,
isPreprocessorEnabled
Expand Down Expand Up @@ -542,6 +553,7 @@ export class Application {
forcedTradeOfferViewService,
withdrawableAssetRepository,
preprocessedUserStatisticsRepository,
preprocessedUserL2TransactionsRepository,
config.starkex.contracts.perpetual
)
const stateUpdateController = new StateUpdateController(
Expand Down
41 changes: 19 additions & 22 deletions packages/backend/src/api/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
PreprocessedAssetHistoryRepository,
} from '../../peripherals/database/PreprocessedAssetHistoryRepository'
import { sumUpTransactionCount } from '../../peripherals/database/PreprocessedL2TransactionsStatistics'
import { PreprocessedUserL2TransactionsRepository } from '../../peripherals/database/PreprocessedUserL2TransactionsRepository'
import { PreprocessedUserStatisticsRepository } from '../../peripherals/database/PreprocessedUserStatisticsRepository'
import {
SentTransactionRecord,
Expand Down Expand Up @@ -61,6 +62,7 @@ export class UserController {
private readonly forcedTradeOfferViewService: ForcedTradeOfferViewService,
private readonly withdrawableAssetRepository: WithdrawableAssetRepository,
private readonly preprocessedUserStatisticsRepository: PreprocessedUserStatisticsRepository,
private readonly preprocessedUserL2TransactionsRepository: PreprocessedUserL2TransactionsRepository,
private readonly exchangeAddress: EthereumAddress
) {}

Expand Down Expand Up @@ -126,6 +128,7 @@ export class UserController {
userAssets,
history,
l2Transactions,
preprocessedUserL2Transactions,
sentTransactions,
userTransactions,
userTransactionsCount,
Expand All @@ -149,6 +152,9 @@ export class UserController {
starkKey,
paginationOpts
),
this.preprocessedUserL2TransactionsRepository.findCurrentByStarkKey(
starkKey
),
this.sentTransactionRepository.getByStarkKey(starkKey),
this.userTransactionRepository.getByStarkKey(
starkKey,
Expand All @@ -173,16 +179,6 @@ export class UserController {
}
}

let l2TransactionsStatistics = userStatistics.l2TransactionsStatistics
if (!l2TransactionsStatistics) {
const lastUserStatisticsWithL2TransactionsStatistics =
await this.preprocessedUserStatisticsRepository.findLastWithL2TransactionsStatisticsByStarkKey(
starkKey
)
l2TransactionsStatistics =
lastUserStatisticsWithL2TransactionsStatistics?.l2TransactionsStatistics
}

const assetDetailsMap = await this.assetDetailsService.getAssetDetailsMap({
userAssets: userAssets,
assetHistory: history,
Expand Down Expand Up @@ -223,7 +219,9 @@ export class UserController {
starkKey,
ethereumAddress: registeredUser?.ethAddress,
l2Transactions: l2Transactions.map(l2TransactionToEntry),
totalL2Transactions: sumUpTransactionCount(l2TransactionsStatistics),
totalL2Transactions: sumUpTransactionCount(
preprocessedUserL2Transactions?.cumulativeL2TransactionsStatistics
),
withdrawableAssets: withdrawableAssets.map((asset) => ({
asset: {
hashOrId:
Expand Down Expand Up @@ -320,23 +318,22 @@ export class UserController {
pagination: PaginationOptions
): Promise<ControllerResult> {
const context = await this.pageContextService.getPageContext(givenUser)
const [l2Transactions, lastUserStatisticsWithL2TransactionsStatistics] =
await Promise.all([
this.l2TransactionRepository.getUserSpecificPaginated(
starkKey,
pagination
),
this.preprocessedUserStatisticsRepository.findLastWithL2TransactionsStatisticsByStarkKey(
starkKey
),
])
const [l2Transactions, preprocessedUserL2Transactions] = await Promise.all([
this.l2TransactionRepository.getUserSpecificPaginated(
starkKey,
pagination
),
this.preprocessedUserL2TransactionsRepository.findCurrentByStarkKey(
starkKey
),
])

const content = renderUserL2TransactionsPage({
context,
starkKey,
l2Transactions: l2Transactions.map(l2TransactionToEntry),
total: sumUpTransactionCount(
lastUserStatisticsWithL2TransactionsStatistics?.l2TransactionsStatistics
preprocessedUserL2Transactions?.cumulativeL2TransactionsStatistics
),
...pagination,
})
Expand Down
Loading

0 comments on commit 5f5745f

Please sign in to comment.