|
1 | 1 | import type { Address } from "abitype"; |
| 2 | +import { resolvePromisedValue } from "src/utils/promise/resolve-promised-value.js"; |
2 | 3 | import { multicall } from "../../../../extensions/common/__generated__/IMulticall/write/multicall.js"; |
3 | 4 | import type { |
4 | 5 | BaseTransactionOptions, |
@@ -49,7 +50,10 @@ export function claimToBatch( |
49 | 50 | return multicall({ |
50 | 51 | contract: options.contract, |
51 | 52 | asyncParams: () => getClaimToBatchParams(options), |
52 | | - overrides: options.overrides, |
| 53 | + overrides: { |
| 54 | + erc20Value: () => getERC20Value(options), |
| 55 | + ...options.content, |
| 56 | + }, |
53 | 57 | }); |
54 | 58 | } |
55 | 59 |
|
@@ -96,6 +100,53 @@ async function getClaimToBatchParams( |
96 | 100 | return { data }; |
97 | 101 | } |
98 | 102 |
|
| 103 | +/** |
| 104 | + * @internal |
| 105 | + */ |
| 106 | +async function getERC20Value( |
| 107 | + options: BaseTransactionOptions<ClaimToBatchParams>, |
| 108 | +): Promise< |
| 109 | + | { |
| 110 | + amountWei: bigint; |
| 111 | + tokenAddress: string; |
| 112 | + } |
| 113 | + | undefined |
| 114 | +> { |
| 115 | + const data = await Promise.all( |
| 116 | + options.content.map(async (item) => { |
| 117 | + const claimParams = await getClaimParams({ |
| 118 | + type: "erc721", |
| 119 | + contract: options.contract, |
| 120 | + to: item.to, |
| 121 | + from: options.from, |
| 122 | + quantity: item.quantity, |
| 123 | + }); |
| 124 | + const erc20Value = await resolvePromisedValue( |
| 125 | + claimParams.overrides.erc20Value, |
| 126 | + ); |
| 127 | + return erc20Value; |
| 128 | + }), |
| 129 | + ); |
| 130 | + |
| 131 | + const filteredData = data.filter((item) => item !== undefined); |
| 132 | + |
| 133 | + if (!filteredData.length || !filteredData[0]) { |
| 134 | + return undefined; |
| 135 | + } |
| 136 | + |
| 137 | + const totalAmountWei = filteredData |
| 138 | + .filter((item) => item !== undefined) |
| 139 | + .reduce( |
| 140 | + (accumulator, currentValue) => accumulator + currentValue.amountWei, |
| 141 | + BigInt(0), |
| 142 | + ); |
| 143 | + |
| 144 | + return { |
| 145 | + amountWei: totalAmountWei, |
| 146 | + tokenAddress: filteredData[0].tokenAddress, |
| 147 | + }; |
| 148 | +} |
| 149 | + |
99 | 150 | /** |
100 | 151 | * Optimization |
101 | 152 | * For identical addresses that stays next to each other in the array, |
|
0 commit comments