Accept a Bid
Learn how to accept a bid on a token using ReservoirKit.
Prerequisites ⚙️
Install and configure ReservoirKit.
AcceptBidModal
ReservoirKit provides a simple to configure modal for facilitating accepting bids in your react app. Below is an example of a simple AcceptBidModal setup.
import { AcceptBidModal } from '@reservoir0x/reservoir-kit-ui'
<AcceptBidModal
trigger={
<button>
Accept Bid
</button>
}
collectionId="0xf5de760f2e916647fd766b4ad9e85ff943ce3a2b"
tokenId="1469875"
bidId="0x4518b3a90ae5873ee3031904b74366b0316329c2f5ef81ec30480c562a30be9c"
onBidAccepted={(data) => {
console.log('Bid Accepted', data)
}}
onBidAcceptError={(error, data) => {
console.log('Bid Acceptance Error', error, data)
}}
onClose={(data, stepData, currentStep) => {
console.log('AcceptBidModal 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 need to provide a collectionId
and atokenId
. These can be set to undefined
until the data is ready.
There are also some additional optional props:
Prop | Description |
---|---|
bidId | You can also optionally supply a bidId which will pull the order directly from the bids api |
openState | This 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. |
normalizeRoyalties | Use 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. |
Finally you can set some optional callbacks:
onBidAccepted: Triggered when the bid was accepted successfully, returns some useful data about the bid.
onBidAcceptError: Triggered when accepting the bid resulted in an error, returns the error and the bid data.
onClose: Triggered when the modal was closed. Returns some useful data about the bid as well as the step data and the current step.
Conditional Rendering
ReservoirKit's AcceptBidModal doesn't take care of conditionally showing the button/modal based on if the item has a bid, we leave that logic up to the developer. Below are the requirements for accepting a bid 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 token must have a valid offer, you can check if the
token.market.topBid.price.amount.decimal
is available for that token using the Tokens api endpoint.
Custom AcceptBidModal
The AcceptBidModal also comes with a custom renderer which can be used to just get the data layer that the AcceptBidModal 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 { AcceptBidModal, AcceptBidStep } from '@reservoir0x/reservoir-kit-ui'
<AcceptBidModal.Custom
open={open}
tokenId={tokenId}
collectionId={collectionId}>
{({
token,
collection,
source,
expiration,
totalPrice,
bidAmount,
bidAmountCurrency,
ethBidAmount,
fees,
acceptBidStep,
transactionError,
txHash,
totalUsd,
ethUsdPrice,
address,
etherscanBaseUrl,
stepData,
acceptBid,
}) => {
{ Your Custom React Elements }
})}
</AcceptBidModal.Custom>
The custom AcceptBidModal 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 AcceptBidModal 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 AcceptBidStep
here which is used to manage the internal state of the AcceptBidModal logic. You can decide to use all or some of the data passed into your custom implementation.
Nice job!
Now that we've added an accept bid modal to your dApp, let's customize the theme to match your brand.
Updated almost 2 years ago