Cancel a Listing

Learn how to cancel a listing using ReservoirKit.

Prerequisites ⚙️

Install and configure ReservoirKit.

CancelListingModal

ReservoirKit provides a simple to configure modal for canceling a valid listing in your react app. Below is an example of a simple CancelListingModal setup.

import { CancelListingModal } from '@reservoir0x/reservoir-kit-ui'

<CancelListingModal
  trigger={
    <button>
      Cancel Listing
    </button>
  }
  listingId="0x26546d77a4511a2c0c2ab24970cdad886fad35f9f875bbb524f1d92775115153"
  onCancelComplete={(data: any) => {
    console.log('Listing Canceled', data)
  }}
  onCancelError={(error: any, data: any) => {
    console.log('Listing Cancel Error', error, data)
  }}
  onClose={(data, currentStep) => {
    console.log('CancelListingModal Closed')
  }}
/>

Let's dive into the parameters. You'll need to provide an element to trigger the modal, this can be any valid html element. Next you'll want to provide a listingId, it can be set to undefined until the data is ready.

There are also some additional optional props:

PropDescription
normalizeRoyaltiesUse this to ensure that royalties are added to the listing data displayed. If royalties are missing from the order then the correct royalties are added on top. If unspecified then the global setting will be used if provided.
openStateThis is a state tuple from react's useState, you can use this to programmatically open or close the modal. The modal will use this state when determining if the modal is open or closed.

Finally you can set some optional callbacks:

onCancelComplete: Triggered when cancelling was completed successfully, returns some useful data about the listing and the cancel step.

onCancelError: Triggered when the cancel resulted in an error, returns the error and the listing data.

onClose: Triggered when the modal was closed. Returns some useful data about the listing as well as the step data and the current step.

Conditional Rendering

ReservoirKit's CancelListingModal doesn't take care of conditionally showing the button/modal based on if the item is cancelable by the user, we leave that logic up to the developer. Below are the requirements for canceling a listing and how to go about getting that data:

  • The user needs to have a connected wallet. You can check this by looking for a signer from the useWalletClient wagmi hook.
  • The user must be the maker of the listing, you can check this by comparing maker in the listing object to the connected wallet address. You can get a full list of a wallet's listings by using the useListings hook or the underlying api.

Custom CancelListingModal

The CancelListingModal also comes with a custom renderer which can be used to just get the data layer that the CancelListingModal uses internally to handle the underlying business logic. With the renderer you can rebuild the UI completely to your liking. Below is an example of how it works in practice.

import { CancelListingModal, CancelListingStep } from '@reservoir0x/reservoir-kit-ui'

<CancelListingModal.Custom
    open={open}
    listingId="0x26546d77a4511a2c0c2ab24970cdad886fad35f9f875bbb524f1d92775115153"
  >
    {({
      loading,
      listing,
      tokenId,
      contract,
      cancelStep,
      transactionError,
      stepData,
      totalUsd,
      blockExplorerBaseUrl,
      cancelOrder,
    }) => {
  { Your Custom React Elements }
})}
</CancelListingModal.Custom>

The custom CancelListingModal takes a few parameters like before with one additional one being the open parameter. This is because there is no trigger, you have control over what sort of modal you want this to eventually live in and how to trigger that modal. You'll have the ability to add a custom button with a custom handler, etc. The custom CancelListingModal then passes some key data into the children which we parse above and use in our custom UI. It's also important to note the CancelListingStep here which is used to manage the internal state of the CancelListingModal's logic. You can decide to use all or some of the data passed into your custom implementation.

👍

Nice job!

Now that we've added a cancel listing modal to your dApp, let's customize the theme to match your brand.