This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
mint_and_list_kitty_item.cdc
111 lines (86 loc) · 5.06 KB
/
mint_and_list_kitty_item.cdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import NonFungibleToken from "../../contracts/NonFungibleToken.cdc"
import KittyItems from "../../contracts/KittyItems.cdc"
import FungibleToken from "../../contracts/FungibleToken.cdc"
import FlowToken from "../../contracts/FlowToken.cdc"
//import MetadataViews from "../../contracts/MetadataViews.cdc"
import NFTStorefrontV2 from "../../contracts/NFTStorefrontV2.cdc"
// This transction uses the NFTMinter resource to mint a new NFT.
transaction(recipient: Address, kind: UInt8, rarity: UInt8) {
// Mint
// local variable for storing the minter reference
let minter: &KittyItems.NFTMinter
/// Reference to the receiver's collection
let recipientCollectionRef: &{NonFungibleToken.CollectionPublic}
/// Previous NFT ID before the transaction executes
let mintingIDBefore: UInt64
// List
let flowReceiver: Capability<&FlowToken.Vault{FungibleToken.Receiver}>
let kittyItemsProvider: Capability<&KittyItems.Collection{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>
let storefront: &NFTStorefrontV2.Storefront
var saleCuts: [NFTStorefrontV2.SaleCut]
var marketplacesCapability: [Capability<&AnyResource{FungibleToken.Receiver}>]
prepare(signer: AuthAccount) {
// Prepare to mint
self.mintingIDBefore = KittyItems.totalSupply
// Borrow a reference to the NFTMinter resource in storage
self.minter = signer.borrow<&KittyItems.NFTMinter>(from: KittyItems.MinterStoragePath)
?? panic("Could not borrow a reference to the NFT minter")
// Borrow the recipient's public NFT collection reference
self.recipientCollectionRef = getAccount(recipient)
.getCapability(KittyItems.CollectionPublicPath)
.borrow<&{NonFungibleToken.CollectionPublic}>()
?? panic("Could not get receiver reference to the NFT Collection")
// Prepare to list
self.saleCuts = []
self.marketplacesCapability = []
// We need a provider capability, but one is not provided by default so we create one if needed.
let kittyItemsCollectionProviderPrivatePath = /private/kittyItemsCollectionProviderV14
// Receiver for the sale cut.
self.flowReceiver = signer.getCapability<&FlowToken.Vault{FungibleToken.Receiver}>(/public/flowTokenReceiver)!
assert(self.flowReceiver.borrow() != nil, message: "Missing or mis-typed FLOW receiver")
// Check if the Provider capability exists or not if `no` then create a new link for the same.
if !signer.getCapability<&KittyItems.Collection{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>(kittyItemsCollectionProviderPrivatePath)!.check() {
signer.link<&KittyItems.Collection{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>(kittyItemsCollectionProviderPrivatePath, target: KittyItems.CollectionStoragePath)
}
self.kittyItemsProvider = signer.getCapability<&KittyItems.Collection{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>(kittyItemsCollectionProviderPrivatePath)!
assert(self.kittyItemsProvider.borrow() != nil, message: "Missing or mis-typed KittyItems.Collection provider")
self.storefront = signer.borrow<&NFTStorefrontV2.Storefront>(from: NFTStorefrontV2.StorefrontStoragePath)
?? panic("Missing or mis-typed NFTStorefrontV2 Storefront")
}
execute {
// Execute to mint
let kindValue = KittyItems.Kind(rawValue: kind) ?? panic("invalid kind")
let rarityValue = KittyItems.Rarity(rawValue: rarity) ?? panic("invalid rarity")
// mint the NFT and deposit it to the recipient's collection
self.minter.mintNFT(
recipient: self.recipientCollectionRef,
kind: kindValue,
rarity: rarityValue,
royalties: []
)
var totalRoyaltyCut = 0.0
let effectiveSaleItemPrice = KittyItems.getItemPrice(rarity: rarityValue) // commission amount is 0
// Skip this step - Check whether the NFT implements the MetadataResolver or not.
// Append the cut for the seller
self.saleCuts.append(NFTStorefrontV2.SaleCut(
receiver: self.flowReceiver,
amount: effectiveSaleItemPrice - totalRoyaltyCut
))
// Execute to create listing
self.storefront.createListing(
nftProviderCapability: self.kittyItemsProvider,
nftType: Type<@KittyItems.NFT>(),
nftID: KittyItems.totalSupply - 1,
salePaymentVaultType: Type<@FlowToken.Vault>(),
saleCuts: self.saleCuts,
marketplacesCapability: self.marketplacesCapability.length == 0 ? nil : self.marketplacesCapability,
customID: nil,
commissionAmount: UFix64(0),
expiry: UInt64(getCurrentBlock().timestamp) + UInt64(500)
)
}
post {
self.recipientCollectionRef.getIDs().contains(self.mintingIDBefore): "The next NFT ID should have been minted and delivered"
KittyItems.totalSupply == self.mintingIDBefore + 1: "The total supply should have been increased by 1"
}
}