Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into implement-apex-star…
Browse files Browse the repository at this point in the history
…kkey-recovery
  • Loading branch information
torztomasz committed Jul 10, 2023
2 parents c991471 + 969e3f4 commit 0c70fa5
Show file tree
Hide file tree
Showing 132 changed files with 5,277 additions and 826 deletions.
37 changes: 26 additions & 11 deletions packages/backend/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ApiServer } from './api/ApiServer'
import { ForcedActionController } from './api/controllers/ForcedActionController'
import { ForcedTradeOfferController } from './api/controllers/ForcedTradeOfferController'
import { HomeController } from './api/controllers/HomeController'
import { L2TransactionController } from './api/controllers/L2TransactionController'
import { MerkleProofController } from './api/controllers/MerkleProofController'
import { SearchController } from './api/controllers/SearchController'
import { StateUpdateController } from './api/controllers/StateUpdateController'
Expand Down Expand Up @@ -88,6 +89,7 @@ import { FeederGatewayClient } from './peripherals/starkware/FeederGatewayClient
import { FetchClient } from './peripherals/starkware/FetchClient'
import { handleServerError, reportError } from './tools/ErrorReporter'
import { Logger } from './tools/Logger'
import { shouldShowL2Transactions } from './utils/shouldShowL2Transactions'

