@haven-fi/solauto-sdk 1.0.16 → 1.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/transactions/transactionsManager.d.ts +3 -1
- package/dist/transactions/transactionsManager.d.ts.map +1 -1
- package/dist/transactions/transactionsManager.js +10 -8
- package/dist/utils/generalUtils.d.ts +2 -1
- package/dist/utils/generalUtils.d.ts.map +1 -1
- package/dist/utils/generalUtils.js +11 -6
- package/package.json +1 -1
- package/src/transactions/transactionsManager.ts +45 -33
- package/src/utils/generalUtils.ts +25 -12
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TransactionBuilder } from "@metaplex-foundation/umi";
|
|
2
2
|
import { SolautoClient } from "../clients/solautoClient";
|
|
3
|
+
import { ErrorsToThrow } from "../utils/generalUtils";
|
|
3
4
|
export declare class TransactionItem {
|
|
4
5
|
fetchTx: (attemptNum: number) => Promise<{
|
|
5
6
|
tx: TransactionBuilder;
|
|
@@ -33,9 +34,10 @@ export declare class TransactionsManager {
|
|
|
33
34
|
private statusCallback?;
|
|
34
35
|
private simulateOnly?;
|
|
35
36
|
private mustBeAtomic?;
|
|
37
|
+
private errorsToThrow?;
|
|
36
38
|
private statuses;
|
|
37
39
|
private lookupTables;
|
|
38
|
-
constructor(client: SolautoClient, items: TransactionItem[], statusCallback?: ((statuses: TransactionManagerStatuses) => void) | undefined, simulateOnly?: boolean | undefined, mustBeAtomic?: boolean | undefined);
|
|
40
|
+
constructor(client: SolautoClient, items: TransactionItem[], statusCallback?: ((statuses: TransactionManagerStatuses) => void) | undefined, simulateOnly?: boolean | undefined, mustBeAtomic?: boolean | undefined, errorsToThrow?: ErrorsToThrow | undefined);
|
|
39
41
|
private assembleTransactionSets;
|
|
40
42
|
updateStatus(name: string, status: TransactionStatus, txSig?: string): void;
|
|
41
43
|
send(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionsManager.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,kBAAkB,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionsManager.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,kBAAkB,EACnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAKzD,OAAO,EACL,aAAa,EAEd,MAAM,uBAAuB,CAAC;AAqC/B,qBAAa,eAAe;IAKjB,OAAO,EAAE,CACd,UAAU,EAAE,MAAM,KACf,OAAO,CACV;QAAE,EAAE,EAAE,kBAAkB,CAAC;QAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,SAAS,CACxE;IACM,IAAI,CAAC;IATd,oBAAoB,EAAG,MAAM,EAAE,CAAC;IAChC,EAAE,CAAC,EAAE,kBAAkB,CAAC;gBAGf,OAAO,EAAE,CACd,UAAU,EAAE,MAAM,KACf,OAAO,CACV;QAAE,EAAE,EAAE,kBAAkB,CAAC;QAAC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,SAAS,CACxE,EACM,IAAI,CAAC,oBAAQ;IAGhB,UAAU;IAIV,OAAO,CAAC,UAAU,EAAE,MAAM;IAMhC,cAAc,IAAI,MAAM,EAAE;CAY3B;AAgFD,oBAAY,iBAAiB;IAC3B,OAAO,YAAY;IACnB,UAAU,eAAe;IACzB,MAAM,WAAW;IACjB,UAAU,eAAe;CAC1B;AAED,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,iBAAiB,CAAC;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,EAAE,CAAC;AAEJ,qBAAa,mBAAmB;IAK5B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,cAAc,CAAC;IACvB,OAAO,CAAC,YAAY,CAAC;IACrB,OAAO,CAAC,YAAY,CAAC;IACrB,OAAO,CAAC,aAAa,CAAC;IATxB,OAAO,CAAC,QAAQ,CAAkC;IAClD,OAAO,CAAC,YAAY,CAAe;gBAGzB,MAAM,EAAE,aAAa,EACrB,KAAK,EAAE,eAAe,EAAE,EACxB,cAAc,CAAC,6CAA4C,IAAI,aAAA,EAC/D,YAAY,CAAC,qBAAS,EACtB,YAAY,CAAC,qBAAS,EACtB,aAAa,CAAC,2BAAe;YAKzB,uBAAuB;IAsCrC,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,iBAAiB,EAAE,KAAK,CAAC,EAAE,MAAM;IAgB9D,IAAI;CA8IX"}
|
|
@@ -15,13 +15,14 @@ class LookupTables {
|
|
|
15
15
|
this.client = client;
|
|
16
16
|
this.defaultLuts = [];
|
|
17
17
|
this.cache = [];
|
|
18
|
-
this.defaultLuts = [
|
|
19
|
-
client.authorityLutAddress.toString(),
|
|
20
|
-
...client.defaultLookupTables(),
|
|
21
|
-
];
|
|
18
|
+
this.defaultLuts = [...client.defaultLookupTables()];
|
|
22
19
|
}
|
|
23
20
|
async getLutInputs(additionalAddresses) {
|
|
24
|
-
const addresses = [
|
|
21
|
+
const addresses = [
|
|
22
|
+
...this.defaultLuts,
|
|
23
|
+
this.client.authorityLutAddress.toString(),
|
|
24
|
+
...additionalAddresses,
|
|
25
|
+
];
|
|
25
26
|
const currentCacheAddresses = this.cache.map((x) => x.publicKey.toString());
|
|
26
27
|
const missingAddresses = addresses.filter((x) => !currentCacheAddresses.includes(x));
|
|
27
28
|
if (missingAddresses) {
|
|
@@ -119,12 +120,13 @@ var TransactionStatus;
|
|
|
119
120
|
TransactionStatus["Successful"] = "Successful";
|
|
120
121
|
})(TransactionStatus = exports.TransactionStatus || (exports.TransactionStatus = {}));
|
|
121
122
|
class TransactionsManager {
|
|
122
|
-
constructor(client, items, statusCallback, simulateOnly, mustBeAtomic) {
|
|
123
|
+
constructor(client, items, statusCallback, simulateOnly, mustBeAtomic, errorsToThrow) {
|
|
123
124
|
this.client = client;
|
|
124
125
|
this.items = items;
|
|
125
126
|
this.statusCallback = statusCallback;
|
|
126
127
|
this.simulateOnly = simulateOnly;
|
|
127
128
|
this.mustBeAtomic = mustBeAtomic;
|
|
129
|
+
this.errorsToThrow = errorsToThrow;
|
|
128
130
|
this.statuses = [];
|
|
129
131
|
this.lookupTables = new LookupTables(client);
|
|
130
132
|
}
|
|
@@ -178,7 +180,7 @@ class TransactionsManager {
|
|
|
178
180
|
const updateLookupTable = await this.client.updateLookupTable();
|
|
179
181
|
if (updateLookupTable && updateLookupTable?.needsToBeIsolated) {
|
|
180
182
|
this.updateStatus("update lookup table", TransactionStatus.Processing);
|
|
181
|
-
await (0, generalUtils_1.retryWithExponentialBackoff)(async (attemptNum) => await (0, solanaUtils_1.sendSingleOptimizedTransaction)(this.client.umi, this.client.connection, updateLookupTable.updateLutTx, this.simulateOnly, attemptNum), 2);
|
|
183
|
+
await (0, generalUtils_1.retryWithExponentialBackoff)(async (attemptNum) => await (0, solanaUtils_1.sendSingleOptimizedTransaction)(this.client.umi, this.client.connection, updateLookupTable.updateLutTx, this.simulateOnly, attemptNum), 2, undefined, this.errorsToThrow);
|
|
182
184
|
this.updateStatus("update lookup table", TransactionStatus.Successful);
|
|
183
185
|
}
|
|
184
186
|
for (const item of this.items) {
|
|
@@ -254,7 +256,7 @@ class TransactionsManager {
|
|
|
254
256
|
const txSig = await (0, solanaUtils_1.sendSingleOptimizedTransaction)(this.client.umi, this.client.connection, tx, this.simulateOnly, attemptNum);
|
|
255
257
|
this.updateStatus(itemSet.name(), TransactionStatus.Successful, txSig ? bs58_1.default.encode(txSig) : undefined);
|
|
256
258
|
}
|
|
257
|
-
}, 3);
|
|
259
|
+
}, 3, undefined, this.errorsToThrow);
|
|
258
260
|
}
|
|
259
261
|
}
|
|
260
262
|
if (!this.simulateOnly) {
|
|
@@ -7,5 +7,6 @@ export declare function getSolanaAccountCreated(umi: Umi, pk: PublicKey): Promis
|
|
|
7
7
|
export declare function rpcAccountCreated(account: MaybeRpcAccount): boolean;
|
|
8
8
|
export declare function arraysAreEqual(arrayA: number[], arrayB: number[]): boolean;
|
|
9
9
|
export declare function getTokenPrices(mints: PublicKey[]): Promise<number[]>;
|
|
10
|
-
export
|
|
10
|
+
export type ErrorsToThrow = Array<new (...args: any[]) => Error>;
|
|
11
|
+
export declare function retryWithExponentialBackoff<T>(fn: (attemptNum: number) => Promise<T>, retries?: number, delay?: number, errorsToThrow?: ErrorsToThrow): Promise<T>;
|
|
11
12
|
//# sourceMappingURL=generalUtils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generalUtils.d.ts","sourceRoot":"","sources":["../../src/utils/generalUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAK3E,wBAAgB,gBAAgB,IAAI,MAAM,CAEzC;AAED,wBAAgB,iBAAiB,IAAI,MAAM,CAO1C;AAED,wBAAgB,kBAAkB,IAAI,MAAM,CAE3C;AAED,wBAAsB,uBAAuB,
|
|
1
|
+
{"version":3,"file":"generalUtils.d.ts","sourceRoot":"","sources":["../../src/utils/generalUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAK3E,wBAAgB,gBAAgB,IAAI,MAAM,CAEzC;AAED,wBAAgB,iBAAiB,IAAI,MAAM,CAO1C;AAED,wBAAgB,kBAAkB,IAAI,MAAM,CAE3C;AAED,wBAAsB,uBAAuB,CAC3C,GAAG,EAAE,GAAG,EACR,EAAE,EAAE,SAAS,GACZ,OAAO,CAAC,OAAO,CAAC,CAGlB;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAEnE;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAU1E;AAED,wBAAsB,cAAc,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAiD1E;AAED,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,KAAK,CAAC,CAAC;AAEjE,wBAAgB,2BAA2B,CAAC,CAAC,EAC3C,EAAE,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,EACtC,OAAO,GAAE,MAAU,EACnB,KAAK,GAAE,MAAY,EACnB,aAAa,CAAC,EAAE,aAAa,GAC5B,OAAO,CAAC,CAAC,CAAC,CA8BZ"}
|
|
@@ -45,9 +45,10 @@ function arraysAreEqual(arrayA, arrayB) {
|
|
|
45
45
|
exports.arraysAreEqual = arraysAreEqual;
|
|
46
46
|
async function getTokenPrices(mints) {
|
|
47
47
|
const currentTime = currentUnixSeconds();
|
|
48
|
-
if (!mints.some(mint => !(mint.toString() in solautoConstants_1.PRICES) ||
|
|
48
|
+
if (!mints.some((mint) => !(mint.toString() in solautoConstants_1.PRICES) ||
|
|
49
|
+
currentTime - solautoConstants_1.PRICES[mint.toString()].time > 3)) {
|
|
49
50
|
console.log("Accessing price cache");
|
|
50
|
-
return mints.map(mint => solautoConstants_1.PRICES[mint.toString()].price);
|
|
51
|
+
return mints.map((mint) => solautoConstants_1.PRICES[mint.toString()].price);
|
|
51
52
|
}
|
|
52
53
|
console.log("Fetching prices...");
|
|
53
54
|
const priceFeedIds = mints.map((mint) => pythConstants_1.PYTH_PRICE_FEED_IDS[mint.toString()]);
|
|
@@ -74,21 +75,25 @@ async function getTokenPrices(mints) {
|
|
|
74
75
|
for (var i = 0; i < mints.length; i++) {
|
|
75
76
|
solautoConstants_1.PRICES[mints[i].toString()] = {
|
|
76
77
|
price: prices[i],
|
|
77
|
-
time: currentUnixSeconds()
|
|
78
|
+
time: currentUnixSeconds(),
|
|
78
79
|
};
|
|
79
80
|
}
|
|
80
81
|
return prices;
|
|
81
82
|
}
|
|
82
83
|
exports.getTokenPrices = getTokenPrices;
|
|
83
|
-
function retryWithExponentialBackoff(fn, retries = 5, delay = 150,
|
|
84
|
+
function retryWithExponentialBackoff(fn, retries = 5, delay = 150, errorsToThrow) {
|
|
84
85
|
return new Promise((resolve, reject) => {
|
|
85
86
|
const attempt = (attemptNum) => {
|
|
86
87
|
fn(attemptNum)
|
|
87
88
|
.then(resolve)
|
|
88
89
|
.catch((error) => {
|
|
89
90
|
attemptNum++;
|
|
90
|
-
if (
|
|
91
|
-
(
|
|
91
|
+
if (errorsToThrow &&
|
|
92
|
+
errorsToThrow.some((errorType) => error instanceof errorType)) {
|
|
93
|
+
reject(error);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (attemptNum < retries) {
|
|
92
97
|
console.log(error);
|
|
93
98
|
setTimeout(() => {
|
|
94
99
|
console.log("Retrying...");
|
package/package.json
CHANGED
|
@@ -9,7 +9,10 @@ import {
|
|
|
9
9
|
getAdressLookupInputs,
|
|
10
10
|
sendSingleOptimizedTransaction,
|
|
11
11
|
} from "../utils/solanaUtils";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
ErrorsToThrow,
|
|
14
|
+
retryWithExponentialBackoff,
|
|
15
|
+
} from "../utils/generalUtils";
|
|
13
16
|
import { getTransactionChores } from "./transactionUtils";
|
|
14
17
|
// import { sendJitoBundledTransactions } from "../utils/jitoUtils";
|
|
15
18
|
|
|
@@ -18,16 +21,17 @@ class LookupTables {
|
|
|
18
21
|
cache: AddressLookupTableInput[] = [];
|
|
19
22
|
|
|
20
23
|
constructor(private client: SolautoClient) {
|
|
21
|
-
this.defaultLuts = [
|
|
22
|
-
client.authorityLutAddress!.toString(),
|
|
23
|
-
...client.defaultLookupTables(),
|
|
24
|
-
];
|
|
24
|
+
this.defaultLuts = [...client.defaultLookupTables()];
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
async getLutInputs(
|
|
28
28
|
additionalAddresses: string[]
|
|
29
29
|
): Promise<AddressLookupTableInput[]> {
|
|
30
|
-
const addresses = [
|
|
30
|
+
const addresses = [
|
|
31
|
+
...this.defaultLuts,
|
|
32
|
+
this.client.authorityLutAddress!.toString(),
|
|
33
|
+
...additionalAddresses,
|
|
34
|
+
];
|
|
31
35
|
const currentCacheAddresses = this.cache.map((x) => x.publicKey.toString());
|
|
32
36
|
|
|
33
37
|
const missingAddresses = addresses.filter(
|
|
@@ -182,7 +186,8 @@ export class TransactionsManager {
|
|
|
182
186
|
private items: TransactionItem[],
|
|
183
187
|
private statusCallback?: (statuses: TransactionManagerStatuses) => void,
|
|
184
188
|
private simulateOnly?: boolean,
|
|
185
|
-
private mustBeAtomic?: boolean
|
|
189
|
+
private mustBeAtomic?: boolean,
|
|
190
|
+
private errorsToThrow?: ErrorsToThrow
|
|
186
191
|
) {
|
|
187
192
|
this.lookupTables = new LookupTables(client);
|
|
188
193
|
}
|
|
@@ -254,7 +259,9 @@ export class TransactionsManager {
|
|
|
254
259
|
this.simulateOnly,
|
|
255
260
|
attemptNum
|
|
256
261
|
),
|
|
257
|
-
2
|
|
262
|
+
2,
|
|
263
|
+
undefined,
|
|
264
|
+
this.errorsToThrow
|
|
258
265
|
);
|
|
259
266
|
this.updateStatus("update lookup table", TransactionStatus.Successful);
|
|
260
267
|
}
|
|
@@ -344,31 +351,36 @@ export class TransactionsManager {
|
|
|
344
351
|
};
|
|
345
352
|
|
|
346
353
|
let itemSet = itemSets[i];
|
|
347
|
-
await retryWithExponentialBackoff(
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
354
|
+
await retryWithExponentialBackoff(
|
|
355
|
+
async (attemptNum) => {
|
|
356
|
+
itemSet =
|
|
357
|
+
i > 0 || attemptNum > 0
|
|
358
|
+
? await getFreshItemSet(itemSet, attemptNum)
|
|
359
|
+
: itemSet;
|
|
360
|
+
const tx = await itemSet.getSingleTransaction();
|
|
361
|
+
|
|
362
|
+
if (tx.getInstructions().length === 0) {
|
|
363
|
+
this.updateStatus(itemSet.name(), TransactionStatus.Skipped);
|
|
364
|
+
} else {
|
|
365
|
+
this.updateStatus(itemSet.name(), TransactionStatus.Processing);
|
|
366
|
+
const txSig = await sendSingleOptimizedTransaction(
|
|
367
|
+
this.client.umi,
|
|
368
|
+
this.client.connection,
|
|
369
|
+
tx,
|
|
370
|
+
this.simulateOnly,
|
|
371
|
+
attemptNum
|
|
372
|
+
);
|
|
373
|
+
this.updateStatus(
|
|
374
|
+
itemSet.name(),
|
|
375
|
+
TransactionStatus.Successful,
|
|
376
|
+
txSig ? bs58.encode(txSig) : undefined
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
3,
|
|
381
|
+
undefined,
|
|
382
|
+
this.errorsToThrow
|
|
383
|
+
);
|
|
372
384
|
}
|
|
373
385
|
}
|
|
374
386
|
|
|
@@ -21,7 +21,10 @@ export function currentUnixSeconds(): number {
|
|
|
21
21
|
return Math.round(new Date().getTime() / 1000);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export async function getSolanaAccountCreated(
|
|
24
|
+
export async function getSolanaAccountCreated(
|
|
25
|
+
umi: Umi,
|
|
26
|
+
pk: PublicKey
|
|
27
|
+
): Promise<boolean> {
|
|
25
28
|
const account = await umi.rpc.getAccount(publicKey(pk));
|
|
26
29
|
return rpcAccountCreated(account);
|
|
27
30
|
}
|
|
@@ -44,9 +47,15 @@ export function arraysAreEqual(arrayA: number[], arrayB: number[]): boolean {
|
|
|
44
47
|
|
|
45
48
|
export async function getTokenPrices(mints: PublicKey[]): Promise<number[]> {
|
|
46
49
|
const currentTime = currentUnixSeconds();
|
|
47
|
-
if (
|
|
50
|
+
if (
|
|
51
|
+
!mints.some(
|
|
52
|
+
(mint) =>
|
|
53
|
+
!(mint.toString() in PRICES) ||
|
|
54
|
+
currentTime - PRICES[mint.toString()].time > 3
|
|
55
|
+
)
|
|
56
|
+
) {
|
|
48
57
|
console.log("Accessing price cache");
|
|
49
|
-
return mints.map(mint => PRICES[mint.toString()].price);
|
|
58
|
+
return mints.map((mint) => PRICES[mint.toString()].price);
|
|
50
59
|
}
|
|
51
60
|
|
|
52
61
|
console.log("Fetching prices...");
|
|
@@ -71,10 +80,7 @@ export async function getTokenPrices(mints: PublicKey[]): Promise<number[]> {
|
|
|
71
80
|
if (x.price.expo > 0) {
|
|
72
81
|
return Number(toBaseUnit(Number(x.price.price), x.price.expo));
|
|
73
82
|
} else if (x.price.expo < 0) {
|
|
74
|
-
return fromBaseUnit(
|
|
75
|
-
BigInt(x.price.price),
|
|
76
|
-
Math.abs(x.price.expo)
|
|
77
|
-
);
|
|
83
|
+
return fromBaseUnit(BigInt(x.price.price), Math.abs(x.price.expo));
|
|
78
84
|
} else {
|
|
79
85
|
return Number(x.price.price);
|
|
80
86
|
}
|
|
@@ -83,19 +89,20 @@ export async function getTokenPrices(mints: PublicKey[]): Promise<number[]> {
|
|
|
83
89
|
for (var i = 0; i < mints.length; i++) {
|
|
84
90
|
PRICES[mints[i].toString()] = {
|
|
85
91
|
price: prices[i],
|
|
86
|
-
time: currentUnixSeconds()
|
|
92
|
+
time: currentUnixSeconds(),
|
|
87
93
|
};
|
|
88
94
|
}
|
|
89
95
|
|
|
90
|
-
|
|
91
96
|
return prices;
|
|
92
97
|
}
|
|
93
98
|
|
|
99
|
+
export type ErrorsToThrow = Array<new (...args: any[]) => Error>;
|
|
100
|
+
|
|
94
101
|
export function retryWithExponentialBackoff<T>(
|
|
95
102
|
fn: (attemptNum: number) => Promise<T>,
|
|
96
103
|
retries: number = 5,
|
|
97
104
|
delay: number = 150,
|
|
98
|
-
|
|
105
|
+
errorsToThrow?: ErrorsToThrow
|
|
99
106
|
): Promise<T> {
|
|
100
107
|
return new Promise((resolve, reject) => {
|
|
101
108
|
const attempt = (attemptNum: number) => {
|
|
@@ -103,10 +110,16 @@ export function retryWithExponentialBackoff<T>(
|
|
|
103
110
|
.then(resolve)
|
|
104
111
|
.catch((error: Error) => {
|
|
105
112
|
attemptNum++;
|
|
113
|
+
|
|
106
114
|
if (
|
|
107
|
-
|
|
108
|
-
(
|
|
115
|
+
errorsToThrow &&
|
|
116
|
+
errorsToThrow.some((errorType) => error instanceof errorType)
|
|
109
117
|
) {
|
|
118
|
+
reject(error);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (attemptNum < retries) {
|
|
110
123
|
console.log(error);
|
|
111
124
|
setTimeout(() => {
|
|
112
125
|
console.log("Retrying...");
|