Skip to content
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

Fix rate limit errors: add 1s timeout to retry throttle #1422

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout } from "timers/promises";
import { ethers } from "ethers";
import {
getCollectionPath,
Expand Down Expand Up @@ -606,20 +607,36 @@ export class OpenSeaAPI {
* @param body Optional body to send. If set, will POST, otherwise GET
*/
private async _fetch(url: string, headers?: object, body?: object) {
// Create the fetch request
const req = new ethers.FetchRequest(url);

// Set the headers
headers = {
"x-app-id": "opensea-js",
...(this.apiKey ? { "X-API-KEY": this.apiKey } : {}),
...headers,
};

const req = new ethers.FetchRequest(url);
for (const [key, value] of Object.entries(headers)) {
req.setHeader(key, value);
}

// Set the body if provided
if (body) {
req.body = body;
}

// Set the throttle params
// - Should be able to replace this retryFunc with `setThrottleParams({ slotInterval: 1000 })`
// when this bug is fixed in ethers: https://github.com/ethers-io/ethers.js/issues/4663
req.retryFunc = async (_req, resp, attempt) => {
this.logger(
`Fetch attempt ${attempt} failed with status ${resp.statusCode}`,
);
// Wait 1s between tries
await setTimeout(1000);
return true;
};

this.logger(
`Sending request: ${url} ${JSON.stringify({
request: req,
Expand Down
Loading