github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/main/cadence/transactions/mint_nft.cdc (about) 1 import NonFungibleToken from 0xf8d6e0586b0a20c7 2 import ExampleNFT from 0xf8d6e0586b0a20c7 3 import MetadataViews from 0xf8d6e0586b0a20c7 4 import FungibleToken from 0xee82856bf20e2aa6 5 6 // This script uses the NFTMinter resource to mint a new NFT 7 // It must be run with the account that has the minter resource 8 // stored in /storage/NFTMinter 9 transaction( 10 recipient: Address, 11 name: String, 12 description: String, 13 thumbnail: String, 14 cuts: [UFix64], 15 royaltyDescriptions: [String], 16 royaltyBeneficiaries: [Address] 17 ) { 18 19 /// local variable for storing the minter reference 20 let minter: &ExampleNFT.NFTMinter 21 22 /// Reference to the receiver's collection 23 let recipientCollectionRef: &{NonFungibleToken.CollectionPublic} 24 25 /// Previous NFT ID before the transaction executes 26 let mintingIDBefore: UInt64 27 28 prepare(signer: AuthAccount) { 29 self.mintingIDBefore = ExampleNFT.totalSupply 30 31 // borrow a reference to the NFTMinter resource in storage 32 self.minter = signer.borrow<&ExampleNFT.NFTMinter>(from: ExampleNFT.MinterStoragePath) 33 ?? panic("Account does not store an object at the specified path") 34 35 // Borrow the recipient's public NFT collection reference 36 self.recipientCollectionRef = getAccount(recipient) 37 .getCapability(ExampleNFT.CollectionPublicPath) 38 .borrow<&{NonFungibleToken.CollectionPublic}>() 39 ?? panic("Could not get receiver reference to the NFT Collection") 40 } 41 42 pre { 43 cuts.length == royaltyDescriptions.length && cuts.length == royaltyBeneficiaries.length: "Array length should be equal for royalty related details" 44 } 45 46 execute { 47 48 // Create the royalty details 49 var count = 0 50 var royalties: [MetadataViews.Royalty] = [] 51 while royaltyBeneficiaries.length > count { 52 let beneficiary = royaltyBeneficiaries[count] 53 let beneficiaryCapability = getAccount(beneficiary) 54 .getCapability<&{FungibleToken.Receiver}>(MetadataViews.getRoyaltyReceiverPublicPath()) 55 56 // Make sure the royalty capability is valid before minting the NFT 57 if !beneficiaryCapability.check() { panic("Beneficiary capability is not valid!") } 58 59 royalties.append( 60 MetadataViews.Royalty( 61 recepient: beneficiaryCapability, 62 cut: cuts[count], 63 description: royaltyDescriptions[count] 64 ) 65 ) 66 count = count + 1 67 } 68 69 70 71 // Mint the NFT and deposit it to the recipient's collection 72 self.minter.mintNFT( 73 recipient: self.recipientCollectionRef, 74 name: name, 75 description: description, 76 thumbnail: thumbnail, 77 royalties: royalties 78 ) 79 } 80 81 post { 82 self.recipientCollectionRef.getIDs().contains(self.mintingIDBefore): "The next NFT ID should have been minted and delivered" 83 ExampleNFT.totalSupply == self.mintingIDBefore + 1: "The total supply should have been increased by 1" 84 } 85 } 86