diff --git a/packages/backend/src/api/controllers/HomeController.ts b/packages/backend/src/api/controllers/HomeController.ts index 5f7564189..620d68416 100644 --- a/packages/backend/src/api/controllers/HomeController.ts +++ b/packages/backend/src/api/controllers/HomeController.ts @@ -1,4 +1,5 @@ import { + renderHomeAvailableOffersPage, renderHomeL2TransactionsPage, renderHomePage, renderHomeStateUpdatesPage, @@ -243,6 +244,31 @@ export class HomeController { return { type: 'success', content } } + async getHomeAvailableOffersPage( + givenUser: Partial, + pagination: PaginationOptions + ): Promise { + const context = await this.pageContextService.getPageContext(givenUser) + if (context.tradingMode === 'spot') { + throw new Error('Page unavailable in spot trading mode') + } + + const [availableOffers, availableOffersCount] = await Promise.all([ + this.forcedTradeOfferRepository.getAvailablePaginated(pagination), + this.forcedTradeOfferRepository.countAvailable(), + ]) + + const content = renderHomeAvailableOffersPage({ + context, + offers: availableOffers.map((offer) => + this.forcedTradeOfferViewService.toOfferEntry(offer) + ), + total: availableOffersCount, + ...pagination, + }) + return { type: 'success', content } + } + private calculateStateUpdateLimit( recordCounts: { l2Transactions: number diff --git a/packages/backend/src/api/routers/FrontendRouter.ts b/packages/backend/src/api/routers/FrontendRouter.ts index 33b7945b5..0b9ae00c7 100644 --- a/packages/backend/src/api/routers/FrontendRouter.ts +++ b/packages/backend/src/api/routers/FrontendRouter.ts @@ -481,6 +481,7 @@ export function createFrontendRouter( addPerpetualTradingRoutes( router, + homeController, forcedTradeOfferController, forcedActionController, l2TransactionController, diff --git a/packages/backend/src/api/routers/PerpetualFrontendRouter.ts b/packages/backend/src/api/routers/PerpetualFrontendRouter.ts index a2eb440f8..a2e8314de 100644 --- a/packages/backend/src/api/routers/PerpetualFrontendRouter.ts +++ b/packages/backend/src/api/routers/PerpetualFrontendRouter.ts @@ -1,4 +1,9 @@ -import { stringAs, stringAsBigInt, stringAsInt } from '@explorer/shared' +import { + stringAs, + stringAsBigInt, + stringAsInt, + stringAsPositiveInt, +} from '@explorer/shared' import { AssetId } from '@explorer/types' import Router from '@koa/router' import { z } from 'zod' @@ -6,12 +11,14 @@ import { z } from 'zod' import { Config } from '../../config' import { ForcedActionController } from '../controllers/ForcedActionController' import { ForcedTradeOfferController } from '../controllers/ForcedTradeOfferController' +import { HomeController } from '../controllers/HomeController' import { L2TransactionController } from '../controllers/L2TransactionController' import { withTypedContext } from './types' -import { applyControllerResult, getGivenUser } from './utils' +import { applyControllerResult, getGivenUser, getPagination } from './utils' export function addPerpetualTradingRoutes( router: Router, + homeController: HomeController, forcedTradeOfferController: ForcedTradeOfferController, forcedActionController: ForcedActionController, l2TransactionController: L2TransactionController, @@ -48,6 +55,27 @@ export function addPerpetualTradingRoutes( ) ) + router.get( + '/offers', + withTypedContext( + z.object({ + query: z.object({ + page: z.optional(stringAsPositiveInt()), + perPage: z.optional(stringAsPositiveInt()), + }), + }), + async (ctx) => { + const givenUser = getGivenUser(ctx) + const pagination = getPagination(ctx.query) + const result = await homeController.getHomeAvailableOffersPage( + givenUser, + pagination + ) + applyControllerResult(ctx, result) + } + ) + ) + router.get( '/offers/:offerId', withTypedContext( diff --git a/packages/frontend/src/preview/routes.ts b/packages/frontend/src/preview/routes.ts index e7582d732..5f68bd22e 100644 --- a/packages/frontend/src/preview/routes.ts +++ b/packages/frontend/src/preview/routes.ts @@ -17,8 +17,8 @@ import { renderErrorPage, renderFinalizeEscapeDetailsPage, renderFreezeRequestDetailsPage, + renderHomeAvailableOffersPage, renderHomeL2TransactionsPage, - renderHomeOffersPage, renderHomePage, renderHomeStateUpdatesPage, renderHomeTransactionsPage, @@ -240,12 +240,13 @@ const routes: Route[] = [ }, { path: '/offers', - description: 'Offer list accessible from home page. Supports pagination.', + description: + 'Available offer list accessible from home page. Supports pagination.', render: (ctx) => { const context = getPerpetualPageContext(ctx) const total = 68 const { limit, offset, visible } = getPagination(ctx, total) - ctx.body = renderHomeOffersPage({ + ctx.body = renderHomeAvailableOffersPage({ context, offers: repeat(visible, randomHomeOfferEntry), limit, diff --git a/packages/frontend/src/view/index.ts b/packages/frontend/src/view/index.ts index 50e0e91f0..ee8ad5e11 100644 --- a/packages/frontend/src/view/index.ts +++ b/packages/frontend/src/view/index.ts @@ -8,8 +8,8 @@ export * from './pages/forced-actions/NewPerpetualForcedActionPage' export * from './pages/forced-actions/NewSpotForcedWithdrawalPage' export type { HomeStateUpdateEntry } from './pages/home/components/HomeStateUpdatesTable' export type { HomeTutorialEntry } from './pages/home/components/HomeTutorials' +export * from './pages/home/HomeAvailableOffersPage' export * from './pages/home/HomeL2TransactionsPage' -export * from './pages/home/HomeOffersPage' export * from './pages/home/HomePage' export * from './pages/home/HomeStateUpdatesPage' export * from './pages/home/HomeTransactionsPage' diff --git a/packages/frontend/src/view/pages/home/HomeOffersPage.tsx b/packages/frontend/src/view/pages/home/HomeAvailableOffersPage.tsx similarity index 85% rename from packages/frontend/src/view/pages/home/HomeOffersPage.tsx rename to packages/frontend/src/view/pages/home/HomeAvailableOffersPage.tsx index d7167dedf..22102ff6f 100644 --- a/packages/frontend/src/view/pages/home/HomeOffersPage.tsx +++ b/packages/frontend/src/view/pages/home/HomeAvailableOffersPage.tsx @@ -16,11 +16,11 @@ interface HomeOffersPageProps { total: number } -export function renderHomeOffersPage(props: HomeOffersPageProps) { - return reactToHtml() +export function renderHomeAvailableOffersPage(props: HomeOffersPageProps) { + return reactToHtml() } -function HomeOffersPage(props: HomeOffersPageProps) { +function HomeAvailableOffersPage(props: HomeOffersPageProps) { return (