diff --git a/.changeset/moody-points-serve.md b/.changeset/moody-points-serve.md new file mode 100644 index 00000000000..0b2ef04e22b --- /dev/null +++ b/.changeset/moody-points-serve.md @@ -0,0 +1,5 @@ +--- +"@thirdweb-dev/sdk": patch +--- + +Performance improve for erc-1155-signature-mintable diff --git a/packages/sdk/src/evm/core/classes/erc-1155-signature-mintable.ts b/packages/sdk/src/evm/core/classes/erc-1155-signature-mintable.ts index 19f6469b3fc..f3e7c1d5395 100644 --- a/packages/sdk/src/evm/core/classes/erc-1155-signature-mintable.ts +++ b/packages/sdk/src/evm/core/classes/erc-1155-signature-mintable.ts @@ -83,8 +83,10 @@ export class Erc1155SignatureMintable implements DetectableFeature { ): Promise> => { const mintRequest = signedPayload.payload; const signature = signedPayload.signature; - const message = await this.mapPayloadToContractStruct(mintRequest); - const overrides = await this.contractWrapper.getCallOverrides(); + const [message, overrides] = await Promise.all([ + this.mapPayloadToContractStruct(mintRequest), + this.contractWrapper.getCallOverrides(), + ]); // TODO: Transaction Sequence Pattern await setErc20Allowance( this.contractWrapper, @@ -136,22 +138,23 @@ export class Erc1155SignatureMintable implements DetectableFeature { async ( signedPayloads: SignedPayload1155[], ): Promise> => { - const contractPayloads = await Promise.all( - signedPayloads.map(async (s) => { - const message = await this.mapPayloadToContractStruct(s.payload); - const signature = s.signature; - const price = s.payload.price; - if (BigNumber.from(price).gt(0)) { - throw new Error( - "Can only batch free mints. For mints with a price, use regular mint()", - ); - } - return { - message, - signature, - }; - }), + const contractStructs = await Promise.all( + signedPayloads.map((s) => this.mapPayloadToContractStruct(s.payload)), ); + const contractPayloads = signedPayloads.map((s, index) => { + const message = contractStructs[index]; + const signature = s.signature; + const price = s.payload.price; + if (BigNumber.from(price).gt(0)) { + throw new Error( + "Can only batch free mints. For mints with a price, use regular mint()", + ); + } + return { + message, + signature, + }; + }); const contractEncoder = new ContractEncoder(this.contractWrapper); const encoded = contractPayloads.map((p) => { return contractEncoder.encode("mintWithSignature", [ @@ -276,7 +279,7 @@ export class Erc1155SignatureMintable implements DetectableFeature { } /** - * Generate a signature that can be used to mint additionaly supply to an existing NFT. + * Generate a signature that can be used to mint additionally supply to an existing NFT. * * @remarks Takes in a payload with the token ID of an existing NFT, and signs it with your private key. The generated signature can then be used to mint additional supply to the NFT using the exact payload and signature generated. * @@ -291,7 +294,7 @@ export class Erc1155SignatureMintable implements DetectableFeature { * const startTime = new Date(); * const endTime = new Date(Date.now() + 60 * 60 * 24 * 1000); * const payload = { - * tokenId: 0, // Instead of metadata, we specificy the token ID of the NFT to mint supply to + * tokenId: 0, // Instead of metadata, we specify the token ID of the NFT to mint supply to * to: {{wallet_address}}, // Who will receive the NFT (or AddressZero for anyone) * quantity: 2, // the quantity of NFTs to mint * price: 0.5, // the price per NFT @@ -337,7 +340,7 @@ export class Erc1155SignatureMintable implements DetectableFeature { } /** - * Genrate a batch of signatures that can be used to mint new NFTs or additionaly supply to existing NFTs dynamically. + * Generate a batch of signatures that can be used to mint new NFTs or additionally supply to existing NFTs dynamically. * * @remarks See {@link Erc1155SignatureMintable.generateFromTokenId} * @@ -348,10 +351,10 @@ export class Erc1155SignatureMintable implements DetectableFeature { public async generateBatchFromTokenIds( payloadsToSign: PayloadToSign1155WithTokenId[], ): Promise { - await this.roles?.verify( - ["minter"], - await this.contractWrapper.getSignerAddress(), - ); + const signer = this.contractWrapper.getSigner(); + invariant(signer, "No signer available"); + + await this.roles?.verify(["minter"], await signer.getAddress()); const parsedRequests: FilledSignaturePayload1155WithTokenId[] = await Promise.all( @@ -361,26 +364,34 @@ export class Erc1155SignatureMintable implements DetectableFeature { ); const metadatas = parsedRequests.map((r) => r.metadata); - const uris = await uploadOrExtractURIs(metadatas, this.storage); - const chainId = await this.contractWrapper.getChainID(); - const signer = this.contractWrapper.getSigner(); - invariant(signer, "No signer available"); + const [uris, chainId, contractInfo] = await Promise.all([ + uploadOrExtractURIs(metadatas, this.storage), + this.contractWrapper.getChainID(), + getPrebuiltInfo( + this.contractWrapper.address, + this.contractWrapper.getProvider(), + ), + ]); - const contractInfo = await getPrebuiltInfo( - this.contractWrapper.address, - this.contractWrapper.getProvider(), + const finalPayloads = await Promise.all( + parsedRequests.map((m, i) => + Signature1155PayloadOutput.parseAsync({ + ...m, + uri: uris[i], + }), + ), + ); + const contractStructs = await Promise.all( + finalPayloads.map((finalPayload) => + this.mapPayloadToContractStruct(finalPayload), + ), ); - const isLegacyContract = contractInfo?.type === "TokenERC1155"; - return await Promise.all( - parsedRequests.map(async (m, i) => { - const uri = uris[i]; - const finalPayload = await Signature1155PayloadOutput.parseAsync({ - ...m, - uri, - }); - const signature = await this.contractWrapper.signTypedData( + const isLegacyContract = contractInfo?.type === "TokenERC1155"; + const signatures = await Promise.all( + contractStructs.map((contractStruct) => + this.contractWrapper.signTypedData( signer, { name: isLegacyContract ? "TokenERC1155" : "SignatureMintERC1155", @@ -389,14 +400,14 @@ export class Erc1155SignatureMintable implements DetectableFeature { verifyingContract: this.contractWrapper.address, }, { MintRequest: MintRequest1155 }, // TYPEHASH - await this.mapPayloadToContractStruct(finalPayload), - ); - return { - payload: finalPayload, - signature: signature.toString(), - }; - }), + contractStruct, + ), + ), ); + return signatures.map((signature, index) => ({ + payload: finalPayloads[index], + signature: signature.toString(), + })); } /** ******************************