Skip to content

Commit 3d65b2d

Browse files
committed
Fix display of NFT when minting
Closes #1533
1 parent 71cf8bd commit 3d65b2d

File tree

10 files changed

+70
-43
lines changed

10 files changed

+70
-43
lines changed

apps/desktop-wallet/src/components/TokenAmountsBox.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ interface AssetAmountRowProps extends AmountBaseProps {
9090
tokenId: string
9191
amount: string
9292
extraAlphForDust: bigint
93+
skipCaching?: boolean
9394
}
9495

9596
const AssetAmountRow = ({ tokenId, amount, extraAlphForDust, ...props }: AssetAmountRowProps) => {

apps/desktop-wallet/src/features/transactionsDisplay/TransactionSummary.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import { TransactionDisplayProps } from '@/features/transactionsDisplay/transact
77
interface TransactionSummaryProps extends TransactionDisplayProps {
88
hideType?: boolean
99
className?: string
10+
skipCaching?: boolean
1011
}
1112

12-
const TransactionSummary = ({ tx, referenceAddress, hideType, className }: TransactionSummaryProps) => (
13+
const TransactionSummary = ({ tx, referenceAddress, hideType, className, skipCaching }: TransactionSummaryProps) => (
1314
<SummaryStyled className={className}>
1415
<SummaryContent>
1516
{!hideType && <TransactionType tx={tx} referenceAddress={referenceAddress} />}
16-
<TransactionAmounts tx={tx} referenceAddress={referenceAddress} />
17+
<TransactionAmounts tx={tx} referenceAddress={referenceAddress} skipCaching={skipCaching} />
1718
</SummaryContent>
1819
</SummaryStyled>
1920
)

apps/desktop-wallet/src/features/transactionsDisplay/transactionDetailsModal/TransactionAmounts.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { useTransactionAmountDeltas, useTransactionInfoType } from '@alephium/shared-react'
1+
import { NOCACHE_PREFIX, useTransactionAmountDeltas, useTransactionInfoType } from '@alephium/shared-react'
22
import { ALPH } from '@alephium/token-list'
33
import { useMemo } from 'react'
44

55
import TokenAmountsBox from '@/components/TokenAmountsBox'
66
import { TransactionDisplayProps } from '@/features/transactionsDisplay/transactionDisplayTypes'
77

8-
const TransactionAmounts = ({ tx, referenceAddress }: TransactionDisplayProps) => {
8+
const TransactionAmounts = ({
9+
tx,
10+
referenceAddress,
11+
skipCaching
12+
}: TransactionDisplayProps & { skipCaching?: boolean }) => {
913
const { alphAmount, tokenAmounts } = useTransactionAmountDeltas(tx, referenceAddress)
10-
const assetAmounts = useMemo(
11-
() => (alphAmount !== BigInt(0) ? [{ id: ALPH.id, amount: alphAmount }, ...tokenAmounts] : tokenAmounts),
12-
[alphAmount, tokenAmounts]
13-
)
14+
const assetAmounts = useMemo(() => {
15+
const _tokenAmounts = tokenAmounts.map((t) => ({ ...t, id: skipCaching ? `${NOCACHE_PREFIX}${t.id}` : t.id }))
16+
return alphAmount !== BigInt(0) ? [{ id: ALPH.id, amount: alphAmount }, ..._tokenAmounts] : _tokenAmounts
17+
}, [alphAmount, skipCaching, tokenAmounts])
1418

1519
const infoType = useTransactionInfoType({ tx, referenceAddress: referenceAddress, view: 'wallet' })
1620

apps/desktop-wallet/src/features/walletConnect/SignExecuteScriptTxModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const SimulatedResult = ({
120120
<SectionTitle>{t('Simulated result')}</SectionTitle>
121121
{isRelevant ? (
122122
<>
123-
<TransactionSummaryStyled tx={unsignedData} referenceAddress={txParams.signerAddress} hideType />
123+
<TransactionSummaryStyled tx={unsignedData} referenceAddress={txParams.signerAddress} hideType skipCaching />
124124
<Box hasBg hasHorizontalPadding>
125125
<AddressesDataRows tx={unsignedData} referenceAddress={txParams.signerAddress} />
126126
</Box>

apps/mobile-wallet/src/features/ecosystem/modals/SignExecuteScriptTxModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ const SimulatedResult = ({
152152
/>
153153
</Row>
154154
)}
155-
<TransactionAmounts tx={unsignedData} referenceAddress={txParams.signerAddress} isLast />
155+
<TransactionAmounts tx={unsignedData} referenceAddress={txParams.signerAddress} isLast skipCaching />
156156
</>
157157
) : (
158158
<AppText style={{ textAlign: 'center', marginTop: DEFAULT_MARGIN }} color="secondary">

apps/mobile-wallet/src/features/transactionsDisplay/TransactionModal.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,16 @@ const TransactionStatus = ({ tx }: Pick<TransactionModalSubcomponentProps, 'tx'>
193193

194194
interface TransactionAmountsProps extends TransactionModalSubcomponentProps {
195195
isLast?: boolean
196+
skipCaching?: boolean
196197
}
197198

198-
export const TransactionAmounts = ({ tx, referenceAddress, isLast }: TransactionAmountsProps) => {
199+
export const TransactionAmounts = ({ tx, referenceAddress, isLast, skipCaching }: TransactionAmountsProps) => {
199200
const { t } = useTranslation()
200201
const theme = useTheme()
201202
const dispatch = useAppDispatch()
202203
const {
203204
data: { fungibleTokens, nfts, nsts }
204-
} = useFetchTransactionTokens(tx, referenceAddress)
205+
} = useFetchTransactionTokens(tx, referenceAddress, skipCaching)
205206
const infoType = useTransactionInfoType({ tx, referenceAddress, view: 'wallet' })
206207

207208
const groupedFtAmounts = useMemo(

packages/shared-react/src/api/apiDataHooks/token/useFetchToken.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { useQuery } from '@tanstack/react-query'
44
import { tokenQuery } from '@/api/queries/tokenQueries'
55
import { useCurrentlyOnlineNetworkId } from '@/network'
66

7-
export const useFetchToken = (id: TokenId) => {
7+
export const useFetchToken = (id: TokenId, skipCaching?: boolean) => {
88
const networkId = useCurrentlyOnlineNetworkId()
99

10-
const { data, isLoading } = useQuery(tokenQuery({ id, networkId }))
10+
const { data, isLoading } = useQuery(tokenQuery({ id, networkId, skipCaching }))
1111

1212
return {
1313
data,

packages/shared-react/src/api/apiDataHooks/transaction/useFetchTransactionTokens.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ type TransactionTokens = {
3737

3838
export const useFetchTransactionTokens = (
3939
tx: e.Transaction | e.PendingTransaction | SentTransaction | ExecuteScriptTx,
40-
addressHash: AddressHash
40+
addressHash: AddressHash,
41+
skipCaching: boolean = false
4142
): TransactionTokens => {
4243
const networkId = useCurrentlyOnlineNetworkId()
4344
const { alphAmount, tokenAmounts } = useTransactionAmountDeltas(tx, addressHash)
4445

4546
const { data: tokens, isLoading } = useQueries({
46-
queries: tokenAmounts.map(({ id }) => tokenQuery({ id, networkId })),
47+
queries: tokenAmounts.map(({ id }) => tokenQuery({ id, networkId, skipCaching })),
4748
combine: (results) => combineTokens(results, tokenAmounts)
4849
})
4950

packages/shared-react/src/api/apiUtils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AddressWithGroup,
33
getMissingBalancesChainedTxParams,
44
getTransactionExpectedBalances,
5+
ONE_MINUTE_MS,
56
TokenId,
67
TransactionParams,
78
UnlistedFT
@@ -16,11 +17,12 @@ interface GetQueryConfigProps {
1617
gcTime: number
1718
staleTime?: number
1819
networkId?: number
20+
skipCaching?: boolean
1921
}
2022

21-
export const getQueryConfig = ({ staleTime, gcTime, networkId }: GetQueryConfigProps) => ({
22-
staleTime,
23-
gcTime,
23+
export const getQueryConfig = ({ staleTime, gcTime, networkId, skipCaching }: GetQueryConfigProps) => ({
24+
staleTime: skipCaching ? 0 : staleTime,
25+
gcTime: skipCaching ? ONE_MINUTE_MS : gcTime,
2426
meta: { isMainnet: networkId === 0 }
2527
})
2628

packages/shared-react/src/api/queries/tokenQueries.ts

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const StdInterfaceIds = Object.values(e.TokenStdInterfaceId)
2828
interface TokenQueryProps extends SkipProp {
2929
id: TokenId
3030
networkId?: number
31+
skipCaching?: boolean
3132
}
3233

3334
interface NFTQueryProps extends TokenQueryProps {
@@ -82,12 +83,12 @@ export const ftListQuery = ({ networkId, skip }: Omit<TokenQueryProps, 'id'>) =>
8283
})
8384
}
8485

85-
export const tokenTypeQuery = ({ id, networkId, skip }: TokenQueryProps) =>
86+
export const tokenTypeQuery = ({ id, networkId, skip, skipCaching }: TokenQueryProps) =>
8687
queryOptions({
87-
queryKey: ['token', 'type', id],
88+
queryKey: ['token', 'type', getId(id, skipCaching)],
8889
// We always want to remember the type of a token, even when user navigates for too long from components that use
8990
// tokens.
90-
...getQueryConfig({ staleTime: Infinity, gcTime: Infinity, networkId }),
91+
...getQueryConfig({ staleTime: Infinity, gcTime: Infinity, networkId, skipCaching }),
9192
queryFn:
9293
!skip && networkId !== undefined
9394
? async () => {
@@ -127,10 +128,10 @@ export const combineTokenTypes = (tokenTypes: (e.TokenInfo | { data: e.TokenInfo
127128
} as Record<e.TokenStdInterfaceId, TokenId[]>
128129
)
129130

130-
export const fungibleTokenMetadataQuery = ({ id, networkId, skip }: TokenQueryProps) =>
131+
export const fungibleTokenMetadataQuery = ({ id, networkId, skip, skipCaching }: TokenQueryProps) =>
131132
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 }),
134135
queryFn: !skip
135136
? async () => {
136137
const tokenMetadata = await batchers.ftMetadataBatcher.fetch(id)
@@ -140,18 +141,18 @@ export const fungibleTokenMetadataQuery = ({ id, networkId, skip }: TokenQueryPr
140141
: skipToken
141142
})
142143

143-
export const nftMetadataQuery = ({ id, networkId, skip }: TokenQueryProps) =>
144+
export const nftMetadataQuery = ({ id, networkId, skip, skipCaching }: TokenQueryProps) =>
144145
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 }),
147148
queryFn: !skip ? async () => (await batchers.nftMetadataBatcher.fetch(id)) ?? null : skipToken
148149
})
149150

150-
export const nftDataQuery = ({ id, tokenUri, networkId, skip }: NFTQueryProps) =>
151+
export const nftDataQuery = ({ id, tokenUri, networkId, skip, skipCaching }: NFTQueryProps) =>
151152
queryOptions({
152-
queryKey: ['token', 'non-fungible', 'data', id],
153+
queryKey: ['token', 'non-fungible', 'data', getId(id, skipCaching)],
153154
// 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 }),
155156
queryFn:
156157
!skip && !!tokenUri
157158
? async () => {
@@ -203,10 +204,21 @@ export const nftDataQuery = ({ id, tokenUri, networkId, skip }: NFTQueryProps) =
203204
: skipToken
204205
})
205206

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+
}),
210222
queryFn: async (): Promise<Token> => {
211223
const nst = { id } as NonStandardToken
212224

@@ -218,18 +230,18 @@ export const tokenQuery = ({ id, networkId, skip }: TokenQueryProps) =>
218230
if (listedFT) return listedFT
219231

220232
// 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 }))
222234

223235
// 3. If it is a fungible token, fetch the fungible token metadata
224236
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 }))
226238

227239
return ftMetadata ?? nst
228240
}
229241

230242
// 4. If it is an NFT, fetch the NFT metadata and data
231243
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 }))
233245

234246
return nft ?? nst
235247
}
@@ -246,21 +258,26 @@ export const tokenQuery = ({ id, networkId, skip }: TokenQueryProps) =>
246258
},
247259
enabled: !skip
248260
})
261+
}
249262

250-
export const nftQuery = ({ id, networkId, skip }: TokenQueryProps) =>
263+
export const nftQuery = ({ id, networkId, skip, skipCaching }: TokenQueryProps) =>
251264
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 }),
254267
queryFn: async (): Promise<NFT | null> => {
255-
const nftMetadata = await queryClient.fetchQuery(nftMetadataQuery({ id, networkId }))
268+
const nftMetadata = await queryClient.fetchQuery(nftMetadataQuery({ id, networkId, skipCaching }))
256269

257270
if (!nftMetadata) return null
258271

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+
)
260275

261276
if (!nftData) return null
262277

263278
return { ...nftData, ...nftMetadata }
264279
},
265280
enabled: !skip
266281
})
282+
283+
const getId = (id: string, skipCaching?: boolean) => `${skipCaching ? NOCACHE_PREFIX : ''}${id}`

0 commit comments

Comments
 (0)