-
Notifications
You must be signed in to change notification settings - Fork 619
[SDK] ERC1155 extension: mintToBatch #5032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "thirdweb": minor | ||
| --- | ||
|
|
||
| Add ERC1155 extension: mintToBatch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/thirdweb/src/extensions/erc1155/write/mintToBatch.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { ANVIL_CHAIN } from "~test/chains.js"; | ||
| import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js"; | ||
| import { TEST_CLIENT } from "~test/test-clients.js"; | ||
| import { TEST_ACCOUNT_C } from "~test/test-wallets.js"; | ||
| import { getContract } from "../../../contract/contract.js"; | ||
| import { deployERC1155Contract } from "../../../extensions/prebuilts/deploy-erc1155.js"; | ||
| import { sendAndConfirmTransaction } from "../../../transaction/actions/send-and-confirm-transaction.js"; | ||
| import { getNFTs } from "../read/getNFTs.js"; | ||
| import { mintToBatch } from "./mintToBatch.js"; | ||
|
|
||
| const chain = ANVIL_CHAIN; | ||
| const client = TEST_CLIENT; | ||
| const account = TEST_ACCOUNT_C; | ||
|
|
||
| describe("ERC1155 Edition: mintToBatch", () => { | ||
| it("should mint multiple tokens in one tx", async () => { | ||
| const contract = getContract({ | ||
| chain, | ||
| client, | ||
| address: await deployERC1155Contract({ | ||
| chain, | ||
| client, | ||
| account, | ||
| type: "TokenERC1155", | ||
| params: { | ||
| name: "edition", | ||
| contractURI: TEST_CONTRACT_URI, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| await sendAndConfirmTransaction({ | ||
| account, | ||
| transaction: mintToBatch({ | ||
| contract, | ||
| to: account.address, | ||
| nfts: [ | ||
| { metadata: { name: "token 0" }, supply: 1n }, | ||
| { metadata: { name: "token 1" }, supply: 2n }, | ||
| { metadata: { name: "token 2" }, supply: 3n }, | ||
| ], | ||
| }), | ||
| }); | ||
|
|
||
| const nfts = await getNFTs({ contract }); | ||
| expect(nfts).toStrictEqual([ | ||
| { | ||
| metadata: { name: "token 0" }, | ||
| owner: null, | ||
| id: 0n, | ||
| tokenURI: "ipfs://QmPZ6LpGqMuFbHKTXrNW1NRNLHf1nrxS4dtoFqdZZTKvPX/0", | ||
| type: "ERC1155", | ||
| supply: 1n, | ||
| }, | ||
| { | ||
| metadata: { name: "token 1" }, | ||
| owner: null, | ||
| id: 1n, | ||
| tokenURI: "ipfs://QmRFPyc3yEYxR4pQxwyTQWTine51TxWCoD6nzJWR3eX45b/0", | ||
| type: "ERC1155", | ||
| supply: 2n, | ||
| }, | ||
| { | ||
| metadata: { name: "token 2" }, | ||
| owner: null, | ||
| id: 2n, | ||
| tokenURI: "ipfs://QmesQiRLHCgqWZM2GFCs7Nb7rr2S72hU1BVQc7xiTyKZtT/0", | ||
| type: "ERC1155", | ||
| supply: 3n, | ||
| }, | ||
| ]); | ||
| }); | ||
| }); |
117 changes: 117 additions & 0 deletions
117
packages/thirdweb/src/extensions/erc1155/write/mintToBatch.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { maxUint256 } from "viem"; | ||
| import { multicall } from "../../../extensions/common/__generated__/IMulticall/write/multicall.js"; | ||
| import { upload } from "../../../storage/upload.js"; | ||
| import type { | ||
| BaseTransactionOptions, | ||
| WithOverrides, | ||
| } from "../../../transaction/types.js"; | ||
| import type { NFTInput } from "../../../utils/nft/parseNft.js"; | ||
| import { encodeMintTo } from "../__generated__/IMintableERC1155/write/mintTo.js"; | ||
|
|
||
| /** | ||
| * @extension ERC1155 | ||
| */ | ||
| export type MintToBatchParams = WithOverrides<{ | ||
| /** | ||
| * The wallet that the NFTs will be minted to | ||
| */ | ||
| to: string; | ||
| /** | ||
| * An array of NFT metadata & supply to mint | ||
| * @example | ||
| * ```ts | ||
| * const nfts = [ | ||
| * { | ||
| * metadata: { name: "token 0" }, | ||
| * supply: 1n, | ||
| * }, | ||
| * { | ||
| * metadata: { name: "token 1" }, | ||
| * supply: 10n, | ||
| * }, | ||
| * ] | ||
| * ``` | ||
| */ | ||
| nfts: Array<{ | ||
| supply: bigint; | ||
| metadata: NFTInput | string; | ||
| }>; | ||
| }>; | ||
|
|
||
| /** | ||
| * This extension batches multiple `mintTo` extensions into one single multicall. | ||
| * Keep in mind that there is a limit of how many NFTs you can mint per transaction. | ||
| * This limit varies depends on the network that you are transacting on. | ||
| * | ||
| * You are recommended to experiment with the number to figure out the best number for your chain of choice. | ||
| * @param options - the transaction options | ||
| * @returns A promise that resolves to the transaction result. | ||
| * @extension ERC1155 | ||
| * @example | ||
| * ```ts | ||
| * import { mintBatchTo } from "thirdweb/extension/erc1155"; | ||
| * | ||
| * const transaction = mintToBatch({ | ||
| * contract: editionContract, | ||
| * to: "0x...", | ||
| * nfts: [ | ||
| * { | ||
| * metadata: { | ||
| * name: "Token #0", | ||
| * image: "...", | ||
| * attributes: [], | ||
| * }, | ||
| * supply: 100n, | ||
| * }, | ||
| * { | ||
| * metadata: { | ||
| * name: "Token #1", | ||
| * image: "...", | ||
| * attributes: [], | ||
| * }, | ||
| * supply: 111n, | ||
| * }, | ||
| * ], | ||
| * }); | ||
| * | ||
| * await sendTransaction({ transaction, account }); | ||
| * ``` | ||
| */ | ||
| export function mintToBatch( | ||
| options: BaseTransactionOptions<MintToBatchParams>, | ||
| ) { | ||
| return multicall({ | ||
| contract: options.contract, | ||
| asyncParams: async () => { | ||
| const uris = await Promise.all( | ||
| options.nfts.map((item) => { | ||
| if (typeof item.metadata === "string") { | ||
| return item.metadata; | ||
| } | ||
| return upload({ | ||
| client: options.contract.client, | ||
| files: [item.metadata], | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| const data = uris.map((uri, index) => { | ||
| const item = options.nfts[index]; | ||
| if (!item) { | ||
| // Should not happen | ||
| throw new Error("Index mismatch"); | ||
| } | ||
| return encodeMintTo({ | ||
| to: options.to, | ||
| // maxUint256 is used to indicate that this is a NEW token! | ||
| tokenId: maxUint256, | ||
| uri, | ||
| amount: item.supply, | ||
| }); | ||
| }); | ||
|
|
||
| return { data }; | ||
| }, | ||
| overrides: options.overrides, | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add TS doc
also make sure you add a comment about the size of the batch - should not be too big otherwise it will not fit / not be included in a block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ty ser. was waiting for a review on the code before I add the docs, seems all good!