import type { AbiParameterToPrimitiveType } from "abitype"; import { decodeAbiParameters } from "viem"; import { readContract } from "../../../../../transaction/read-contract.js"; import type { BaseTransactionOptions } from "../../../../../transaction/types.js"; import { encodeAbiParameters } from "../../../../../utils/abi/encodeAbiParameters.js"; import { detectMethod } from "../../../../../utils/bytecode/detectExtension.js"; import type { Hex } from "../../../../../utils/encoding/hex.js"; /** * Represents the parameters for the "getAllValidOffers" function. */ export type GetAllValidOffersParams = { startId: AbiParameterToPrimitiveType<{ type: "uint256"; name: "_startId" }>; endId: AbiParameterToPrimitiveType<{ type: "uint256"; name: "_endId" }>; }; export const FN_SELECTOR = "0x91940b3e" as const; const FN_INPUTS = [ { name: "_startId", type: "uint256", }, { name: "_endId", type: "uint256", }, ] as const; const FN_OUTPUTS = [ { components: [ { name: "offerId", type: "uint256", }, { name: "tokenId", type: "uint256", }, { name: "quantity", type: "uint256", }, { name: "totalPrice", type: "uint256", }, { name: "expirationTimestamp", type: "uint256", }, { name: "offeror", type: "address", }, { name: "assetContract", type: "address", }, { name: "currency", type: "address", }, { name: "tokenType", type: "uint8", }, { name: "status", type: "uint8", }, ], name: "offers", type: "tuple[]", }, ] as const; /** * Checks if the `getAllValidOffers` 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 `getAllValidOffers` method is supported. * @extension MARKETPLACE * @example * ```ts * import { isGetAllValidOffersSupported } from "thirdweb/extensions/marketplace"; * const supported = isGetAllValidOffersSupported(["0x..."]); * ``` */ export function isGetAllValidOffersSupported(availableSelectors: string[]) { return detectMethod({ availableSelectors, method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const, }); } /** * Encodes the parameters for the "getAllValidOffers" function. * @param options - The options for the getAllValidOffers function. * @returns The encoded ABI parameters. * @extension MARKETPLACE * @example * ```ts * import { encodeGetAllValidOffersParams } from "thirdweb/extensions/marketplace"; * const result = encodeGetAllValidOffersParams({ * startId: ..., * endId: ..., * }); * ``` */ export function encodeGetAllValidOffersParams( options: GetAllValidOffersParams, ) { return encodeAbiParameters(FN_INPUTS, [options.startId, options.endId]); } /** * Encodes the "getAllValidOffers" function into a Hex string with its parameters. * @param options - The options for the getAllValidOffers function. * @returns The encoded hexadecimal string. * @extension MARKETPLACE * @example * ```ts * import { encodeGetAllValidOffers } from "thirdweb/extensions/marketplace"; * const result = encodeGetAllValidOffers({ * startId: ..., * endId: ..., * }); * ``` */ export function encodeGetAllValidOffers(options: GetAllValidOffersParams) { // we do a "manual" concat here to avoid the overhead of the "concatHex" function // we can do this because we know the specific formats of the values return (FN_SELECTOR + encodeGetAllValidOffersParams(options).slice( 2, )) as `${typeof FN_SELECTOR}${string}`; } /** * Decodes the result of the getAllValidOffers function call. * @param result - The hexadecimal result to decode. * @returns The decoded result as per the FN_OUTPUTS definition. * @extension MARKETPLACE * @example * ```ts * import { decodeGetAllValidOffersResult } from "thirdweb/extensions/marketplace"; * const result = decodeGetAllValidOffersResultResult("..."); * ``` */ export function decodeGetAllValidOffersResult(result: Hex) { return decodeAbiParameters(FN_OUTPUTS, result)[0]; } /** * Calls the "getAllValidOffers" function on the contract. * @param options - The options for the getAllValidOffers function. * @returns The parsed result of the function call. * @extension MARKETPLACE * @example * ```ts * import { getAllValidOffers } from "thirdweb/extensions/marketplace"; * * const result = await getAllValidOffers({ * contract, * startId: ..., * endId: ..., * }); * * ``` */ export async function getAllValidOffers( options: BaseTransactionOptions, ) { return readContract({ contract: options.contract, method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const, params: [options.startId, options.endId], }); }