Skip to content

Commit c202eb6

Browse files
committed
Update
1 parent df128bf commit c202eb6

File tree

5 files changed

+170
-24
lines changed

5 files changed

+170
-24
lines changed

.changeset/silent-eels-explain.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 new ERC1155 extension: mintAdditionalSupplyToBatch

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,8 @@ export {
203203
mintToBatch,
204204
type MintToBatchParams,
205205
} from "../../extensions/erc1155/write/mintToBatch.js";
206+
207+
export {
208+
mintAdditionalSupplyToBatch,
209+
type MintAdditionalSupplyToBatchParams,
210+
} from "../../extensions/erc1155/write/mintAdditionalSupplyToBatch.js";
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 { mintAdditionalSupplyToBatch } from "./mintAdditionalSupplyToBatch.js";
11+
import { mintToBatch } from "./mintToBatch.js";
12+
13+
const chain = ANVIL_CHAIN;
14+
const client = TEST_CLIENT;
15+
const account = TEST_ACCOUNT_C;
16+
17+
describe("ERC1155 Edition: mintToBatch", () => {
18+
it("should mint multiple tokens in one tx", async () => {
19+
const contract = getContract({
20+
chain,
21+
client,
22+
address: await deployERC1155Contract({
23+
chain,
24+
client,
25+
account,
26+
type: "TokenERC1155",
27+
params: {
28+
name: "edition",
29+
contractURI: TEST_CONTRACT_URI,
30+
},
31+
}),
32+
});
33+
34+
await sendAndConfirmTransaction({
35+
account,
36+
transaction: mintToBatch({
37+
contract,
38+
to: account.address,
39+
nfts: [
40+
{ metadata: { name: "token 0" }, supply: 1n },
41+
{ metadata: { name: "token 1" }, supply: 2n },
42+
{ metadata: { name: "token 2" }, supply: 3n },
43+
],
44+
}),
45+
});
46+
47+
await sendAndConfirmTransaction({
48+
account,
49+
transaction: mintAdditionalSupplyToBatch({
50+
contract,
51+
nfts: [
52+
{ tokenId: 0n, supply: 99n, to: account.address },
53+
{ tokenId: 1n, supply: 98n, to: account.address },
54+
{ tokenId: 2n, supply: 97n, to: account.address },
55+
],
56+
}),
57+
});
58+
59+
const nfts = await getNFTs({ contract });
60+
expect(nfts).toStrictEqual([
61+
{
62+
metadata: { name: "token 0" },
63+
owner: null,
64+
id: 0n,
65+
tokenURI: "ipfs://QmPZ6LpGqMuFbHKTXrNW1NRNLHf1nrxS4dtoFqdZZTKvPX/0",
66+
type: "ERC1155",
67+
supply: 100n,
68+
},
69+
{
70+
metadata: { name: "token 1" },
71+
owner: null,
72+
id: 1n,
73+
tokenURI: "ipfs://QmRFPyc3yEYxR4pQxwyTQWTine51TxWCoD6nzJWR3eX45b/0",
74+
type: "ERC1155",
75+
supply: 100n,
76+
},
77+
{
78+
metadata: { name: "token 2" },
79+
owner: null,
80+
id: 2n,
81+
tokenURI: "ipfs://QmesQiRLHCgqWZM2GFCs7Nb7rr2S72hU1BVQc7xiTyKZtT/0",
82+
type: "ERC1155",
83+
supply: 100n,
84+
},
85+
]);
86+
});
87+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { multicall } from "../../../extensions/common/__generated__/IMulticall/write/multicall.js";
2+
import type { BaseTransactionOptions } from "../../../transaction/types.js";
3+
import { uri } from "../__generated__/IERC1155/read/uri.js";
4+
import { encodeMintTo } from "../__generated__/IMintableERC1155/write/mintTo.js";
5+
import type { MintAdditionalSupplyToParams } from "./mintAdditionalSupplyTo.js";
6+
7+
/**
8+
* @extension ERC1155
9+
*/
10+
export type MintAdditionalSupplyToBatchParams = {
11+
nfts: MintAdditionalSupplyToParams[];
12+
};
13+
14+
/**
15+
* This extension batches multiple `mintAdditionalSupplyToBatch` extensions into one single multicall.
16+
* Keep in mind that there is a limit of how many NFTs you can mint per transaction.
17+
* This limit varies depends on the network that you are transacting on.
18+
*
19+
* You are recommended to experiment with the number to figure out the best number for your chain of choice.
20+
* @extension ERC1155
21+
* @example
22+
* ```ts
23+
* import { mintAdditionalSupplyToBatch } from "thirdweb/extensions/erc1155";
24+
*
25+
* const transaction = mintAdditionalSupplyToBatch({
26+
* contract,
27+
* nfts: [
28+
* { tokenId: 0n, supply: 99n, to: account.address },
29+
* { tokenId: 1n, supply: 98n, to: account.address },
30+
* { tokenId: 2n, supply: 97n, to: account.address },
31+
* ],
32+
* });
33+
* ```
34+
*/
35+
export function mintAdditionalSupplyToBatch(
36+
options: BaseTransactionOptions<MintAdditionalSupplyToBatchParams>,
37+
) {
38+
return multicall({
39+
contract: options.contract,
40+
asyncParams: async () => {
41+
const data = await Promise.all(
42+
options.nfts.map(async (nft) => {
43+
const tokenUri = await uri({
44+
contract: options.contract,
45+
tokenId: nft.tokenId,
46+
});
47+
return encodeMintTo({
48+
to: nft.to,
49+
tokenId: nft.tokenId,
50+
amount: nft.supply,
51+
uri: tokenUri,
52+
});
53+
}),
54+
);
55+
return { data };
56+
},
57+
});
58+
}

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,24 @@ export function mintToBatch(
8383
return multicall({
8484
contract: options.contract,
8585
asyncParams: async () => {
86-
const uris = await Promise.all(
87-
options.nfts.map((item) => {
88-
if (typeof item.metadata === "string") {
89-
return item.metadata;
90-
}
91-
return upload({
92-
client: options.contract.client,
93-
files: [item.metadata],
86+
const data = await Promise.all(
87+
options.nfts.map(async (nft) => {
88+
const uri =
89+
typeof nft.metadata === "string"
90+
? nft.metadata
91+
: await upload({
92+
client: options.contract.client,
93+
files: [nft.metadata],
94+
});
95+
return encodeMintTo({
96+
to: options.to,
97+
// maxUint256 is used to indicate that this is a NEW token!
98+
tokenId: maxUint256,
99+
uri,
100+
amount: nft.supply,
94101
});
95102
}),
96103
);
97-
98-
const data = uris.map((uri, index) => {
99-
const item = options.nfts[index];
100-
if (!item) {
101-
// Should not happen
102-
throw new Error("Index mismatch");
103-
}
104-
return encodeMintTo({
105-
to: options.to,
106-
// maxUint256 is used to indicate that this is a NEW token!
107-
tokenId: maxUint256,
108-
uri,
109-
amount: item.supply,
110-
});
111-
});
112-
113104
return { data };
114105
},
115106
overrides: options.overrides,

0 commit comments

Comments
 (0)