@@ -28,6 +28,7 @@ export const StdInterfaceIds = Object.values(e.TokenStdInterfaceId)
28
28
interface TokenQueryProps extends SkipProp {
29
29
id : TokenId
30
30
networkId ?: number
31
+ skipCaching ?: boolean
31
32
}
32
33
33
34
interface NFTQueryProps extends TokenQueryProps {
@@ -82,12 +83,12 @@ export const ftListQuery = ({ networkId, skip }: Omit<TokenQueryProps, 'id'>) =>
82
83
} )
83
84
}
84
85
85
- export const tokenTypeQuery = ( { id, networkId, skip } : TokenQueryProps ) =>
86
+ export const tokenTypeQuery = ( { id, networkId, skip, skipCaching } : TokenQueryProps ) =>
86
87
queryOptions ( {
87
- queryKey : [ 'token' , 'type' , id ] ,
88
+ queryKey : [ 'token' , 'type' , getId ( id , skipCaching ) ] ,
88
89
// We always want to remember the type of a token, even when user navigates for too long from components that use
89
90
// tokens.
90
- ...getQueryConfig ( { staleTime : Infinity , gcTime : Infinity , networkId } ) ,
91
+ ...getQueryConfig ( { staleTime : Infinity , gcTime : Infinity , networkId, skipCaching } ) ,
91
92
queryFn :
92
93
! skip && networkId !== undefined
93
94
? async ( ) => {
@@ -127,10 +128,10 @@ export const combineTokenTypes = (tokenTypes: (e.TokenInfo | { data: e.TokenInfo
127
128
} as Record < e . TokenStdInterfaceId , TokenId [ ] >
128
129
)
129
130
130
- export const fungibleTokenMetadataQuery = ( { id, networkId, skip } : TokenQueryProps ) =>
131
+ export const fungibleTokenMetadataQuery = ( { id, networkId, skip, skipCaching } : TokenQueryProps ) =>
131
132
queryOptions ( {
132
- queryKey : [ 'token' , 'fungible' , 'metadata' , id ] ,
133
- ...getQueryConfig ( { staleTime : Infinity , gcTime : Infinity , networkId } ) ,
133
+ queryKey : [ 'token' , 'fungible' , 'metadata' , getId ( id , skipCaching ) ] ,
134
+ ...getQueryConfig ( { staleTime : Infinity , gcTime : Infinity , networkId, skipCaching } ) ,
134
135
queryFn : ! skip
135
136
? async ( ) => {
136
137
const tokenMetadata = await batchers . ftMetadataBatcher . fetch ( id )
@@ -140,18 +141,18 @@ export const fungibleTokenMetadataQuery = ({ id, networkId, skip }: TokenQueryPr
140
141
: skipToken
141
142
} )
142
143
143
- export const nftMetadataQuery = ( { id, networkId, skip } : TokenQueryProps ) =>
144
+ export const nftMetadataQuery = ( { id, networkId, skip, skipCaching } : TokenQueryProps ) =>
144
145
queryOptions ( {
145
- queryKey : [ 'token' , 'non-fungible' , 'metadata' , id ] ,
146
- ...getQueryConfig ( { staleTime : Infinity , gcTime : Infinity , networkId } ) ,
146
+ queryKey : [ 'token' , 'non-fungible' , 'metadata' , getId ( id , skipCaching ) ] ,
147
+ ...getQueryConfig ( { staleTime : Infinity , gcTime : Infinity , networkId, skipCaching } ) ,
147
148
queryFn : ! skip ? async ( ) => ( await batchers . nftMetadataBatcher . fetch ( id ) ) ?? null : skipToken
148
149
} )
149
150
150
- export const nftDataQuery = ( { id, tokenUri, networkId, skip } : NFTQueryProps ) =>
151
+ export const nftDataQuery = ( { id, tokenUri, networkId, skip, skipCaching } : NFTQueryProps ) =>
151
152
queryOptions ( {
152
- queryKey : [ 'token' , 'non-fungible' , 'data' , id ] ,
153
+ queryKey : [ 'token' , 'non-fungible' , 'data' , getId ( id , skipCaching ) ] ,
153
154
// We don't want to delete the NFT data when the user navigates away from NFT components.
154
- ...getQueryConfig ( { staleTime : ONE_DAY_MS , gcTime : Infinity , networkId } ) ,
155
+ ...getQueryConfig ( { staleTime : ONE_DAY_MS , gcTime : Infinity , networkId, skipCaching } ) ,
155
156
queryFn :
156
157
! skip && ! ! tokenUri
157
158
? async ( ) => {
@@ -203,10 +204,21 @@ export const nftDataQuery = ({ id, tokenUri, networkId, skip }: NFTQueryProps) =
203
204
: skipToken
204
205
} )
205
206
206
- export const tokenQuery = ( { id, networkId, skip } : TokenQueryProps ) =>
207
- queryOptions ( {
208
- queryKey : [ 'token' , id , { networkId } ] ,
209
- ...getQueryConfig ( { staleTime : Infinity , gcTime : Infinity , networkId } ) ,
207
+ export const NOCACHE_PREFIX = 'nocache-'
208
+
209
+ export const tokenQuery = ( { id : dirtyId , networkId, skip, skipCaching : skipCachingProp } : TokenQueryProps ) => {
210
+ const isDirtyId = dirtyId . includes ( NOCACHE_PREFIX )
211
+ const id = isDirtyId ? dirtyId . split ( NOCACHE_PREFIX ) [ 1 ] : dirtyId
212
+ const skipCaching = isDirtyId || skipCachingProp
213
+
214
+ return queryOptions ( {
215
+ queryKey : [ 'token' , getId ( id , skipCaching ) , { networkId } ] ,
216
+ ...getQueryConfig ( {
217
+ staleTime : Infinity ,
218
+ gcTime : Infinity ,
219
+ networkId,
220
+ skipCaching
221
+ } ) ,
210
222
queryFn : async ( ) : Promise < Token > => {
211
223
const nst = { id } as NonStandardToken
212
224
@@ -218,18 +230,18 @@ export const tokenQuery = ({ id, networkId, skip }: TokenQueryProps) =>
218
230
if ( listedFT ) return listedFT
219
231
220
232
// 2. If not, find the type of the token
221
- const tokenInfo = await queryClient . fetchQuery ( tokenTypeQuery ( { id, networkId } ) )
233
+ const tokenInfo = await queryClient . fetchQuery ( tokenTypeQuery ( { id, networkId, skipCaching } ) )
222
234
223
235
// 3. If it is a fungible token, fetch the fungible token metadata
224
236
if ( tokenInfo ?. stdInterfaceId === e . TokenStdInterfaceId . Fungible ) {
225
- const ftMetadata = await queryClient . fetchQuery ( fungibleTokenMetadataQuery ( { id, networkId } ) )
237
+ const ftMetadata = await queryClient . fetchQuery ( fungibleTokenMetadataQuery ( { id, networkId, skipCaching } ) )
226
238
227
239
return ftMetadata ?? nst
228
240
}
229
241
230
242
// 4. If it is an NFT, fetch the NFT metadata and data
231
243
if ( tokenInfo ?. stdInterfaceId === e . TokenStdInterfaceId . NonFungible ) {
232
- const nft = await queryClient . fetchQuery ( nftQuery ( { id, networkId } ) )
244
+ const nft = await queryClient . fetchQuery ( nftQuery ( { id, networkId, skipCaching } ) )
233
245
234
246
return nft ?? nst
235
247
}
@@ -246,21 +258,26 @@ export const tokenQuery = ({ id, networkId, skip }: TokenQueryProps) =>
246
258
} ,
247
259
enabled : ! skip
248
260
} )
261
+ }
249
262
250
- export const nftQuery = ( { id, networkId, skip } : TokenQueryProps ) =>
263
+ export const nftQuery = ( { id, networkId, skip, skipCaching } : TokenQueryProps ) =>
251
264
queryOptions ( {
252
- queryKey : [ 'token' , 'non-fungible' , id , { networkId } ] ,
253
- ...getQueryConfig ( { staleTime : Infinity , gcTime : Infinity , networkId } ) ,
265
+ queryKey : [ 'token' , 'non-fungible' , getId ( id , skipCaching ) , { networkId } ] ,
266
+ ...getQueryConfig ( { staleTime : Infinity , gcTime : Infinity , networkId, skipCaching } ) ,
254
267
queryFn : async ( ) : Promise < NFT | null > => {
255
- const nftMetadata = await queryClient . fetchQuery ( nftMetadataQuery ( { id, networkId } ) )
268
+ const nftMetadata = await queryClient . fetchQuery ( nftMetadataQuery ( { id, networkId, skipCaching } ) )
256
269
257
270
if ( ! nftMetadata ) return null
258
271
259
- const nftData = await queryClient . fetchQuery ( nftDataQuery ( { id, tokenUri : nftMetadata . tokenUri , networkId } ) )
272
+ const nftData = await queryClient . fetchQuery (
273
+ nftDataQuery ( { id, tokenUri : nftMetadata . tokenUri , networkId, skipCaching } )
274
+ )
260
275
261
276
if ( ! nftData ) return null
262
277
263
278
return { ...nftData , ...nftMetadata }
264
279
} ,
265
280
enabled : ! skip
266
281
} )
282
+
283
+ const getId = ( id : string , skipCaching ?: boolean ) => `${ skipCaching ? NOCACHE_PREFIX : '' } ${ id } `
0 commit comments