-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of API data transformation
Related to #1010
- Loading branch information
Showing
14 changed files
with
257 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright 2018 - 2024 The Alephium Authors | ||
This file is part of the alephium project. | ||
The library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
The library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { ReactNode } from 'react' | ||
|
||
import { UseFetchWalletBalancesTokensArrayContextProvider } from '@/api/apiDataHooks/wallet/useFetchWalletBalancesTokensArray' | ||
import { UseFetchWalletBalancesTokensByAddressContextProvider } from '@/api/apiDataHooks/wallet/useFetchWalletBalancesTokensByAddress' | ||
|
||
const ApiContextProvider = ({ children }: { children: ReactNode }) => ( | ||
<UseFetchWalletBalancesTokensArrayContextProvider> | ||
<UseFetchWalletBalancesTokensByAddressContextProvider> | ||
{children} | ||
</UseFetchWalletBalancesTokensByAddressContextProvider> | ||
</UseFetchWalletBalancesTokensArrayContextProvider> | ||
) | ||
|
||
export default ApiContextProvider |
48 changes: 48 additions & 0 deletions
48
apps/desktop-wallet/src/api/apiDataHooks/utils/useFetchWalletBalancesTokens.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright 2018 - 2024 The Alephium Authors | ||
This file is part of the alephium project. | ||
The library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
The library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { useQueries, UseQueryResult } from '@tanstack/react-query' | ||
|
||
import { addressTokensBalancesQuery, AddressTokensBalancesQueryFnData } from '@/api/queries/addressQueries' | ||
import { useAppSelector } from '@/hooks/redux' | ||
import { useUnsortedAddressesHashes } from '@/hooks/useAddresses' | ||
import { selectCurrentlyOnlineNetworkId } from '@/storage/network/networkSelectors' | ||
|
||
export const useFetchWalletBalancesTokens = <T>( | ||
combine: (results: UseQueryResult<AddressTokensBalancesQueryFnData>[]) => { | ||
data: T | ||
isLoading: boolean | ||
isFetching?: boolean | ||
error?: boolean | ||
} | ||
) => { | ||
const networkId = useAppSelector(selectCurrentlyOnlineNetworkId) | ||
const allAddressHashes = useUnsortedAddressesHashes() | ||
|
||
const { data, isLoading, isFetching, error } = useQueries({ | ||
queries: allAddressHashes.map((addressHash) => addressTokensBalancesQuery({ addressHash, networkId })), | ||
combine | ||
}) | ||
|
||
return { | ||
data, | ||
isLoading, | ||
isFetching, | ||
error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 0 additions & 109 deletions
109
apps/desktop-wallet/src/api/apiDataHooks/wallet/useFetchWalletBalancesTokens.ts
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
apps/desktop-wallet/src/api/apiDataHooks/wallet/useFetchWalletBalancesTokensArray.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2018 - 2024 The Alephium Authors | ||
This file is part of the alephium project. | ||
The library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
The library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { UseQueryResult } from '@tanstack/react-query' | ||
import { createContext, ReactNode, useContext, useMemo } from 'react' | ||
|
||
import { combineError, combineIsFetching, combineIsLoading } from '@/api/apiDataHooks/apiDataHooksUtils' | ||
import { useFetchWalletBalancesTokens } from '@/api/apiDataHooks/utils/useFetchWalletBalancesTokens' | ||
import { ApiContextProps } from '@/api/apiTypes' | ||
import { AddressTokensBalancesQueryFnData } from '@/api/queries/addressQueries' | ||
import { ApiBalances, TokenApiBalances, TokenId } from '@/types/tokens' | ||
|
||
const useFetchWalletBalancesTokensArray = () => useContext(UseFetchWalletBalancesTokensArrayContext) | ||
|
||
export default useFetchWalletBalancesTokensArray | ||
|
||
export const UseFetchWalletBalancesTokensArrayContextProvider = ({ children }: { children: ReactNode }) => { | ||
const { data, isLoading, isFetching, error } = useFetchWalletBalancesTokens(combineBalancesToArray) | ||
|
||
const value = useMemo(() => ({ data, isLoading, isFetching, error }), [data, isLoading, isFetching, error]) | ||
|
||
return ( | ||
<UseFetchWalletBalancesTokensArrayContext.Provider value={value}> | ||
{children} | ||
</UseFetchWalletBalancesTokensArrayContext.Provider> | ||
) | ||
} | ||
|
||
const UseFetchWalletBalancesTokensArrayContext = createContext<ApiContextProps<TokenApiBalances[]>>({ | ||
data: [], | ||
isLoading: false, | ||
isFetching: false, | ||
error: false | ||
}) | ||
|
||
const combineBalancesToArray = (results: UseQueryResult<AddressTokensBalancesQueryFnData>[]) => { | ||
const tokenBalancesByToken = results.reduce( | ||
(tokensBalances, { data: balances }) => { | ||
balances?.balances.forEach(({ id, totalBalance, lockedBalance, availableBalance }) => { | ||
tokensBalances[id] = { | ||
totalBalance: (BigInt(totalBalance) + BigInt(tokensBalances[id]?.totalBalance ?? 0)).toString(), | ||
lockedBalance: (BigInt(lockedBalance) + BigInt(tokensBalances[id]?.lockedBalance ?? 0)).toString(), | ||
availableBalance: (BigInt(availableBalance) + BigInt(tokensBalances[id]?.availableBalance ?? 0)).toString() | ||
} | ||
}) | ||
return tokensBalances | ||
}, | ||
{} as Record<TokenId, ApiBalances | undefined> | ||
) | ||
|
||
return { | ||
data: Object.keys(tokenBalancesByToken).map((id) => ({ | ||
id, | ||
...tokenBalancesByToken[id] | ||
})) as TokenApiBalances[], | ||
...combineIsLoading(results), | ||
...combineIsFetching(results), | ||
...combineError(results) | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
apps/desktop-wallet/src/api/apiDataHooks/wallet/useFetchWalletBalancesTokensByAddress.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright 2018 - 2024 The Alephium Authors | ||
This file is part of the alephium project. | ||
The library is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
The library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with the library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { AddressHash } from '@alephium/shared' | ||
import { UseQueryResult } from '@tanstack/react-query' | ||
import { createContext, ReactNode, useContext, useMemo } from 'react' | ||
|
||
import { combineIsLoading } from '@/api/apiDataHooks/apiDataHooksUtils' | ||
import { useFetchWalletBalancesTokens } from '@/api/apiDataHooks/utils/useFetchWalletBalancesTokens' | ||
import { ApiContextProps } from '@/api/apiTypes' | ||
import { AddressTokensBalancesQueryFnData } from '@/api/queries/addressQueries' | ||
import { TokenApiBalances } from '@/types/tokens' | ||
|
||
const useFetchWalletBalancesTokensByAddress = () => useContext(UseFetchWalletBalancesTokensByAddressContext) | ||
|
||
export default useFetchWalletBalancesTokensByAddress | ||
|
||
export const UseFetchWalletBalancesTokensByAddressContextProvider = ({ children }: { children: ReactNode }) => { | ||
const { data, isLoading, isFetching, error } = useFetchWalletBalancesTokens(combineBalancesByAddress) | ||
|
||
const value = useMemo(() => ({ data, isLoading, isFetching, error }), [data, isLoading, isFetching, error]) | ||
|
||
return ( | ||
<UseFetchWalletBalancesTokensByAddressContext.Provider value={value}> | ||
{children} | ||
</UseFetchWalletBalancesTokensByAddressContext.Provider> | ||
) | ||
} | ||
|
||
const UseFetchWalletBalancesTokensByAddressContext = createContext< | ||
ApiContextProps<Record<AddressHash, TokenApiBalances[] | undefined>> | ||
>({ | ||
data: {}, | ||
isLoading: false, | ||
isFetching: false, | ||
error: false | ||
}) | ||
|
||
const combineBalancesByAddress = (results: UseQueryResult<AddressTokensBalancesQueryFnData>[]) => ({ | ||
data: results.reduce( | ||
(acc, { data }) => { | ||
if (data) { | ||
acc[data.addressHash] = data.balances | ||
} | ||
return acc | ||
}, | ||
{} as Record<AddressHash, TokenApiBalances[] | undefined> | ||
), | ||
...combineIsLoading(results) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.