export class Application {
start: () => Promise<void>
Expand Down Expand Up @@ -159,6 +161,11 @@ export class Application {
logger
)

const l2TransactionRepository = new L2TransactionRepository(
database,
logger
)

const ethereumClient = new EthereumClient(
config.starkex.blockchain.jsonRpcUrl,
config.starkex.blockchain.safeBlockDistance
Expand Down Expand Up @@ -234,10 +241,7 @@ export class Application {
config.starkex.contracts.perpetual
)
stateTransitionCollector = perpetualValidiumStateTransitionCollector
const transactionRepository = new L2TransactionRepository(
database,
logger
)

const feederGatewayClient = config.starkex.feederGateway
? new FeederGatewayClient(
config.starkex.feederGateway,
Expand All @@ -248,7 +252,7 @@ export class Application {
feederGatewayCollector = feederGatewayClient
? new FeederGatewayCollector(
feederGatewayClient,
transactionRepository,
l2TransactionRepository,
stateUpdateRepository,
logger
)
Expand Down Expand Up @@ -523,13 +527,16 @@ export class Application {

// #endregion core
// #region api
const showL2Transactions = shouldShowL2Transactions(config)
const homeController = new HomeController(
pageContextService,
assetDetailsService,
forcedTradeOfferViewService,
userTransactionRepository,
forcedTradeOfferRepository,
preprocessedStateDetailsRepository
l2TransactionRepository,
preprocessedStateDetailsRepository,
showL2Transactions
)

const userController = new UserController(
Expand All @@ -539,19 +546,22 @@ export class Application {
sentTransactionRepository,
userTransactionRepository,
forcedTradeOfferRepository,
l2TransactionRepository,
userRegistrationEventRepository,
forcedTradeOfferViewService,
withdrawableAssetRepository,
preprocessedUserStatisticsRepository,
config.starkex.contracts.perpetual,
collateralAsset
showL2Transactions
)
const stateUpdateController = new StateUpdateController(
pageContextService,
assetDetailsService,
stateUpdateRepository,
userTransactionRepository,
preprocessedAssetHistoryRepository
l2TransactionRepository,
preprocessedAssetHistoryRepository,
showL2Transactions
)
const transactionController = new TransactionController(
pageContextService,
Expand All @@ -576,6 +586,11 @@ export class Application {
config.starkex.tradingMode
)

const l2TransactionController = new L2TransactionController(
pageContextService,
l2TransactionRepository
)

const userTransactionController = new TransactionSubmitController(
ethereumClient,
sentTransactionRepository,
Expand All @@ -601,9 +616,9 @@ export class Application {
forcedActionsController,
forcedTradeOfferController,
merkleProofController,
collateralAsset,
config.starkex.tradingMode,
searchController
searchController,
l2TransactionController,
config
),
createTransactionRouter(
forcedTradeOfferController,
Expand Down
77 changes: 58 additions & 19 deletions packages/backend/src/api/controllers/HomeController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
renderHomeL2TransactionsPage,
renderHomePage,
renderHomeStateUpdatesPage,
renderHomeTransactionsPage,
Expand All @@ -10,10 +11,12 @@ import { ForcedTradeOfferViewService } from '../../core/ForcedTradeOfferViewServ
import { PageContextService } from '../../core/PageContextService'
import { PaginationOptions } from '../../model/PaginationOptions'
import { ForcedTradeOfferRepository } from '../../peripherals/database/ForcedTradeOfferRepository'
import { L2TransactionRepository } from '../../peripherals/database/L2TransactionRepository'
import { PreprocessedStateDetailsRepository } from '../../peripherals/database/PreprocessedStateDetailsRepository'
import { UserTransactionData } from '../../peripherals/database/transactions/UserTransaction'
import { UserTransactionRepository } from '../../peripherals/database/transactions/UserTransactionRepository'
import { ControllerResult } from './ControllerResult'
import { l2TransactionToEntry } from './l2TransactionToEntry'
import { userTransactionToEntry } from './userTransactionToEntry'

const FORCED_TRANSACTION_TYPES: UserTransactionData['type'][] = [
Expand All @@ -29,7 +32,9 @@ export class HomeController {
private readonly forcedTradeOfferViewService: ForcedTradeOfferViewService,
private readonly userTransactionRepository: UserTransactionRepository,
private readonly forcedTradeOfferRepository: ForcedTradeOfferRepository,
private readonly preprocessedStateDetailsRepository: PreprocessedStateDetailsRepository
private readonly l2TransactionRepository: L2TransactionRepository,
private readonly preprocessedStateDetailsRepository: PreprocessedStateDetailsRepository,
private readonly showL2Transactions: boolean
) {}

async getHomePage(
Expand All @@ -38,13 +43,17 @@ export class HomeController {
const context = await this.pageContextService.getPageContext(givenUser)
const paginationOpts = { offset: 0, limit: 6 }
const [
l2Transactions,
l2TransactionsCount,
stateUpdates,
totalStateUpdates,
stateUpdatesCount,
forcedUserTransactions,
forcedUserTransactionsCount,
availableOffers,
availableOffersCount,
] = await Promise.all([
this.l2TransactionRepository.getPaginatedWithoutMulti(paginationOpts),
this.l2TransactionRepository.countAllDistinctTransactionIds(),
this.preprocessedStateDetailsRepository.getPaginated(paginationOpts),
this.preprocessedStateDetailsRepository.countAll(),
this.userTransactionRepository.getPaginated({
Expand All @@ -61,22 +70,30 @@ export class HomeController {
})

const collateralAsset = this.pageContextService.getCollateralAsset(context)
const transactions = forcedUserTransactions.map((t) =>
const forcedTransactionEntries = forcedUserTransactions.map((t) =>
userTransactionToEntry(t, collateralAsset, assetDetailsMap)
)

const stateUpdateEntries = stateUpdates.map((update) => ({
timestamp: update.timestamp,
id: update.stateUpdateId.toString(),
hash: update.stateTransitionHash,
updateCount: update.assetUpdateCount,
forcedTransactionCount: update.forcedTransactionCount,
}))

const content = renderHomePage({
context,
tutorials: [], // explicitly no tutorials
stateUpdates: stateUpdates.map((update) => ({
timestamp: update.timestamp,
id: update.stateUpdateId.toString(),
hash: update.stateTransitionHash,
updateCount: update.assetUpdateCount,
forcedTransactionCount: update.forcedTransactionCount,
})),
totalStateUpdates,
transactions,
l2Transactions: this.showL2Transactions
? {
data: l2Transactions.map(l2TransactionToEntry),
total: l2TransactionsCount,
}
: undefined,
stateUpdates: stateUpdateEntries,
totalStateUpdates: stateUpdatesCount,
forcedTransactions: forcedTransactionEntries,
totalForcedTransactions: forcedUserTransactionsCount,
// We use forcedTradeOfferToEntry here because we only need status from the offer,
// as we do not show other statuses on this page
Expand All @@ -88,6 +105,26 @@ export class HomeController {
return { type: 'success', content }
}

async getHomeL2TransactionsPage(
givenUser: Partial<UserDetails>,
pagination: PaginationOptions
): Promise<ControllerResult> {
const context = await this.pageContextService.getPageContext(givenUser)

const [total, l2Transactions] = await Promise.all([
this.l2TransactionRepository.countAllDistinctTransactionIds(),
this.l2TransactionRepository.getPaginatedWithoutMulti(pagination),
])

const content = renderHomeL2TransactionsPage({
context,
l2Transactions: l2Transactions.map(l2TransactionToEntry),
...pagination,
total,
})
return { type: 'success', content }
}

async getHomeStateUpdatesPage(
givenUser: Partial<UserDetails>,
pagination: PaginationOptions
Expand All @@ -99,15 +136,17 @@ export class HomeController {
this.preprocessedStateDetailsRepository.getPaginated(pagination),
])

const stateUpdateEntries = stateUpdates.map((update) => ({
timestamp: update.timestamp,
id: update.stateUpdateId.toString(),
hash: update.stateTransitionHash,
updateCount: update.assetUpdateCount,
forcedTransactionCount: update.forcedTransactionCount,
}))

const content = renderHomeStateUpdatesPage({
context,
stateUpdates: stateUpdates.map((update) => ({
timestamp: update.timestamp,
id: update.stateUpdateId.toString(),
hash: update.stateTransitionHash,
updateCount: update.assetUpdateCount,
forcedTransactionCount: update.forcedTransactionCount,
})),
stateUpdates: stateUpdateEntries,
...pagination,
total,
})
Expand Down
Loading

0 comments on commit 0c70fa5

Please sign in to comment.