@haven-fi/solauto-sdk 1.0.17 → 1.0.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -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;AAwCzD,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,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zB,uBAAuB;IAsCrC,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,iBAAiB,EAAE,KAAK,CAAC,EAAE,MAAM;IAgB9D,IAAI;CA8IX"}
@@ -15,12 +15,14 @@ class LookupTables {
15
15
  this.client = client;
16
16
  this.defaultLuts = [];
17
17
  this.cache = [];
18
- this.defaultLuts = [
19
- ...client.defaultLookupTables(),
20
- ];
18
+ this.defaultLuts = [...client.defaultLookupTables()];
21
19
  }
22
20
  async getLutInputs(additionalAddresses) {
23
- const addresses = [...this.defaultLuts, this.client.authorityLutAddress.toString(), ...additionalAddresses];
21
+ const addresses = [
22
+ ...this.defaultLuts,
23
+ this.client.authorityLutAddress.toString(),
24
+ ...additionalAddresses,
25
+ ];
24
26
  const currentCacheAddresses = this.cache.map((x) => x.publicKey.toString());
25
27
  const missingAddresses = addresses.filter((x) => !currentCacheAddresses.includes(x));
26
28
  if (missingAddresses) {
@@ -118,12 +120,13 @@ var TransactionStatus;
118
120
  TransactionStatus["Successful"] = "Successful";
119
121
  })(TransactionStatus = exports.TransactionStatus || (exports.TransactionStatus = {}));
120
122
  class TransactionsManager {
121
- constructor(client, items, statusCallback, simulateOnly, mustBeAtomic) {
123
+ constructor(client, items, statusCallback, simulateOnly, mustBeAtomic, errorsToThrow) {
122
124
  this.client = client;
123
125
  this.items = items;
124
126
  this.statusCallback = statusCallback;
125
127
  this.simulateOnly = simulateOnly;
126
128
  this.mustBeAtomic = mustBeAtomic;
129
+ this.errorsToThrow = errorsToThrow;
127
130
  this.statuses = [];
128
131
  this.lookupTables = new LookupTables(client);
129
132
  }
@@ -177,7 +180,7 @@ class TransactionsManager {
177
180
  const updateLookupTable = await this.client.updateLookupTable();
178
181
  if (updateLookupTable && updateLookupTable?.needsToBeIsolated) {
179
182
  this.updateStatus("update lookup table", TransactionStatus.Processing);
180
- 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, 150, this.errorsToThrow);
181
184
  this.updateStatus("update lookup table", TransactionStatus.Successful);
182
185
  }
183
186
  for (const item of this.items) {
@@ -253,7 +256,7 @@ class TransactionsManager {
253
256
  const txSig = await (0, solanaUtils_1.sendSingleOptimizedTransaction)(this.client.umi, this.client.connection, tx, this.simulateOnly, attemptNum);
254
257
  this.updateStatus(itemSet.name(), TransactionStatus.Successful, txSig ? bs58_1.default.encode(txSig) : undefined);
255
258
  }
256
- }, 3);
259
+ }, 3, 150, this.errorsToThrow);
257
260
  }
258
261
  }
259
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 declare function retryWithExponentialBackoff<T>(fn: (attemptNum: number) => Promise<T>, retries?: number, delay?: number, errorsToRetry?: Set<Function>): Promise<T>;
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,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,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,CAoCZ"}
@@ -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) || currentTime - solautoConstants_1.PRICES[mint.toString()].time > 3)) {
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,25 +75,35 @@ 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, errorsToRetry) {
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 (attemptNum < retries &&
91
- (!errorsToRetry || errorsToRetry.has(error.constructor))) {
91
+ console.log(errorsToThrow?.length);
92
+ if (errorsToThrow) {
93
+ for (const e of errorsToThrow) {
94
+ console.log(error instanceof e);
95
+ }
96
+ }
97
+ if (errorsToThrow &&
98
+ errorsToThrow.some((errorType) => error instanceof errorType)) {
99
+ reject(error);
100
+ return;
101
+ }
102
+ if (attemptNum < retries) {
92
103
  console.log(error);
93
104
  setTimeout(() => {
94
105
  console.log("Retrying...");
95
- attempt(attemptNum);
106
+ return attempt(attemptNum);
96
107
  }, delay);
97
108
  delay *= 2;
98
109
  }
@@ -101,7 +112,7 @@ function retryWithExponentialBackoff(fn, retries = 5, delay = 150, errorsToRetry
101
112
  }
102
113
  });
103
114
  };
104
- attempt(0);
115
+ return attempt(0);
105
116
  });
106
117
  }
107
118
  exports.retryWithExponentialBackoff = retryWithExponentialBackoff;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haven-fi/solauto-sdk",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "Typescript SDK for the Solauto program on the Solana blockchain",
@@ -9,7 +9,10 @@ import {
9
9
  getAdressLookupInputs,
10
10
  sendSingleOptimizedTransaction,
11
11
  } from "../utils/solanaUtils";
