Skip to content

feat: enhance attribute calc on mass mint #11374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/gallery/GalleryItemDescription.vue
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ const propertiesTabDisabled = computed(() => {
return !nftMetadata.value?.attributes?.length
})

const { getAttributeRarity } = useCollectionAttributes(computed(() => propertiesTabDisabled.value ? undefined : nft.value?.collection?.id))
const { getAttributeRarity } = useCollectionAttributes({
collectionId: computed(() => propertiesTabDisabled.value ? undefined : nft.value?.collection?.id),
})

const properties = computed<{ trait_type: string, value: string, rarity: number }[]>(() => {
const attributes = (nftMetadata.value?.attributes || []) as Array<{
Expand Down
1 change: 1 addition & 0 deletions components/massmint/Massmint.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<OverviewTable
:disabled="!mediaLoaded"
:nfts="NFTS"
:collection-id="selectedCollection?.id"
@open-side-bar-with="openSideBarWith"
@delete="openDeleteModalWith"
/>
Expand Down
41 changes: 9 additions & 32 deletions components/massmint/OverviewTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
class="flex items-center gap-2"
>
<span>{{ attr.trait_type }}: <span class="font-bold">{{ attr.value }}</span></span>
<span class="text-k-blue">{{ allNftAttributesRarityMaps[attr.trait_type]?.[attr.value] }}%</span>
<span class="text-k-blue">{{ getAttributeRarity(attr.trait_type, attr.value) }}%</span>
</div>
</div>
<div v-else>
Expand Down Expand Up @@ -157,47 +157,24 @@ const props = withDefaults(
defineProps<{
disabled?: boolean
nfts?: NFTS
collectionId?: string
}>(),
{
disabled: false,
nfts: undefined,
nfts: () => ({}),
collectionId: undefined,
},
)

const { getAttributeRarity } = useCollectionAttributes({
collectionId: computed(() => props.collectionId),
extraNfts: computed(() => Object.values(props.nfts)),
})

const displayedNFTS = computed<NFT[]>(() =>
Object.values(props.nfts).slice(0, offset.value).map(addStatus),
)

const allNftAttributesRarityMaps = computed(() => {
const attributeCounts: Record<string, Record<string, number>> = {}
const nfts = Object.values(props.nfts || {})
nfts.forEach((nft) => {
if (!nft.attributes?.length) return

nft.attributes.forEach((attr) => {
if (!attributeCounts[attr.trait_type]) {
attributeCounts[attr.trait_type] = {}
}

attributeCounts[attr.trait_type][attr.value]
= (attributeCounts[attr.trait_type][attr.value] || 0) + 1
})
})

const rarityMaps: Record<string, Record<string, number>> = {}
const totalNfts = nfts?.length || 0

Object.entries(attributeCounts).forEach(([traitType, valueCounts]) => {
rarityMaps[traitType] = {}

Object.entries(valueCounts).forEach(([value, count]) => {
rarityMaps[traitType][value] = parseFloat((count / totalNfts * 100).toFixed(1))
})
})

return rarityMaps
})

const openSideBarWith = (nft: NFT) => {
emit('openSideBarWith', nft)
}
Expand Down
27 changes: 21 additions & 6 deletions composables/useCollectionAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import { useQuery } from '@tanstack/vue-query'
import type { NFT, Attribute } from '@/types'
import type { Attribute } from '@/types'
import type { NFT } from '@/components/massmint/types'

export const useCollectionAttributes = (collectionId: ComputedRef<string | undefined>) => {
const { data: nftsListData } = useQuery<NFT[]>({
type NftWithAttributes = { meta?: { attributes?: Attribute[] } }

export const useCollectionAttributes = ({ collectionId, extraNfts }: { collectionId: ComputedRef<string | undefined>, extraNfts?: ComputedRef<NFT[]> }) => {
const { data: nftsListData } = useQuery<NftWithAttributes[]>({
queryKey: ['nft-attributes-by-collection', collectionId],
queryFn: async () =>
(collectionId.value
? (await useAsyncGraphql<{ nfts: NFT[] } >({
? (await useAsyncGraphql<{ nfts: NftWithAttributes[] } >({
query: 'nftAttributesListByCollection',
variables: { id: collectionId.value },
})).data.value.nfts
: []),
})

const nftsList = computed(() => nftsListData.value || [])
const nftsList = computed(() => (nftsListData.value || []).concat(
extraNfts?.value
.map(nft => ({ meta: {
attributes:
nft.attributes
?.filter(attr => String(attr.trait_type) && String(attr.value))
.map(attr => ({
trait: attr.trait_type,
value: String(attr.value),
})),
} }))
|| [],
))

const attributesList = computed<Attribute[]>(() => {
return (nftsList.value || []).reduce((acc, nft) => {
Expand Down Expand Up @@ -52,7 +67,7 @@ export const useCollectionAttributes = (collectionId: ComputedRef<string | undef
)
})

const getAttributeRarity = (trait: string, value: string) => {
const getAttributeRarity = (trait: string, value: string | number) => {
return attributesRarityMaps.value[trait]?.[value] || 0
}

Expand Down