-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: persist NFT collections (#2452)
* feat: persist nft collections * refactor: remove derived and subscribe * feat: updates collections where nfts are added * fix: collections display * chore: adds Mark suggestions --------- Co-authored-by: Mark Nardi <[email protected]>
- Loading branch information
1 parent
c6a374e
commit f44ba33
Showing
28 changed files
with
192 additions
and
117 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
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
4 changes: 2 additions & 2 deletions
4
packages/shared/src/lib/core/activity/utils/outputs/getIssuerFromNftOutput.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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Address, IssuerFeature, NftOutput, FeatureType } from '@iota/sdk/out/types' | ||
import { IssuerFeature, NftOutput, FeatureType } from '@iota/sdk/out/types' | ||
|
||
export function getIssuerFromNftOutput(output: NftOutput): Address { | ||
export function getIssuerFromNftOutput(output: NftOutput): { type: number; nftId?: string; aliasId?: string } { | ||
const metadata = output.immutableFeatures?.find((feature) => feature.type === FeatureType.Issuer) as IssuerFeature | ||
return metadata?.address | ||
} |
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
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
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
24 changes: 24 additions & 0 deletions
24
packages/shared/src/lib/core/nfts/actions/persistAndUpdateCollections.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,24 @@ | ||
import { get } from 'svelte/store' | ||
import { addCollectionsToPersistedCollections, addNftsToCollection, persistedCollections } from '../stores' | ||
import { Nft } from '../interfaces' | ||
import { buildPersistedCollectionFromNft } from '../utils' | ||
import { Collections } from '../types' | ||
|
||
export async function persistAndUpdateCollections(accountIndex: number, nfts: Nft[]): Promise<void> { | ||
const _persistedCollections = get(persistedCollections) | ||
|
||
const collections: Collections = {} | ||
for (const nft of nfts) { | ||
if (!nft.collectionId) { | ||
continue | ||
} | ||
if (!_persistedCollections[nft.collectionId] && !collections[nft.collectionId]) { | ||
const collection = await buildPersistedCollectionFromNft(nft) | ||
if (collection) { | ||
collections[nft.collectionId] = { ...collection, nfts: [] } | ||
} | ||
} | ||
} | ||
addCollectionsToPersistedCollections(Object.values(collections)) | ||
addNftsToCollection(accountIndex, nfts) | ||
} |
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
12 changes: 12 additions & 0 deletions
12
packages/shared/src/lib/core/nfts/interfaces/persisted-collection.interface.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,12 @@ | ||
import { IErc721ContractMetadata } from './erc721-contract-metadata.interface' | ||
import { IIrc27Metadata } from './nft-metadata.interface' | ||
|
||
interface IBaseCollection { | ||
id: string | ||
} | ||
|
||
export type PersistedCollection = IPersistedIrc27Collection | IPersistedErc721Collection | ||
|
||
export interface IPersistedIrc27Collection extends IIrc27Metadata, IBaseCollection {} | ||
|
||
export interface IPersistedErc721Collection extends IErc721ContractMetadata, IBaseCollection {} |
50 changes: 50 additions & 0 deletions
50
packages/shared/src/lib/core/nfts/stores/active-profile-collections-per-account.store.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,50 @@ | ||
import { Writable, get, writable } from 'svelte/store' | ||
import { persistedCollections } from '.' | ||
import { Collections } from '../types' | ||
import { Nft } from '../interfaces' | ||
import { NftStandard } from '../enums' | ||
|
||
export const collectionsSearchTerm = writable('') | ||
|
||
export const activeProfileCollectionsPerAccount: Writable<{ | ||
[accountIndex: number]: Collections | ||
}> = writable({}) | ||
|
||
export function addNftsToCollection(accountIndex: number, nfts: Nft[]): void { | ||
if (!nfts.length) return | ||
|
||
const $persistedCollections = get(persistedCollections) | ||
activeProfileCollectionsPerAccount.update((state) => { | ||
for (const nft of nfts) { | ||
if (!nft.collectionId || !$persistedCollections[nft.collectionId]) { | ||
continue | ||
} | ||
|
||
if (!state[accountIndex]) { | ||
state[accountIndex] = {} | ||
} | ||
|
||
let collection = state[accountIndex][nft.collectionId] | ||
if (!collection) { | ||
collection = { ...$persistedCollections[nft.collectionId], nfts: [] } | ||
} | ||
|
||
if (collection.nfts.some((_nft) => _nft.id === nft.id) || !nft.isSpendable) { | ||
continue | ||
} | ||
|
||
if (collection.standard === NftStandard.Irc27 && nft.standard === NftStandard.Irc27) { | ||
collection.nfts?.push(nft) | ||
} else if (collection.standard === NftStandard.Erc721 && nft.standard === NftStandard.Erc721) { | ||
collection.nfts?.push(nft) | ||
} | ||
state[accountIndex][nft.collectionId] = collection | ||
} | ||
|
||
return state | ||
}) | ||
} | ||
|
||
export function clearActiveProfileCollectionsPerAccount(): void { | ||
activeProfileCollectionsPerAccount.set({}) | ||
} |
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
22 changes: 22 additions & 0 deletions
22
packages/shared/src/lib/core/nfts/stores/persisted-collections.store.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,22 @@ | ||
import { persistent } from '@core/utils/store' | ||
import { Collection } from '../interfaces' | ||
import { PersistedCollections } from '../types' | ||
|
||
export const persistedCollections = persistent<PersistedCollections>('persistedCollections', {}) | ||
|
||
export function addCollectionsToPersistedCollections(collections: Collection[]): void { | ||
persistedCollections.update((state) => { | ||
for (const collection of collections) { | ||
if (state[collection.id]) { | ||
continue | ||
} | ||
|
||
state[collection.id] = collection | ||
} | ||
return state | ||
}) | ||
} | ||
|
||
export function addCollectionToPersistedCollections(collection: Collection): void { | ||
addCollectionsToPersistedCollections([collection]) | ||
} |
Oops, something went wrong.