Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] get ERC20Value for claimToBatch #5157

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@
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 @@
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<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure we're handling this being a promise where its used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so far I believe only getApprovalForTransaction is using it (which is the reason why you added erc20Value in the first place?) - I'll double check anyway

Readonly<
| {
amountWei: bigint;
tokenAddress: Address;
}
| undefined
>
>;
};

export type EIP712TransactionOptions = {
Expand Down