Skip to content

Commit

Permalink
🔧 owner calculation (#6243)
Browse files Browse the repository at this point in the history
  • Loading branch information
daiagi authored Jun 18, 2023
1 parent 2e0174e commit 9df9905
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions composables/collectionActivity/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const flipperInitialState = () => ({
totalsold: 0,
})

const newOwnerEntry = (lastActivityTimestamp, nft) => ({
nftCount: 1,
const newOwnerEntry = () => ({
nftCount: 0,
totalBought: 0,
totalSold: 0,
lastActivityTimestamp,
nfts: [nft],
lastActivityTimestamp: -Infinity,
nfts: [],
})

const preProccessForFindingFlippers = (interactions: InteractionWithNFT[]) => {
Expand Down Expand Up @@ -75,12 +75,15 @@ const getLatestPrice = (previousNFTState: NFTHistoryState) =>

const updateOwnerWithNewNft = ({
owner,
latestInteraction,
latestEvent,
lastestTimeStamp,
nft,
}) => {
owner.nftCount++
owner.totalBought += parseInt(latestInteraction.meta)
if (latestEvent.interaction === Interaction.BUY) {
owner.totalBought += parseInt(latestEvent.meta)
}

owner.lastActivityTimestamp =
lastestTimeStamp > owner.lastActivityTimestamp
? lastestTimeStamp
Expand Down Expand Up @@ -108,8 +111,8 @@ export const getOwners = (nfts) => {

nfts.forEach((nft) => {
const interactions = nft.events.map((e) => e.interaction)
const { events, ...nftExcludingEvents } = nft
const owner = owners[nft.currentOwner]
const { events } = nft
const owner = owners[nft.currentOwner] || newOwnerEntry()

if (interactions.includes(Interaction.CONSUME)) {
// no owner
Expand All @@ -120,34 +123,34 @@ export const getOwners = (nfts) => {
interactions.includes(Interaction.SEND)
) {
// NFT changed hands
const latestInteraction = events[events.length - 1]
const lastestTimeStamp = new Date(latestInteraction.timestamp).getTime()

owners[nft.currentOwner] =
owner === undefined
? newOwnerEntry(lastestTimeStamp, nftExcludingEvents)
: updateOwnerWithNewNft({
owner,
latestInteraction,
lastestTimeStamp,
nft,
})
const latestchangeHandsEvent = events.findLast(
(event) =>
event.interaction === Interaction.BUY ||
event.interaction === Interaction.SEND
)
const lastestTimeStamp = new Date(
latestchangeHandsEvent.timestamp
).getTime()

owners[nft.currentOwner] = updateOwnerWithNewNft({
owner,
latestEvent: latestchangeHandsEvent,
lastestTimeStamp,
nft,
})
return
}

// nft isn't consumed and it hasn't change hands => it is owned by it's creator
const mintInteraction = events[0]
const mintTimeStamp = new Date(mintInteraction.timestamp).getTime()

owners[nft.currentOwner] =
owner === undefined
? newOwnerEntry(mintTimeStamp, nftExcludingEvents)
: updateOwnerWithNewNft({
owner,
latestInteraction: mintInteraction,
lastestTimeStamp: mintTimeStamp,
nft,
})
owners[nft.currentOwner] = updateOwnerWithNewNft({
owner,
latestEvent: mintInteraction,
lastestTimeStamp: mintTimeStamp,
nft,
})
})
return owners
}
Expand Down

0 comments on commit 9df9905

Please sign in to comment.