Skip to content

Commit

Permalink
Merge pull request #229 from decentraland/fix/ethereum-orders
Browse files Browse the repository at this point in the history
fix: orders query for eth
  • Loading branch information
juanmahidalgo authored Dec 16, 2024
2 parents d87951a + ad6b3a9 commit eaff62b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/adapters/items/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EmoteCategory, Item, NFTCategory, WearableCategory } from '@dcl/schemas
import { isAddressZero } from '../../logic/address'
import { getNetwork, getNetworkChainId } from '../../logic/chainIds'
import { DBItem, ItemType } from '../../ports/items'
import { fixUrn } from '../../ports/nfts/utils'

export function getCategoryFromDBItem(dbItem: DBItem): NFTCategory {
if (
Expand Down Expand Up @@ -49,7 +50,7 @@ export function fromDBItemToItem(dbItem: DBItem): Item {
return {
id: dbItem.id,
name: dbItem.name,
thumbnail: dbItem.image,
thumbnail: fixUrn(dbItem.image),
url: `/contracts/${dbItem.contract_address}/items/${dbItem.item_id}`,
category: getCategoryFromDBItem(dbItem),
contractAddress: dbItem.contract_address,
Expand All @@ -68,7 +69,7 @@ export function fromDBItemToItem(dbItem: DBItem): Item {
data: getDataFromDBItem(dbItem),
network: getNetwork(dbItem.network),
chainId: getNetworkChainId(dbItem.network),
urn: dbItem.urn,
urn: fixUrn(dbItem.urn),
firstListedAt: dbItem.first_listed_at?.getTime(),
tradeExpiresAt: dbItem.trade_expires_at?.getTime(),
picks: { count: 0 }, // TODO: check this
Expand Down
5 changes: 3 additions & 2 deletions src/adapters/nfts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fromSecondsToMilliseconds } from '../../logic/date'
import { capitalize } from '../../logic/strings'
import { ItemType } from '../../ports/items'
import { DBNFT, NFTResult } from '../../ports/nfts/types'
import { fixUrn } from '../../ports/nfts/utils'
import { DBOrder } from '../../ports/orders/types'
import { fromDBOrderToOrder } from '../orders'

Expand Down Expand Up @@ -73,7 +74,7 @@ export function fromDBNFTToNFT(dbNFT: DBNFT): NFT {
createdAt: fromSecondsToMilliseconds(Number(dbNFT.created_at)),
data: getDataFromDBNFT(dbNFT),
id: `${dbNFT.contract_address}-${dbNFT.token_id}`,
image: dbNFT.image || '',
image: fixUrn(dbNFT.image || ''),
issuedId: dbNFT.issued_id,
itemId: dbNFT.item_id,
name: dbNFT.name || capitalize(dbNFT.category),
Expand All @@ -84,7 +85,7 @@ export function fromDBNFTToNFT(dbNFT: DBNFT): NFT {
soldAt: 0, // TODO: Calculate sold at
updatedAt: fromSecondsToMilliseconds(Number(dbNFT.updated_at)), // Convert to ms
url: `/contracts/${dbNFT.contract_address}/tokens/${dbNFT.token_id}`,
urn: dbNFT.urn || undefined
urn: dbNFT.urn ? fixUrn(dbNFT.urn) : undefined
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ports/nfts/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function createNFTsComponent(components: Pick<AppComponents, 'dappsDataba
query = getNFTsQuery(nftFilters)
const nfts = await client.query<DBNFT>(query)
const nftIds = nfts.rows.map(nft => nft.id)
query = getOrdersQuery({ nftIds, status: ListingStatus.OPEN, owner })
query = getOrdersQuery({ nftIds, status: ListingStatus.OPEN, owner }, 'combined_nft_orders') // Added a specific prefix to track this queries in the logs easily
const orders = await client.query<DBOrder>(query)

const landNftIds = nfts.rows
Expand Down
4 changes: 4 additions & 0 deletions src/ports/nfts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ export async function getNFTFilters(filters: NFTFilters, listsServer: string, re

return { ...filters, bannedNames }
}

export function fixUrn(urn: string) {
return urn.replace('mainnet', 'ethereum')
}
27 changes: 17 additions & 10 deletions src/ports/orders/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ function getOrdersAndTradesFilters(filters: OrderFilters & { nftIds?: string[] }
const FILTER_BY_NETWORK = filters.network ? SQL` network = ANY(${getDBNetworks(filters.network)}) ` : null
const FILTER_ORDER_BY_ITEM_ID = filters.itemId ? SQL` ord.item_id = ${`${filters.contractAddress}-${filters.itemId}`} ` : null
const FILTER_TRADE_BY_ITEM_ID = filters.itemId ? SQL` item_id = ${filters.itemId} ` : null
const FILTER_BY_NFT_NAME = filters.nftName ? SQL` LOWER(nft_name) = LOWER(${filters.nftName}) ` : null
const FILTER_BY_NFT_ID = filters.nftIds ? SQL` nft_id = ANY(${filters.nftIds}) ` : null
const FILTER_ORDER_NOT_EXPIRED = SQL` expires_normalized > NOW() `
const FILTER_TRADE_NOT_EXPIRED = SQL` expires_at > EXTRACT(EPOCH FROM now()::timestamptz(3)) `
Expand All @@ -142,7 +141,6 @@ function getOrdersAndTradesFilters(filters: OrderFilters & { nftIds?: string[] }
FILTER_BY_TOKEN_ID,
FILTER_BY_STATUS,
FILTER_BY_NETWORK,
FILTER_BY_NFT_NAME,
FILTER_BY_NFT_ID
]
return {
Expand Down Expand Up @@ -177,20 +175,29 @@ export function getOrderAndTradeQueries(filters: OrderFilters & { nftIds?: strin
}

// The original getOrdersQuery can now use the new function if needed
export function getOrdersQuery(filters: OrderFilters & { nftIds?: string[] }): SQLStatement {
export function getOrdersQuery(filters: OrderFilters & { nftIds?: string[] }, prefix = 'combined_orders'): SQLStatement {
const { orderTradesQuery, legacyOrdersQuery } = getOrderAndTradeQueries(filters)

return SQL`
SELECT combined_orders.* FROM (
(`
.append(orderTradesQuery)
SELECT `
.append(prefix)
.append(
SQL`)
SQL`.* FROM (
(`
.append(orderTradesQuery)
.append(
SQL`)
UNION ALL
(`.append(legacyOrdersQuery).append(SQL`)
) as combined_orders`)
(`
.append(legacyOrdersQuery)
.append(
SQL`)
) as `
)
.append(prefix)
.append(getOrdersSortByStatement(filters).append(getOrdersLimitAndOffsetStatement(filters)))
)
)
.append(getOrdersSortByStatement(filters).append(getOrdersLimitAndOffsetStatement(filters)))
}

export function getOrdersCountQuery(filters: OrderFilters & { nftIds?: string[] }): SQLStatement {
Expand Down

0 comments on commit eaff62b

Please sign in to comment.