Skip to content

Commit

Permalink
feat: modifications to order endpoint to filter by itemId and sort by…
Browse files Browse the repository at this point in the history
… issue id and oldest (#250)

* feat: modifications to order endpoint to filter by itemId and sort by issue id and oldest

* fix: query

* feat: added name as filter and modify query on orders endpoint

* fix: test

* feat: updated dcl schemas
  • Loading branch information
flobarreto authored Mar 22, 2023
1 parent e255477 commit b65cead
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 20 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"printWidth": 80
},
"dependencies": {
"@dcl/schemas": "^6.12.0",
"@dcl/schemas": "^6.13.2",
"@well-known-components/env-config-provider": "^1.2.0",
"@well-known-components/http-requests-logger-component": "^2.1.0",
"@well-known-components/http-server": "^1.1.6",
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/handlers/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export function createOrdersHandler(
const tokenId = params.getString('tokenId')
const status = params.getValue<ListingStatus>('status', ListingStatus)
const network = params.getValue<Network>('network', Network)
const itemId = params.getString('itemId')
const nftName = params.getString('nftName')

return asJSON(() =>
orders.fetchAndCount({
Expand All @@ -34,6 +36,8 @@ export function createOrdersHandler(
tokenId,
status,
network,
itemId,
nftName,
})
)
}
Expand Down
3 changes: 3 additions & 0 deletions src/adapters/sources/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export function createOrdersSource(
[OrderSortBy.RECENTLY_LISTED]: result.createdAt,
[OrderSortBy.RECENTLY_UPDATED]: result.updatedAt,
[OrderSortBy.CHEAPEST]: +result.price,
[OrderSortBy.ISSUED_ID_ASC]: result.tokenId,
[OrderSortBy.ISSUED_ID_DESC]: result.tokenId,
[OrderSortBy.OLDEST]: result.createdAt,
},
}))
}
Expand Down
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ import {
getMarketplaceContracts,
} from './logic/contracts'
import { BID_DEFAULT_SORT_BY } from './ports/bids/utils'
import { ORDER_DEFAULT_SORT_BY } from './ports/orders/utils'
import {
getCollectionsItemIdFilter,
getCollectionsNameFilter,
getMarketplaceItemIdFilter,
getMarketplaceNameFilter,
ORDER_DEFAULT_SORT_BY,
} from './ports/orders/utils'
import { createItemsSource } from './adapters/sources/items'
import { createItemsComponent } from './ports/items/component'
import { ITEM_DEFAULT_SORT_BY } from './ports/items/utils'
Expand Down Expand Up @@ -237,12 +243,16 @@ async function initComponents(): Promise<AppComponents> {
subgraph: marketplaceSubgraph,
network: Network.ETHEREUM,
chainId: marketplaceChainId,
getItemIdFilter: getMarketplaceItemIdFilter,
getNameFilter: getMarketplaceNameFilter,
})

const collectionsOrders = createOrdersComponent({
subgraph: collectionsSubgraph,
network: Network.MATIC,
chainId: collectionsChainId,
getItemIdFilter: getCollectionsItemIdFilter,
getNameFilter: getCollectionsNameFilter,
})

