@@ -15,8 +15,10 @@ import {
15
15
PoolQueryObject , PoolQueryType ,
16
16
} from '../graphql/pools' ;
17
17
import { TokenPrices } from '../state' ;
18
- import { getIconUrl } from '../components/common/Icons' ;
19
18
import { graphqlRequest } from '../graphql/utils' ;
19
+ import { tokenIconUtils } from '@reef-chain/util-lib' ;
20
+ import { useAsyncEffect } from './useAsyncEffect' ;
21
+ import { getIconUrl } from '../components/common/Icons' ;
20
22
21
23
interface PoolItem {
22
24
address : string ;
@@ -126,6 +128,8 @@ export const usePoolsList = ({
126
128
const [ dataPoolsCount , setDataPoolsCount ] = useState < AllPoolsListCountQuery | UserPoolsListCountQuery | undefined > ( ) ;
127
129
const [ loadingPoolsList , setLoadingPoolsList ] = useState < boolean > ( true ) ;
128
130
const [ loadingPoolsCount , setLoadingPoolsCount ] = useState < boolean > ( true ) ;
131
+ const [ tokenAddresses , setTokenAddresses ] = useState < string [ ] > ( [ ] ) ;
132
+ const [ tokenIconsMap , setTokenIconsMap ] = useState ( { } ) ;
129
133
130
134
useEffect ( ( ) => {
131
135
const handleResp = async ( ) : Promise < void > => {
@@ -149,20 +153,47 @@ export const usePoolsList = ({
149
153
handleResp ( ) ;
150
154
} , [ ] ) ;
151
155
156
+ useAsyncEffect ( async ( ) => {
157
+ if ( tokenAddresses . length ) {
158
+ const _fetchedTokenIconsMap = await tokenIconUtils . resolveTokenUrl ( tokenAddresses ) ;
159
+ setTokenIconsMap ( _fetchedTokenIconsMap )
160
+ }
161
+ } , [ tokenAddresses ] )
162
+
152
163
const processed = useMemo ( ( ) : PoolItem [ ] => {
153
164
if ( ! dataPoolsList ) return [ ] ;
154
165
const poolsList = queryType === 'User'
155
166
? ( dataPoolsList as UserPoolsListQuery ) . userPoolsList
156
167
: ( dataPoolsList as AllPoolsListQuery ) . allPoolsList ;
168
+
169
+ let emptyTokenIconsAddresses : string [ ] = [ ]
170
+ let _tokenIconsMap = { } ;
171
+
172
+ poolsList . map ( ( pool ) => {
173
+ if ( ! pool . iconUrl1 ) emptyTokenIconsAddresses . push ( pool . token1 ) ;
174
+ else _tokenIconsMap [ pool . token1 ] = pool . iconUrl1
175
+ if ( ! pool . iconUrl2 ) emptyTokenIconsAddresses . push ( pool . token2 ) ;
176
+ else _tokenIconsMap [ pool . token2 ] = pool . iconUrl2
177
+ } )
178
+
179
+ if ( tokenAddresses != emptyTokenIconsAddresses ) {
180
+ setTokenAddresses ( emptyTokenIconsAddresses ) ;
181
+ }
182
+
183
+ const mergedTokenIconsMap = {
184
+ ...tokenIconsMap ,
185
+ ..._tokenIconsMap
186
+ }
187
+
157
188
return poolsList . map ( ( pool ) => ( {
158
189
address : pool . id ,
159
190
token1 : {
160
- image : ! pool . iconUrl1 ? getIconUrl ( pool . token1 ) : pool . iconUrl1 ,
191
+ image : ! pool . iconUrl1 ? mergedTokenIconsMap [ pool . token1 ] ?? getIconUrl ( pool . token1 ) : pool . iconUrl1 ,
161
192
name : pool . name1 ,
162
193
address :pool . token1
163
194
} ,
164
195
token2 : {
165
- image : ! pool . iconUrl2 ? getIconUrl ( pool . token2 ) : pool . iconUrl2 ,
196
+ image : ! pool . iconUrl2 ? mergedTokenIconsMap [ pool . token2 ] ?? getIconUrl ( pool . token2 ) : pool . iconUrl2 ,
166
197
name : pool . name2 ,
167
198
address :pool . token2
168
199
} ,
@@ -171,7 +202,7 @@ export const usePoolsList = ({
171
202
volumeChange24h : calculateVolumeChange ( pool , tokenPrices ) ,
172
203
myLiquidity : calculateUserLiquidity ( pool , tokenPrices ) ,
173
204
} ) ) ;
174
- } , [ dataPoolsList ] ) ;
205
+ } , [ dataPoolsList , tokenIconsMap ] ) ;
175
206
176
207
let count = 0 ;
177
208
0 commit comments