-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new hook to get sharedobjects
- Loading branch information
1 parent
364a26e
commit 662b862
Showing
3 changed files
with
112 additions
and
53 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
64 changes: 64 additions & 0 deletions
64
apps/wallet/src/ui/app/hooks/useGetSharedObjectsMultipleAddresses.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,64 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useInfiniteQuery } from '@tanstack/react-query'; | ||
import { | ||
mapStardustBasicOutputs, | ||
mapStardustNftOutputs, | ||
useStardustIndexerClientContext, | ||
} from '@iota/core'; | ||
|
||
const MAX_OBJECTS_PER_REQ = 6; | ||
|
||
export function useGetSharedObjectsMultipleAddresses( | ||
addresses: string[], | ||
pageSize: number = MAX_OBJECTS_PER_REQ, | ||
) { | ||
const { stardustIndexerClient } = useStardustIndexerClientContext(); | ||
|
||
return useInfiniteQuery({ | ||
initialPageParam: { page: 1 }, | ||
queryKey: ['get-shared-objects', addresses, pageSize, stardustIndexerClient?.baseUrl], | ||
queryFn: async ({ pageParam }) => { | ||
try { | ||
const responses = await Promise.all( | ||
addresses.map(async (address) => { | ||
const nftOutputs = await stardustIndexerClient?.getNftResolvedOutputs( | ||
address, | ||
{ | ||
page: pageParam?.page, | ||
pageSize, | ||
}, | ||
); | ||
|
||
const basicOutputs = await stardustIndexerClient?.getBasicResolvedOutputs( | ||
address, | ||
{ | ||
page: pageParam?.page, | ||
pageSize, | ||
}, | ||
); | ||
|
||
return { | ||
address, | ||
nftOutputs: nftOutputs ? nftOutputs.map(mapStardustNftOutputs) : [], | ||
basicOutputs: basicOutputs | ||
? basicOutputs.map(mapStardustBasicOutputs) | ||
: [], | ||
}; | ||
}), | ||
); | ||
|
||
return responses; | ||
} catch (error) { | ||
return []; | ||
} | ||
}, | ||
staleTime: 10 * 1000, | ||
enabled: addresses.length > 0, | ||
getNextPageParam: (lastPage) => { | ||
const nextPage = lastPage.find((res) => res.nftOutputs.length === pageSize); | ||
return nextPage ? { page: lastPage.length + 1 } : null; | ||
}, | ||
}); | ||
} |