From 6eaf7bbc94c7506c1064d3f7b8f3497cfb071308 Mon Sep 17 00:00:00 2001 From: iain nash Date: Sat, 21 Oct 2023 09:05:16 -0400 Subject: [PATCH] update premint-sdk --- .changeset/brave-llamas-wash.md | 5 ++++ packages/premint-sdk/src/premint-api.ts | 37 ++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 .changeset/brave-llamas-wash.md diff --git a/.changeset/brave-llamas-wash.md b/.changeset/brave-llamas-wash.md new file mode 100644 index 000000000..5648c2886 --- /dev/null +++ b/.changeset/brave-llamas-wash.md @@ -0,0 +1,5 @@ +--- +"@zoralabs/premint-sdk": patch +--- + +add retries diff --git a/packages/premint-sdk/src/premint-api.ts b/packages/premint-sdk/src/premint-api.ts index 95a0f6277..8f074bbda 100644 --- a/packages/premint-sdk/src/premint-api.ts +++ b/packages/premint-sdk/src/premint-api.ts @@ -28,6 +28,12 @@ export const enum BackendChainNames { ZORA_GOERLI = "ZORA-GOERLI", } +async function wait(delayMs: number) { + return new Promise((resolve) => { + setTimeout(resolve, delayMs); + }); +} + const ZORA_API_BASE = "https://api.zora.co/premint/"; export const networkConfigByChain: Record = { @@ -51,6 +57,15 @@ export const networkConfigByChain: Record = { }, }; +export class BadResponse extends Error { + status: number; + constructor(message: string, status: number) { + super(message); + this.name = "BadResponse"; + this.status = status; + } +} + type MintArgumentsSettings = { tokenURI: string; maxSupply?: bigint; @@ -409,7 +424,9 @@ export class PremintAPI { signature: signature, }; - const premint = await this.post(`${ZORA_API_BASE}signature`, apiData); + let premint = await this.retries(() => + this.post(`${ZORA_API_BASE}signature`, apiData), + ); return { zoraUrl: `https://${ @@ -423,6 +440,24 @@ export class PremintAPI { }; } + private async retries( + tryFn: () => T, + maxTries: number = 3, + atTry: number = 1, + ): Promise { + try { + return await tryFn(); + } catch (err: any) { + if (err instanceof BadResponse) { + if (atTry >= maxTries) { + await wait(500); + return await this.retries(tryFn, maxTries, atTry++); + } + } + throw err; + } + } + /** * Create premint *