-
Notifications
You must be signed in to change notification settings - Fork 17
/
mint.template.cdc
143 lines (116 loc) · 4.98 KB
/
mint.template.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {{ contractName }} from {{{ contractAddress }}}
import NonFungibleToken from {{{ imports.NonFungibleToken }}}
import MetadataViews from {{{ imports.MetadataViews }}}
import FreshmintQueue from {{{ imports.FreshmintQueue }}}
/// This transaction mints a batch of NFTs.
///
/// Parameters:
/// - bucketName: (optional) the name of the collection bucket to mint into. If nil, the default collection is used.
/// - mintIDs: a unique string for each NFT used to prevent duplicate mints.
{{#each fields}}
/// - {{ this.name }}: a {{ this.name }} metadata value for each NFT (must be same length as mintIDs).
{{/each}}
///
transaction(
bucketName: String?,
mintIDs: [String],
{{#each fields}}
{{ this.name }}: [{{ this.asCadenceTypeString }}],
{{/each}}
) {
let admin: &{{ contractName }}.Admin
let mintQueue: &FreshmintQueue.CollectionQueue
prepare(signer: AuthAccount) {
self.admin = signer.borrow<&{{ contractName }}.Admin>(from: {{ contractName }}.AdminStoragePath)
?? panic("Could not borrow a reference to the NFT admin")
self.mintQueue = getOrCreateQueue(
account: signer,
bucketName: bucketName
)
}
execute {
for i, mintID in mintIDs {
let token <- self.admin.mintNFT(
mintID: mintID,
{{#each fields}}
{{ this.name }}: {{ this.name }}[i],
{{/each}}
// Use the attributes dictionary to add additional metadata
// not defined in the original schema.
//
// The attributes dictionary is empty by default.
//
// Attributes must be string values.
//
attributes: {}
)
// NFTs are minted into a queue to preserve the mint order.
// A CollectionQueue is linked to a collection. All NFTs minted into
// the queue are deposited into the underlying collection.
//
self.mintQueue.deposit(token: <- token)
}
}
}
/// Borrow a reference to the queue with the given bucket name
/// or create one if it does not exist.
///
/// If bucketName is nil, the default queue path is used.
///
pub fun getOrCreateQueue(
account: AuthAccount,
bucketName: String?
): &FreshmintQueue.CollectionQueue {
let queueName = {{ contractName }}.makeQueueName(bucketName: bucketName)
let queuePrivatePath = {{ contractName }}.getPrivatePath(suffix: queueName)
// Check if a queue already exists with this name.
//
let queueCap = account.getCapability<&FreshmintQueue.CollectionQueue>(queuePrivatePath)
if let queueRef = queueCap.borrow() {
return queueRef
}
// Create a new collection queue if one does not exist.
//
// The underlying collection will have the same bucket name as the queue.
//
let queue <- FreshmintQueue.createCollectionQueue(
collection: getOrCreateCollection(account: account, bucketName: bucketName)
)
// Borrow a reference to the queue before we move it to storage
// so that we can return the reference from this function.
//
let queueRef = &queue as &FreshmintQueue.CollectionQueue
let queueStoragePath = {{ contractName }}.getStoragePath(suffix: queueName)
account.save(<- queue, to: queueStoragePath)
// Create a private link for the queue. Queues are not linked publicly.
//
account.link<&FreshmintQueue.CollectionQueue>(queuePrivatePath, target: queueStoragePath)
return queueRef
}
/// Borrow a capability to the collection with the given bucket name
/// or create one if it does not exist.
///
/// If bucketName is nil, the default collection path is used.
///
pub fun getOrCreateCollection(
account: AuthAccount,
bucketName: String?
): Capability<&NonFungibleToken.Collection> {
let collectionName = {{ contractName }}.makeCollectionName(bucketName: bucketName)
let collectionPrivatePath = {{ contractName }}.getPrivatePath(suffix: collectionName)
let collectionCap = account.getCapability<&NonFungibleToken.Collection>(collectionPrivatePath)
// Check to see if the capability links to an existing collection.
//
if collectionCap.check() {
return collectionCap
}
// Create an empty collection if one does not exist.
//
let collection <- {{ contractName }}.createEmptyCollection()
let collectionStoragePath = {{ contractName }}.getStoragePath(suffix: collectionName)
let collectionPublicPath = {{ contractName }}.getPublicPath(suffix: collectionName)
account.save(<-collection, to: collectionStoragePath)
account.link<&{{ contractName }}.Collection>(collectionPrivatePath, target: collectionStoragePath)
account.link<&{{ contractName }}.Collection{NonFungibleToken.CollectionPublic, {{ contractName }}.{{ contractName }}CollectionPublic, MetadataViews.ResolverCollection}>(collectionPublicPath, target: collectionStoragePath)
return collectionCap
}