Skip to content

Commit

Permalink
Feat/ethereum items from collection (#273)
Browse files Browse the repository at this point in the history
* feat: added collections ethereum integration

* fix: add toLocaleLowerCase in the find for marketplaceContracts

* fix: tests

* feat: removed comment

* fix: comment

* fix: timestamps

---------

Signed-off-by: Florencia Barreto <32873485+flobarreto@users.noreply.github.com>
Co-authored-by: Juanma Hidalgo <juanma06@gmail.com>
  • Loading branch information
flobarreto and juanmahidalgo authored May 3, 2023
1 parent a2fc7cc commit 685376b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ CORS_METHOD=*
MARKETPLACE_SUBGRAPH_URL=https://api.thegraph.com/subgraphs/name/decentraland/marketplace
COLLECTIONS_SUBGRAPH_URL=https://api.thegraph.com/subgraphs/name/decentraland/collections-matic-mainnet
RENTALS_SUBGRAPH_URL=https://api.thegraph.com/subgraphs/name/decentraland/rentals-ethereum-mainnet
COLLECTIONS_ETHEREUM_SUBGRAPH_URL=https://api.thegraph.com/subgraphs/name/decentraland/collections-ethereum-mainnet
SIGNATURES_SERVER_URL=
MARKETPLACE_CHAIN_ID=1
COLLECTIONS_CHAIN_ID=137
MIN_SALE_VALUE_IN_WEI=
MIN_SALE_VALUE_IN_WEI=
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ async function initComponents(): Promise<AppComponents> {
await config.requireString('COLLECTIONS_SUBGRAPH_URL')
)

const collectionsEthereumSubgraph = await createSubgraphComponent(
{ logs, config, fetch, metrics },
await config.requireString('COLLECTIONS_ETHEREUM_SUBGRAPH_URL')
)

const rentalsSubgraph = await createSubgraphComponent(
{ logs, config, fetch, metrics },
await config.requireString('RENTALS_SUBGRAPH_URL')
Expand Down Expand Up @@ -431,11 +436,18 @@ async function initComponents(): Promise<AppComponents> {
)

// items
const collectionsItems = createItemsComponent({
subgraph: collectionsSubgraph,
network: Network.MATIC,
chainId: collectionsChainId,
})
const collectionsItems = createItemsComponent([
{
subgraph: collectionsSubgraph,
network: Network.MATIC,
chainId: collectionsChainId,
},
{
subgraph: collectionsEthereumSubgraph,
network: Network.ETHEREUM,
chainId: marketplaceChainId,
},
])

const items = createMergerComponent<Item, ItemOptions, ItemSortBy>({
sources: [
Expand Down
2 changes: 1 addition & 1 deletion src/ports/catalog/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export const getCollectionsItemsCatalogQuery = (
.append(MAX_ORDER_TIMESTAMP)
.append(
`
AND to_timestamp(orders.expires_at / 100.0) > now()
AND to_timestamp(orders.expires_at / 1000.0) > now()
GROUP BY nft.item
) AS nfts_with_orders ON nfts_with_orders.item = items.id
LEFT JOIN (
Expand Down
27 changes: 19 additions & 8 deletions src/ports/items/component.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { ChainId, ItemFilters, Network } from '@dcl/schemas'
import { ISubgraphComponent } from '@well-known-components/thegraph-component'
import { IItemsComponent, ItemFragment } from './types'
import { fromItemFragment, getItemsQuery } from './utils'

export function createItemsComponent(options: {
subgraph: ISubgraphComponent
network: Network
chainId: ChainId
}): IItemsComponent {
const { subgraph, network, chainId } = options
import { fromItemFragment, getItemsQuery, getSubgraph } from './utils'

export function createItemsComponent(
options: {
subgraph: ISubgraphComponent
network: Network
chainId: ChainId
}[]
): IItemsComponent {
async function fetch(filters: ItemFilters) {
const option = getSubgraph(filters, options)
if (!option) return []

const { subgraph, network, chainId } = option

if (filters.network && filters.network !== network) {
return []
}
Expand All @@ -26,6 +31,12 @@ export function createItemsComponent(options: {
}

async function count(filters: ItemFilters) {
const option = getSubgraph(filters, options)

if (!option) return 0

const { subgraph, network } = option

if (filters.network && filters.network !== network) {
return 0
}
Expand Down
41 changes: 36 additions & 5 deletions src/ports/items/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import {
Network,
NFTCategory,
} from '@dcl/schemas'
import { ISubgraphComponent } from '@well-known-components/thegraph-component'
import { ItemFragment, FragmentItemType } from './types'
import { isAddressZero } from '../../logic/address'
import { getGenderFilterQuery } from '../utils'
import { SortDirection } from '../merger/types'
import { getMarketplaceContracts } from '../../logic/contracts'
import { getMarketplaceChainId } from '../../logic/chainIds'

export const ITEM_DEFAULT_SORT_BY = ItemSortBy.NEWEST

Expand Down Expand Up @@ -216,11 +219,7 @@ export function getItemsQuery(filters: ItemFilters, isCount = false) {
}

if (ids && ids.length > 0) {
where.push(
`id_in: [${ids
.map((id) => `"${id}"`)
.join(',')}]`
)
where.push(`id_in: [${ids.map((id) => `"${id}"`).join(',')}]`)
}

if (contractAddresses && contractAddresses.length > 0) {
Expand Down Expand Up @@ -375,6 +374,38 @@ export function getItemsQuery(filters: ItemFilters, isCount = false) {
`
}

export const getSubgraph = (
filters: ItemFilters,
options: {
subgraph: ISubgraphComponent
network: Network
chainId: ChainId
}[]
) => {
// By default, we use the MATIC subgraph, unless we're asking for an item with a contractAddress that belongs to the Marketplace legacy contracts
let option = options.find((option) => option.network === Network.MATIC)
if (filters.contractAddresses?.length) {
const marketplaceContracts = getMarketplaceContracts(
getMarketplaceChainId()
)
const marketplaceContract = marketplaceContracts.find(
(marketplaceContract) =>
!!filters.contractAddresses &&
filters.contractAddresses[0] &&
marketplaceContract.address.toLocaleLowerCase() ===
filters.contractAddresses[0]
)
if (marketplaceContract) {
option = options.find(
(option) =>
option.network === marketplaceContract.network &&
option.chainId === marketplaceContract.chainId
)
}
}
return option
}

/**
* It returns the minimum sale value allowed for item sales if any.
* The price is not expected to be used as an inclusive cap, meaning that prices that equal the value SHOULD NOT be filtered
Expand Down
12 changes: 7 additions & 5 deletions src/tests/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,13 @@ export async function initComponents(): Promise<AppComponents> {
)

// items
const collectionsItems = createItemsComponent({
subgraph: collectionsSubgraph,
network: Network.MATIC,
chainId: collectionsChainId,
})
const collectionsItems = createItemsComponent([
{
subgraph: collectionsSubgraph,
network: Network.MATIC,
chainId: collectionsChainId,
},
])

const items = createMergerComponent<Item, ItemFilters, ItemSortBy>({
sources: [
Expand Down

0 comments on commit 685376b

Please sign in to comment.