1
+ import { useEffect , useState } from "react" ;
2
+ import { StyledTd } from "./Table" ;
3
+
4
+
5
+
6
+ const fetchLazerPriceIdMetadata = async ( ) => {
7
+ const response = await fetch ( "https://pyth-lazer-staging.dourolabs.app/history/v1/symbols" ) ;
8
+ const data = await response . json ( ) ;
9
+ return data ;
10
+ }
11
+
12
+ type LazerPriceIdMetadata = {
13
+ asset_type : string ;
14
+ description : string ;
15
+ exponent : number ;
16
+ name : string ;
17
+ pyth_lazer_id : number ;
18
+ symbol : string ;
19
+ }
20
+
21
+ enum LazerPriceIdStateType {
22
+ NotLoaded ,
23
+ Loading ,
24
+ Loaded ,
25
+ Error ,
26
+ }
27
+
28
+ const LazerPriceIdState = {
29
+ NotLoaded : ( ) => ( { type : LazerPriceIdStateType . NotLoaded as const } ) ,
30
+ Loading : ( ) => ( { type : LazerPriceIdStateType . Loading as const } ) ,
31
+ Loaded : ( priceFeeds : LazerPriceIdMetadata [ ] ) => ( {
32
+ type : LazerPriceIdStateType . Loaded as const ,
33
+ priceFeeds,
34
+ } ) ,
35
+ Error : ( error : unknown ) => ( {
36
+ type : LazerPriceIdStateType . Error as const ,
37
+ error,
38
+ } ) ,
39
+ } ;
40
+
41
+ type LazerPriceIdState = ReturnType < typeof LazerPriceIdState [ keyof typeof LazerPriceIdState ] > ;
42
+
43
+ const useLazerPriceIdState = ( ) => {
44
+ const [ state , setState ] = useState < LazerPriceIdState > ( LazerPriceIdState . NotLoaded ( ) ) ;
45
+
46
+ useEffect ( ( ) => {
47
+ setState ( LazerPriceIdState . Loading ( ) ) ;
48
+ fetchLazerPriceIdMetadata ( ) . then ( priceFeeds => setState ( LazerPriceIdState . Loaded ( priceFeeds ) ) )
49
+ . catch ( error => setState ( LazerPriceIdState . Error ( error ) ) ) ;
50
+ } , [ ] ) ;
51
+
52
+ return state ;
53
+ }
54
+
55
+ export function LazerPriceIdTable ( ) {
56
+ const lazerPriceIdState = useLazerPriceIdState ( ) ;
57
+
58
+ switch ( lazerPriceIdState . type ) {
59
+ case LazerPriceIdStateType . NotLoaded :
60
+ return < div > Loading...</ div > ;
61
+ case LazerPriceIdStateType . Loading :
62
+ return < div > Loading...</ div > ;
63
+ case LazerPriceIdStateType . Loaded :
64
+ return (
65
+ < table >
66
+ < thead >
67
+ < tr >
68
+ < th > Asset Type</ th >
69
+ < th > Description</ th >
70
+ < th > Name</ th >
71
+ < th > Symbol</ th >
72
+ < th > Pyth Lazer Id</ th >
73
+ < th > Exponent</ th >
74
+ </ tr >
75
+ </ thead >
76
+ < tbody >
77
+ { lazerPriceIdState . priceFeeds . map ( priceFeed => (
78
+ < tr key = { priceFeed . symbol } >
79
+ < StyledTd > { priceFeed . asset_type } </ StyledTd >
80
+ < StyledTd > { priceFeed . description } </ StyledTd >
81
+ < StyledTd > { priceFeed . name } </ StyledTd >
82
+ < StyledTd > { priceFeed . symbol } </ StyledTd >
83
+ < StyledTd > { priceFeed . pyth_lazer_id } </ StyledTd >
84
+ < StyledTd > { priceFeed . exponent } </ StyledTd >
85
+ </ tr >
86
+ ) ) }
87
+ </ tbody >
88
+ </ table >
89
+ )
90
+ case LazerPriceIdStateType . Error :
91
+ return < div > Error</ div > ;
92
+ }
93
+ }
0 commit comments