Skip to content

Commit 1c3e58b

Browse files
committed
[SDK] get ERC20Value for claimToBatch
1 parent 2fdb69d commit 1c3e58b

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Address } from "abitype";
2+
import { resolvePromisedValue } from "src/utils/promise/resolve-promised-value.js";
23
import { multicall } from "../../../../extensions/common/__generated__/IMulticall/write/multicall.js";
34
import type {
45
BaseTransactionOptions,
@@ -49,7 +50,10 @@ export function claimToBatch(
4950
return multicall({
5051
contract: options.contract,
5152
asyncParams: () => getClaimToBatchParams(options),
52-
overrides: options.overrides,
53+
overrides: {
54+
erc20Value: () => getERC20Value(options),
55+
...options.content,
56+
},
5357
});
5458
}
5559

@@ -96,6 +100,51 @@ async function getClaimToBatchParams(
96100
return { data };
97101
}
98102

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 totalAmountWei = data
132+
.filter((item) => item !== undefined)
133+
.reduce(
134+
(accumulator, currentValue) => accumulator + currentValue.amountWei,
135+
BigInt(0),
136+
);
137+
138+
if (!data.length || !data[0]) {
139+
return undefined;
140+
}
141+
142+
return {
143+
amountWei: totalAmountWei,
144+
tokenAddress: data[0].tokenAddress,
145+
};
146+
}
147+
99148
/**
100149
* Optimization
101150
* For identical addresses that stays next to each other in the array,

packages/thirdweb/src/transaction/prepare-transaction.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,20 @@ export type StaticPrepareTransactionOptions = {
2525
client: ThirdwebClient;
2626
// extras
2727
extraCallData?: Hex;
28-
erc20Value?: {
29-
amountWei: bigint;
30-
tokenAddress: Address;
31-
};
28+
erc20Value?:
29+
| {
30+
amountWei: bigint;
31+
tokenAddress: Address;
32+
}
33+
| (() => Promise<
34+
Readonly<
35+
| {
36+
amountWei: bigint;
37+
tokenAddress: Address;
38+
}
39+
| undefined
40+
>
41+
>);
3242
};
3343

3444
export type EIP712TransactionOptions = {

0 commit comments

Comments
 (0)