Skip to content

Commit 9733a91

Browse files
committed
Update
1 parent 44b2634 commit 9733a91

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

.changeset/six-drinks-joke.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 ERC1155 extension: mintToBatch

packages/thirdweb/src/exports/extensions/erc1155.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,8 @@ export {
198198

199199
// Zora 1155 contract
200200
export { nextTokenId } from "../../extensions/erc1155/__generated__/Zora1155/read/nextTokenId.js";
201+
202+
export {
203+
mintToBatch,
204+
type MintToBatchParams,
205+
} from "../../extensions/erc1155/write/mintToBatch.js";
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { describe, expect, it } from "vitest";
2+
import { ANVIL_CHAIN } from "~test/chains.js";
3+
import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js";
4+
import { TEST_CLIENT } from "~test/test-clients.js";
5+
import { TEST_ACCOUNT_C } from "~test/test-wallets.js";
6+
import { getContract } from "../../../contract/contract.js";
7+
import { deployERC1155Contract } from "../../../extensions/prebuilts/deploy-erc1155.js";
8+
import { sendAndConfirmTransaction } from "../../../transaction/actions/send-and-confirm-transaction.js";
9+
import { getNFTs } from "../read/getNFTs.js";
10+
import { mintToBatch } from "./mintToBatch.js";
11+
12+
const chain = ANVIL_CHAIN;
13+
const client = TEST_CLIENT;
14+
const account = TEST_ACCOUNT_C;
15+
16+
describe("ERC1155 Edition: mintToBatch", () => {
17+
it("should mint multiple tokens in one tx", async () => {
18+
const contract = getContract({
19+
chain,
20+
client,
21+
address: await deployERC1155Contract({
22+
chain,
23+
client,
24+
account,
25+
type: "TokenERC1155",
26+
params: {
27+
name: "edition",
28+
contractURI: TEST_CONTRACT_URI,
29+
},
30+
}),
31+
});
32+
33+
await sendAndConfirmTransaction({
34+
account,
35+
transaction: mintToBatch({
36+
contract,
37+
to: account.address,
38+
nfts: [
39+
{ metadata: { name: "token 0" }, supply: 1n },
40+
{ metadata: { name: "token 1" }, supply: 2n },
41+
{ metadata: { name: "token 2" }, supply: 3n },
42+
],
43+
}),
44+
});
45+
46+
const nfts = await getNFTs({ contract });
47+
expect(nfts).toStrictEqual([
48+
{
49+
metadata: { name: "token 0" },
50+
owner: null,
51+
id: 0n,
52+
tokenURI: "ipfs://QmPZ6LpGqMuFbHKTXrNW1NRNLHf1nrxS4dtoFqdZZTKvPX/0",
53+
type: "ERC1155",
54+
supply: 1n,
55+
},
56+
{
57+
metadata: { name: "token 1" },
58+
owner: null,
59+
id: 1n,
60+
tokenURI: "ipfs://QmRFPyc3yEYxR4pQxwyTQWTine51TxWCoD6nzJWR3eX45b/0",
61+
type: "ERC1155",
62+
supply: 2n,
63+
},
64+
{
65+
metadata: { name: "token 2" },
66+
owner: null,
67+
id: 2n,
68+
tokenURI: "ipfs://QmesQiRLHCgqWZM2GFCs7Nb7rr2S72hU1BVQc7xiTyKZtT/0",
69+
type: "ERC1155",
70+
supply: 3n,
71+
},
72+
]);
73+
});
74+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { maxUint256 } from "viem";
2+
import { multicall } from "../../../extensions/common/__generated__/IMulticall/write/multicall.js";
3+
import { upload } from "../../../storage/upload.js";
4+
import type {
5+
BaseTransactionOptions,
6+
WithOverrides,
7+
} from "../../../transaction/types.js";
8+
import type { NFTInput } from "../../../utils/nft/parseNft.js";
9+
import { encodeMintTo } from "../__generated__/IMintableERC1155/write/mintTo.js";
10+
11+
export type MintToBatchParams = WithOverrides<{
12+
to: string;
13+
nfts: Array<{
14+
supply: bigint;
15+
metadata: NFTInput | string;
16+
}>;
17+
}>;
18+
19+
export function mintToBatch(
20+
options: BaseTransactionOptions<MintToBatchParams>,
21+
) {
22+
return multicall({
23+
contract: options.contract,
24+
asyncParams: async () => {
25+
const uris = await Promise.all(
26+
options.nfts.map((item) => {
27+
if (typeof item.metadata === "string") {
28+
return item.metadata;
29+
}
30+
return upload({
31+
client: options.contract.client,
32+
files: [item.metadata],
33+
});
34+
}),
35+
);
36+
37+
const data = uris.map((uri, index) => {
38+
const item = options.nfts[index];
39+
if (!item) {
40+
// Should not happen
41+
throw new Error("Index mismatch");
42+
}
43+
return encodeMintTo({
44+
to: options.to,
45+
// maxUint256 is used to indicate that this is a NEW token!
46+
tokenId: maxUint256,
47+
uri,
48+
amount: item.supply,
49+
});
50+
});
51+
52+
return { data };
53+
},
54+
overrides: options.overrides,
55+
});
56+
}

0 commit comments

Comments
 (0)