From 7e937687d8162ab25bd7a016a69806ea4bd2dda2 Mon Sep 17 00:00:00 2001 From: Kien Ngo Date: Thu, 24 Oct 2024 17:53:53 +0700 Subject: [PATCH] Update --- .../erc721/drops/write/claimToBatch.ts | 53 ++++++++++++++++++- .../src/transaction/prepare-transaction.ts | 18 +++++-- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts b/packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts index 9da88f30009..593adbf4ece 100644 --- a/packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts +++ b/packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts @@ -5,6 +5,7 @@ import type { WithOverrides, } from "../../../../transaction/types.js"; import { getClaimParams } from "../../../../utils/extensions/drops/get-claim-params.js"; +import { resolvePromisedValue } from "../../../../utils/promise/resolve-promised-value.js"; import { encodeClaim } from "../../__generated__/IDrop/write/claim.js"; /** @@ -49,7 +50,10 @@ export function claimToBatch( return multicall({ contract: options.contract, asyncParams: () => getClaimToBatchParams(options), - overrides: options.overrides, + overrides: { + erc20Value: getERC20Value(options), + ...options.overrides, + }, }); } @@ -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..2165d21c217 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 = {