BuyModal

Leverage the BuyModal to purchase a token using ReservoirKit

Prerequisites ⚙️

Install and configure ReservoirKit.

BuyModal

ReservoirKit provides a simple to configure modal for facilitating token purchases in your react app. Below is an example of a simple BuyModal setup.

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

<BuyModal
  trigger={
    <button>
      Buy Token
    </button>
  }
  collectionId="0xf5de760f2e916647fd766b4ad9e85ff943ce3a2b"
  tokenId="1236715"
  onPurchaseComplete={(data) => console.log('Purchase Complete')}
  onPurchaseError={(error, data) => console.log('Transaction Error', error, data)}
  onClose={(data, stepData, currentStep) => console.log('Modal 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 collectionId and a tokenId These can be set to undefined until the data is ready.

There are also some additional optional props:

PropDescription
feesOnTopBpsUsed to conditionally apply a referral fee on top of an order when purchasing. The parameter is an array of strings with the referrer and the fee seperated by a colon: ['0xabc:250']. The fee is in bps. The fee currency will follow the default rules for the execute buy endpoint feesOnTop parameter. This takes precedence over the feesOnTopFixed.
feesOnTopFixedUsed to conditionally apply a referral fee on top of an order when purchasing. The parameter is an array of strings with the referrer and the fee seperated by a colon: ['0xabc:10000000000000000']. The fee is a flat fee in the atomic unit of whatever the listing currency will be in (for ether this will be wei). The fee currency will follow the default rules for the execute buy endpoint feesOnTop parameter.
normalizeRoyaltiesUse this to ensure that royalties are added to the order. 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:

onPurchaseComplete: Triggered when the purchase was completed successfully, returns some useful data about the purchase.

onPurchaseError: Triggered when the purchase resulted in an error, returns the error and the purchase data.

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

Conditional Rendering

ReservoirKit's BuyModal doesn't take care of conditionally showing the button/modal based on if the item is purchasable, we leave that logic up to the developer. Below are the requirements for purchasing 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 useSigner wagmi hook.
  • The user cannot be the owner of the token, you can check this by comparing the current address and the address of the owner.
  • The token must have a valid listing, you can check if the floorAskPrice is available for that token using the Tokens api endpoint.

Custom BuyModal

The BuyModal also comes with a custom renderer which can be used to just get the data layer that the BuyModal 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 { BuyModal, BuyStep } from '@reservoir0x/reservoir-kit-ui'

<BuyModal.Custom
  open={open}
  tokenId={tokenId}
  collectionId={collectionId}>
  {({
    loading,
    token,
    collection,
    listing,
    quantityAvailable,
    quantity,
    averageUnitPrice,
    currency,
    mixedCurrencies,
    totalPrice,
    feeOnTop,
    buyStep,
    transactionError,
    hasEnoughCurrency,
    steps,
    stepData,
    feeUsd,
    totalUsd,
    usdPrice,
    isBanned,
    balance,
    address,
    blockExplorerBaseUrl,
    setQuantity,
    setBuyStep,
    buyToken,
  }) => {
    { Your Custom React Elements }
  })}
</BuyModal.Custom>

The custom BuyModal 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 BuyModal 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 BuyStep here which is used to manage the internal state of the BuyModal's logic. You can decide to use all or some of the data passed into your custom implementation.

👍

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