1
1
"use client" ;
2
2
3
- import CSSLoader from "@/components/CSSLoader" ;
4
- import Card from "@/components/Card" ;
5
- import Graph from "@/components/Graph" ;
6
- import LoadingComponent from "@/components/LoadingComponent" ;
7
- import Pill from "@/components/Pill" ;
8
- import Table from "@/components/Table" ;
9
- import { MOCK_PRICES , defaultChain , pills , supportedChains } from "@/const/Variables" ;
10
- import { QueryForAlliances } from "@/lib/AllianceQuery" ;
11
- import { Alliance , AllianceResponse } from "@/types/ResponseTypes" ;
3
+ import CSSLoader from "../components/CSSLoader" ;
4
+ import Card from "../components/Card" ;
5
+ import Kpi from "../components/Kpi" ;
6
+ import Table from "../components/Table" ;
7
+ import { DEFAULT_CHAIN , SUPPORTED_CHAINS } from "../const/chains" ;
8
+ import { QueryAlliances } from "../lib/QueryAlliances" ;
12
9
import Link from "next/link" ;
13
- import { useSearchParams } from "next/navigation" ;
10
+ import { useSearchParams , useRouter } from "next/navigation" ;
14
11
import { Suspense , useEffect , useState } from "react" ;
12
+ import { Prices , QueryAndMergePrices } from "../models/Prices" ;
13
+ import { Kpis } from "../const/kpis" ;
14
+ import { LCD } from "../models/LCDConfig" ;
15
+ import TableState from "../models/TableState" ;
16
+ import { GetInflationEndpoint , ParseInflation } from "../lib/QueryInflation" ;
17
+ import { QueryLP } from "../lib/QueryLP" ;
18
+ import { Chain } from "../models/Chain" ;
15
19
16
20
export default function Home ( ) {
17
- const [ usdValues , setUsdValues ] = useState < any > ( ) ;
18
- const [ pillPrices , setPillPrices ] = useState < any > ( null ) ;
19
- const [ data , setData ] = useState < Alliance [ ] > ( [ ] ) ;
20
21
const params = useSearchParams ( ) ;
21
- const [ loading , setLoading ] = useState ( true ) ;
22
+ const router = useRouter ( ) ;
23
+
24
+ const [ tableState , setTableState ] = useState < TableState | null > ( null ) ;
25
+ const [ selectedChain , setSelectedChain ] = useState < Chain | undefined > ( undefined ) ;
26
+ const [ prices , setPrices ] = useState < Prices > ( { } ) ;
27
+ const [ isLoading , setIsLoading ] = useState ( true ) ;
22
28
23
29
useEffect ( ( ) => {
24
- ( async ( ) => {
25
- if ( ! usdValues || ! pillPrices ) {
26
- setLoading ( true ) ;
27
- const result = await fetch ( "https://price.api.tfm.com/tokens/?limit=1500" ) ;
28
- const json = await result . json ( ) ;
29
- setUsdValues ( {
30
- ...json ,
31
- ...MOCK_PRICES ,
32
- } ) ;
30
+ const selectedParamIsSupported = SUPPORTED_CHAINS [ params . get ( "selected" ) as any ] ;
31
+ if ( selectedParamIsSupported ) {
32
+ setSelectedChain ( SUPPORTED_CHAINS [ params . get ( "selected" ) as string ] ) ;
33
+ } else {
34
+ setSelectedChain ( SUPPORTED_CHAINS [ DEFAULT_CHAIN ] )
35
+ router . push ( `?selected=${ SUPPORTED_CHAINS [ DEFAULT_CHAIN ] . id } ` ) ;
36
+ }
37
+ } , [ params ] )
38
+
39
+ useEffect ( ( ) => {
40
+ if ( selectedChain ) {
41
+ setIsLoading ( true ) ;
42
+ ( async ( ) => {
43
+ // Load the prices only the first time a user lands on
44
+ // the page, then use the prices from the state.
45
+ //
46
+ // NOTE: the variable is not being shadowed because otherwise
47
+ // the first load will result on 0 prices.
48
+ const _prices = Object . keys ( prices ) . length === 0 ? await QueryAndMergePrices ( ) : prices ;
49
+ setPrices ( _prices ) ;
50
+
51
+ // Query selectedChain alliances info
52
+ const _allianceAssetRes = await QueryAlliances ( selectedChain ) . catch ( ( ) => [ ] ) ;
33
53
34
- const priceResult = await fetch ( "https://pisco-price-server.terra.dev/latest" ) ;
35
- const pillJson = await priceResult . json ( ) ;
36
- setPillPrices ( Object . fromEntries ( pillJson . prices . map ( ( p : any ) => {
37
- return [ p . denom , {
38
- usd : p . price
39
- } ]
40
- } ) ) )
41
- setLoading ( false ) ;
42
- }
54
+ // Query chain info on parallel to speed up the loading time
55
+ let chainInfoRes = await Promise . all ( [
56
+ LCD . alliance . params ( selectedChain . id ) ,
57
+ LCD . bank . supplyByDenom ( selectedChain . id , selectedChain . bondDenom ) ,
58
+ GetInflationEndpoint ( selectedChain . id ) ,
59
+ ] ) . catch ( ( e ) => {
60
+ console . error ( e )
61
+ return [ ]
62
+ } ) ;
43
63
44
- const chain = supportedChains [ params . get ( "selected" ) ?? defaultChain ] ;
45
- let response = [ ] ;
64
+ // Query this info in parallel to speed up the loading time
65
+ // because the requested data is independent from each other
66
+ const [ inflation , allianceCoins ] = await Promise . all ( [
67
+ ParseInflation ( selectedChain . id , chainInfoRes [ 2 ] ) ,
68
+ QueryLP ( selectedChain . allianceCoins )
69
+ ] )
46
70
47
- try {
48
- const resp = await QueryForAlliances ( chain ) ;
49
- response = [ ...resp . alliances ] ;
50
- } catch {
51
- response = [ ...[ ] ] ;
52
- }
71
+ selectedChain . allianceCoins = allianceCoins ;
53
72
54
- setData ( response ) ;
55
- } ) ( ) ;
56
- } , [ params ] ) ;
73
+ // If no error occured, set the data
74
+ // otherwise, keep the default data
75
+ if ( chainInfoRes != undefined ) {
76
+ let tableState = new TableState (
77
+ selectedChain ,
78
+ _allianceAssetRes ,
79
+ _prices ,
80
+ chainInfoRes [ 0 ] . params ,
81
+ chainInfoRes [ 1 ] ,
82
+ inflation ,
83
+ ) ;
84
+ setTableState ( tableState )
85
+ }
86
+ setIsLoading ( false ) ;
87
+ } ) ( ) ;
88
+ }
89
+ } , [ selectedChain ] ) ;
57
90
58
91
return (
59
92
< section className = "w-full flex-col" >
@@ -69,26 +102,19 @@ export default function Home() {
69
102
Learn how to < Link href = "https://medium.com/terra-money/how-to-stake-alliance-assets-a-step-by-step-guide-8e1b263830c2" > < u > stake Alliance assets</ u > </ Link > .
70
103
</ h3 >
71
104
</ div >
72
- < div className = "flex flex-col pt-3 pb-3 mt-12 overflow-auto" >
73
- < LoadingComponent isLoading = { loading } values = { pillPrices } >
74
- < div className = "flex gap-3" > { pillPrices && pills . map ( ( pill ) => < Pill key = { pill . id } pill = { pill } data = { pillPrices [ pill . token ] } /> ) } </ div >
75
- </ LoadingComponent >
105
+ < div className = "flex flex-col pt-3 mt-12 overflow-auto" >
106
+ < div className = "flex gap-3" >
107
+ { Kpis . map ( ( kpi ) => < Kpi key = { kpi . id } kpi = { kpi } data = { prices [ kpi . token ] } /> ) }
108
+ </ div >
76
109
</ div >
77
110
< div className = "flex w-full flex-col lg:flex-row gap-3" >
78
111
< div className = "w-full lg:w-6/6" >
79
112
< Card name = "Assets" >
80
113
< Suspense fallback = { < CSSLoader /> } >
81
- < Table usdValues = { usdValues } values = { data } />
114
+ < Table tableState = { tableState } isLoading = { isLoading } />
82
115
</ Suspense >
83
116
</ Card >
84
117
</ div >
85
- { /* <div className="w-full lg:w-2/6">
86
- <Card name="Overview" className="flex flex-col items-center overflow-auto">
87
- <Suspense fallback={<CSSLoader />}>
88
- <Graph values={data} />
89
- </Suspense>
90
- </Card>
91
- </div> */ }
92
118
</ div >
93
119
</ section >
94
120
) ;
0 commit comments