-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathutils.ts
82 lines (70 loc) · 2.19 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {
BRIDGE_API_BASE_URL,
BRIDGE_CLIENT_ID,
} from '../../../../shared/constants/bridge';
import fetchWithCache from '../../../../shared/lib/fetch-with-cache';
import {
StatusResponse,
StatusRequestWithSrcTxHash,
StatusRequestDto,
} from '../../../../shared/types/bridge-status';
import type { Quote } from '../../../../shared/types/bridge';
import { validateResponse, validators } from './validators';
const CLIENT_ID_HEADER = { 'X-Client-Id': BRIDGE_CLIENT_ID };
export const BRIDGE_STATUS_BASE_URL = `${BRIDGE_API_BASE_URL}/getTxStatus`;
export const getStatusRequestDto = (
statusRequest: StatusRequestWithSrcTxHash,
): StatusRequestDto => {
const { quote, ...statusRequestNoQuote } = statusRequest;
const statusRequestNoQuoteFormatted = Object.fromEntries(
Object.entries(statusRequestNoQuote).map(([key, value]) => [
key,
value.toString(),
]),
) as unknown as Omit<StatusRequestDto, 'requestId'>;
const requestId: { requestId: string } | Record<string, never> =
quote?.requestId ? { requestId: quote.requestId } : {};
return {
...statusRequestNoQuoteFormatted,
...requestId,
};
};
export const fetchBridgeTxStatus = async (
statusRequest: StatusRequestWithSrcTxHash,
) => {
const statusRequestDto = getStatusRequestDto(statusRequest);
const params = new URLSearchParams(statusRequestDto);
// Fetch
const url = `${BRIDGE_STATUS_BASE_URL}?${params.toString()}`;
const rawTxStatus = await fetchWithCache({
url,
fetchOptions: { method: 'GET', headers: CLIENT_ID_HEADER },
cacheOptions: { cacheRefreshTime: 0 },
functionName: 'fetchBridgeTxStatus',
});
// Validate
const isValid = validateResponse<StatusResponse, unknown>(
validators,
rawTxStatus,
BRIDGE_STATUS_BASE_URL,
);
if (!isValid) {
throw new Error('Invalid response from bridge');
}
// Return
return rawTxStatus;
};
export const getStatusRequestWithSrcTxHash = (
quote: Quote,
srcTxHash: string,
): StatusRequestWithSrcTxHash => {
return {
bridgeId: quote.bridgeId,
srcTxHash,
bridge: quote.bridges[0],
srcChainId: quote.srcChainId,
destChainId: quote.destChainId,
quote,
refuel: Boolean(quote.refuel),
};
};