Skip to content

Commit

Permalink
fix: retry redstone errors
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Sep 13, 2024
1 parent e63a919 commit 9f18c6b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/services/RedstoneServiceV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {
} from "../data/index.js";
import { DI } from "../di.js";
import { type ILogger, Logger } from "../log/index.js";
import { formatTs } from "../utils/index.js";
import { formatTs, retry } from "../utils/index.js";
import type { AddressProviderService } from "./AddressProviderService.js";
import type Client from "./Client.js";
import type { PriceOnDemandExtras, PriceUpdate } from "./liquidate/index.js";
Expand Down Expand Up @@ -364,7 +364,12 @@ export class RedstoneServiceV3 {
historicalTimestamp: this.#optimisticTimestamp,
});
wrapper.setMetadataTimestamp(Date.now());
const dataPayload = await wrapper.prepareRedstonePayload(true);
// redstone does not provide any error types, just string messages
// so just retry all redstone errors
const dataPayload = await retry(
() => wrapper.prepareRedstonePayload(true),
{ attempts: 2 },
);

// unsigned metadata looks like
// "1724772413180#0.6.1#redstone-primary-prod___"
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from "./bigint-serializer.js";
export * from "./detect-network.js";
export * from "./etherscan.js";
export * from "./formatters.js";
export * from "./retry.js";
export * from "./types.js";
export * from "./typescript.js";
24 changes: 24 additions & 0 deletions src/utils/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { setTimeout } from "node:timers/promises";

export interface RetryOptions {
attempts?: number;
interval?: number;
}

export async function retry<T>(
fn: () => Promise<T>,
options: RetryOptions = {},
): Promise<T> {
const { attempts = 3, interval = 200 } = options;
let err: any;
for (let i = 0; i < attempts; i++) {
try {
const result = await fn();
return result;
} catch (e) {
err = e;
await setTimeout(interval);
}
}
throw err ?? new Error("all attempts failed");
}

0 comments on commit 9f18c6b

Please sign in to comment.