Skip to content

Commit

Permalink
Add missing available offers page (#475)
Browse files Browse the repository at this point in the history
* add missing available offers page

* fix build issues
  • Loading branch information
torztomasz authored Aug 22, 2023
1 parent 8bca191 commit 440bb96
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
26 changes: 26 additions & 0 deletions packages/backend/src/api/controllers/HomeController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
renderHomeAvailableOffersPage,
renderHomeL2TransactionsPage,
renderHomePage,
renderHomeStateUpdatesPage,
Expand Down Expand Up @@ -243,6 +244,31 @@ export class HomeController {
return { type: 'success', content }
}

async getHomeAvailableOffersPage(
givenUser: Partial<UserDetails>,
pagination: PaginationOptions
): Promise<ControllerResult> {
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
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/api/routers/FrontendRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ export function createFrontendRouter(

addPerpetualTradingRoutes(
router,
homeController,
forcedTradeOfferController,
forcedActionController,
l2TransactionController,
Expand Down
32 changes: 30 additions & 2 deletions packages/backend/src/api/routers/PerpetualFrontendRouter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
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'

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,
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions packages/frontend/src/preview/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
renderErrorPage,
renderFinalizeEscapeDetailsPage,
renderFreezeRequestDetailsPage,
renderHomeAvailableOffersPage,
renderHomeL2TransactionsPage,
renderHomeOffersPage,
renderHomePage,
renderHomeStateUpdatesPage,
renderHomeTransactionsPage,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ interface HomeOffersPageProps {
total: number
}

export function renderHomeOffersPage(props: HomeOffersPageProps) {
return reactToHtml(<HomeOffersPage {...props} />)
export function renderHomeAvailableOffersPage(props: HomeOffersPageProps) {
return reactToHtml(<HomeAvailableOffersPage {...props} />)
}

function HomeOffersPage(props: HomeOffersPageProps) {
function HomeAvailableOffersPage(props: HomeOffersPageProps) {
return (
<Page
path={OFFER_TABLE_PROPS.path}
Expand Down

0 comments on commit 440bb96

Please sign in to comment.