Skip to content

Commit d8d48a9

Browse files
authored
[SDK] Perf improvement for erc-1155-signature-mintable (#1824)
1 parent 5ffbcfc commit d8d48a9

File tree

2 files changed

+63
-47
lines changed

2 files changed

+63
-47
lines changed

.changeset/moody-points-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/sdk": patch
3+
---
4+
5+
Performance improve for erc-1155-signature-mintable

packages/sdk/src/evm/core/classes/erc-1155-signature-mintable.ts

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ export class Erc1155SignatureMintable implements DetectableFeature {
8383
): Promise<Transaction<TransactionResultWithId>> => {
8484
const mintRequest = signedPayload.payload;
8585
const signature = signedPayload.signature;
86-
const message = await this.mapPayloadToContractStruct(mintRequest);
87-
const overrides = await this.contractWrapper.getCallOverrides();
86+
const [message, overrides] = await Promise.all([
87+
this.mapPayloadToContractStruct(mintRequest),
88+
this.contractWrapper.getCallOverrides(),
89+
]);
8890
// TODO: Transaction Sequence Pattern
8991
await setErc20Allowance(
9092
this.contractWrapper,
@@ -136,22 +138,23 @@ export class Erc1155SignatureMintable implements DetectableFeature {
136138
async (
137139
signedPayloads: SignedPayload1155[],
138140
): Promise<Transaction<TransactionResultWithId[]>> => {
139-
const contractPayloads = await Promise.all(
140-
signedPayloads.map(async (s) => {
141-
const message = await this.mapPayloadToContractStruct(s.payload);
142-
const signature = s.signature;
143-
const price = s.payload.price;
144-
if (BigNumber.from(price).gt(0)) {
145-
throw new Error(
146-
"Can only batch free mints. For mints with a price, use regular mint()",
147-
);
148-
}
149-
return {
150-
message,
151-
signature,
152-
};
153-
}),
141+
const contractStructs = await Promise.all(
142+
signedPayloads.map((s) => this.mapPayloadToContractStruct(s.payload)),
154143
);
144+
const contractPayloads = signedPayloads.map((s, index) => {
145+
const message = contractStructs[index];
146+
const signature = s.signature;
147+
const price = s.payload.price;
148+
if (BigNumber.from(price).gt(0)) {
149+
throw new Error(
150+
"Can only batch free mints. For mints with a price, use regular mint()",
151+
);
152+
}
153+
return {
154+
message,
155+
signature,
156+
};
157+
});
155158
const contractEncoder = new ContractEncoder(this.contractWrapper);
156159
const encoded = contractPayloads.map((p) => {
157160
return contractEncoder.encode("mintWithSignature", [
@@ -276,7 +279,7 @@ export class Erc1155SignatureMintable implements DetectableFeature {
276279
}
277280

278281
/**
279-
* Generate a signature that can be used to mint additionaly supply to an existing NFT.
282+
* Generate a signature that can be used to mint additionally supply to an existing NFT.
280283
*
281284
* @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.
282285
*
@@ -291,7 +294,7 @@ export class Erc1155SignatureMintable implements DetectableFeature {
291294
* const startTime = new Date();
292295
* const endTime = new Date(Date.now() + 60 * 60 * 24 * 1000);
293296
* const payload = {
294-
* tokenId: 0, // Instead of metadata, we specificy the token ID of the NFT to mint supply to
297+
* tokenId: 0, // Instead of metadata, we specify the token ID of the NFT to mint supply to
295298
* to: {{wallet_address}}, // Who will receive the NFT (or AddressZero for anyone)
296299
* quantity: 2, // the quantity of NFTs to mint
297300
* price: 0.5, // the price per NFT
@@ -337,7 +340,7 @@ export class Erc1155SignatureMintable implements DetectableFeature {
337340
}
338341

339342
/**
340-
* Genrate a batch of signatures that can be used to mint new NFTs or additionaly supply to existing NFTs dynamically.
343+
* Generate a batch of signatures that can be used to mint new NFTs or additionally supply to existing NFTs dynamically.
341344
*
342345
* @remarks See {@link Erc1155SignatureMintable.generateFromTokenId}
343346
*
@@ -348,10 +351,10 @@ export class Erc1155SignatureMintable implements DetectableFeature {
348351
public async generateBatchFromTokenIds(
349352
payloadsToSign: PayloadToSign1155WithTokenId[],
350353
): Promise<SignedPayload1155[]> {
351-
await this.roles?.verify(
352-
["minter"],
353-
await this.contractWrapper.getSignerAddress(),
354-
);
354+
const signer = this.contractWrapper.getSigner();
355+
invariant(signer, "No signer available");
356+
357+
await this.roles?.verify(["minter"], await signer.getAddress());
355358

356359
const parsedRequests: FilledSignaturePayload1155WithTokenId[] =
357360
await Promise.all(
@@ -361,26 +364,34 @@ export class Erc1155SignatureMintable implements DetectableFeature {
361364
);
362365

363366
const metadatas = parsedRequests.map((r) => r.metadata);
364-
const uris = await uploadOrExtractURIs(metadatas, this.storage);
365367

366-
const chainId = await this.contractWrapper.getChainID();
367-
const signer = this.contractWrapper.getSigner();
368-
invariant(signer, "No signer available");
368+
const [uris, chainId, contractInfo] = await Promise.all([
369+
uploadOrExtractURIs(metadatas, this.storage),
370+
this.contractWrapper.getChainID(),
371+
getPrebuiltInfo(
372+
this.contractWrapper.address,
373+
this.contractWrapper.getProvider(),
374+
),
375+
]);
369376

370-
const contractInfo = await getPrebuiltInfo(
371-
this.contractWrapper.address,
372-
this.contractWrapper.getProvider(),
377+
const finalPayloads = await Promise.all(
378+
parsedRequests.map((m, i) =>
379+
Signature1155PayloadOutput.parseAsync({
380+
...m,
381+
uri: uris[i],
382+
}),
383+
),
384+
);
385+
const contractStructs = await Promise.all(
386+
finalPayloads.map((finalPayload) =>
387+
this.mapPayloadToContractStruct(finalPayload),
388+
),
373389
);
374-
const isLegacyContract = contractInfo?.type === "TokenERC1155";
375390

376-
return await Promise.all(
377-
parsedRequests.map(async (m, i) => {
378-
const uri = uris[i];
379-
const finalPayload = await Signature1155PayloadOutput.parseAsync({
380-
...m,
381-
uri,
382-
});
383-
const signature = await this.contractWrapper.signTypedData(
391+
const isLegacyContract = contractInfo?.type === "TokenERC1155";
392+
const signatures = await Promise.all(
393+
contractStructs.map((contractStruct) =>
394+
this.contractWrapper.signTypedData(
384395
signer,
385396
{
386397
name: isLegacyContract ? "TokenERC1155" : "SignatureMintERC1155",
@@ -389,14 +400,14 @@ export class Erc1155SignatureMintable implements DetectableFeature {
389400
verifyingContract: this.contractWrapper.address,
390401
},
391402
{ MintRequest: MintRequest1155 }, // TYPEHASH
392-
await this.mapPayloadToContractStruct(finalPayload),
393-
);
394-
return {
395-
payload: finalPayload,
396-
signature: signature.toString(),
397-
};
398-
}),
403+
contractStruct,
404+
),
405+
),
399406
);
407+
return signatures.map((signature, index) => ({
408+
payload: finalPayloads[index],
409+
signature: signature.toString(),
410+
}));
400411
}
401412

402413
/** ******************************

0 commit comments

Comments
 (0)