Skip to content

Commit

Permalink
Merge pull request #906 from alephium/2.4.1-rc-fixxes
Browse files Browse the repository at this point in the history
2.4.1 rc fixxes
  • Loading branch information
nop33 authored Oct 22, 2024
2 parents 80995f7 + 5f78c3e commit 1f50bf0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ import { useQuery } from '@tanstack/react-query'
import { SkipProp } from '@/api/apiDataHooks/apiDataHooksTypes'
import { tokensPriceQuery } from '@/api/queries/priceQueries'
import { useAppSelector } from '@/hooks/redux'
import { selectCurrentlyOnlineNetworkId } from '@/storage/settings/networkSelectors'

const pricedTokens = Object.keys(explorer.TokensWithPrice)

const useFetchTokenPrices = (props?: SkipProp) => {
const fiatCurrency = useAppSelector((s) => s.settings.fiatCurrency)
const networkIsOffline = useAppSelector(selectCurrentlyOnlineNetworkId) === undefined

const { data, isLoading } = useQuery(
tokensPriceQuery({ symbols: pricedTokens, currency: fiatCurrency.toLowerCase(), skip: props?.skip })
tokensPriceQuery({
symbols: pricedTokens,
currency: fiatCurrency.toLowerCase(),
skip: props?.skip || networkIsOffline
})
)

return {
Expand All @@ -42,9 +48,10 @@ export default useFetchTokenPrices

export const useFetchTokenPrice = (symbol: string) => {
const fiatCurrency = useAppSelector((s) => s.settings.fiatCurrency)
const networkIsOffline = useAppSelector(selectCurrentlyOnlineNetworkId) === undefined

const { data, isLoading } = useQuery({
...tokensPriceQuery({ symbols: pricedTokens, currency: fiatCurrency.toLowerCase() }),
...tokensPriceQuery({ symbols: pricedTokens, currency: fiatCurrency.toLowerCase(), skip: networkIsOffline }),
select: (data) => data.find((tokenPrice) => tokenPrice.symbol === symbol)?.price
})

Expand Down
17 changes: 9 additions & 8 deletions apps/desktop-wallet/src/api/queries/tokenQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ export const tokenTypeQuery = ({ id, networkId, skip }: TokenQueryProps) =>
// We always want to remember the type of a token, even when user navigates for too long from components that use
// tokens.
...getQueryConfig({ staleTime: Infinity, gcTime: Infinity, networkId }),
queryFn: !skip
? async () => {
const tokenInfo = await batchers.tokenTypeBatcher.fetch(id)
queryFn:
!skip && networkId !== undefined
? async () => {
const tokenInfo = await batchers.tokenTypeBatcher.fetch(id)

return tokenInfo?.stdInterfaceId
? { ...tokenInfo, stdInterfaceId: tokenInfo.stdInterfaceId as TokenStdInterfaceId }
: null
}
: skipToken
return tokenInfo?.stdInterfaceId
? { ...tokenInfo, stdInterfaceId: tokenInfo.stdInterfaceId as TokenStdInterfaceId }
: null
}
: skipToken
})

export const combineTokenTypeQueryResults = (results: UseQueryResult<TokenInfo | null>[]) => ({
Expand Down
10 changes: 9 additions & 1 deletion apps/desktop-wallet/src/api/queries/transactionQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { AddressHash, FIVE_MINUTES_MS, throttledClient } from '@alephium/shared'
import { sleep } from '@alephium/web3'
import { Transaction } from '@alephium/web3/dist/src/api/api-explorer'
import { infiniteQueryOptions, queryOptions, skipToken } from '@tanstack/react-query'

Expand Down Expand Up @@ -155,5 +156,12 @@ export const pendingTransactionQuery = ({ txHash, networkId, skip }: Transaction
// just for this one, but is it worth it?
...getQueryConfig({ gcTime: FIVE_MINUTES_MS, networkId }),
refetchInterval: 3000,
queryFn: !skip ? () => throttledClient.explorer.transactions.getTransactionsTransactionHash(txHash) : skipToken
queryFn: !skip
? async () => {
// Delay initial query to give the tx some time to enter the mempool instead of getting 404's
if (!queryClient.getQueryData(['transaction', 'pending', txHash])) await sleep(3000)

return throttledClient.explorer.transactions.getTransactionsTransactionHash(txHash)
}
: skipToken
})
55 changes: 30 additions & 25 deletions apps/desktop-wallet/src/features/historicChart/useHistoricData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { AddressHash, CHART_DATE_FORMAT, ONE_DAY_MS, throttledClient, TokenHisto
import { ALPH } from '@alephium/token-list'
import { explorer } from '@alephium/web3'
import { AmountHistory } from '@alephium/web3/dist/src/api/api-explorer'
import { useQueries, useQuery, UseQueryResult } from '@tanstack/react-query'
import { skipToken, useQueries, useQuery, UseQueryResult } from '@tanstack/react-query'
import dayjs from 'dayjs'

import { combineIsLoading } from '@/api/apiDataHooks/apiDataHooksUtils'
Expand All @@ -42,31 +42,36 @@ const useHistoricData = () => {
queryKey: ['history', 'price', ALPH.symbol, { currency }],
// We don't want to delete the price history if the user stays on a page without a chart for too long
...getQueryConfig({ staleTime: ONE_DAY_MS, gcTime: Infinity }),
queryFn: () =>
throttledClient.explorer.market.getMarketPricesSymbolCharts(ALPH.symbol, { currency }).then((rawHistory) => {
const today = dayjs().format(CHART_DATE_FORMAT)
const history = [] as TokenHistoricalPrice[]

if (rawHistory.timestamps && rawHistory.prices) {
for (let index = 0; index < rawHistory.timestamps.length; index++) {
const timestamp = rawHistory.timestamps[index]
const price = rawHistory.prices[index]

const itemDate = dayjs(timestamp).format(CHART_DATE_FORMAT)
const prevItemDate =
index > 1 ? dayjs(rawHistory.timestamps[index - 1]).format(CHART_DATE_FORMAT) : undefined

if (itemDate !== prevItemDate && itemDate !== today) {
history.push({
date: itemDate,
value: price
})
}
}
}
queryFn:
networkId !== undefined
? () =>
throttledClient.explorer.market
.getMarketPricesSymbolCharts(ALPH.symbol, { currency })
.then((rawHistory) => {
const today = dayjs().format(CHART_DATE_FORMAT)
const history = [] as TokenHistoricalPrice[]

if (rawHistory.timestamps && rawHistory.prices) {
for (let index = 0; index < rawHistory.timestamps.length; index++) {
const timestamp = rawHistory.timestamps[index]
const price = rawHistory.prices[index]

const itemDate = dayjs(timestamp).format(CHART_DATE_FORMAT)
const prevItemDate =
index > 1 ? dayjs(rawHistory.timestamps[index - 1]).format(CHART_DATE_FORMAT) : undefined

if (itemDate !== prevItemDate && itemDate !== today) {
history.push({
date: itemDate,
value: price
})
}
}
}

return history
})
return history
})
: skipToken
})

const {
Expand Down
15 changes: 10 additions & 5 deletions apps/desktop-wallet/src/hooks/useAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ import useFetchLatestTransactionOfEachAddress from '@/api/apiDataHooks/wallet/us
import { useFetchWalletBalancesAlphByAddress } from '@/api/apiDataHooks/wallet/useFetchWalletBalancesAlph'
import { useAppSelector } from '@/hooks/redux'
import { selectAllAddressHashes, selectDefaultAddress } from '@/storage/addresses/addressesSelectors'
import { selectCurrentlyOnlineNetworkId } from '@/storage/settings/networkSelectors'

export const useUnsortedAddressesHashes = (): AddressHash[] => useAppSelector(selectAllAddressHashes)

export const useFetchSortedAddressesHashes = (props?: SkipProp) => {
const isNetworkOffline = useAppSelector(selectCurrentlyOnlineNetworkId) === undefined
const allAddressHashes = useUnsortedAddressesHashes()
const { data: sortedAddresses, isLoading } = useFetchSortedAddressesHashesWithLatestTx(props)

const sortedAddressHashes = useMemo(() => sortedAddresses.map(({ addressHash }) => addressHash), [sortedAddresses])

return {
data: !isLoading && !props?.skip ? sortedAddressHashes : allAddressHashes,
data: !isLoading && !props?.skip && !isNetworkOffline ? sortedAddressHashes : allAddressHashes,
isLoading
}
}
Expand Down Expand Up @@ -81,15 +83,18 @@ export const useCappedAddressesHashes = () => {
}

export const useFetchAddressesHashesWithBalance = () => {
const isNetworkOffline = useAppSelector(selectCurrentlyOnlineNetworkId) === undefined
const allAddressHashes = useUnsortedAddressesHashes()
const { data: addressesAlphBalances, isLoading } = useFetchWalletBalancesAlphByAddress()

const filteredAddressHashes = useMemo(
() =>
allAddressHashes.filter(
(addressHash) => addressesAlphBalances[addressHash] && addressesAlphBalances[addressHash].totalBalance > 0
),
[addressesAlphBalances, allAddressHashes]
isNetworkOffline
? allAddressHashes
: allAddressHashes.filter(
(addressHash) => addressesAlphBalances[addressHash] && addressesAlphBalances[addressHash].totalBalance > 0
),
[addressesAlphBalances, allAddressHashes, isNetworkOffline]
)

return {
Expand Down

0 comments on commit 1f50bf0

Please sign in to comment.