1
- import { QuoteRequest , TokensRequest , TokensResponse } from '@lifinance/types'
2
- import { GetStatusRequest } from '@lifinance/types/dist/api'
1
+ import {
2
+ GetStatusRequest ,
3
+ QuoteRequest ,
4
+ RequestOptions ,
5
+ TokensRequest ,
6
+ TokensResponse ,
7
+ } from '@lifinance/types'
3
8
import axios from 'axios'
4
9
import { isRoutesRequest , isStep } from '../typeguards'
5
10
import {
@@ -22,7 +27,8 @@ import { parseBackendError } from '../utils/parseError'
22
27
import ConfigService from './ConfigService'
23
28
24
29
const getPossibilities = async (
25
- request ?: PossibilitiesRequest
30
+ request ?: PossibilitiesRequest ,
31
+ options ?: RequestOptions
26
32
) : Promise < PossibilitiesResponse > => {
27
33
if ( ! request ) {
28
34
request = { }
@@ -39,7 +45,10 @@ const getPossibilities = async (
39
45
try {
40
46
const result = await axios . post < PossibilitiesResponse > (
41
47
config . apiUrl + 'advanced/possibilities' ,
42
- request
48
+ request ,
49
+ {
50
+ signal : options ?. signal ,
51
+ }
43
52
)
44
53
return result . data
45
54
} catch ( e ) {
@@ -49,7 +58,8 @@ const getPossibilities = async (
49
58
50
59
const getToken = async (
51
60
chain : ChainKey | ChainId ,
52
- token : string
61
+ token : string ,
62
+ options ?: RequestOptions
53
63
) : Promise < Token > => {
54
64
if ( ! chain ) {
55
65
throw new ValidationError ( 'Required parameter "chain" is missing.' )
@@ -67,31 +77,35 @@ const getToken = async (
67
77
chain,
68
78
token,
69
79
} ,
80
+ signal : options ?. signal ,
70
81
} )
71
82
return result . data
72
83
} catch ( e ) {
73
84
throw parseBackendError ( e )
74
85
}
75
86
}
76
87
77
- const getQuote = async ( {
78
- fromChain,
79
- fromToken,
80
- fromAddress,
81
- fromAmount,
82
- toChain,
83
- toToken,
84
- order,
85
- slippage,
86
- integrator,
87
- referrer,
88
- allowBridges,
89
- denyBridges,
90
- preferBridges,
91
- allowExchanges,
92
- denyExchanges,
93
- preferExchanges,
94
- } : QuoteRequest ) : Promise < Step > => {
88
+ const getQuote = async (
89
+ {
90
+ fromChain,
91
+ fromToken,
92
+ fromAddress,
93
+ fromAmount,
94
+ toChain,
95
+ toToken,
96
+ order,
97
+ slippage,
98
+ integrator,
99
+ referrer,
100
+ allowBridges,
101
+ denyBridges,
102
+ preferBridges,
103
+ allowExchanges,
104
+ denyExchanges,
105
+ preferExchanges,
106
+ } : QuoteRequest ,
107
+ options ?: RequestOptions
108
+ ) : Promise < Step > => {
95
109
if ( ! fromChain ) {
96
110
throw new ValidationError ( 'Required parameter "fromChain" is missing.' )
97
111
}
@@ -133,19 +147,18 @@ const getQuote = async ({
133
147
denyExchanges,
134
148
preferExchanges,
135
149
} ,
150
+ signal : options ?. signal ,
136
151
} )
137
152
return result . data
138
153
} catch ( e ) {
139
154
throw parseBackendError ( e )
140
155
}
141
156
}
142
157
143
- const getStatus = async ( {
144
- bridge,
145
- fromChain,
146
- toChain,
147
- txHash,
148
- } : GetStatusRequest ) : Promise < StatusResponse > => {
158
+ const getStatus = async (
159
+ { bridge, fromChain, toChain, txHash } : GetStatusRequest ,
160
+ options ?: RequestOptions
161
+ ) : Promise < StatusResponse > => {
149
162
if ( fromChain !== toChain && ! bridge ) {
150
163
throw new ValidationError (
151
164
'Parameter "bridge" is required for cross chain transfers.'
@@ -174,54 +187,64 @@ const getStatus = async ({
174
187
toChain,
175
188
txHash,
176
189
} ,
190
+ signal : options ?. signal ,
177
191
} )
178
192
return result . data
179
193
} catch ( e ) {
180
194
throw parseBackendError ( e )
181
195
}
182
196
}
183
197
184
- const getChains = async ( ) : Promise < Chain [ ] > => {
198
+ const getChains = async ( options ?: RequestOptions ) : Promise < Chain [ ] > => {
185
199
const configService = ConfigService . getInstance ( )
186
200
const config = configService . getConfig ( )
187
201
188
202
try {
189
- const result = await axios . get < ChainsResponse > ( config . apiUrl + 'chains' )
203
+ const result = await axios . get < ChainsResponse > ( config . apiUrl + 'chains' , {
204
+ signal : options ?. signal ,
205
+ } )
190
206
return result . data . chains
191
207
} catch ( e ) {
192
208
throw parseBackendError ( e )
193
209
}
194
210
}
195
211
196
212
const getRoutes = async (
197
- routesRequest : RoutesRequest
213
+ request : RoutesRequest ,
214
+ options ?: RequestOptions
198
215
) : Promise < RoutesResponse > => {
199
- if ( ! isRoutesRequest ( routesRequest ) ) {
216
+ if ( ! isRoutesRequest ( request ) ) {
200
217
throw new ValidationError ( 'Invalid routes request.' )
201
218
}
202
219
203
220
const configService = ConfigService . getInstance ( )
204
221
const config = configService . getConfig ( )
205
222
206
223
// apply defaults
207
- routesRequest . options = {
224
+ request . options = {
208
225
...config . defaultRouteOptions ,
209
- ...routesRequest . options ,
226
+ ...request . options ,
210
227
}
211
228
212
229
// send request
213
230
try {
214
231
const result = await axios . post < RoutesResponse > (
215
232
config . apiUrl + 'advanced/routes' ,
216
- routesRequest
233
+ request ,
234
+ {
235
+ signal : options ?. signal ,
236
+ }
217
237
)
218
238
return result . data
219
239
} catch ( e ) {
220
240
throw parseBackendError ( e )
221
241
}
222
242
}
223
243
224
- const getStepTransaction = async ( step : Step ) : Promise < Step > => {
244
+ const getStepTransaction = async (
245
+ step : Step ,
246
+ options ?: RequestOptions
247
+ ) : Promise < Step > => {
225
248
if ( ! isStep ( step ) ) {
226
249
// While the validation fails for some users we should not enforce it
227
250
// eslint-disable-next-line no-console
@@ -233,28 +256,39 @@ const getStepTransaction = async (step: Step): Promise<Step> => {
233
256
try {
234
257
const result = await axios . post < Step > (
235
258
config . apiUrl + 'advanced/stepTransaction' ,
236
- step
259
+ step ,
260
+ {
261
+ signal : options ?. signal ,
262
+ }
237
263
)
238
264
return result . data
239
265
} catch ( e ) {
240
266
throw parseBackendError ( e )
241
267
}
242
268
}
243
269
244
- const getTools = async ( request ?: ToolsRequest ) : Promise < ToolsResponse > => {
270
+ const getTools = async (
271
+ request ?: ToolsRequest ,
272
+ options ?: RequestOptions
273
+ ) : Promise < ToolsResponse > => {
245
274
const configService = ConfigService . getInstance ( )
246
275
const config = configService . getConfig ( )
247
276
const r = await axios . get < ToolsResponse > ( config . apiUrl + 'tools' , {
248
277
params : request ,
278
+ signal : options ?. signal ,
249
279
} )
250
280
return r . data
251
281
}
252
282
253
- const getTokens = async ( request ?: TokensRequest ) : Promise < TokensResponse > => {
283
+ const getTokens = async (
284
+ request ?: TokensRequest ,
285
+ options ?: RequestOptions
286
+ ) : Promise < TokensResponse > => {
254
287
const configService = ConfigService . getInstance ( )
255
288
const config = configService . getConfig ( )
256
289
const r = await axios . get < TokensResponse > ( config . apiUrl + 'tokens' , {
257
290
params : request ,
291
+ signal : options ?. signal ,
258
292
} )
259
293
return r . data
260
294
}
0 commit comments