const orders = createMergerComponent<Order, OrderFilters, OrderSortBy>({
Expand All @@ -255,6 +265,9 @@ async function initComponents(): Promise<AppComponents> {
[OrderSortBy.RECENTLY_LISTED]: SortDirection.DESC,
[OrderSortBy.RECENTLY_UPDATED]: SortDirection.DESC,
[OrderSortBy.CHEAPEST]: SortDirection.ASC,
[OrderSortBy.ISSUED_ID_ASC]: SortDirection.ASC,
[OrderSortBy.ISSUED_ID_DESC]: SortDirection.DESC,
[OrderSortBy.OLDEST]: SortDirection.ASC,
},
maxCount: 1000,
})
Expand Down
8 changes: 5 additions & 3 deletions src/ports/orders/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ export function createOrdersComponent(options: {
subgraph: ISubgraphComponent
network: Network
chainId: ChainId
getItemIdFilter: (itemId: string) => string
getNameFilter: (name: string) => string
}): IOrdersComponent {
const { subgraph, network, chainId } = options
const { subgraph, network, chainId, getItemIdFilter, getNameFilter } = options

async function fetch(filters: OrderFilters) {
if (filters.network && filters.network !== network) {
return []
}

const query = getOrdersQuery(filters)
const query = getOrdersQuery(filters, false, getItemIdFilter, getNameFilter)
const { orders: fragments } = await subgraph.query<{
orders: OrderFragment[]
}>(query)
Expand All @@ -32,7 +34,7 @@ export function createOrdersComponent(options: {
return 0
}

const query = getOrdersQuery(filters, true)
const query = getOrdersQuery(filters, true, getItemIdFilter, getNameFilter)
const { orders: fragments } = await subgraph.query<{
orders: OrderFragment[]
}>(query)
Expand Down
53 changes: 48 additions & 5 deletions src/ports/orders/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import { OrderFragment } from './types'

export const ORDER_DEFAULT_SORT_BY = OrderSortBy.RECENTLY_LISTED

// We're filtering on collections subgraph by itemId and on marketplace subgraph by name as there's not itemId
export const getCollectionsItemIdFilter = (itemId: string) => `
nft_: {itemBlockchainId: "${itemId}"}
`
export const getCollectionsNameFilter = (_name: string) => ``

export const getMarketplaceItemIdFilter = (_itemId: string) => ``

export const getMarketplaceNameFilter = (name: string) => `
nft_: {name: "${name}"}
`

export const getOrderFields = () => `
fragment orderFields on Order {
id
Expand All @@ -27,14 +39,20 @@ export const getOrderFields = () => `
}
}
`

export const getOrderFragment = () => `
fragment orderFragment on Order {
...orderFields
}
${getOrderFields()}
`

export const getOrdersQuery = (filters: OrderFilters, isCount = false) => {
export const getOrdersQuery = (
filters: OrderFilters,
isCount = false,
getItemIdFilter: (itemId: string) => string,
getNameFilter: (name: string) => string
) => {
const {
first,
skip,
Expand All @@ -45,6 +63,8 @@ export const getOrdersQuery = (filters: OrderFilters, isCount = false) => {
buyer,
owner,
status,
itemId,
nftName,
} = filters

const where: string[] = []
Expand All @@ -69,6 +89,16 @@ export const getOrdersQuery = (filters: OrderFilters, isCount = false) => {
where.push(`owner: "${owner}"`)
}

if (itemId) {
const itemIdFilter = getItemIdFilter(itemId)
where.push(itemIdFilter)
}

if (nftName) {
const nameFilter = getNameFilter(nftName)
where.push(nameFilter)
}

if (status) {
if (status === ListingStatus.OPEN) {
where.push(`expiresAt_gt: "${Date.now()}"`)
Expand All @@ -80,13 +110,14 @@ export const getOrdersQuery = (filters: OrderFilters, isCount = false) => {
const total = isCount
? max
: typeof first !== 'undefined'
? typeof skip !== 'undefined'
? skip + first
: first
: max
? typeof skip !== 'undefined'
? skip + first
: first
: max

let orderBy: string
let orderDirection: string

switch (sortBy || ORDER_DEFAULT_SORT_BY) {
case OrderSortBy.RECENTLY_LISTED:
orderBy = 'createdAt'
Expand All @@ -100,6 +131,18 @@ export const getOrdersQuery = (filters: OrderFilters, isCount = false) => {
orderBy = 'price'
orderDirection = 'asc'
break
case OrderSortBy.ISSUED_ID_ASC:
orderBy = 'tokenId'
orderDirection = 'asc'
break
case OrderSortBy.ISSUED_ID_DESC:
orderBy = 'tokenId'
orderDirection = 'desc'
break
case OrderSortBy.OLDEST:
orderBy = 'createdAt'
orderDirection = 'asc'
break
default:
orderBy = 'createdAt'
orderDirection = 'desc'
Expand Down
19 changes: 16 additions & 3 deletions src/tests/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ import { MINT_DEFAULT_SORT_BY } from '../ports/mints/utils'
import { createNFTComponent } from '../ports/nfts/component'
import { NFTResult } from '../ports/nfts/types'
import { NFT_DEFAULT_SORT_BY } from '../ports/nfts/utils'
import { ORDER_DEFAULT_SORT_BY } from '../ports/orders/utils'
import {
getCollectionsItemIdFilter,
getCollectionsNameFilter,
getMarketplaceItemIdFilter,
getMarketplaceNameFilter,
ORDER_DEFAULT_SORT_BY,
} from '../ports/orders/utils'
import { createSalesComponent } from '../ports/sales/component'
import { SALE_DEFAULT_SORT_BY } from '../ports/sales/utils'
import { getMarketplaceChainId, getCollectionsChainId } from '../logic/chainIds'
Expand Down Expand Up @@ -207,12 +213,16 @@ export async function initComponents(): Promise<AppComponents> {
subgraph: marketplaceSubgraph,
network: Network.ETHEREUM,
chainId: marketplaceChainId,
getItemIdFilter: getMarketplaceItemIdFilter,
getNameFilter: getMarketplaceNameFilter,
})

const collectionsOrders = createOrdersComponent({
subgraph: collectionsSubgraph,
network: Network.MATIC,
chainId: collectionsChainId,
getItemIdFilter: getCollectionsItemIdFilter,
getNameFilter: getCollectionsNameFilter,
})

const orders = createMergerComponent<Order, OrderFilters, OrderSortBy>({
Expand All @@ -225,6 +235,9 @@ export async function initComponents(): Promise<AppComponents> {
[OrderSortBy.RECENTLY_LISTED]: SortDirection.DESC,
[OrderSortBy.RECENTLY_UPDATED]: SortDirection.DESC,
[OrderSortBy.CHEAPEST]: SortDirection.ASC,
[OrderSortBy.ISSUED_ID_ASC]: SortDirection.ASC,
[OrderSortBy.ISSUED_ID_DESC]: SortDirection.DESC,
[OrderSortBy.OLDEST]: SortDirection.ASC,
},
maxCount: 1000,
})
Expand Down Expand Up @@ -566,7 +579,7 @@ export async function initComponents(): Promise<AppComponents> {
const statusChecks = await createStatusCheckComponent({ config, server })

const owners = createOwnersComponent({
subgraph: collectionsSubgraph
subgraph: collectionsSubgraph,
})

return {
Expand All @@ -593,6 +606,6 @@ export async function initComponents(): Promise<AppComponents> {
rankings,
prices,
stats,
owners
owners,
}
}

0 comments on commit b65cead

Please sign in to comment.