@@ -35,39 +35,29 @@ export function getQuickPollingInterval(orderStartTime: number) {
35
35
return QUICK_POLL_MAX_INTERVAL
36
36
}
37
37
38
- function isUniswapXOrder ( order : UniswapXOrderDetails ) : boolean {
39
- return (
40
- order . type === SignatureType . SIGN_UNISWAPX_ORDER ||
41
- order . type === SignatureType . SIGN_UNISWAPX_V2_ORDER ||
42
- order . type === SignatureType . SIGN_UNISWAPX_V3_ORDER
43
- )
44
- }
45
-
46
38
async function fetchStatuses (
39
+ endpoint : 'limit-orders' | 'orders' ,
47
40
orders : UniswapXOrderDetails [ ] ,
48
- filter : ( order : UniswapXOrderDetails ) => boolean ,
49
- path : ( hashes : string [ ] ) => string ,
41
+ swapper : string ,
50
42
) : Promise < UniswapXBackendOrder [ ] > {
51
- const hashes = orders . filter ( filter ) . map ( ( order ) => order . orderHash )
52
- if ( ! hashes || hashes . length === 0 ) {
43
+ const hashes = orders . map ( ( order ) => order . orderHash )
44
+ if ( hashes . length === 0 ) {
53
45
return [ ]
54
46
}
55
- const baseURL = UNISWAP_GATEWAY_DNS_URL
56
- const result = await global . fetch ( `${ baseURL } ${ path ( hashes ) } ` )
47
+
48
+ const result = await global . fetch ( `${ UNISWAP_GATEWAY_DNS_URL } / ${ endpoint } ?swapper= ${ swapper } &orderHashes= ${ hashes } ` )
57
49
const statuses = ( await result . json ( ) ) as OrderQueryResponse
58
50
return statuses . orders
59
51
}
60
52
61
53
async function fetchLimitStatuses ( account : string , orders : UniswapXOrderDetails [ ] ) : Promise < UniswapXBackendOrder [ ] > {
62
- return fetchStatuses (
63
- orders ,
64
- ( order ) => order . type === SignatureType . SIGN_LIMIT ,
65
- ( hashes ) => `/limit-orders?swapper=${ account } &orderHashes=${ hashes } ` ,
66
- )
54
+ const limitOrders = orders . filter ( ( order ) => order . type === SignatureType . SIGN_LIMIT )
55
+ return fetchStatuses ( 'limit-orders' , limitOrders , account )
67
56
}
68
57
69
58
async function fetchOrderStatuses ( account : string , orders : UniswapXOrderDetails [ ] ) : Promise < UniswapXBackendOrder [ ] > {
70
- return fetchStatuses ( orders , isUniswapXOrder , ( hashes ) => `/orders?swapper=${ account } &orderHashes=${ hashes } ` )
59
+ const uniswapXOrders = orders . filter ( ( order ) => order . type !== SignatureType . SIGN_LIMIT )
60
+ return fetchStatuses ( 'orders' , uniswapXOrders , account )
71
61
}
72
62
73
63
function updateOrders (
@@ -135,31 +125,21 @@ function useQuickPolling({
135
125
let timeout : NodeJS . Timeout
136
126
137
127
async function poll ( ) {
138
- if ( ! account . address || pendingOrders . length === 0 ) {
128
+ const l2Orders = pendingOrders . filter ( ( order ) => isL2ChainId ( order . chainId ) )
129
+ if ( ! account . address || l2Orders . length === 0 ) {
139
130
return
140
131
}
141
132
142
- const orders = pendingOrders . filter (
143
- ( order ) =>
144
- ( isUniswapXOrder ( order ) && isL2ChainId ( order . chainId ) ) || order . type === SignatureType . SIGN_PRIORITY_ORDER ,
145
- )
146
- if ( orders . length === 0 ) {
147
- return
148
- }
149
-
150
- if ( orders . every ( ( order ) => isFinalizedOrder ( order ) ) ) {
133
+ if ( l2Orders . every ( ( order ) => isFinalizedOrder ( order ) ) ) {
151
134
clearTimeout ( timeout )
152
135
return
153
136
}
154
137
155
138
try {
156
- const statuses = await Promise . all ( [ fetchOrderStatuses ( account . address , orders . filter ( isUniswapXOrder ) ) ] ) . then (
157
- ( results ) => results . flat ( ) ,
158
- )
159
-
139
+ const statuses = await fetchOrderStatuses ( account . address , l2Orders )
160
140
updateOrders ( pendingOrders , statuses , onActivityUpdate )
161
141
162
- const earliestOrder = orders . find ( ( order ) => ! isFinalizedOrder ( order ) )
142
+ const earliestOrder = l2Orders . find ( ( order ) => ! isFinalizedOrder ( order ) )
163
143
if ( earliestOrder ) {
164
144
const newDelay = getQuickPollingInterval ( earliestOrder . addedTime )
165
145
setDelay ( newDelay )
@@ -199,29 +179,20 @@ function useStandardPolling({
199
179
let timeout : NodeJS . Timeout
200
180
201
181
async function poll ( ) {
202
- if ( ! account . address || pendingOrders . length === 0 ) {
182
+ const mainnetOrders = pendingOrders . filter ( ( order ) => ! isL2ChainId ( order . chainId ) )
183
+ if ( ! account . address || mainnetOrders . length === 0 ) {
203
184
return
204
185
}
205
186
206
- const orders = pendingOrders . filter (
207
- ( order ) => ( isUniswapXOrder ( order ) && ! isL2ChainId ( order . chainId ) ) || order . type === SignatureType . SIGN_LIMIT ,
208
- )
209
- if ( orders . length === 0 ) {
210
- return
211
- }
212
-
213
- if ( orders . every ( ( order ) => isFinalizedOrder ( order ) ) ) {
187
+ if ( mainnetOrders . every ( ( order ) => isFinalizedOrder ( order ) ) ) {
214
188
clearTimeout ( timeout )
215
189
return
216
190
}
217
191
218
192
try {
219
193
const statuses = await Promise . all ( [
220
- fetchOrderStatuses ( account . address , orders . filter ( isUniswapXOrder ) ) ,
221
- fetchLimitStatuses (
222
- account . address ,
223
- orders . filter ( ( order ) => order . type === SignatureType . SIGN_LIMIT ) ,
224
- ) ,
194
+ fetchOrderStatuses ( account . address , mainnetOrders ) ,
195
+ fetchLimitStatuses ( account . address , mainnetOrders ) ,
225
196
] ) . then ( ( results ) => results . flat ( ) )
226
197
227
198
updateOrders ( pendingOrders , statuses , onActivityUpdate )
@@ -243,17 +214,8 @@ export function usePollPendingOrders(onActivityUpdate: OnActivityUpdate) {
243
214
const account = useAccount ( )
244
215
const pendingOrders = usePendingOrders ( )
245
216
246
- useQuickPolling ( {
247
- account,
248
- pendingOrders,
249
- onActivityUpdate,
250
- } )
251
-
252
- useStandardPolling ( {
253
- account,
254
- pendingOrders,
255
- onActivityUpdate,
256
- } )
217
+ useQuickPolling ( { account, pendingOrders, onActivityUpdate } )
218
+ useStandardPolling ( { account, pendingOrders, onActivityUpdate } )
257
219
258
220
return null
259
221
}
0 commit comments