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 7e93768
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
Expand Up @@ -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";

/**
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.overrides,
},
});
}

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),
);

Check warning on line 142 in packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts#L137-L142

Added lines #L137 - L142 were not covered by tests

return {
amountWei: totalAmountWei,
tokenAddress: filteredData[0].tokenAddress,
};
}

Check warning on line 148 in packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/erc721/drops/write/claimToBatch.ts#L144-L148

Added lines #L144 - L148 were not covered by tests

/**
* 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 7e93768

Please sign in to comment.