useBuyDirectListing
Hook for buying a direct listing on a Marketplace V3 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.
import { useBuyDirectListing } from "@thirdweb-dev/react";
const { mutateAsync, isLoading, error } = useBuyDirectListing(contract);
Usage
Provide your marketplace contract as the argument.
import {
useBuyDirectListing,
useContract,
Web3Button,
} from "@thirdweb-dev/react";
import { ListingType } from "@thirdweb-dev/sdk";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress, "marketplace-v3");
const {
mutateAsync: buyDirectListing,
isLoading,
error,
} = useBuyDirectListing(contract);
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
buyDirectListing({
listingId: "{{listing_id}}", // ID of the listing to buy
quantity: "1",
buyer: "{{wallet_address}}", // Wallet to buy for
})
}
>
Buy Now
</Web3Button>
);
}
Configuration
listingId
listingId (required)
The ID of the direct listing you want to buy.
import {
useBuyDirectListing,
useContract,
Web3Button,
} from "@thirdweb-dev/react";
import { ListingType } from "@thirdweb-dev/sdk";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress, "marketplace-v3");
const {
mutateAsync: buyDirectListing,
isLoading,
error,
} = useBuyDirectListing(contract);
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
buyDirectListing({
listingId: "{{listing_id}}", // ID of the listing to buy
quantity: "1",
buyer: "{{wallet_address}}", // Wallet to buy for
})
}
>
Buy Now
</Web3Button>
);
}
quantity
quantity (required)
The amount of tokens you want to buy from the listing.
Applicable for ERC1155 listings only, should always be 1
for ERC721 listings.
import {
useBuyDirectListing,
useContract,
Web3Button,
} from "@thirdweb-dev/react";
import { ListingType } from "@thirdweb-dev/sdk";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress, "marketplace-v3");
const {
mutateAsync: buyDirectListing,
isLoading,
error,
} = useBuyDirectListing(contract);
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
buyDirectListing({
listingId: "{{listing_id}}",
quantity: "{{buy_amount}}", // Amount to buy
buyer: "{{wallet_address}}", // Wallet to buy for
})
}
>
Buy Now
</Web3Button>
);
}
buyer
buyer (required)
Specify a different wallet address to buy the listing for.
import {
useBuyDirectListing,
useContract,
Web3Button,
} from "@thirdweb-dev/react";
import { ListingType } from "@thirdweb-dev/sdk";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress, "marketplace-3");
const {
mutateAsync: buyDirectListing,
isLoading,
error,
} = useBuyDirectListing(contract);
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
buyDirectListing({
listingId: "{{listing_id}}",
quantity: "{{buy_amount}}", // Amount to buy
buyer: "{{wallet_address}}", // Wallet to buy for
})
}
>
Buy Now
</Web3Button>
);
}