@gearbox-protocol/sdk 9.1.6 → 9.1.8
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.
|
@@ -34,6 +34,7 @@ class RevolverTransport {
|
|
|
34
34
|
#index = 0;
|
|
35
35
|
#config;
|
|
36
36
|
#rotating = false;
|
|
37
|
+
#requests = /* @__PURE__ */ new WeakMap();
|
|
37
38
|
overrides;
|
|
38
39
|
/**
|
|
39
40
|
* Create a new RevolverTransport
|
|
@@ -90,7 +91,7 @@ class RevolverTransport {
|
|
|
90
91
|
get value() {
|
|
91
92
|
return {
|
|
92
93
|
rotate: (reason) => this.rotate(reason),
|
|
93
|
-
currentTransportName: this.currentTransportName
|
|
94
|
+
currentTransportName: () => this.currentTransportName()
|
|
94
95
|
};
|
|
95
96
|
}
|
|
96
97
|
request = async (r) => {
|
|
@@ -99,16 +100,25 @@ class RevolverTransport {
|
|
|
99
100
|
}
|
|
100
101
|
do {
|
|
101
102
|
try {
|
|
103
|
+
this.#requests.set(r, this.currentTransportName);
|
|
102
104
|
const resp = await this.#transport({ ...this.overrides }).request(r);
|
|
105
|
+
this.#requests.delete(r);
|
|
103
106
|
return resp;
|
|
104
107
|
} catch (e) {
|
|
105
108
|
if (e instanceof import_viem.RpcError || e instanceof import_viem.HttpRequestError) {
|
|
106
|
-
|
|
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
|
+
}
|
|
107
115
|
} else {
|
|
116
|
+
this.#requests.delete(r);
|
|
108
117
|
throw e;
|
|
109
118
|
}
|
|
110
119
|
}
|
|
111
120
|
} while (this.#hasAvailableTransports);
|
|
121
|
+
this.#requests.delete(r);
|
|
112
122
|
throw new NoAvailableTransportsError();
|
|
113
123
|
};
|
|
114
124
|
get config() {
|
|
@@ -146,7 +156,7 @@ class RevolverTransport {
|
|
|
146
156
|
},
|
|
147
157
|
"rotating transport"
|
|
148
158
|
);
|
|
149
|
-
const oldTransportName = this.currentTransportName;
|
|
159
|
+
const oldTransportName = this.currentTransportName();
|
|
150
160
|
const now = Date.now();
|
|
151
161
|
this.#transports[this.#index].cooldown = now + (this.#config.cooldown ?? 6e4);
|
|
152
162
|
for (let i = 0; i < this.#transports.length - 1; i++) {
|
|
@@ -163,7 +173,7 @@ class RevolverTransport {
|
|
|
163
173
|
try {
|
|
164
174
|
await this.#config.onRotateSuccess?.(
|
|
165
175
|
oldTransportName,
|
|
166
|
-
this.currentTransportName,
|
|
176
|
+
this.currentTransportName(),
|
|
167
177
|
reason
|
|
168
178
|
);
|
|
169
179
|
} catch {
|
|
@@ -188,7 +198,7 @@ class RevolverTransport {
|
|
|
188
198
|
this.#rotating = false;
|
|
189
199
|
return false;
|
|
190
200
|
}
|
|
191
|
-
|
|
201
|
+
currentTransportName() {
|
|
192
202
|
return this.#transport({}).config.name;
|
|
193
203
|
}
|
|
194
204
|
get #logger() {
|
|
@@ -17,6 +17,7 @@ class RevolverTransport {
|
|
|
17
17
|
#index = 0;
|
|
18
18
|
#config;
|
|
19
19
|
#rotating = false;
|
|
20
|
+
#requests = /* @__PURE__ */ new WeakMap();
|
|
20
21
|
overrides;
|
|
21
22
|
/**
|
|
22
23
|
* Create a new RevolverTransport
|
|
@@ -73,7 +74,7 @@ class RevolverTransport {
|
|
|
73
74
|
get value() {
|
|
74
75
|
return {
|
|
75
76
|
rotate: (reason) => this.rotate(reason),
|
|
76
|
-
currentTransportName: this.currentTransportName
|
|
77
|
+
currentTransportName: () => this.currentTransportName()
|
|
77
78
|
};
|
|
78
79
|
}
|
|
79
80
|
request = async (r) => {
|
|
@@ -82,16 +83,25 @@ class RevolverTransport {
|
|
|
82
83
|
}
|
|
83
84
|
do {
|
|
84
85
|
try {
|
|
86
|
+
this.#requests.set(r, this.currentTransportName);
|
|
85
87
|
const resp = await this.#transport({ ...this.overrides }).request(r);
|
|
88
|
+
this.#requests.delete(r);
|
|
86
89
|
return resp;
|
|
87
90
|
} catch (e) {
|
|
88
91
|
if (e instanceof RpcError || e instanceof HttpRequestError) {
|
|
89
|
-
|
|
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
|
+
}
|
|
90
98
|
} else {
|
|
99
|
+
this.#requests.delete(r);
|
|
91
100
|
throw e;
|
|
92
101
|
}
|
|
93
102
|
}
|
|
94
103
|
} while (this.#hasAvailableTransports);
|
|
104
|
+
this.#requests.delete(r);
|
|
95
105
|
throw new NoAvailableTransportsError();
|
|
96
106
|
};
|
|
97
107
|
get config() {
|
|
@@ -129,7 +139,7 @@ class RevolverTransport {
|
|
|
129
139
|
},
|
|
130
140
|
"rotating transport"
|
|
131
141
|
);
|
|
132
|
-
const oldTransportName = this.currentTransportName;
|
|
142
|
+
const oldTransportName = this.currentTransportName();
|
|
133
143
|
const now = Date.now();
|
|
134
144
|
this.#transports[this.#index].cooldown = now + (this.#config.cooldown ?? 6e4);
|
|
135
145
|
for (let i = 0; i < this.#transports.length - 1; i++) {
|
|
@@ -146,7 +156,7 @@ class RevolverTransport {
|
|
|
146
156
|
try {
|
|
147
157
|
await this.#config.onRotateSuccess?.(
|
|
148
158
|
oldTransportName,
|
|
149
|
-
this.currentTransportName,
|
|
159
|
+
this.currentTransportName(),
|
|
150
160
|
reason
|
|
151
161
|
);
|
|
152
162
|
} catch {
|
|
@@ -171,7 +181,7 @@ class RevolverTransport {
|
|
|
171
181
|
this.#rotating = false;
|
|
172
182
|
return false;
|
|
173
183
|
}
|
|
174
|
-
|
|
184
|
+
currentTransportName() {
|
|
175
185
|
return this.#transport({}).config.name;
|
|
176
186
|
}
|
|
177
187
|
get #logger() {
|
|
@@ -50,9 +50,9 @@ export interface RevolverTransportValue {
|
|
|
50
50
|
*/
|
|
51
51
|
rotate: (reason?: BaseError) => Promise<boolean>;
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
53
|
+
* Returns the name of the current transport
|
|
54
54
|
*/
|
|
55
|
-
currentTransportName: string;
|
|
55
|
+
currentTransportName: () => string;
|
|
56
56
|
}
|
|
57
57
|
export declare class RevolverTransport implements ReturnType<Transport<"revolver", RevolverTransportValue>> {
|
|
58
58
|
#private;
|
|
@@ -76,6 +76,6 @@ export declare class RevolverTransport implements ReturnType<Transport<"revolver
|
|
|
76
76
|
* @returns true if rotation was successful
|
|
77
77
|
*/
|
|
78
78
|
rotate(reason?: BaseError): Promise<boolean>;
|
|
79
|
-
|
|
79
|
+
currentTransportName(): string;
|
|
80
80
|
}
|
|
81
81
|
export {};
|