Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
kien-ngo committed Oct 24, 2024
1 parent 2fdb69d commit 550be2c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -49,7 +50,10 @@ export function claimToBatch(
return multicall({
contract: options.contract,
asyncParams: () => getClaimToBatchParams(options),
overrides: options.overrides,
overrides: {
erc20Value: () => getERC20Value(options),
...options.content,
},
});
}

Expand Down Expand Up @@ -96,6 +100,53 @@ async function getClaimToBatchParams(
return { data };
}

/**
* @internal
*/
async function getERC20Value(
options: BaseTransactionOptions<ClaimToBatchParams>,
): 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,
Expand Down
18 changes: 14 additions & 4 deletions packages/thirdweb/src/transaction/prepare-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 550be2c

Please sign in to comment.