Skip to content

Commit

Permalink
fix: long retry for redstone in historic mode
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Feb 5, 2025
1 parent 848fa4b commit b8689f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/sdk/market/pricefeeds/RedstoneUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { encodeAbiParameters, toBytes } from "viem";
import { SDKConstruct } from "../../base";
import type { GearboxSDK } from "../../GearboxSDK";
import type { ILogger, RawTx } from "../../types";
import { childLogger } from "../../utils";
import { childLogger, retry } from "../../utils";
import type { RedstonePriceFeedContract } from "./RedstonePriceFeed";

interface TimestampedCalldata {
Expand Down Expand Up @@ -194,7 +194,10 @@ export class RedstoneUpdater extends SDKConstruct {
urls: this.#gateways,
});

const dataPayload = await wrapper.prepareRedstonePayload(true);
const dataPayload = await retry(
() => wrapper.prepareRedstonePayload(true),
{ attempts: 5, interval: this.#historicalTimestampMs ? 30_500 : 250 },
);

const parsed = RedstonePayload.parse(toBytes(`0x${dataPayload}`));
const packagesByDataFeedId = groupDataPackages(parsed.signedDataPackages);
Expand Down
1 change: 1 addition & 0 deletions src/sdk/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from "./filterDust";
export * from "./formatter";
export * from "./json";
export * from "./mappers";
export * from "./retry";
24 changes: 24 additions & 0 deletions src/sdk/utils/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 cause: any;
for (let i = 0; i < attempts; i++) {
try {
const result = await fn();
return result;
} catch (e) {
cause = e;
await new Promise(resolve => {
setTimeout(resolve, interval);
});
}
}
throw new Error(`all attempts failed: ${cause}`);
}

0 comments on commit b8689f8

Please # to comment.