-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuseTransactionDetail.ts
116 lines (107 loc) · 2.57 KB
/
useTransactionDetail.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {
type ChainUniqueId,
InnerChainUniqueId,
} from "core/types/ChainUniqueId";
import { AssetType, type TokenV2 } from "core/types/Token";
import { useCallback, useMemo } from "react";
import useSWR from "swr";
import { useCoinService } from "@/hooks/useCoinService";
export type TransactionDetailReq = {
uniqueId: ChainUniqueId;
address: string;
txId?: string;
token: TokenV2;
filter?: {
address?: string;
logIndex?: number;
addressType?: "sender" | "receiver";
};
auth?: { [key: string]: string };
refreshInterval?: number;
};
export const useTransactionDetail = (params: TransactionDetailReq) => {
const {
uniqueId,
address,
txId,
token,
refreshInterval = 8000,
filter,
auth,
} = params;
const { coinService } = useCoinService(uniqueId);
const isAleo = uniqueId === InnerChainUniqueId.ALEO_MAINNET;
const key = useMemo(() => {
return `/transaction_detail/${uniqueId}/${txId}`;
}, [txId, uniqueId]);
const fetchDetail = useCallback(async () => {
let res;
if (isAleo) {
if (!coinService.supportGetTxStatus() || !txId) {
return;
}
const res = await coinService.getTxStatus({
txId,
filter: filter ?? { address },
auth,
});
if (res) {
const {
fee,
height,
confirmations,
timestamp,
status,
from,
to,
value,
} = res;
return {
fees: fee,
height,
confirmations,
timestamp,
};
}
} else {
if (token.type === AssetType.COIN) {
if (!coinService.supportNativeCoinTxDetail() || !txId) {
return undefined;
}
res = await coinService.getNativeCoinTxDetail({
txId,
filter: filter ?? { address },
auth,
});
} else {
if (!coinService.supportTokenTxDetail() || !txId) {
return undefined;
}
res = await coinService.getTokenTxDetail({
txId,
token,
filter: filter ?? { address },
});
}
}
// console.log(" res", res);
return res;
}, [address, auth, coinService, filter, isAleo, token, txId]);
const {
data: evmData,
error,
mutate: getTransactionDetail,
isLoading,
} = useSWR(key, fetchDetail, {
refreshInterval,
});
const res = useMemo(() => {
return {
data: evmData,
error,
getTransactionDetail,
isLoading,
};
}, [evmData, error, getTransactionDetail, isLoading]);
return res;
};