diff --git a/cadence/transactions/example-assets/setup/setup_generic_nft_collection.cdc b/cadence/transactions/example-assets/setup/setup_generic_nft_collection.cdc index a4a5c664..ef7519b6 100644 --- a/cadence/transactions/example-assets/setup/setup_generic_nft_collection.cdc +++ b/cadence/transactions/example-assets/setup/setup_generic_nft_collection.cdc @@ -3,7 +3,13 @@ import "MetadataViews" import "FlowEVMBridgeUtils" +/// Configures a Collection according to the shared NonFungibleToken standard and the defaults specified by the NFT's +/// defining contract. +/// +/// @param nftIdentifier: The identifier of the NFT to configure. +/// transaction(nftIdentifier: String) { + prepare(signer: auth(BorrowValue, SaveValue, IssueStorageCapabilityController, PublishCapability, UnpublishCapability) &Account) { // Gather identifying information about the NFT and its defining contract let nftType = CompositeType(nftIdentifier) ?? panic("Invalid NFT identifier: ".concat(nftIdentifier)) @@ -18,7 +24,20 @@ transaction(nftIdentifier: String) { resourceType: nftType, viewType: Type() ) as! MetadataViews.NFTCollectionData? - ?? panic("Could not resolve collection data for NFT type: ".concat(nftIdentifier)) + ?? panic("Could not resolve NFTCollection data for NFT type: ".concat(nftIdentifier)) + + // Check for collision, returning if the collection already exists or reverting on unexpected collision + let storedType = signer.storage.type(at: data.storagePath) + if storedType == nftType { + return + } else if storedType != nil { + panic( + "Another resource of type " + .concat(storedType!.identifier) + .concat(" already exists at the storage path: ") + .concat(data.storagePath.toString()) + ) + } // Create a new collection and save it to signer's storage at the collection's default storage path signer.storage.save(<-data.createEmptyCollection(), to: data.storagePath) diff --git a/cadence/transactions/example-assets/setup/setup_generic_vault.cdc b/cadence/transactions/example-assets/setup/setup_generic_vault.cdc index bb4b0700..8c4b37fc 100644 --- a/cadence/transactions/example-assets/setup/setup_generic_vault.cdc +++ b/cadence/transactions/example-assets/setup/setup_generic_vault.cdc @@ -5,7 +5,13 @@ import "FungibleTokenMetadataViews" import "FlowEVMBridgeUtils" +/// Configures a Vault according to the shared FungibleToken standard and the defaults specified by the Vault's +/// defining contract. +/// +/// @param vaultIdentifier: The identifier of the Vault to configure. +/// transaction(vaultIdentifier: String) { + prepare(signer: auth(BorrowValue, SaveValue, IssueStorageCapabilityController, PublishCapability, UnpublishCapability) &Account) { // Gather identifying information about the Vault and its defining contract let vaultType = CompositeType(vaultIdentifier) ?? panic("Invalid Vault identifier: ".concat(vaultIdentifier)) @@ -20,12 +26,25 @@ transaction(vaultIdentifier: String) { resourceType: vaultType, viewType: Type() ) as! FungibleTokenMetadataViews.FTVaultData? - ?? panic("Could not resolve collection data for Vault type: ".concat(vaultIdentifier)) + ?? panic("Could not resolve FTVaultData for Vault type: ".concat(vaultIdentifier)) + + // Check for collision, returning if the vault already exists or reverting on unexpected collision + let storedType = signer.storage.type(at: data.storagePath) + if storedType == vaultType { + return + } else if storedType != nil { + panic( + "Another resource of type " + .concat(storedType!.identifier) + .concat(" already exists at the storage path: ") + .concat(data.storagePath.toString()) + ) + } - // Create a new collection and save it to signer's storage at the collection's default storage path + // Create a new vault and save it to signer's storage at the vault's default storage path signer.storage.save(<-data.createEmptyVault(), to: data.storagePath) - // Issue a public Collection capability and publish it to the collection's default public path + // Issue a public Vault capability and publish it to the vault's default public path signer.capabilities.unpublish(data.receiverPath) let receiverCap = signer.capabilities.storage.issue<&{FungibleToken.Vault}>(data.storagePath) signer.capabilities.publish(receiverCap, at: data.receiverPath)