@gearbox-protocol/sdk 9.1.5 → 9.1.7

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.
@@ -33,6 +33,8 @@ class RevolverTransport {
33
33
  #transports;
34
34
  #index = 0;
35
35
  #config;
36
+ #rotating = false;
37
+ #requests = /* @__PURE__ */ new WeakMap();
36
38
  overrides;
37
39
  /**
38
40
  * Create a new RevolverTransport
@@ -98,16 +100,25 @@ class RevolverTransport {
98
100
  }
99
101
  do {
100
102
  try {
103
+ this.#requests.set(r, this.currentTransportName);
101
104
  const resp = await this.#transport({ ...this.overrides }).request(r);
105
+ this.#requests.delete(r);
102
106
  return resp;
103
107
  } catch (e) {
104
108
  if (e instanceof import_viem.RpcError || e instanceof import_viem.HttpRequestError) {
105
- await this.rotate(e);
109
+ const reqTransport = this.#requests.get(r);
110
+ if (reqTransport === this.currentTransportName) {
111
+ await this.rotate(e);
112
+ } else {
113
+ this.#logger?.debug("request made with old transport, try again");
114
+ }
106
115
  } else {
116
+ this.#requests.delete(r);
107
117
  throw e;
108
118
  }
109
119
  }
110
120
  } while (this.#hasAvailableTransports);
121
+ this.#requests.delete(r);
111
122
  throw new NoAvailableTransportsError();
112
123
  };
113
124
  get config() {
@@ -131,6 +142,11 @@ class RevolverTransport {
131
142
  if (this.#transports.length === 1) {
132
143
  return true;
133
144
  }
145
+ if (this.#rotating) {
146
+ this.#logger?.debug("already rotating, skipping");
147
+ return false;
148
+ }
149
+ this.#rotating = true;
134
150
  this.#logger?.debug(
135
151
  {
136
152
  reason,
@@ -154,11 +170,15 @@ class RevolverTransport {
154
170
  },
155
171
  "switched to next transport"
156
172
  );
157
- await this.#config.onRotateSuccess?.(
158
- oldTransportName,
159
- this.currentTransportName,
160
- reason
161
- );
173
+ try {
174
+ await this.#config.onRotateSuccess?.(
175
+ oldTransportName,
176
+ this.currentTransportName,
177
+ reason
178
+ );
179
+ } catch {
180
+ }
181
+ this.#rotating = false;
162
182
  return true;
163
183
  } else {
164
184
  this.#logger?.warn(
@@ -171,7 +191,11 @@ class RevolverTransport {
171
191
  );
172
192
  }
173
193
  }
174
- await this.#config.onRotateFailed?.(oldTransportName, reason);
194
+ try {
195
+ await this.#config.onRotateFailed?.(oldTransportName, reason);
196
+ } catch {
197
+ }
198
+ this.#rotating = false;
175
199
  return false;
176
200
  }
177
201
  get currentTransportName() {
@@ -16,6 +16,8 @@ class RevolverTransport {
16
16
  #transports;
17
17
  #index = 0;
18
18
  #config;
19
+ #rotating = false;
20
+ #requests = /* @__PURE__ */ new WeakMap();
19
21
  overrides;
20
22
  /**
21
23
  * Create a new RevolverTransport
@@ -81,16 +83,25 @@ class RevolverTransport {
81
83
  }
82
84
  do {
83
85
  try {
86
+ this.#requests.set(r, this.currentTransportName);
84
87
  const resp = await this.#transport({ ...this.overrides }).request(r);
88
+ this.#requests.delete(r);
85
89
  return resp;
86
90
  } catch (e) {
87
91
  if (e instanceof RpcError || e instanceof HttpRequestError) {
88
- await this.rotate(e);
92
+ const reqTransport = this.#requests.get(r);
93
+ if (reqTransport === this.currentTransportName) {
94
+ await this.rotate(e);
95
+ } else {
96
+ this.#logger?.debug("request made with old transport, try again");
97
+ }
89
98
  } else {
99
+ this.#requests.delete(r);
90
100
  throw e;
91
101
  }
92
102
  }
93
103
  } while (this.#hasAvailableTransports);
104
+ this.#requests.delete(r);
94
105
  throw new NoAvailableTransportsError();
95
106
  };
96
107
  get config() {
@@ -114,6 +125,11 @@ class RevolverTransport {
114
125
  if (this.#transports.length === 1) {
115
126
  return true;
116
127
  }
128
+ if (this.#rotating) {
129
+ this.#logger?.debug("already rotating, skipping");
130
+ return false;
131
+ }
132
+ this.#rotating = true;
117
133
  this.#logger?.debug(
118
134
  {
119
135
  reason,
@@ -137,11 +153,15 @@ class RevolverTransport {
137
153
  },
138
154
  "switched to next transport"
139
155
  );
140
- await this.#config.onRotateSuccess?.(
141
- oldTransportName,
142
- this.currentTransportName,
143
- reason
144
- );
156
+ try {
157
+ await this.#config.onRotateSuccess?.(
158
+ oldTransportName,
159
+ this.currentTransportName,
160
+ reason
161
+ );
162
+ } catch {
163
+ }
164
+ this.#rotating = false;
145
165
  return true;
146
166
  } else {
147
167
  this.#logger?.warn(
@@ -154,7 +174,11 @@ class RevolverTransport {
154
174
  );
155
175
  }
156
176
  }
157
- await this.#config.onRotateFailed?.(oldTransportName, reason);
177
+ try {
178
+ await this.#config.onRotateFailed?.(oldTransportName, reason);
179
+ } catch {
180
+ }
181
+ this.#rotating = false;
158
182
  return false;
159
183
  }
160
184
  get currentTransportName() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gearbox-protocol/sdk",
3
- "version": "9.1.5",
3
+ "version": "9.1.7",
4
4
  "description": "Gearbox SDK",
5
5
  "license": "MIT",
6
6
  "main": "./dist/cjs/sdk/index.js",