Leverage the SweepModal to sweep tokens using ReservoirKit
SweepModal offers a simple way to make bulk purchases within a collection.
Prerequisites ⚙️
Install and configure ReservoirKit.
SweepModal Setup
ReservoirKit provides a simple to configure modal for facilitating sweeping tokens in your react app. Below is an example of a simple SweepModal setup.
import { SweepModal } from '@reservoir0x/reservoir-kit-ui'
<SweepModal
trigger={
<button>
Sweep
</button>
}
onConnectWallet={onConnectWallet}
contract={contract}
onSweepComplete={(data) => {
console.log('Sweep Complete', data)
}}
onSweepError={(error, data) => {
console.log('Sweep Error', error, data)
}}
onClose={() => {
console.log('SweepModal Closed')
}}
/>
Let's dive into the parameters:
Prop | Description | Required |
---|---|---|
trigger | A react node that will be presented to the user, usually a button or something clickable. | Y |
onConnectWallet | A function that opens the connect wallet flow. Use this to prompt your connect modal and continue the ux of connecting the user's wallet. | Y |
contract | The contract address of the collection you wish to mint from. Can be undefined until the data is ready. Note: A contract, collectionId or token is required | N |
collectionId | The collection id of the collection you wish to mint from. Can be undefined until the data is ready. Note: A contract, collectionId or token is required | N |
token | The token you wish to sweep from. Can be undefined until the data is ready. Format: 0x8d04a8c79ceb0889bdd12acdf3fa9d207ed3ff63:704 Note: A contract, collectionId or token is required | N |
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. | N |
copyOverrides | An object containing copy overrides for titles and ctas contained in the modal. Refer to the SweepModalCopy type for which tokens map to what copy. | N |
feesOnTopBps | Used 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 separated 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 . | N |
feesOnTopUsd | Used 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 separated by a colon: ['0xabc:1000000'] . The fee is a flat fee in the atomic unit of usd (6 decimals). It will then be converted to the purchase currency to match the same value in USD. So for example if you have 1000000 configured (1 usd) and the total price is in ETH the fee applied will be ~0.00054 ETH (or whatever is equivalent to 1 usd in realtime). | N |
chainId | The chain id to force the modal to render, ensure that this is a chain that is configured in the ReservoirKit provider. If not specified then the active chain configured in the ReservoirKitProvider will be used. | N |
walletClient | A valid WalletClient from viem or a ReservoirWallet generated from an adapter. Learn more about adapters | N |
onSweepComplete | Triggered when the purchase was completed successfully, returns some useful data about the transaction. | N |
onSweepError | Triggered when the purchase resulted in an error, returns the error and the sweep data. | N |
onClose | Triggered when the modal was closed. Returns some useful data about the mint as well as and the current step. | N |
Conditional Rendering
ReservoirKit's SweepModal doesn't take care of conditionally showing the button/modal, we leave that logic up to the developer. The user also needs to have a connected wallet. You can check this by looking for a signer from the useWalletClient wagmi hook.
Custom SweepModal
The SweepModal also comes with a custom renderer which can be used to just get the data layer that the SweepModal 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 { SweepModal, SweepStep } from '@reservoir0x/reservoir-kit-ui'
<SweepModal.Custom
open={open}
collectionId={collectionId}>
{({
collection,
token,
loading,
isFetchingPath,
address,
selectedTokens,
setSelectedTokens,
itemAmount,
setItemAmount,
maxItemAmount,
setMaxItemAmount,
paymentCurrency,
setPaymentCurrency,
chainCurrency,
paymentTokens,
totalIncludingFees,
averageUnitPrice,
feeOnTop,
feeUsd,
usdPrice,
disableJumperLink,
usdPriceRaw,
isConnected,
currentChain,
orders,
balance,
hasEnoughCurrency,
addFundsLink,
blockExplorerBaseUrl,
transactionError,
stepData,
setStepData,
sweepStep,
setSweepStep,
sweepTokens,
}) => {
{ Your Custom React Elements }
})}
</SweepModal.Custom>
The custom SweepModal 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 SweepModal 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 SweepStep
here which is used to manage the internal state of the SweepModal'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 sweep modal to your dApp, let's customize the theme to match your brand.