thirdweb 5.65.1 → 5.65.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,34 +53,88 @@ export function transferBatch(
53
53
  return multicall({
54
54
  contract: options.contract,
55
55
  asyncParams: async () => {
56
+ const content = await optimizeTransferContent(options);
56
57
  return {
57
- data: await Promise.all(
58
- options.batch.map(async (transfer) => {
59
- let amount: bigint;
60
- if ("amount" in transfer) {
61
- // if we need to parse the amount from ether to gwei then we pull in the decimals extension
62
- const { decimals } = await import("../read/decimals.js");
63
- // it's OK to call this multiple times because the call is cached
64
- // if this fails we fall back to `18` decimals
65
- const d = await decimals(options).catch(() => 18);
66
- // turn ether into gwei
67
- amount = toUnits(transfer.amount.toString(), d);
68
- } else {
69
- amount = transfer.amountWei;
70
- }
71
- return encodeTransfer({
72
- to: transfer.to,
73
- value: amount,
74
- overrides: {
75
- erc20Value: {
76
- amountWei: amount,
77
- tokenAddress: options.contract.address,
78
- },
58
+ data: content.map((item) => {
59
+ return encodeTransfer({
60
+ to: item.to,
61
+ value: item.amountWei,
62
+ overrides: {
63
+ erc20Value: {
64
+ amountWei: item.amountWei,
65
+ tokenAddress: options.contract.address,
79
66
  },
80
- });
81
- }),
82
- ),
67
+ },
68
+ });
69
+ }),
83
70
  };
84
71
  },
85
72
  });
86
73
  }
74
+
75
+ /**
76
+ * Records with the same recipient (`to`) can be packed into one transaction
77
+ * For example, the data below:
78
+ * ```ts
79
+ * [
80
+ * {
81
+ * to: "wallet-a",
82
+ * amount: 1,
83
+ * },
84
+ * {
85
+ * to: "wallet-A",
86
+ * amountWei: 1000000000000000000n,
87
+ * },
88
+ * ]
89
+ * ```
90
+ *
91
+ * can be packed to:
92
+ * ```ts
93
+ * [
94
+ * {
95
+ * to: "wallet-a",
96
+ * amountWei: 2000000000000000000n,
97
+ * },
98
+ * ]
99
+ * ```
100
+ * @internal
101
+ */
102
+ export async function optimizeTransferContent(
103
+ options: BaseTransactionOptions<TransferBatchParams>,
104
+ ): Promise<Array<{ to: string; amountWei: bigint }>> {
105
+ const groupedRecords = await options.batch.reduce(
106
+ async (accPromise, record) => {
107
+ const acc = await accPromise;
108
+ let amountInWei: bigint;
109
+ if ("amount" in record) {
110
+ // it's OK to call this multiple times because the call is cached
111
+ const { decimals } = await import("../read/decimals.js");
112
+ // if this fails we fall back to `18` decimals
113
+ const d = await decimals(options).catch(() => undefined);
114
+ if (d === undefined) {
115
+ throw new Error(
116
+ `Failed to get the decimals for contract: ${options.contract.address}`,
117
+ );
118
+ }
119
+ amountInWei = toUnits(record.amount.toString(), d);
120
+ } else {
121
+ amountInWei = record.amountWei;
122
+ }
123
+ const existingRecord = acc.find(
124
+ (r) => r.to.toLowerCase() === record.to.toLowerCase(),
125
+ );
126
+ if (existingRecord) {
127
+ existingRecord.amountWei = existingRecord.amountWei + amountInWei;
128
+ } else {
129
+ acc.push({
130
+ to: record.to,
131
+ amountWei: amountInWei,
132
+ });
133
+ }
134
+
135
+ return acc;
136
+ },
137
+ Promise.resolve([] as { to: string; amountWei: bigint }[]),
138
+ );
139
+ return groupedRecords;
140
+ }
@@ -984,7 +984,8 @@ function SwapScreenContent(props: {
984
984
 
985
985
  const disableContinue =
986
986
  (swapRequired && !quoteQuery.data) || isNotEnoughBalance;
987
- const switchChainRequired = props.payer.chain.id !== fromChain.id;
987
+ const switchChainRequired =
988
+ props.payer.wallet.getChain()?.id !== fromChain.id;
988
989
 
989
990
  // biome-ignore lint/suspicious/noExplicitAny: <explanation>
990
991
  function getErrorMessage(err: any) {
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = "5.65.1";
1
+ export const version = "5.65.2";