From 5f9abbd2eeaefa9a5dc99a294beb500b5b38f5d4 Mon Sep 17 00:00:00 2001 From: Austin Kline Date: Sat, 24 Aug 2024 18:39:59 -0700 Subject: [PATCH] fixes to get OpenEdition drops to work (#24) --- contracts/DropTypes.cdc | 4 +++ contracts/FlowtyDrops.cdc | 11 +++++-- .../initializers/OpenEditionInitializer.cdc | 29 +++++++++---------- contracts/nft/NFTMetadata.cdc | 4 +-- contracts/nft/OpenEditionNFT.cdc | 1 + contracts/nft/OpenEditionTemplate.cdc | 1 + tests/test_helpers.cdc | 7 +++-- 7 files changed, 34 insertions(+), 23 deletions(-) diff --git a/contracts/DropTypes.cdc b/contracts/DropTypes.cdc index 4682ea7..6255cef 100644 --- a/contracts/DropTypes.cdc +++ b/contracts/DropTypes.cdc @@ -259,6 +259,10 @@ access(all) contract DropTypes { phaseSummaries.append(summary) } + if CompositeType(dropDetails.nftType) == nil { + continue + } + summaries.append(DropSummary( id: drop!.uuid, display: dropDetails.display, diff --git a/contracts/FlowtyDrops.cdc b/contracts/FlowtyDrops.cdc index 08fa471..1925506 100644 --- a/contracts/FlowtyDrops.cdc +++ b/contracts/FlowtyDrops.cdc @@ -7,7 +7,7 @@ access(all) contract FlowtyDrops { access(all) let ContainerStoragePath: StoragePath access(all) let ContainerPublicPath: PublicPath - access(all) event DropAdded(address: Address, id: UInt64, name: String, description: String, imageUrl: String, start: UInt64?, end: UInt64?) + access(all) event DropAdded(address: Address, id: UInt64, name: String, description: String, imageUrl: String, start: UInt64?, end: UInt64?, nftType: String) access(all) event Minted(address: Address, dropID: UInt64, phaseID: UInt64, nftID: UInt64, nftType: String) access(all) event PhaseAdded(dropID: UInt64, dropAddress: Address, id: UInt64, index: Int, switcherType: String, pricerType: String, addressVerifierType: String) access(all) event PhaseRemoved(dropID: UInt64, dropAddress: Address, id: UInt64) @@ -203,6 +203,10 @@ access(all) contract FlowtyDrops { } init(display: MetadataViews.Display, medias: MetadataViews.Medias?, commissionRate: UFix64, nftType: String) { + pre { + nftType != "": "nftType should be a composite type identifier" + } + self.display = display self.medias = medias self.totalMinted = 0 @@ -372,7 +376,8 @@ access(all) contract FlowtyDrops { description: details.display.description, imageUrl: details.display.thumbnail.uri(), start: firstPhaseDetails.switcher.getStart(), - end: firstPhaseDetails.switcher.getEnd() + end: firstPhaseDetails.switcher.getEnd(), + nftType: details.nftType ) destroy self.drops.insert(key: drop.uuid, <-drop) } @@ -416,7 +421,7 @@ access(all) contract FlowtyDrops { access(all) fun getMinterStoragePath(type: Type): StoragePath { let segments = type.identifier.split(separator: ".") - let identifier = "FlowtyDrops_Minter_".concat(segments[1]).concat(segments[2]) + let identifier = "FlowtyDrops_Minter_".concat(segments[1]).concat("_").concat(segments[2]) return StoragePath(identifier: identifier)! } diff --git a/contracts/initializers/OpenEditionInitializer.cdc b/contracts/initializers/OpenEditionInitializer.cdc index cc40627..fd001d2 100644 --- a/contracts/initializers/OpenEditionInitializer.cdc +++ b/contracts/initializers/OpenEditionInitializer.cdc @@ -1,6 +1,8 @@ import "ContractInitializer" import "NFTMetadata" import "FlowtyDrops" +import "NonFungibleToken" +import "UniversalCollection" access(all) contract OpenEditionInitializer: ContractInitializer { access(all) fun initialize(contractAcct: auth(Storage, Contracts, Keys, Inbox, Capabilities) &Account, params: {String: AnyStruct}): NFTMetadata.InitializedCaps { @@ -9,18 +11,15 @@ access(all) contract OpenEditionInitializer: ContractInitializer { params["data"]!.getType() == Type(): "data param must be of type NFTMetadata.Metadata" params["collectionInfo"] != nil: "missing param collectionInfo" params["collectionInfo"]!.getType() == Type(): "collectionInfo param must be of type NFTMetadata.CollectionInfo" + params["type"] != nil: "missing param type" + params["type"]!.getType() == Type(): "type param must be of type Type" } let data = params["data"]! as! NFTMetadata.Metadata let collectionInfo = params["collectionInfo"]! as! NFTMetadata.CollectionInfo - let acct: auth(Storage, Contracts, Keys, Inbox, Capabilities) &Account = Account(payer: contractAcct) - let cap = acct.capabilities.account.issue() - - let t = self.getType() - let contractName = t.identifier.split(separator: ".")[2] - - contractAcct.storage.save(cap, to: StoragePath(identifier: "metadataAuthAccount_".concat(contractName))!) + let nftType = params["type"]! as! Type + let contractName = nftType.identifier.split(separator: ".")[2] // do we have information to setup a drop as well? if params.containsKey("dropDetails") && params.containsKey("phaseDetails") && params.containsKey("minterController") { @@ -30,7 +29,7 @@ access(all) contract OpenEditionInitializer: ContractInitializer { let phaseDetails = params["phaseDetails"]! as! [FlowtyDrops.PhaseDetails] assert(minterCap.check(), message: "invalid minter capability") - + assert(CompositeType(dropDetails.nftType) != nil, message: "dropDetails.nftType must be a valid CompositeType") let phases: @[FlowtyDrops.Phase] <- [] for p in phaseDetails { @@ -38,21 +37,21 @@ access(all) contract OpenEditionInitializer: ContractInitializer { } let drop <- FlowtyDrops.createDrop(details: dropDetails, minterCap: minterCap, phases: <- phases) - if acct.storage.borrow<&AnyResource>(from: FlowtyDrops.ContainerStoragePath) == nil { - acct.storage.save(<- FlowtyDrops.createContainer(), to: FlowtyDrops.ContainerStoragePath) + if contractAcct.storage.borrow<&AnyResource>(from: FlowtyDrops.ContainerStoragePath) == nil { + contractAcct.storage.save(<- FlowtyDrops.createContainer(), to: FlowtyDrops.ContainerStoragePath) - acct.capabilities.unpublish(FlowtyDrops.ContainerPublicPath) - acct.capabilities.publish( - acct.capabilities.storage.issue<&{FlowtyDrops.ContainerPublic}>(FlowtyDrops.ContainerStoragePath), + contractAcct.capabilities.unpublish(FlowtyDrops.ContainerPublicPath) + contractAcct.capabilities.publish( + contractAcct.capabilities.storage.issue<&{FlowtyDrops.ContainerPublic}>(FlowtyDrops.ContainerStoragePath), at: FlowtyDrops.ContainerPublicPath ) } - let container = acct.storage.borrow(from: FlowtyDrops.ContainerStoragePath) + let container = contractAcct.storage.borrow(from: FlowtyDrops.ContainerStoragePath) ?? panic("drops container not found") container.addDrop(<- drop) } - return NFTMetadata.initialize(acct: acct, collectionInfo: collectionInfo, collectionType: self.getType()) + return NFTMetadata.initialize(acct: contractAcct, collectionInfo: collectionInfo, nftType: nftType) } } \ No newline at end of file diff --git a/contracts/nft/NFTMetadata.cdc b/contracts/nft/NFTMetadata.cdc index 7e0ca49..416efdc 100644 --- a/contracts/nft/NFTMetadata.cdc +++ b/contracts/nft/NFTMetadata.cdc @@ -95,8 +95,8 @@ access(all) contract NFTMetadata { return <- create Container(collectionInfo: collectionInfo) } - access(all) fun initialize(acct: auth(Storage, Capabilities) &Account, collectionInfo: CollectionInfo, collectionType: Type): InitializedCaps { - let storagePath = self.getCollectionStoragePath(type: collectionType) + access(all) fun initialize(acct: auth(Storage, Capabilities) &Account, collectionInfo: CollectionInfo, nftType: Type): InitializedCaps { + let storagePath = self.getCollectionStoragePath(type: nftType) let container <- self.createContainer(collectionInfo: collectionInfo) acct.storage.save(<-container, to: storagePath) let pubCap = acct.capabilities.storage.issue<&Container>(storagePath) diff --git a/contracts/nft/OpenEditionNFT.cdc b/contracts/nft/OpenEditionNFT.cdc index 4179815..450c759 100644 --- a/contracts/nft/OpenEditionNFT.cdc +++ b/contracts/nft/OpenEditionNFT.cdc @@ -35,6 +35,7 @@ access(all) contract OpenEditionNFT: BaseCollection { self.totalSupply = 0 self.account.storage.save(<- create NFTMinter(), to: FlowtyDrops.getMinterStoragePath(type: self.getType())) params["minterController"] = self.account.capabilities.storage.issue<&{FlowtyDrops.Minter}>(FlowtyDrops.getMinterStoragePath(type: self.getType())) + params["type"] = Type<@NFT>() self.MetadataCap = ContractBorrower.borrowInitializer(typeIdentifier: initializeIdentifier).initialize(contractAcct: self.account, params: params).pubCap } diff --git a/contracts/nft/OpenEditionTemplate.cdc b/contracts/nft/OpenEditionTemplate.cdc index 393b20d..32cb387 100644 --- a/contracts/nft/OpenEditionTemplate.cdc +++ b/contracts/nft/OpenEditionTemplate.cdc @@ -42,6 +42,7 @@ access(all) contract OpenEditionTemplate: ContractFactoryTemplate { .concat(" let minter <- create NFTMinter()\n") .concat(" self.account.storage.save(<-minter, to: FlowtyDrops.getMinterStoragePath(type: self.getType()))\n") .concat(" params[\"minterController\"] = self.account.capabilities.storage.issue<&{FlowtyDrops.Minter}>(FlowtyDrops.getMinterStoragePath(type: self.getType()))\n") + .concat(" params[\"type\"] = Type<@NFT>()\n") .concat("\n\n") .concat(" self.MetadataCap = ContractBorrower.borrowInitializer(typeIdentifier: initializeIdentifier).initialize(contractAcct: self.account, params: params).pubCap\n") .concat(" }\n") diff --git a/tests/test_helpers.cdc b/tests/test_helpers.cdc index c2e3d67..165d1a8 100644 --- a/tests/test_helpers.cdc +++ b/tests/test_helpers.cdc @@ -94,15 +94,16 @@ access(all) fun deployAll() { deploy("StringUtils", "../node_modules/@flowtyio/flow-contracts/contracts/flow-utils/StringUtils.cdc", []) deploy("AddressUtils", "../node_modules/@flowtyio/flow-contracts/contracts/flow-utils/AddressUtils.cdc", []) - deploy("ContractManager", "../contracts/ContractManager.cdc", []) deploy("FlowtyDrops", "../contracts/FlowtyDrops.cdc", []) deploy("NFTMetadata", "../contracts/nft/NFTMetadata.cdc", []) + deploy("BaseCollection", "../contracts/nft/BaseCollection.cdc", []) + + deploy("ContractManager", "../contracts/ContractManager.cdc", []) + deploy("UniversalCollection", "../contracts/nft/UniversalCollection.cdc", []) deploy("ContractInitializer", "../contracts/initializers/ContractInitializer.cdc", []) deploy("ContractBorrower", "../contracts/initializers/ContractBorrower.cdc", []) deploy("OpenEditionInitializer", "../contracts/initializers/OpenEditionInitializer.cdc", []) - deploy("BaseCollection", "../contracts/nft/BaseCollection.cdc", []) - deploy("UniversalCollection", "../contracts/nft/UniversalCollection.cdc", []) deploy("BaseNFT", "../contracts/nft/BaseNFT.cdc", []) deploy("FlowtySwitchers", "../contracts/FlowtySwitchers.cdc", []) deploy("FlowtyPricers", "../contracts/FlowtyPricers.cdc", [])