useBuyNow

Hook for buying a listing on a Marketplace smart contract.

If the listing is in a currency that is not native to the chain (e.g. not Ether on Ethereum), the hook will prompt the user to approve the marketplace contract to spend the currency on their behalf before performing the buy.

This hook is only for Marketplace contracts. Not for MarketplaceV3

Example

import {
useBuyNow,
useContract,
Web3Button,
} from "@thirdweb-dev/react";
import { ListingType } from "@thirdweb-dev/sdk";
function App() {
const { contract } = useContract(contractAddress, "marketplace");
const {
mutateAsync: buyNow,
isLoading,
error,
} = useBuyNow(contract);
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
buyNow({
id: "{{listing_id}}", // ID of the listing to buy
type: ListingType.Direct, // Direct (0) or Auction (1)
buyAmount: "{{buy_amount}}", // Amount to buy
buyForWallet: "{{wallet_address}}", // Wallet to buy for, defaults to current wallet
})
}
>
Buy Now
</Web3Button>
);
}

Parameters

Returns

Mutation object to buy out an auction listing

const { mutateAsync, isLoading, error } = useBuyNow(contract);

Options

The mutation function accepts an object as argument with the following properties:

id

The ID of the listing you want to buy.

type

The type of listing. Either ListingType.Direct (0) or ListingType.Auction (1).

buyAmount

The amount of tokens you want to buy from the listing.

Applicable for ERC1155 listings only, should always be 1 for ERC721 listings.

buyForWallet (optional)

Optionally, specify a different wallet address to buy the listing for.