import type { AbiParameterToPrimitiveType } from "abitype"; import { prepareContractCall } from "../../../../../transaction/prepare-contract-call.js"; import type { BaseTransactionOptions, WithOverrides, } from "../../../../../transaction/types.js"; import { encodeAbiParameters } from "../../../../../utils/abi/encodeAbiParameters.js"; import { detectMethod } from "../../../../../utils/bytecode/detectExtension.js"; import { once } from "../../../../../utils/promise/once.js"; /** * Represents the parameters for the "onERC1155Received" function. */ export type OnERC1155ReceivedParams = WithOverrides<{ operator: AbiParameterToPrimitiveType<{ type: "address"; name: "operator" }>; from: AbiParameterToPrimitiveType<{ type: "address"; name: "from" }>; id: AbiParameterToPrimitiveType<{ type: "uint256"; name: "id" }>; value: AbiParameterToPrimitiveType<{ type: "uint256"; name: "value" }>; data: AbiParameterToPrimitiveType<{ type: "bytes"; name: "data" }>; }>; export const FN_SELECTOR = "0xf23a6e61" as const; const FN_INPUTS = [ { name: "operator", type: "address", }, { name: "from", type: "address", }, { name: "id", type: "uint256", }, { name: "value", type: "uint256", }, { name: "data", type: "bytes", }, ] as const; const FN_OUTPUTS = [ { type: "bytes4", }, ] as const; /** * Checks if the `onERC1155Received` 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 `onERC1155Received` method is supported. * @extension ERC1155 * @example * ```ts * import { isOnERC1155ReceivedSupported } from "thirdweb/extensions/erc1155"; * * const supported = isOnERC1155ReceivedSupported(["0x..."]); * ``` */ export function isOnERC1155ReceivedSupported(availableSelectors: string[]) { return detectMethod({ availableSelectors, method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const, }); } /** * Encodes the parameters for the "onERC1155Received" function. * @param options - The options for the onERC1155Received function. * @returns The encoded ABI parameters. * @extension ERC1155 * @example * ```ts * import { encodeOnERC1155ReceivedParams } from "thirdweb/extensions/erc1155"; * const result = encodeOnERC1155ReceivedParams({ * operator: ..., * from: ..., * id: ..., * value: ..., * data: ..., * }); * ``` */ export function encodeOnERC1155ReceivedParams( options: OnERC1155ReceivedParams, ) { return encodeAbiParameters(FN_INPUTS, [ options.operator, options.from, options.id, options.value, options.data, ]); } /** * Encodes the "onERC1155Received" function into a Hex string with its parameters. * @param options - The options for the onERC1155Received function. * @returns The encoded hexadecimal string. * @extension ERC1155 * @example * ```ts * import { encodeOnERC1155Received } from "thirdweb/extensions/erc1155"; * const result = encodeOnERC1155Received({ * operator: ..., * from: ..., * id: ..., * value: ..., * data: ..., * }); * ``` */ export function encodeOnERC1155Received(options: OnERC1155ReceivedParams) { // 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 + encodeOnERC1155ReceivedParams(options).slice( 2, )) as `${typeof FN_SELECTOR}${string}`; } /** * Prepares a transaction to call the "onERC1155Received" function on the contract. * @param options - The options for the "onERC1155Received" function. * @returns A prepared transaction object. * @extension ERC1155 * @example * ```ts * import { sendTransaction } from "thirdweb"; * import { onERC1155Received } from "thirdweb/extensions/erc1155"; * * const transaction = onERC1155Received({ * contract, * operator: ..., * from: ..., * id: ..., * value: ..., * data: ..., * overrides: { * ... * } * }); * * // Send the transaction * await sendTransaction({ transaction, account }); * ``` */ export function onERC1155Received( options: BaseTransactionOptions< | OnERC1155ReceivedParams | { asyncParams: () => Promise; } >, ) { const asyncOptions = once(async () => { return "asyncParams" in options ? await options.asyncParams() : options; }); return prepareContractCall({ accessList: async () => (await asyncOptions()).overrides?.accessList, authorizationList: async () => (await asyncOptions()).overrides?.authorizationList, contract: options.contract, erc20Value: async () => (await asyncOptions()).overrides?.erc20Value, extraGas: async () => (await asyncOptions()).overrides?.extraGas, gas: async () => (await asyncOptions()).overrides?.gas, gasPrice: async () => (await asyncOptions()).overrides?.gasPrice, maxFeePerGas: async () => (await asyncOptions()).overrides?.maxFeePerGas, maxPriorityFeePerGas: async () => (await asyncOptions()).overrides?.maxPriorityFeePerGas, method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const, nonce: async () => (await asyncOptions()).overrides?.nonce, params: async () => { const resolvedOptions = await asyncOptions(); return [ resolvedOptions.operator, resolvedOptions.from, resolvedOptions.id, resolvedOptions.value, resolvedOptions.data, ] as const; }, value: async () => (await asyncOptions()).overrides?.value, }); }