From 78b16d7e2ae3ac138b46c9fb67dda89e4d7a19ac Mon Sep 17 00:00:00 2001 From: Bjarte Stien Karlsen Date: Fri, 15 Jul 2022 13:40:32 +0200 Subject: [PATCH 1/3] added getInvetory retriever method --- cadence/contracts/NFTRetrieval.cdc | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cadence/contracts/NFTRetrieval.cdc b/cadence/contracts/NFTRetrieval.cdc index 4bdbf7d..8479820 100644 --- a/cadence/contracts/NFTRetrieval.cdc +++ b/cadence/contracts/NFTRetrieval.cdc @@ -148,6 +148,24 @@ pub contract NFTRetrieval { return false } + pub fun getInventory(address:Address) : {String: [UInt64]} { + let inventory : {String:[UInt64]}={} + let account=getAccount(address) + let collections : {String:PublicPath} ={} + let types = NFTCatalog.getCatalogTypeData() + for nftType in types.keys { + let typeData=types[nftType]! + let collectionKey=typeData.keys[0] + let catalogEntry = NFTCatalog.getCatalogEntry(collectionIdentifier:collectionKey)! + let path =catalogEntry.collectionData.publicPath + let cap= account.getCapability<&{MetadataViews.ResolverCollection}>(path) + if cap.check(){ + inventory[nftType] = cap.borrow()!.getIDs() + } + } + return inventory + } + init() {} -} \ No newline at end of file +} From 68fb40318915f2c4e57629d6708613fec913908a Mon Sep 17 00:00:00 2001 From: Bjarte Karlsen Date: Mon, 18 Jul 2022 20:02:18 +0200 Subject: [PATCH 2/3] moved to script since we need AuthAccount access --- cadence/contracts/NFTRetrieval.cdc | 18 ------------- .../get_nft_collections_in_account.cdc | 26 +++++++++++++++++++ 2 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 cadence/scripts/get_nft_collections_in_account.cdc diff --git a/cadence/contracts/NFTRetrieval.cdc b/cadence/contracts/NFTRetrieval.cdc index 8479820..b39536b 100644 --- a/cadence/contracts/NFTRetrieval.cdc +++ b/cadence/contracts/NFTRetrieval.cdc @@ -148,24 +148,6 @@ pub contract NFTRetrieval { return false } - pub fun getInventory(address:Address) : {String: [UInt64]} { - let inventory : {String:[UInt64]}={} - let account=getAccount(address) - let collections : {String:PublicPath} ={} - let types = NFTCatalog.getCatalogTypeData() - for nftType in types.keys { - let typeData=types[nftType]! - let collectionKey=typeData.keys[0] - let catalogEntry = NFTCatalog.getCatalogEntry(collectionIdentifier:collectionKey)! - let path =catalogEntry.collectionData.publicPath - let cap= account.getCapability<&{MetadataViews.ResolverCollection}>(path) - if cap.check(){ - inventory[nftType] = cap.borrow()!.getIDs() - } - } - return inventory - } - init() {} } diff --git a/cadence/scripts/get_nft_collections_in_account.cdc b/cadence/scripts/get_nft_collections_in_account.cdc new file mode 100644 index 0000000..d6a919b --- /dev/null +++ b/cadence/scripts/get_nft_collections_in_account.cdc @@ -0,0 +1,26 @@ +import MetadataViews from "./MetadataViews.cdc" +import NFTCatalog from "./NFTCatalog.cdc" + + +pub fun main(ownerAddress: Address) : {String:[UInt64]} { + + let account = getAuthAccount(ownerAddress) + + let inventory : {String:[UInt64]}={} + let collections : {String:PublicPath} ={} + let types = NFTCatalog.getCatalogTypeData() + for nftType in types.keys { + + let typeData=types[nftType]! + let collectionKey=typeData.keys[0] + let catalogEntry = NFTCatalog.getCatalogEntry(collectionIdentifier:collectionKey)! + let tempPathStr = "catalog".concat(collectionKey) + let tempPublicPath = PublicPath(identifier: tempPathStr)! + account.link<&{MetadataViews.ResolverCollection}>(tempPublicPath, target: catalogEntry.collectionData.storagePath) + let cap= account.getCapability<&{MetadataViews.ResolverCollection}>(tempPublicPath) + if cap.check(){ + inventory[nftType] = cap.borrow()!.getIDs() + } + } + return inventory +} From 3c3635491877e3d6489a5c968c8a80e8258bd683 Mon Sep 17 00:00:00 2001 From: Bjarte Karlsen Date: Mon, 18 Jul 2022 20:03:36 +0200 Subject: [PATCH 3/3] fixed formatting --- .../get_nft_collections_in_account.cdc | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/cadence/scripts/get_nft_collections_in_account.cdc b/cadence/scripts/get_nft_collections_in_account.cdc index d6a919b..3ef8a32 100644 --- a/cadence/scripts/get_nft_collections_in_account.cdc +++ b/cadence/scripts/get_nft_collections_in_account.cdc @@ -4,23 +4,23 @@ import NFTCatalog from "./NFTCatalog.cdc" pub fun main(ownerAddress: Address) : {String:[UInt64]} { - let account = getAuthAccount(ownerAddress) + let account = getAuthAccount(ownerAddress) - let inventory : {String:[UInt64]}={} - let collections : {String:PublicPath} ={} - let types = NFTCatalog.getCatalogTypeData() - for nftType in types.keys { + let inventory : {String:[UInt64]}={} + let collections : {String:PublicPath} ={} + let types = NFTCatalog.getCatalogTypeData() + for nftType in types.keys { - let typeData=types[nftType]! - let collectionKey=typeData.keys[0] - let catalogEntry = NFTCatalog.getCatalogEntry(collectionIdentifier:collectionKey)! - let tempPathStr = "catalog".concat(collectionKey) - let tempPublicPath = PublicPath(identifier: tempPathStr)! - account.link<&{MetadataViews.ResolverCollection}>(tempPublicPath, target: catalogEntry.collectionData.storagePath) - let cap= account.getCapability<&{MetadataViews.ResolverCollection}>(tempPublicPath) - if cap.check(){ - inventory[nftType] = cap.borrow()!.getIDs() - } - } - return inventory + let typeData=types[nftType]! + let collectionKey=typeData.keys[0] + let catalogEntry = NFTCatalog.getCatalogEntry(collectionIdentifier:collectionKey)! + let tempPathStr = "catalog".concat(collectionKey) + let tempPublicPath = PublicPath(identifier: tempPathStr)! + account.link<&{MetadataViews.ResolverCollection}>(tempPublicPath, target: catalogEntry.collectionData.storagePath) + let cap= account.getCapability<&{MetadataViews.ResolverCollection}>(tempPublicPath) + if cap.check(){ + inventory[nftType] = cap.borrow()!.getIDs() + } + } + return inventory }