Skip to content

Commit

Permalink
fix: for ENS remove the trades CTE HAVING
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmahidalgo committed Jan 2, 2025
1 parent 2e54d45 commit 066846a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/ports/nfts/ensQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function getENSs(nftFilters: GetNFTsFilters): SQLStatement {
ORDER BY created_at
)
`
.append(getTradesCTE(nftFilters))
.append(getTradesCTE(nftFilters, false))
.append(
SQL`
`
Expand Down Expand Up @@ -96,6 +96,7 @@ export function getENSs(nftFilters: GetNFTsFilters): SQLStatement {
LEFT JOIN trades ON (trades.assets -> 'sent' ->> 'token_id')::numeric = nft.token_id
AND trades.assets -> 'sent' ->> 'contract_address' = nft.contract_address
AND trades.status = 'open'
AND trades.signer || '-' || nft.network = nft.owner_id
LEFT JOIN squid_marketplace.ens ens ON ens.id = nft.ens_id
`
.append(isOnSale ? SQL`LEFT JOIN valid_orders orders ON orders.nft_id = nft.id` : SQL``)
Expand Down
15 changes: 13 additions & 2 deletions src/ports/nfts/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function getParcelEstateDataCTE(filters: GetNFTsFilters): SQLStatement {
* const filters = { owner: '0x123...', tokenId: '1234' };
* const tradesCTE = getTradesCTE(filters);
*/
export function getTradesCTE(filters: GetNFTsFilters): SQLStatement {
export function getTradesCTE(filters: GetNFTsFilters, addHavingStatement = true): SQLStatement {
const FILTER_BY_OWNER = filters.owner ? SQL` t.signer = ${filters.owner.toLocaleLowerCase()} ` : null
const FILTER_BY_TOKEN_ID = filters.tokenId ? SQL` (assets_with_values.nft_id = ${filters.tokenId}) ` : null
const marketplacePolygon = getContract(ContractName.OffChainMarketplace, getPolygonChainId())
Expand Down Expand Up @@ -262,9 +262,20 @@ export function getTradesCTE(filters: GetNFTsFilters): SQLStatement {
LEFT JOIN squid_trades.trade as trade_status ON trade_status.signature = t.hashed_signature
LEFT JOIN squid_trades.signature_index as signer_signature_index ON LOWER(signer_signature_index.address) = LOWER(t.signer)
LEFT JOIN (select * from squid_trades.signature_index signature_index where LOWER(signature_index.address) IN (${marketplaceEthereum.address.toLowerCase()}, ${marketplacePolygon.address.toLowerCase()})) as contract_signature_index ON t.network = contract_signature_index.network
`.append(where).append(SQL`
`
.append(where)
.append(
SQL`
GROUP BY t.id, t.created_at, t.network, t.chain_id, t.signer, t.checks, contract_signature_index.index, signer_signature_index.index
`
)
.append(
addHavingStatement
? SQL`
HAVING t.signer = ALL(ARRAY_AGG(assets_with_values.owner) FILTER (WHERE assets_with_values.owner IS NOT NULL AND assets_with_values.direction = 'sent'))
`
: SQL``
).append(SQL`
)
`)
}
Expand Down

0 comments on commit 066846a

Please sign in to comment.