Marketplace provider

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.

Provider base

The props:

interface ListingParameters {
  tokenId: number
  amount: number
  price: number
  expiry: Date
}

interface Listing {
  saleId: number
  seller: Address
  parameters: ListingParameters
}

interface MarketProviderContextProps {
  market?: Market
  isApproved: boolean
  listings: Listing[]
  approve: () => Promise<void>
  revoke: () => Promise<void>
  list_for_sale: (params: ListingParameters) => Promise<CallResult | undefined>
  remove_listing: (listing: Listing) => Promise<CallResult | undefined>
  buy: (listing: Listing, amount: number) => Promise<CallResult | undefined>
  fetchMarketplaceApproval: () => Promise<void>
  fetchListings: () => Promise<void>
}
  • market is the marketplace contract instance

  • isApproved and sales is the provider's state

  • approve, revoke control the marketplace operator status for the connected wallet

  • list_for_sale, remove_listing and buy are the contract entry points

  • fetchMarketplaceApproval and fetchListings update the state

As for the FA2 contract provider, we initialize it at load:

Fetch approval

This one is a simple contract read.

Fetch sales

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.

Fetch on load

As usual, we fetch the state on component load:

Entry points

Each entrypoint is called using the generated bindings.

approve / revoke

list_for_sale / remove_listing / buy

Wrap up

Memoise the value and pass it to the children:

Export

Export the component.

Include the provider in the App hierarchy, with access to the wallet and Tzombies contexts.

Last updated