Skip to content

Commit

Permalink
show omniflix NFTs on omniflix treasury tab
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Oct 17, 2024
1 parent ed12a3b commit de7af16
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 45 deletions.
76 changes: 55 additions & 21 deletions packages/state/query/queries/omniflix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,29 @@ export const fetchAllOnfts = async (
queryClient: QueryClient,
{
chainId,
id,
id = '',
owner,
}: {
chainId: string
id: string
/**
* Optionally filter by collection.
*/
id?: string
/**
* Optionally filter by owner, returning only those owned by this address.
*/
owner?: string
}
): Promise<ONFT[]> => {
): Promise<
{
collection: OnftCollectionInfo
onfts: ONFT[]
}[]
> => {
const client = await omniflixProtoRpcClientRouter.connect(chainId)

if (owner) {
const onftIds = (
const collections = (
await getAllRpcResponse(
client.onft.v1beta1.ownerONFTs,
{
Expand All @@ -130,43 +138,69 @@ export const fetchAllOnfts = async (
},
'owner'
)
).flatMap((owner) => owner?.idCollections.flatMap((c) => c.onftIds) || [])
).flatMap((owner) => owner?.idCollections || [])

return await Promise.all(
onftIds.map((tokenId) =>
queryClient.fetchQuery(
omniflixQueries.onft({
collections.map(async ({ denomId, onftIds }) => ({
collection: await queryClient.fetchQuery(
omniflixQueries.onftCollectionInfo({
chainId,
collectionId: id,
tokenId,
id: denomId,
})
)
)
),
onfts: await Promise.all(
onftIds.map((tokenId) =>
queryClient.fetchQuery(
omniflixQueries.onft({
chainId,
collectionId: denomId,
tokenId,
})
)
)
),
}))
)
} else {
const onfts = (
const collections = (
await getAllRpcResponse(
client.onft.v1beta1.collection,
{
denomId: id,
},
'collection'
)
).flatMap((collection) => collection?.onfts || [])
).flatMap((collection) =>
collection?.denom
? {
collection: collection.denom,
onfts: collection.onfts,
}
: []
)

// Pre-cache loaded ONFTs.
onfts.forEach((onft: ONFT) => {
// Pre-cache loaded collections and ONFTs.
collections.forEach(({ collection, onfts }) => {
queryClient.setQueryData(
omniflixQueries.onft({
omniflixQueries.onftCollectionInfo({
chainId,
collectionId: id,
tokenId: onft.id,
id: collection.id,
}).queryKey,
onft
collection
)
onfts.forEach((onft) => {
queryClient.setQueryData(
omniflixQueries.onft({
chainId,
collectionId: collection.id,
tokenId: onft.id,
}).queryKey,
onft
)
})
})

return onfts
return collections
}
}

Expand Down
79 changes: 61 additions & 18 deletions packages/state/recoil/selectors/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ChainId,
GenericToken,
LazyNftCardInfo,
LoadingDataWithError,
LoadingNfts,
NftCardInfo,
NftUriData,
Expand All @@ -13,6 +14,7 @@ import {
import {
MAINNET,
STARGAZE_URL_BASE,
combineLoadingDataWithErrors,
getNftKey,
nftCardInfoFromStargazeIndexerNft,
transformIpfsUrlToHttpsIfNecessary,
Expand Down Expand Up @@ -426,6 +428,38 @@ export const lazyNftCardInfosForDaoSelector = selectorFamily<
})
)

const queryClient = get(queryClientAtom)
const allOnfts =
chainId === ChainId.OmniflixHubMainnet ||
chainId === ChainId.OmniflixHubTestnet
? await queryClient.fetchQuery(
omniflixQueries.allOnfts(queryClient, {
chainId,
owner: coreAddress,
})
)
: []

const startingAllNfts: LoadingNfts<LazyNftCardInfo> = allOnfts.length
? {
[chainId]: {
loading: false,
errored: false,
updating: false,
data: allOnfts.flatMap(({ collection, onfts }) =>
onfts.map(
(onft): LazyNftCardInfo => ({
key: getNftKey(chainId, collection.id, onft.id),
chainId,
tokenId: onft.id,
collectionAddress: collection.id,
})
)
),
},
}
: {}

return Object.entries(allNfts).reduce(
(acc, [chainId, { owners, collectionAddresses }]) => {
collectionAddresses = Array.from(new Set(collectionAddresses))
Expand Down Expand Up @@ -461,28 +495,37 @@ export const lazyNftCardInfosForDaoSelector = selectorFamily<
: []
)

const newChainLoadingNfts: LoadingDataWithError<LazyNftCardInfo[]> =
nftCollectionTokenIds.length > 0 &&
nftCollectionTokenIds.every(
(loadable) => loadable.state === 'loading'
)
? {
loading: true,
errored: false,
}
: {
loading: false,
errored: false,
updating: nftCollectionTokenIds.some(
(loadable) => loadable.state === 'loading'
),
data: lazyNftCardProps,
}

const existingChainLoadingNfts = acc[chainId] ? [acc[chainId]] : []

const loadingNfts = combineLoadingDataWithErrors(
newChainLoadingNfts,
...existingChainLoadingNfts
)

return {
...acc,
[chainId]:
nftCollectionTokenIds.length > 0 &&
nftCollectionTokenIds.every(
(loadable) => loadable.state === 'loading'
)
? {
loading: true,
errored: false,
}
: {
loading: false,
errored: false,
updating: nftCollectionTokenIds.some(
(loadable) => loadable.state === 'loading'
),
data: lazyNftCardProps,
},
[chainId]: loadingNfts,
}
},
{} as LoadingNfts<LazyNftCardInfo>
startingAllNfts
)
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,11 @@ export const useStakingInfo = ({
})

const loadingWalletUnstakedNfts = useCachedLoadingWithError(
!loadingWalletUnstakedOnfts.loading && !loadingWalletUnstakedOnfts.errored
!loadingWalletUnstakedOnfts.loading &&
!loadingWalletUnstakedOnfts.errored &&
loadingWalletUnstakedOnfts.data.length === 1
? waitForAll(
loadingWalletUnstakedOnfts.data.map(({ id }) =>
loadingWalletUnstakedOnfts.data[0].onfts.map(({ id }) =>
nftCardInfoSelector({
chainId: votingModule.chainId,
collection: collectionAddress,
Expand Down
5 changes: 1 addition & 4 deletions packages/stateless/components/dao/tabs/TreasuryTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,7 @@ export const TreasuryTab = <T extends TokenCardInfo, N extends object>({
/>
)}

{/* OmniFlix Hub NFTs are not yet supported. */}
{currentChainId !== ChainId.OmniflixHubMainnet && (
<NftSection NftCard={NftCard} className="mt-10" nfts={allNfts} />
)}
<NftSection NftCard={NftCard} className="mt-10" nfts={allNfts} />

{connected && !!depositFiatChainId && (
<FiatDepositModal
Expand Down

0 comments on commit de7af16

Please sign in to comment.