Skip to content

Commit

Permalink
update premint-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
iainnash committed Oct 21, 2023
1 parent cff1cc4 commit 6eaf7bb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/brave-llamas-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zoralabs/premint-sdk": patch
---

add retries
37 changes: 36 additions & 1 deletion packages/premint-sdk/src/premint-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, NetworkConfig> = {
Expand All @@ -51,6 +57,15 @@ export const networkConfigByChain: Record<number, NetworkConfig> = {
},
};

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;
Expand Down Expand Up @@ -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://${
Expand All @@ -423,6 +440,24 @@ export class PremintAPI {
};
}

private async retries<T>(
tryFn: () => T,
maxTries: number = 3,
atTry: number = 1,
): Promise<T> {
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
*
Expand Down

0 comments on commit 6eaf7bb

Please sign in to comment.