12
- import { retryWithExponentialBackoff } from "../utils/generalUtils";
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,15 +21,17 @@ class LookupTables {
18
21
  cache: AddressLookupTableInput[] = [];
19
22
 
20
23
  constructor(private client: SolautoClient) {
21
- this.defaultLuts = [
22
- ...client.defaultLookupTables(),
23
- ];
24
+ this.defaultLuts = [...client.defaultLookupTables()];
24
25
  }
25
26
 
26
27
  async getLutInputs(
27
28
  additionalAddresses: string[]
28
29
  ): Promise<AddressLookupTableInput[]> {
29
- const addresses = [...this.defaultLuts, this.client.authorityLutAddress!.toString(), ...additionalAddresses];
30
+ const addresses = [
31
+ ...this.defaultLuts,
32
+ this.client.authorityLutAddress!.toString(),
33
+ ...additionalAddresses,
34
+ ];
30
35
  const currentCacheAddresses = this.cache.map((x) => x.publicKey.toString());
31
36
 
32
37
  const missingAddresses = addresses.filter(
@@ -181,7 +186,8 @@ export class TransactionsManager {
181
186
  private items: TransactionItem[],
182
187
  private statusCallback?: (statuses: TransactionManagerStatuses) => void,
183
188
  private simulateOnly?: boolean,
184
- private mustBeAtomic?: boolean
189
+ private mustBeAtomic?: boolean,
190
+ private errorsToThrow?: ErrorsToThrow
185
191
  ) {
186
192
  this.lookupTables = new LookupTables(client);
187
193
  }
@@ -253,7 +259,9 @@ export class TransactionsManager {
253
259
  this.simulateOnly,
254
260
  attemptNum
255
261
  ),
256
- 2
262
+ 2,
263
+ 150,
264
+ this.errorsToThrow
257
265
  );
258
266
  this.updateStatus("update lookup table", TransactionStatus.Successful);
259
267
  }
@@ -343,31 +351,36 @@ export class TransactionsManager {
343
351
  };
344
352
 
345
353
  let itemSet = itemSets[i];
346
- await retryWithExponentialBackoff(async (attemptNum) => {
347
- itemSet =
348
- i > 0 || attemptNum > 0
349
- ? await getFreshItemSet(itemSet, attemptNum)
350
- : itemSet;
351
- const tx = await itemSet.getSingleTransaction();
352
-
353
- if (tx.getInstructions().length === 0) {
354
- this.updateStatus(itemSet.name(), TransactionStatus.Skipped);
355
- } else {
356
- this.updateStatus(itemSet.name(), TransactionStatus.Processing);
357
- const txSig = await sendSingleOptimizedTransaction(
358
- this.client.umi,
359
- this.client.connection,
360
- tx,
361
- this.simulateOnly,
362
- attemptNum
363
- );
364
- this.updateStatus(
365
- itemSet.name(),
366
- TransactionStatus.Successful,
367
- txSig ? bs58.encode(txSig) : undefined
368
- );
369
- }
370
- }, 3);
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
+ 150,
382
+ this.errorsToThrow
383
+ );
371
384
  }
372
385
  }
373
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(umi: Umi, pk: PublicKey): Promise<boolean> {
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 (!mints.some(mint => !(mint.toString() in PRICES) || currentTime - PRICES[mint.toString()].time > 3)) {
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
- errorsToRetry?: Set<Function>
105
+ errorsToThrow?: ErrorsToThrow
99
106
  ): Promise<T> {
100
107
  return new Promise((resolve, reject) => {
101
108
  const attempt = (attemptNum: number) => {
@@ -103,14 +110,26 @@ export function retryWithExponentialBackoff<T>(
103
110
  .then(resolve)
104
111
  .catch((error: Error) => {
105
112
  attemptNum++;
113
+
114
+ console.log(errorsToThrow?.length);
115
+ if (errorsToThrow) {
116
+ for (const e of errorsToThrow) {
117
+ console.log(error instanceof e);
118
+ }
119
+ }
106
120
  if (
107
- attemptNum < retries &&
108
- (!errorsToRetry || errorsToRetry.has(error.constructor))
121
+ errorsToThrow &&
122
+ errorsToThrow.some((errorType) => error instanceof errorType)
109
123
  ) {
124
+ reject(error);
125
+ return;
126
+ }
127
+
128
+ if (attemptNum < retries) {
110
129
  console.log(error);
111
130
  setTimeout(() => {
112
131
  console.log("Retrying...");
113
- attempt(attemptNum);
132
+ return attempt(attemptNum);
114
133
  }, delay);
115
134
  delay *= 2;
116
135
  } else {
@@ -118,6 +137,6 @@ export function retryWithExponentialBackoff<T>(
118
137
  }
119
138
  });
120
139
  };
121
- attempt(0);
140
+ return attempt(0);
122
141
  });
123
142
  }