Skip to content

Commit 185d9ef

Browse files
kien-ngogregfromstl
authored andcommitted
Update
1 parent 5351d4e commit 185d9ef

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

.changeset/bright-walls-applaud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Optimize mintAdditionalSupplyToBatch extension

.changeset/unlucky-news-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": minor
3+
---
4+
5+
Add DropERC721 extension: claimToBatch

packages/thirdweb/src/extensions/erc1155/write/mintAdditionalSupplyToBatch.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { getContract } from "../../../contract/contract.js";
77
import { deployERC1155Contract } from "../../../extensions/prebuilts/deploy-erc1155.js";
88
import { sendAndConfirmTransaction } from "../../../transaction/actions/send-and-confirm-transaction.js";
99
import { getNFTs } from "../read/getNFTs.js";
10-
import { mintAdditionalSupplyToBatch } from "./mintAdditionalSupplyToBatch.js";
10+
import {
11+
mintAdditionalSupplyToBatch,
12+
optimizeMintBatchContent,
13+
} from "./mintAdditionalSupplyToBatch.js";
1114
import { mintToBatch } from "./mintToBatch.js";
1215

1316
const chain = ANVIL_CHAIN;
@@ -17,6 +20,19 @@ const account = TEST_ACCOUNT_C;
1720
describe.runIf(process.env.TW_SECRET_KEY)(
1821
"ERC1155 Edition: mintToBatch",
1922
() => {
23+
it("should optimize the mint content", () => {
24+
expect(
25+
optimizeMintBatchContent([
26+
{ tokenId: 0n, supply: 99n, to: account.address },
27+
{ tokenId: 1n, supply: 49n, to: account.address },
28+
{ tokenId: 1n, supply: 51n, to: account.address },
29+
]),
30+
).toStrictEqual([
31+
{ tokenId: 0n, supply: 99n, to: account.address },
32+
{ tokenId: 1n, supply: 100n, to: account.address },
33+
]);
34+
});
35+
2036
it("should mint multiple tokens in one tx", async () => {
2137
const contract = getContract({
2238
chain,
@@ -52,8 +68,9 @@ describe.runIf(process.env.TW_SECRET_KEY)(
5268
contract,
5369
nfts: [
5470
{ tokenId: 0n, supply: 99n, to: account.address },
55-
{ tokenId: 1n, supply: 98n, to: account.address },
71+
{ tokenId: 1n, supply: 94n, to: account.address },
5672
{ tokenId: 2n, supply: 97n, to: account.address },
73+
{ tokenId: 1n, supply: 4n, to: account.address },
5774
],
5875
}),
5976
});

packages/thirdweb/src/extensions/erc1155/write/mintAdditionalSupplyToBatch.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { multicall } from "../../../extensions/common/__generated__/IMulticall/write/multicall.js";
2-
import type { BaseTransactionOptions } from "../../../transaction/types.js";
2+
import type {
3+
BaseTransactionOptions,
4+
WithOverrides,
5+
} from "../../../transaction/types.js";
36
import { uri } from "../__generated__/IERC1155/read/uri.js";
47
import { encodeMintTo } from "../__generated__/IMintableERC1155/write/mintTo.js";
58
import type { MintAdditionalSupplyToParams } from "./mintAdditionalSupplyTo.js";
69

710
/**
811
* @extension ERC1155
912
*/
10-
export type MintAdditionalSupplyToBatchParams = {
13+
export type MintAdditionalSupplyToBatchParams = WithOverrides<{
1114
nfts: MintAdditionalSupplyToParams[];
12-
};
15+
}>;
1316

1417
/**
1518
* This extension batches multiple `mintAdditionalSupplyToBatch` extensions into one single multicall.
@@ -38,8 +41,9 @@ export function mintAdditionalSupplyToBatch(
3841
return multicall({
3942
contract: options.contract,
4043
asyncParams: async () => {
44+
const nfts = optimizeMintBatchContent(options.nfts);
4145
const data = await Promise.all(
42-
options.nfts.map(async (nft) => {
46+
nfts.map(async (nft) => {
4347
const tokenUri = await uri({
4448
contract: options.contract,
4549
tokenId: nft.tokenId,
@@ -54,5 +58,48 @@ export function mintAdditionalSupplyToBatch(
5458
);
5559
return { data };
5660
},
61+
overrides: options.overrides,
5762
});
5863
}
64+
65+
/**
66+
* Optimization
67+
*
68+
* We can batch the records that share the same "to" & "tokenId" into 1 transaction
69+
*
70+
* For example, this struct:
71+
* [
72+
* { tokenId: 0n, supply: 99n, to: account.address },
73+
* { tokenId: 1n, supply: 49n, to: account.address },
74+
* { tokenId: 1n, supply: 51n, to: account.address },
75+
* ]
76+
*
77+
* ...can be packed into:
78+
* [
79+
* { tokenId: 0n, supply: 99n, to: account.address },
80+
* { tokenId: 1n, supply: 100n, to: account.address },
81+
* ]
82+
* @internal
83+
*/
84+
export function optimizeMintBatchContent(
85+
nfts: MintAdditionalSupplyToParams[],
86+
): MintAdditionalSupplyToParams[] {
87+
const results: MintAdditionalSupplyToParams[] = [];
88+
for (const item of nfts) {
89+
const matchingIndex = results.findIndex(
90+
(o) =>
91+
o.tokenId === item.tokenId &&
92+
o.to.toLowerCase() === item.to.toLowerCase(),
93+
);
94+
if (matchingIndex !== -1) {
95+
results[matchingIndex] = {
96+
to: item.to,
97+
tokenId: item.tokenId,
98+
supply: item.supply + (results[matchingIndex]?.supply || 0n),
99+
};
100+
} else {
101+
results.push(item);
102+
}
103+
}
104+
return results;
105+
}

0 commit comments

Comments
 (0)