Skip to content

Commit 20566fb

Browse files
committed
Update
1 parent 3fe33a6 commit 20566fb

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 uris = await Promise.all(
42+
options.nfts.map((item) =>
43+
uri({ contract: options.contract, tokenId: item.tokenId }),
44+
),
45+
);
46+
47+
const data = uris.map((uri, index) => {
48+
const item = options.nfts[index];
49+
if (!item) {
50+
// Should not happen - cant make TS happy here
51+
throw new Error("Index mismatch");
52+
}
53+
return encodeMintTo({
54+
to: item.to,
55+
tokenId: item.tokenId,
56+
amount: item.supply,
57+
uri,
58+
});
59+
});
60+
return { data };
61+
},
62+
});
63+
}

0 commit comments

Comments
 (0)