From fb0daabbf612be5c844de5f66d3cd49f383b7fe5 Mon Sep 17 00:00:00 2001 From: kien-ngo Date: Sat, 14 Sep 2024 01:26:53 +0000 Subject: [PATCH] [Dashboard] Remove extensionDetectedState (#4557) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## PR-Codex overview This PR focuses on exposing Marketplace extensions for feature detection. It updates extension detection states and refactors extension method imports. ### Detailed summary - Exposed Marketplace extensions for feature detection - Refactored extension method imports to use `ExtensionDetectedState` - Added functions to check support for specific methods in Marketplace extensions > The following files were skipped due to too many changes: `packages/thirdweb/src/extensions/marketplace/direct-listings/read/getListing.ts`, `packages/thirdweb/src/extensions/marketplace/direct-listings/write/buyFromListing.ts`, `packages/thirdweb/src/exports/extensions/marketplace.ts`, `packages/thirdweb/src/extensions/marketplace/english-auctions/read/getWinningBid.ts`, `packages/thirdweb/src/extensions/marketplace/english-auctions/write/bidInAuction.ts`, `apps/dashboard/src/contract-ui/hooks/useRouteConfig.tsx` > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .changeset/sour-dolls-press.md | 5 ++ .../buttons/ExtensionDetectButton.tsx | 65 ------------------- .../buttons/ExtensionDetectedState.ts | 1 + .../src/contract-ui/hooks/useRouteConfig.tsx | 65 +++++++++---------- .../settings/components/detected-state.tsx | 2 +- .../tabs/settings/components/metadata.tsx | 2 +- .../settings/components/platform-fees.tsx | 2 +- .../tabs/settings/components/primary-sale.tsx | 2 +- .../tabs/settings/components/royalties.tsx | 2 +- apps/dashboard/src/contract-ui/types/types.ts | 2 +- .../src/core-ui/sidebar/detail-page.tsx | 2 +- .../src/exports/extensions/marketplace.ts | 10 +++ .../direct-listings/read/getListing.ts | 26 ++++++-- .../direct-listings/write/buyFromListing.ts | 27 ++++++-- .../direct-listings/write/createListing.ts | 20 +++++- .../english-auctions/read/getAuction.ts | 26 ++++++-- .../english-auctions/read/getWinningBid.ts | 29 ++++++--- .../english-auctions/write/bidInAuction.ts | 37 ++++++++--- .../english-auctions/write/cancelAuction.ts | 28 +++++++- .../english-auctions/write/createAuction.ts | 20 +++++- 20 files changed, 225 insertions(+), 148 deletions(-) create mode 100644 .changeset/sour-dolls-press.md delete mode 100644 apps/dashboard/src/components/buttons/ExtensionDetectButton.tsx create mode 100644 apps/dashboard/src/components/buttons/ExtensionDetectedState.ts diff --git a/.changeset/sour-dolls-press.md b/.changeset/sour-dolls-press.md new file mode 100644 index 000000000..133854678 --- /dev/null +++ b/.changeset/sour-dolls-press.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Expose some Marketplace extensions for detecting supported features diff --git a/apps/dashboard/src/components/buttons/ExtensionDetectButton.tsx b/apps/dashboard/src/components/buttons/ExtensionDetectButton.tsx deleted file mode 100644 index 205a5b598..000000000 --- a/apps/dashboard/src/components/buttons/ExtensionDetectButton.tsx +++ /dev/null @@ -1,65 +0,0 @@ -import type { useContract } from "@thirdweb-dev/react"; -import { - type ContractWrapper, - type FeatureName, - detectContractFeature, -} from "@thirdweb-dev/sdk"; - -interface ExtensionDetectedStateParams { - /** - * The feature or features to check - */ - feature: FeatureName | FeatureName[]; - - /** - * The contract instance to check - */ - contractQuery: ReturnType; - - /** - * the feature match strategy (default: any) - * - any: any of the features must be available - * - all: all of the features must be available - */ - matchStrategy?: "any" | "all"; -} - -export type ExtensionDetectedState = "enabled" | "disabled" | "loading"; - -export function extensionDetectedState({ - contractQuery, - feature, - matchStrategy = "any", -}: ExtensionDetectedStateParams): ExtensionDetectedState { - // if contract is loading return "loading" - if ("contract" in contractQuery && contractQuery.isLoading) { - return "loading"; - } - - const actualContract = - "contract" in contractQuery ? contractQuery.contract : contractQuery; - - // we're not loading but don't have a contract, so we'll assumed feature is disabled (really this is an error state?) - if (!actualContract) { - return "disabled"; - } - - // biome-ignore lint/suspicious/noExplicitAny: FIXME - const contractWrapper = // biome-ignore lint/suspicious/noExplicitAny: FIXME - (actualContract as any).contractWrapper as ContractWrapper; - - if (!Array.isArray(feature)) { - return detectContractFeature(contractWrapper, feature) - ? "enabled" - : "disabled"; - } - - if (matchStrategy === "all") { - return feature.every((f) => detectContractFeature(contractWrapper, f)) - ? "enabled" - : "disabled"; - } - return feature.some((f) => detectContractFeature(contractWrapper, f)) - ? "enabled" - : "disabled"; -} diff --git a/apps/dashboard/src/components/buttons/ExtensionDetectedState.ts b/apps/dashboard/src/components/buttons/ExtensionDetectedState.ts new file mode 100644 index 000000000..66a162504 --- /dev/null +++ b/apps/dashboard/src/components/buttons/ExtensionDetectedState.ts @@ -0,0 +1 @@ +export type ExtensionDetectedState = "enabled" | "disabled" | "loading"; diff --git a/apps/dashboard/src/contract-ui/hooks/useRouteConfig.tsx b/apps/dashboard/src/contract-ui/hooks/useRouteConfig.tsx index 66771debd..f277a3530 100644 --- a/apps/dashboard/src/contract-ui/hooks/useRouteConfig.tsx +++ b/apps/dashboard/src/contract-ui/hooks/useRouteConfig.tsx @@ -1,6 +1,3 @@ -import { useContract } from "@thirdweb-dev/react"; -import { extensionDetectedState } from "components/buttons/ExtensionDetectButton"; -import { useEns } from "components/contract-components/hooks"; import { ContractOverviewPage } from "contract-ui/tabs/overview/page"; import type { EnhancedRoute } from "contract-ui/types/types"; import dynamic from "next/dynamic"; @@ -11,6 +8,7 @@ import * as ERC20Ext from "thirdweb/extensions/erc20"; import * as ERC721Ext from "thirdweb/extensions/erc721"; import * as ERC1155Ext from "thirdweb/extensions/erc1155"; import * as ERC4337Ext from "thirdweb/extensions/erc4337"; +import * as MarketplaceExt from "thirdweb/extensions/marketplace"; import * as PermissionExt from "thirdweb/extensions/permissions"; import * as ThirdwebExt from "thirdweb/extensions/thirdweb"; import * as ModularExt from "thirdweb/modules"; @@ -229,35 +227,25 @@ export function useContractRouteConfig( ].every(Boolean); }, [functionSelectorQuery.data]); - // old - const ensQuery = useEns(contract.address); + const isDirectListing = useMemo(() => { + return [ + MarketplaceExt.isCreateListingSupported(functionSelectorQuery.data), + MarketplaceExt.isCancelListingSupported(functionSelectorQuery.data), + MarketplaceExt.isBuyFromListingSupported(functionSelectorQuery.data), + ].every(Boolean); + }, [functionSelectorQuery.data]); - // TODO: remove - const contractQuery = useContract(ensQuery.data?.address); - // TODO: remove all below - const contractData = useMemo(() => { - const detectedEnglishAuctions = extensionDetectedState({ - contractQuery, - feature: "EnglishAuctions", - }); - - const detectedDirectListings = extensionDetectedState({ - contractQuery, - feature: "DirectListings", - }); - - return { - detectedEnglishAuctions, - detectedDirectListings, - }; - }, [contractQuery]); + const isEnglishAuction = useMemo(() => { + return [ + MarketplaceExt.isCreateAuctionSupported(functionSelectorQuery.data), + MarketplaceExt.isCancelAuctionSupported(functionSelectorQuery.data), + MarketplaceExt.isBidInAuctionSupported(functionSelectorQuery.data), + ].every(Boolean); + }, [functionSelectorQuery.data]); const embedType: "marketplace-v3" | "erc20" | "erc1155" | "erc721" | null = useMemo(() => { - if ( - contractData.detectedEnglishAuctions === "enabled" && - contractData.detectedDirectListings === "enabled" - ) { + if (isEnglishAuction && isDirectListing) { // this means its marketplace v3 return "marketplace-v3"; } @@ -277,10 +265,11 @@ export function useContractRouteConfig( // otherwise null return null; }, [ - contractData, hasClaimConditions, isERC721Query.data, isERC1155Query.data, + isDirectListing, + isEnglishAuction, ]); return [ @@ -291,10 +280,8 @@ export function useContractRouteConfig( component: () => ( , }, { title: "English Auctions", path: "english-auctions", - isEnabled: contractData.detectedEnglishAuctions, + isEnabled: functionSelectorQuery.isLoading + ? "loading" + : isEnglishAuction + ? "enabled" + : "disabled", component: () => , }, { diff --git a/apps/dashboard/src/contract-ui/tabs/settings/components/detected-state.tsx b/apps/dashboard/src/contract-ui/tabs/settings/components/detected-state.tsx index c4d63d727..76ac6d5b8 100644 --- a/apps/dashboard/src/contract-ui/tabs/settings/components/detected-state.tsx +++ b/apps/dashboard/src/contract-ui/tabs/settings/components/detected-state.tsx @@ -1,5 +1,5 @@ import { Flex, Icon, SimpleGrid, Spinner } from "@chakra-ui/react"; -import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectButton"; +import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectedState"; import { FiExternalLink } from "react-icons/fi"; import { VscExtensions } from "react-icons/vsc"; import { Heading, Text, TrackedLink } from "tw-components"; diff --git a/apps/dashboard/src/contract-ui/tabs/settings/components/metadata.tsx b/apps/dashboard/src/contract-ui/tabs/settings/components/metadata.tsx index 76aeed76b..e000524b6 100644 --- a/apps/dashboard/src/contract-ui/tabs/settings/components/metadata.tsx +++ b/apps/dashboard/src/contract-ui/tabs/settings/components/metadata.tsx @@ -9,7 +9,7 @@ import { Textarea, } from "@chakra-ui/react"; import { zodResolver } from "@hookform/resolvers/zod"; -import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectButton"; +import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectedState"; import { TransactionButton } from "components/buttons/TransactionButton"; import { FileInput } from "components/shared/FileInput"; import { CommonContractSchema } from "constants/schemas"; diff --git a/apps/dashboard/src/contract-ui/tabs/settings/components/platform-fees.tsx b/apps/dashboard/src/contract-ui/tabs/settings/components/platform-fees.tsx index 7045a55ac..8bf3c76e2 100644 --- a/apps/dashboard/src/contract-ui/tabs/settings/components/platform-fees.tsx +++ b/apps/dashboard/src/contract-ui/tabs/settings/components/platform-fees.tsx @@ -1,7 +1,7 @@ import { AdminOnly } from "@3rdweb-sdk/react/components/roles/admin-only"; import { Flex, FormControl } from "@chakra-ui/react"; import { zodResolver } from "@hookform/resolvers/zod"; -import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectButton"; +import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectedState"; import { TransactionButton } from "components/buttons/TransactionButton"; import { BasisPointsInput } from "components/inputs/BasisPointsInput"; import { AddressOrEnsSchema, BasisPointsSchema } from "constants/schemas"; diff --git a/apps/dashboard/src/contract-ui/tabs/settings/components/primary-sale.tsx b/apps/dashboard/src/contract-ui/tabs/settings/components/primary-sale.tsx index 5813be24b..d833e752f 100644 --- a/apps/dashboard/src/contract-ui/tabs/settings/components/primary-sale.tsx +++ b/apps/dashboard/src/contract-ui/tabs/settings/components/primary-sale.tsx @@ -1,7 +1,7 @@ import { AdminOnly } from "@3rdweb-sdk/react/components/roles/admin-only"; import { Flex, FormControl } from "@chakra-ui/react"; import { zodResolver } from "@hookform/resolvers/zod"; -import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectButton"; +import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectedState"; import { TransactionButton } from "components/buttons/TransactionButton"; import { AddressOrEnsSchema } from "constants/schemas"; import { SolidityInput } from "contract-ui/components/solidity-inputs"; diff --git a/apps/dashboard/src/contract-ui/tabs/settings/components/royalties.tsx b/apps/dashboard/src/contract-ui/tabs/settings/components/royalties.tsx index b6e5baf93..cd316856f 100644 --- a/apps/dashboard/src/contract-ui/tabs/settings/components/royalties.tsx +++ b/apps/dashboard/src/contract-ui/tabs/settings/components/royalties.tsx @@ -1,7 +1,7 @@ import { AdminOnly } from "@3rdweb-sdk/react/components/roles/admin-only"; import { Flex, FormControl } from "@chakra-ui/react"; import { zodResolver } from "@hookform/resolvers/zod"; -import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectButton"; +import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectedState"; import { TransactionButton } from "components/buttons/TransactionButton"; import { BasisPointsInput } from "components/inputs/BasisPointsInput"; import { AddressOrEnsSchema, BasisPointsSchema } from "constants/schemas"; diff --git a/apps/dashboard/src/contract-ui/types/types.ts b/apps/dashboard/src/contract-ui/types/types.ts index 2b5b3ced6..00d87a8c5 100644 --- a/apps/dashboard/src/contract-ui/types/types.ts +++ b/apps/dashboard/src/contract-ui/types/types.ts @@ -1,4 +1,4 @@ -import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectButton"; +import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectedState"; import type { ComponentType } from "react"; // biome-ignore lint/suspicious/noExplicitAny: FIXME diff --git a/apps/dashboard/src/core-ui/sidebar/detail-page.tsx b/apps/dashboard/src/core-ui/sidebar/detail-page.tsx index 7fe314986..42cfb6caf 100644 --- a/apps/dashboard/src/core-ui/sidebar/detail-page.tsx +++ b/apps/dashboard/src/core-ui/sidebar/detail-page.tsx @@ -1,5 +1,5 @@ import { Box, Flex, Image, Skeleton, useDisclosure } from "@chakra-ui/react"; -import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectButton"; +import type { ExtensionDetectedState } from "components/buttons/ExtensionDetectedState"; import type { EnhancedRoute } from "contract-ui/types/types"; import { useRouter } from "next/router"; import { useMemo } from "react"; diff --git a/packages/thirdweb/src/exports/extensions/marketplace.ts b/packages/thirdweb/src/exports/extensions/marketplace.ts index 407c374fd..0fc9fb308 100644 --- a/packages/thirdweb/src/exports/extensions/marketplace.ts +++ b/packages/thirdweb/src/exports/extensions/marketplace.ts @@ -9,6 +9,7 @@ export { totalListings } from "../../extensions/marketplace/__generated__/IDirec export { getListing, type GetListingParams, + isGetListingSupported, } from "../../extensions/marketplace/direct-listings/read/getListing.js"; export { getAllListings, @@ -31,10 +32,13 @@ export { type CurrencyPriceForListingParams, } from "../../extensions/marketplace/direct-listings/read/currencyPriceForListing.js"; +export { isCancelListingSupported } from "../../extensions/marketplace/__generated__/IDirectListings/write/cancelListing.js"; + // WRITE export { createListing, type CreateListingParams, + isCreateListingSupported, } from "../../extensions/marketplace/direct-listings/write/createListing.js"; export { updateListing, @@ -47,6 +51,7 @@ export { export { buyFromListing, type BuyFromListingParams, + isBuyFromListingSupported, } from "../../extensions/marketplace/direct-listings/write/buyFromListing.js"; export { approveBuyerForListing, @@ -94,6 +99,7 @@ export { totalAuctions } from "../../extensions/marketplace/__generated__/IEngli export { getAuction, type GetAuctionParams, + isGetAuctionSupported, } from "../../extensions/marketplace/english-auctions/read/getAuction.js"; export { getAllAuctions, @@ -106,6 +112,7 @@ export { export { getWinningBid, type GetWinningBidParams, + isGetWinningBidSupported, } from "../../extensions/marketplace/english-auctions/read/getWinningBid.js"; export { isNewWinningBid, @@ -116,14 +123,17 @@ export { export { createAuction, type CreateAuctionParams, + isCreateAuctionSupported, } from "../../extensions/marketplace/english-auctions/write/createAuction.js"; export { bidInAuction, type BidInAuctionParams, + isBidInAuctionSupported, } from "../../extensions/marketplace/english-auctions/write/bidInAuction.js"; export { cancelAuction, type CancelAuctionParams, + isCancelAuctionSupported, } from "../../extensions/marketplace/__generated__/IEnglishAuctions/write/cancelAuction.js"; export { buyoutAuction, diff --git a/packages/thirdweb/src/extensions/marketplace/direct-listings/read/getListing.ts b/packages/thirdweb/src/extensions/marketplace/direct-listings/read/getListing.ts index 8f69fe268..7119c8643 100644 --- a/packages/thirdweb/src/extensions/marketplace/direct-listings/read/getListing.ts +++ b/packages/thirdweb/src/extensions/marketplace/direct-listings/read/getListing.ts @@ -1,18 +1,14 @@ import { eth_getBlockByNumber } from "../../../../rpc/actions/eth_getBlockByNumber.js"; import { getRpcClient } from "../../../../rpc/rpc.js"; import type { BaseTransactionOptions } from "../../../../transaction/types.js"; - -import { - type GetListingParams as GetListingParamsGenerated, - getListing as getListingGenerated, -} from "../../__generated__/IDirectListings/read/getListing.js"; +import * as GetListing from "../../__generated__/IDirectListings/read/getListing.js"; import type { DirectListing } from "../types.js"; import { mapDirectListing } from "../utils.js"; /** * @extension MARKETPLACE */ -export type GetListingParams = GetListingParamsGenerated; +export type GetListingParams = GetListing.GetListingParams; /** * Retrieves a direct listing based on the provided options. @@ -32,7 +28,7 @@ export async function getListing( ): Promise { const rpcClient = getRpcClient(options.contract); const [rawListing, latestBlock] = await Promise.all([ - getListingGenerated(options), + GetListing.getListing(options), eth_getBlockByNumber(rpcClient, { blockTag: "latest", }), @@ -44,3 +40,19 @@ export async function getListing( rawListing, }); } + +/** + * Checks if the `isGetListingSupported` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `isGetListingSupported` method is supported. + * @extension MARKETPLACE + * @example + * ```ts + * import { isGetListingSupported } from "thirdweb/extensions/marketplace"; + * + * const supported = isGetListingSupported(["0x..."]); + * ``` + */ +export function isGetListingSupported(availableSelectors: string[]) { + return GetListing.isGetListingSupported(availableSelectors); +} diff --git a/packages/thirdweb/src/extensions/marketplace/direct-listings/write/buyFromListing.ts b/packages/thirdweb/src/extensions/marketplace/direct-listings/write/buyFromListing.ts index eab9b2291..b285c4d12 100644 --- a/packages/thirdweb/src/extensions/marketplace/direct-listings/write/buyFromListing.ts +++ b/packages/thirdweb/src/extensions/marketplace/direct-listings/write/buyFromListing.ts @@ -1,8 +1,8 @@ import type { Address } from "abitype"; import { isNativeTokenAddress } from "../../../../constants/addresses.js"; import type { BaseTransactionOptions } from "../../../../transaction/types.js"; -import { buyFromListing as generatedBuyFromListing } from "../../__generated__/IDirectListings/write/buyFromListing.js"; -import { getListing } from "../read/getListing.js"; +import * as BuyFromListing from "../../__generated__/IDirectListings/write/buyFromListing.js"; +import * as GetListing from "../read/getListing.js"; import { isListingValid } from "../utils.js"; /** @@ -38,10 +38,10 @@ export type BuyFromListingParams = { export function buyFromListing( options: BaseTransactionOptions, ) { - return generatedBuyFromListing({ + return BuyFromListing.buyFromListing({ contract: options.contract, asyncParams: async () => { - const listing = await getListing({ + const listing = await GetListing.getListing({ contract: options.contract, listingId: options.listingId, }); @@ -71,3 +71,22 @@ export function buyFromListing( }, }); } + +/** + * Checks if the `buyFromListing` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `buyFromListing` method is supported. + * @extension MARKETPLACE + * @example + * ```ts + * import { isBuyFromListingSupported } from "thirdweb/extensions/marketplace"; + * + * const supported = isBuyFromListingSupported(["0x..."]); + * ``` + */ +export function isBuyFromListingSupported(availableSelectors: string[]) { + return ( + BuyFromListing.isBuyFromListingSupported(availableSelectors) && + GetListing.isGetListingSupported(availableSelectors) + ); +} diff --git a/packages/thirdweb/src/extensions/marketplace/direct-listings/write/createListing.ts b/packages/thirdweb/src/extensions/marketplace/direct-listings/write/createListing.ts index e5db4ad82..d4c898bc1 100644 --- a/packages/thirdweb/src/extensions/marketplace/direct-listings/write/createListing.ts +++ b/packages/thirdweb/src/extensions/marketplace/direct-listings/write/createListing.ts @@ -10,7 +10,7 @@ import type { BaseTransactionOptions } from "../../../../transaction/types.js"; import { toUnits } from "../../../../utils/units.js"; import { isERC721 } from "../../../erc721/read/isERC721.js"; import { isERC1155 } from "../../../erc1155/read/isERC1155.js"; -import { createListing as generatedCreateListing } from "../../__generated__/IDirectListings/write/createListing.js"; +import * as CreateListing from "../../__generated__/IDirectListings/write/createListing.js"; /** * @extension MARKETPLACE @@ -88,7 +88,7 @@ export type CreateListingParams = { export function createListing( options: BaseTransactionOptions, ) { - return generatedCreateListing({ + return CreateListing.createListing({ contract: options.contract, asyncParams: async () => { const assetContract = getContract({ @@ -182,3 +182,19 @@ export function createListing( }, }); } + +/** + * Checks if the `createListing` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `createListing` method is supported. + * @extension MARKETPLACE + * @example + * ```ts + * import { isCreateAuctionSupported } from "thirdweb/extensions/marketplace"; + * + * const supported = isCreateAuctionSupported(["0x..."]); + * ``` + */ +export function isCreateListingSupported(availableSelectors: string[]) { + return CreateListing.isCreateListingSupported(availableSelectors); +} diff --git a/packages/thirdweb/src/extensions/marketplace/english-auctions/read/getAuction.ts b/packages/thirdweb/src/extensions/marketplace/english-auctions/read/getAuction.ts index bc0a94ec6..2e71f17a0 100644 --- a/packages/thirdweb/src/extensions/marketplace/english-auctions/read/getAuction.ts +++ b/packages/thirdweb/src/extensions/marketplace/english-auctions/read/getAuction.ts @@ -1,18 +1,14 @@ import { eth_getBlockByNumber } from "../../../../rpc/actions/eth_getBlockByNumber.js"; import { getRpcClient } from "../../../../rpc/rpc.js"; import type { BaseTransactionOptions } from "../../../../transaction/types.js"; - -import { - type GetAuctionParams as GetAuctionParamsGenerated, - getAuction as getAuctionGenerated, -} from "../../__generated__/IEnglishAuctions/read/getAuction.js"; +import * as GetAuction from "../../__generated__/IEnglishAuctions/read/getAuction.js"; import type { EnglishAuction } from "../types.js"; import { mapEnglishAuction } from "../utils.js"; /** * @extension MARKETPLACE */ -export type GetAuctionParams = GetAuctionParamsGenerated; +export type GetAuctionParams = GetAuction.GetAuctionParams; /** * Retrieves an auction listing based on the provided options. @@ -32,7 +28,7 @@ export async function getAuction( ): Promise { const rpcClient = getRpcClient(options.contract); const [rawAuction, latestBlock] = await Promise.all([ - getAuctionGenerated(options), + GetAuction.getAuction(options), eth_getBlockByNumber(rpcClient, { blockTag: "latest", }), @@ -44,3 +40,19 @@ export async function getAuction( rawAuction, }); } + +/** + * Checks if the `getAuction` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `getAuction` method is supported. + * @extension MARKETPLACE + * @example + * ```ts + * import { isGetAuctionSupported } from "thirdweb/extensions/marketplace"; + * + * const supported = isGetAuctionSupported(["0x..."]); + * ``` + */ +export function isGetAuctionSupported(availableSelectors: string[]) { + return GetAuction.isGetAuctionSupported(availableSelectors); +} diff --git a/packages/thirdweb/src/extensions/marketplace/english-auctions/read/getWinningBid.ts b/packages/thirdweb/src/extensions/marketplace/english-auctions/read/getWinningBid.ts index 811ee9312..7b63ef438 100644 --- a/packages/thirdweb/src/extensions/marketplace/english-auctions/read/getWinningBid.ts +++ b/packages/thirdweb/src/extensions/marketplace/english-auctions/read/getWinningBid.ts @@ -1,15 +1,12 @@ -import { ADDRESS_ZERO } from "../../../../constants/addresses.js"; +import { ZERO_ADDRESS } from "../../../../constants/addresses.js"; import type { BaseTransactionOptions } from "../../../../transaction/types.js"; import { resolveCurrencyValue } from "../../../../utils/extensions/resolve-currency-value.js"; -import { - type GetWinningBidParams as GeneratedWinningBidParams, - getWinningBid as getWinningBidGenerated, -} from "../../__generated__/IEnglishAuctions/read/getWinningBid.js"; +import * as GetWinningBid from "../../__generated__/IEnglishAuctions/read/getWinningBid.js"; /** * @extension MARKETPLACE */ -export type GetWinningBidParams = GeneratedWinningBidParams; +export type GetWinningBidParams = GetWinningBid.GetWinningBidParams; /** * Retrieves the winning bid information for a given auction. @@ -31,9 +28,9 @@ export async function getWinningBid( options: BaseTransactionOptions, ) { const [bidderAddress, currencyAddress, bidAmountWei] = - await getWinningBidGenerated(options); + await GetWinningBid.getWinningBid(options); - if (bidderAddress === ADDRESS_ZERO) { + if (bidderAddress === ZERO_ADDRESS) { return undefined; } @@ -49,3 +46,19 @@ export async function getWinningBid( }), }; } + +/** + * Checks if the `getWinningBid` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `getWinningBid` method is supported. + * @extension MARKETPLACE + * @example + * ```ts + * import { isGetWinningBidSupported } from "thirdweb/extensions/marketplace"; + * + * const supported = isGetWinningBidSupported(["0x..."]); + * ``` + */ +export function isGetWinningBidSupported(availableSelectors: string[]) { + return GetWinningBid.isGetWinningBidSupported(availableSelectors); +} diff --git a/packages/thirdweb/src/extensions/marketplace/english-auctions/write/bidInAuction.ts b/packages/thirdweb/src/extensions/marketplace/english-auctions/write/bidInAuction.ts index 87ed31d26..c3f8b93d2 100644 --- a/packages/thirdweb/src/extensions/marketplace/english-auctions/write/bidInAuction.ts +++ b/packages/thirdweb/src/extensions/marketplace/english-auctions/write/bidInAuction.ts @@ -1,9 +1,9 @@ import { isNativeTokenAddress } from "../../../../constants/addresses.js"; import type { BaseTransactionOptions } from "../../../../transaction/types.js"; -import { isNewWinningBid } from "../../__generated__/IEnglishAuctions/read/isNewWinningBid.js"; -import { bidInAuction as generatedBidInAuction } from "../../__generated__/IEnglishAuctions/write/bidInAuction.js"; -import { getAuction } from "../read/getAuction.js"; -import { getWinningBid } from "../read/getWinningBid.js"; +import * as IsNewWinningBid from "../../__generated__/IEnglishAuctions/read/isNewWinningBid.js"; +import * as BidInAuction from "../../__generated__/IEnglishAuctions/write/bidInAuction.js"; +import * as GetAuction from "../read/getAuction.js"; +import * as GetWinningBid from "../read/getWinningBid.js"; /** * @extension MARKETPLACE @@ -39,10 +39,10 @@ export type BidInAuctionParams = { export function bidInAuction( options: BaseTransactionOptions, ) { - return generatedBidInAuction({ + return BidInAuction.bidInAuction({ contract: options.contract, asyncParams: async () => { - const auction = await getAuction({ + const auction = await GetAuction.getAuction({ contract: options.contract, auctionId: options.auctionId, }); @@ -70,13 +70,13 @@ export function bidInAuction( if (resolvedBidAmountWei > auction.buyoutCurrencyValue.value) { throw new Error("Bid amount is above the buyout amount"); } - const existingWinningBid = await getWinningBid({ + const existingWinningBid = await GetWinningBid.getWinningBid({ auctionId: options.auctionId, contract: options.contract, }); if (existingWinningBid) { // check if the bid amount is sufficient to outbid the existing winning bid - const isNewWinner = await isNewWinningBid({ + const isNewWinner = await IsNewWinningBid.isNewWinningBid({ contract: options.contract, auctionId: options.auctionId, bidAmount: resolvedBidAmountWei, @@ -106,3 +106,24 @@ export function bidInAuction( }, }); } + +/** + * Checks if the `bidInAuction` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `bidInAuction` method is supported. + * @extension MARKETPLACE + * @example + * ```ts + * import { isBidInAuctionSupported } from "thirdweb/extensions/marketplace"; + * + * const supported = isBidInAuctionSupported(["0x..."]); + * ``` + */ +export function isBidInAuctionSupported(availableSelectors: string[]) { + return ( + BidInAuction.isBidInAuctionSupported(availableSelectors) && + GetWinningBid.isGetWinningBidSupported(availableSelectors) && + GetAuction.isGetAuctionSupported(availableSelectors) && + IsNewWinningBid.isIsNewWinningBidSupported(availableSelectors) + ); +} diff --git a/packages/thirdweb/src/extensions/marketplace/english-auctions/write/cancelAuction.ts b/packages/thirdweb/src/extensions/marketplace/english-auctions/write/cancelAuction.ts index 926b2294d..931200f9a 100644 --- a/packages/thirdweb/src/extensions/marketplace/english-auctions/write/cancelAuction.ts +++ b/packages/thirdweb/src/extensions/marketplace/english-auctions/write/cancelAuction.ts @@ -1,6 +1,9 @@ import type { BaseTransactionOptions } from "../../../../transaction/types.js"; -import { cancelAuction as generatedCancelAuction } from "../../__generated__/IEnglishAuctions/write/cancelAuction.js"; -import { getWinningBid } from "../read/getWinningBid.js"; +import * as CancelAuction from "../../__generated__/IEnglishAuctions/write/cancelAuction.js"; +import { + getWinningBid, + isGetWinningBidSupported, +} from "../read/getWinningBid.js"; /** * @extension MARKETPLACE @@ -32,7 +35,7 @@ export type CancelAuctionParams = { export function cancelAuction( options: BaseTransactionOptions, ) { - return generatedCancelAuction({ + return CancelAuction.cancelAuction({ contract: options.contract, asyncParams: async () => { const winningBid = await getWinningBid({ @@ -51,3 +54,22 @@ export function cancelAuction( }, }); } + +/** + * Checks if the `cancelAuction` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `cancelAuction` method is supported. + * @extension MARKETPLACE + * @example + * ```ts + * import { isCancelAuctionSupported } from "thirdweb/extensions/marketplace"; + * + * const supported = isCancelAuctionSupported(["0x..."]); + * ``` + */ +export function isCancelAuctionSupported(availableSelectors: string[]) { + return ( + CancelAuction.isCancelAuctionSupported(availableSelectors) && + isGetWinningBidSupported(availableSelectors) + ); +} diff --git a/packages/thirdweb/src/extensions/marketplace/english-auctions/write/createAuction.ts b/packages/thirdweb/src/extensions/marketplace/english-auctions/write/createAuction.ts index c81f86b20..34db7f541 100644 --- a/packages/thirdweb/src/extensions/marketplace/english-auctions/write/createAuction.ts +++ b/packages/thirdweb/src/extensions/marketplace/english-auctions/write/createAuction.ts @@ -10,7 +10,7 @@ import type { BaseTransactionOptions } from "../../../../transaction/types.js"; import { toUnits } from "../../../../utils/units.js"; import { isERC721 } from "../../../erc721/read/isERC721.js"; import { isERC1155 } from "../../../erc1155/read/isERC1155.js"; -import { createAuction as generatedCreateAuction } from "../../__generated__/IEnglishAuctions/write/createAuction.js"; +import * as CreateAuction from "../../__generated__/IEnglishAuctions/write/createAuction.js"; /** * @extension MARKETPLACE @@ -119,7 +119,7 @@ export type CreateAuctionParams = { export function createAuction( options: BaseTransactionOptions, ) { - return generatedCreateAuction({ + return CreateAuction.createAuction({ ...options, asyncParams: async () => { const assetContract = getContract({ @@ -245,3 +245,19 @@ export function createAuction( }, }); } + +/** + * Checks if the `createAuction` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `createAuction` method is supported. + * @extension MARKETPLACE + * @example + * ```ts + * import { isCreateAuctionSupported } from "thirdweb/extensions/marketplace"; + * + * const supported = isCreateAuctionSupported(["0x..."]); + * ``` + */ +export function isCreateAuctionSupported(availableSelectors: string[]) { + return CreateAuction.isCreateAuctionSupported(availableSelectors); +}