@@ -95,7 +95,7 @@ access(all) contract NFTLocker {
95
95
96
96
/// The path to the Admin resource belonging to the account where this contract is deployed
97
97
///
98
- access (all) view fun getAdminStoragePath (): StoragePath {
98
+ access (all) view fun GetAdminStoragePath (): StoragePath {
99
99
return / storage/ NFTLockerAdmin
100
100
}
101
101
@@ -165,7 +165,7 @@ access(all) contract NFTLocker {
165
165
///
166
166
access (self ) let metadata : {String : AnyStruct }
167
167
168
- /// Add a new deposit method for a given NFT type
168
+ /// Add a deposit handler for given NFT types
169
169
///
170
170
access (Operate) fun addReceiver (
171
171
name : String ,
@@ -211,7 +211,7 @@ access(all) contract NFTLocker {
211
211
self .receiversByName.remove (key : name)
212
212
}
213
213
214
- /// Get the deposit method for the given name if it exists
214
+ /// Get the receiver for the given name if it exists
215
215
///
216
216
access (all) view fun getReceiver (name : String ): Receiver ? {
217
217
return self .receiversByName[name]
@@ -241,19 +241,22 @@ access(all) contract NFTLocker {
241
241
NFTLocker.expireLock (id : id, nftType : nftType)
242
242
}
243
243
244
- /// Create and return a new ReceiverCollector resource
244
+ /// Create and return a ReceiverCollector resource
245
245
///
246
246
access (all) fun createReceiverCollector (): @ReceiverCollector {
247
247
return <- create ReceiverCollector ()
248
248
}
249
249
}
250
250
251
- /// Expire lock if the locked NFT is eligible for force unlock and deposit by authorized receiver
251
+ /// Expire lock
252
+ ///
253
+ /// This can be called either by the admin or by the user unlockWithAuthorizedDeposit, if the locked NFT
254
+ /// type is eligible.
252
255
///
253
256
access (contract) fun expireLock (id : UInt64 , nftType : Type ) {
254
257
if let locker = &NFTLocker.lockedTokens[nftType] as auth (Mutate) &{UInt64: NFTLocker.LockedData}? {
255
258
if locker[id] ! = nil {
256
- // remove old locked data and insert new one with duration 0
259
+ // Update locked data's duration to 0
257
260
if let oldLockedData = locker.remove (key : id){
258
261
locker.insert (
259
262
key : id,
@@ -292,7 +295,7 @@ access(all) contract NFTLocker {
292
295
/// An NFT Collection
293
296
///
294
297
access (all) resource Collection : LockedCollection , LockProvider {
295
- /// Locked NFTs
298
+ /// This collection's locked NFTs
296
299
///
297
300
access (all) var lockedNFTs : @{Type : {UInt64 : {NonFungibleToken .NFT }}}
298
301
@@ -303,22 +306,19 @@ access(all) contract NFTLocker {
303
306
NFTLocker.canUnlockToken (id : id, nftType : nftType): " locked duration has not been met"
304
307
}
305
308
306
- // Get the locked token
307
- let token <- self .lockedNFTs[nftType]? .remove (key : id)!!
308
-
309
- // Remove the locked data
309
+ // Remove the token's locked data
310
310
if let lockedTokens = &NFTLocker.lockedTokens[nftType] as auth (Remove) &{UInt64: NFTLocker.LockedData}? {
311
311
lockedTokens.remove (key : id)
312
312
}
313
313
314
- // Decrement the total locked tokens
314
+ // Decrement the locked tokens count
315
315
NFTLocker.totalLockedTokens = NFTLocker.totalLockedTokens - 1
316
316
317
317
// Emit events
318
- emit NFTUnlocked (id : token. id, from : self .owner? .address, nftType : nftType)
319
- emit Withdraw (id : token. id, from : self .owner? .address)
318
+ emit NFTUnlocked (id : id, from : self .owner? .address, nftType : nftType)
319
+ emit Withdraw (id : id, from : self .owner? .address)
320
320
321
- return <- token
321
+ return <- self .lockedNFTs[nftType] ? . remove ( key : id) !!
322
322
}
323
323
324
324
/// Force unlock the NFT with the given id and type, and deposit it using the receiver's deposit method;
@@ -335,21 +335,21 @@ access(all) contract NFTLocker {
335
335
let lockedTokenDetails = NFTLocker.getNFTLockerDetails (id : id, nftType : nftType)
336
336
?? panic (" No locked token found for the given id and NFT type" )
337
337
338
- // Get the receiver collector, panic if it doesn't exist
338
+ // Get a public reference to the admin's receiver collector, panic if it doesn't exist
339
339
let receiverCollector = NFTLocker.borrowAdminReceiverCollectorPublic ()
340
340
?? panic (" No receiver collector found" )
341
341
342
- // Get the receiver name for the given NFT type, panic if it doesn't exist
343
- let receiverNames = receiverCollector.getReceiverNamesByNFTType (nftType : nftType)
342
+ // Get the receiver names for the given NFT type, panic if there is no record
343
+ let nftTypeReceivers = receiverCollector.getReceiverNamesByNFTType (nftType : nftType)
344
344
?? panic (" No authorized receiver for the given NFT type" )
345
345
346
- // Verify that the receiver is authorized to receive the NFT
346
+ // Verify that the receiver with the given name is authorized
347
347
assert (
348
- receiverNames [receiverName] == true ,
348
+ nftTypeReceivers [receiverName] == true ,
349
349
message : " Provided receiver does not exist or is not authorized for the given NFT type"
350
350
)
351
351
352
- // Expire the lock
352
+ // Expire the NFT's lock
353
353
NFTLocker.expireLock (id : id, nftType : nftType)
354
354
355
355
// Unlock and deposit the NFT using the receiver's deposit method
@@ -360,28 +360,34 @@ access(all) contract NFTLocker {
360
360
)
361
361
}
362
362
363
- /// Lock an NFT of a given type
363
+ /// Lock the given NFT for the specified duration
364
364
///
365
365
access (Operate) fun lock (token : @{NonFungibleToken .NFT }, duration : UInt64 ) {
366
+ // Get the NFT's id and type
366
367
let nftId : UInt64 = token.id
367
368
let nftType : Type = token.getType ()
368
369
369
- if NFTLocker.lockedTokens[nftType] == nil {
370
- NFTLocker.lockedTokens[nftType] = {}
371
- }
372
-
370
+ // Initialize the collection's locked NFTs for the given type if it doesn't exist
373
371
if self .lockedNFTs[nftType] == nil {
374
372
self .lockedNFTs[nftType] <- ! {}
375
373
}
376
374
377
- // Get a reference to the nested map
378
- let ref = &self .lockedNFTs[nftType] as auth (Insert) &{UInt64: {NonFungibleToken.NFT}}?
375
+ // Initialize the contract's locked tokens data for the given type if it doesn't exist
376
+ if NFTLocker.lockedTokens[nftType] == nil {
377
+ NFTLocker.lockedTokens[nftType] = {}
378
+ }
379
379
380
- let oldToken <- ref! .insert (key : nftId, <- token)
380
+ // Get a reference to this collection's locked NFTs map
381
+ let collectionLockedNFTsRef = &self .lockedNFTs[nftType] as auth (Insert) &{UInt64: {NonFungibleToken.NFT}}?
381
382
382
- let nestedLockRef = &NFTLocker.lockedTokens[nftType] as auth (Insert) &{UInt64: NFTLocker.LockedData}?
383
+ // Deposit the provided NFT in this collection's locked NFTs map - Cadence design requires destroying the resource-typed return value
384
+ destroy <- collectionLockedNFTsRef! .insert (key : nftId, <- token)
383
385
384
- // Create a new locked data
386
+ // Get a reference to the contract's nested map containing locked tokens data
387
+ let lockedTokensDataRef = &NFTLocker.lockedTokens[nftType] as auth (Insert) &{UInt64: NFTLocker.LockedData}?
388
+ ?? panic (" Could not get a reference to the locked tokens data" )
389
+
390
+ // Create locked data
385
391
let lockedData = NFTLocker.LockedData (
386
392
id : nftId,
387
393
owner : self .owner! .address,
@@ -390,7 +396,7 @@ access(all) contract NFTLocker {
390
396
)
391
397
392
398
// Insert the locked data
393
- nestedLockRef ! .insert (key : nftId, lockedData)
399
+ lockedTokensDataRef .insert (key : nftId, lockedData)
394
400
395
401
// Increment the total locked tokens
396
402
NFTLocker.totalLockedTokens = NFTLocker.totalLockedTokens + 1
@@ -406,8 +412,6 @@ access(all) contract NFTLocker {
406
412
)
407
413
408
414
emit Deposit (id : nftId, to : self .owner? .address)
409
-
410
- destroy oldToken
411
415
}
412
416
413
417
/// Get the ids of NFTs locked for a given type
@@ -437,7 +441,7 @@ access(all) contract NFTLocker {
437
441
self .CollectionPublicPath = / public/ NFTLockerCollection
438
442
439
443
// Create and save the admin resource
440
- self .account.storage.save (<- create Admin (), to : NFTLocker.getAdminStoragePath ())
444
+ self .account.storage.save (<- create Admin (), to : NFTLocker.GetAdminStoragePath ())
441
445
442
446
// Set contract variables
443
447
self .totalLockedTokens = 0
0 commit comments