The MarketProvider follows the same principles as the TzombiesProvider. This page will cover only the specifics for this provider. If you would like to view the full code, it is available on the repository.
This one is a bit more tricky. If you remember, the marketplace contract does not (and cannot) check all aspects of valid orders, as it is loosely coupled with the token contract. We need to filter out empty or expired orders.
We have a counter: next_order_id. We'll use it to iterate over the orders.
constfetchListings=useCallback(async () => {if (!market ||!fa2 ||!fa2.address) returnconstnOrders=awaitmarket.get_next_order_id()constlistings:Listing[] = []constinventories:Map<Address,UserInventory> =newMap()for (let i =1; i <nOrders.to_number(); i++) {constorder=awaitmarket.get_order_value(newNat(i))if (!order) continue// filter out expired ordersif (newDate(order.expiry) <newDate()) continue// check how many tokens the seller still hasif (!inventories.has(order.seller)) {inventories.set(order.seller,awaitfetchFa2Balance(order.seller)) }constamount= inventories.get(order.seller)?.get(order.token_id.to_number())constqty=Math.min(order.amount.to_number(), amount ??0)if (qty <1) continuelistings.push({ saleId: i, seller:order.seller, parameters: { tokenId:order.token_id.to_number(), amount: qty, price:order.price.to_big_number().toNumber(), expiry:order.expiry, }, }) }setListings(listings)}, [fa2, fetchFa2Balance, market])