Skip to content

Commit 29196d7

Browse files
committed
adjust test
1 parent 4f22e2b commit 29196d7

File tree

4 files changed

+85
-44
lines changed

4 files changed

+85
-44
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

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
import { type Abi, toFunctionSelector } from "viem";
12
import { describe, expect, it } from "vitest";
23
import { ANVIL_CHAIN } from "~test/chains.js";
34
import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js";
45
import { TEST_CLIENT } from "~test/test-clients.js";
56
import { TEST_ACCOUNT_C } from "~test/test-wallets.js";
7+
import { resolveContractAbi } from "../../../contract/actions/resolve-abi.js";
68
import { getContract } from "../../../contract/contract.js";
79
import { deployERC1155Contract } from "../../../extensions/prebuilts/deploy-erc1155.js";
810
import { sendAndConfirmTransaction } from "../../../transaction/actions/send-and-confirm-transaction.js";
911
import { getNFTs } from "../read/getNFTs.js";
10-
import { mintAdditionalSupplyToBatch } from "./mintAdditionalSupplyToBatch.js";
12+
import { isMintAdditionalSupplyToSupported } from "./mintAdditionalSupplyTo.js";
13+
import {
14+
mintAdditionalSupplyToBatch,
15+
optimizeMintBatchContent,
16+
} from "./mintAdditionalSupplyToBatch.js";
1117
import { mintToBatch } from "./mintToBatch.js";
1218

1319
const chain = ANVIL_CHAIN;
@@ -17,6 +23,19 @@ const account = TEST_ACCOUNT_C;
1723
describe.runIf(process.env.TW_SECRET_KEY)(
1824
"ERC1155 Edition: mintToBatch",
1925
() => {
26+
it("should optimize the mint content", () => {
27+
expect(
28+
optimizeMintBatchContent([
29+
{ tokenId: 0n, supply: 99n, to: account.address },
30+
{ tokenId: 1n, supply: 49n, to: account.address },
31+
{ tokenId: 1n, supply: 51n, to: account.address },
32+
]),
33+
).toStrictEqual([
34+
{ tokenId: 0n, supply: 99n, to: account.address },
35+
{ tokenId: 1n, supply: 100n, to: account.address },
36+
]);
37+
});
38+
2039
it("should mint multiple tokens in one tx", async () => {
2140
const contract = getContract({
2241
chain,
@@ -33,6 +52,13 @@ describe.runIf(process.env.TW_SECRET_KEY)(
3352
}),
3453
});
3554

55+
// `isMintAdditionalSupplyToSupported` should work with our Edition contracts
56+
const abi = await resolveContractAbi<Abi>(contract);
57+
const selectors = abi
58+
.filter((f) => f.type === "function")
59+
.map((f) => toFunctionSelector(f));
60+
expect(isMintAdditionalSupplyToSupported(selectors)).toBe(true);
61+
3662
await sendAndConfirmTransaction({
3763
account,
3864
transaction: mintToBatch({
@@ -52,8 +78,9 @@ describe.runIf(process.env.TW_SECRET_KEY)(
5278
contract,
5379
nfts: [
5480
{ tokenId: 0n, supply: 99n, to: account.address },
55-
{ tokenId: 1n, supply: 98n, to: account.address },
81+
{ tokenId: 1n, supply: 94n, to: account.address },
5682
{ tokenId: 2n, supply: 97n, to: account.address },
83+
{ tokenId: 1n, supply: 4n, to: account.address },
5784
],
5885
}),
5986
});

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)