diff --git a/packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts b/packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts index 9da88f30009..09234ee7ff7 100644 --- a/packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts +++ b/packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts @@ -1,4 +1,5 @@ import type { Address } from "abitype"; +import { resolvePromisedValue } from "src/utils/promise/resolve-promised-value.js"; import { multicall } from "../../../../extensions/common/__generated__/IMulticall/write/multicall.js"; import type { BaseTransactionOptions, @@ -49,7 +50,10 @@ export function claimToBatch( return multicall({ contract: options.contract, asyncParams: () => getClaimToBatchParams(options), - overrides: options.overrides, + overrides: { + erc20Value: () => getERC20Value(options), + ...options.content, + }, }); } @@ -96,6 +100,53 @@ async function getClaimToBatchParams( return { data }; } +/** + * @internal + */ +async function getERC20Value( + options: BaseTransactionOptions, +): Promise< + | { + amountWei: bigint; + tokenAddress: string; + } + | undefined +> { + const data = await Promise.all( + options.content.map(async (item) => { + const claimParams = await getClaimParams({ + type: "erc721", + contract: options.contract, + to: item.to, + from: options.from, + quantity: item.quantity, + }); + const erc20Value = await resolvePromisedValue( + claimParams.overrides.erc20Value, + ); + return erc20Value; + }), + ); + + const filteredData = data.filter((item) => item !== undefined); + + if (!filteredData.length || !filteredData[0]) { + return undefined; + } + + const totalAmountWei = filteredData + .filter((item) => item !== undefined) + .reduce( + (accumulator, currentValue) => accumulator + currentValue.amountWei, + BigInt(0), + ); + + return { + amountWei: totalAmountWei, + tokenAddress: filteredData[0].tokenAddress, + }; +} + /** * Optimization * For identical addresses that stays next to each other in the array, diff --git a/packages/thirdweb/src/transaction/prepare-transaction.ts b/packages/thirdweb/src/transaction/prepare-transaction.ts index d510b6b2163..2b4701a981f 100644 --- a/packages/thirdweb/src/transaction/prepare-transaction.ts +++ b/packages/thirdweb/src/transaction/prepare-transaction.ts @@ -25,10 +25,20 @@ export type StaticPrepareTransactionOptions = { client: ThirdwebClient; // extras extraCallData?: Hex; - erc20Value?: { - amountWei: bigint; - tokenAddress: Address; - }; + erc20Value?: + | { + amountWei: bigint; + tokenAddress: Address; + } + | (() => Promise< + Readonly< + | { + amountWei: bigint; + tokenAddress: Address; + } + | undefined + > + >); }; export type EIP712TransactionOptions = {