Skip to content

Commit 1c5c8eb

Browse files
committed
revert GetAdminStoragePath naming fix, update comments
1 parent f57418d commit 1c5c8eb

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

locked-nft/contracts/NFTLocker.cdc

+39-35
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ access(all) contract NFTLocker {
9595

9696
/// The path to the Admin resource belonging to the account where this contract is deployed
9797
///
98-
access(all) view fun getAdminStoragePath(): StoragePath {
98+
access(all) view fun GetAdminStoragePath(): StoragePath {
9999
return /storage/NFTLockerAdmin
100100
}
101101

@@ -165,7 +165,7 @@ access(all) contract NFTLocker {
165165
///
166166
access(self) let metadata: {String: AnyStruct}
167167

168-
/// Add a new deposit method for a given NFT type
168+
/// Add a deposit handler for given NFT types
169169
///
170170
access(Operate) fun addReceiver(
171171
name: String,
@@ -211,7 +211,7 @@ access(all) contract NFTLocker {
211211
self.receiversByName.remove(key: name)
212212
}
213213

214-
/// Get the deposit method for the given name if it exists
214+
/// Get the receiver for the given name if it exists
215215
///
216216
access(all) view fun getReceiver(name: String): Receiver? {
217217
return self.receiversByName[name]
@@ -241,19 +241,22 @@ access(all) contract NFTLocker {
241241
NFTLocker.expireLock(id: id, nftType: nftType)
242242
}
243243

244-
/// Create and return a new ReceiverCollector resource
244+
/// Create and return a ReceiverCollector resource
245245
///
246246
access(all) fun createReceiverCollector(): @ReceiverCollector {
247247
return <- create ReceiverCollector()
248248
}
249249
}
250250

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.
252255
///
253256
access(contract) fun expireLock(id: UInt64, nftType: Type) {
254257
if let locker = &NFTLocker.lockedTokens[nftType] as auth(Mutate) &{UInt64: NFTLocker.LockedData}?{
255258
if locker[id] != nil {
256-
// remove old locked data and insert new one with duration 0
259+
// Update locked data's duration to 0
257260
if let oldLockedData = locker.remove(key: id){
258261
locker.insert(
259262
key: id,
@@ -292,7 +295,7 @@ access(all) contract NFTLocker {
292295
/// An NFT Collection
293296
///
294297
access(all) resource Collection: LockedCollection, LockProvider {
295-
/// Locked NFTs
298+
/// This collection's locked NFTs
296299
///
297300
access(all) var lockedNFTs: @{Type: {UInt64: {NonFungibleToken.NFT}}}
298301

@@ -303,22 +306,19 @@ access(all) contract NFTLocker {
303306
NFTLocker.canUnlockToken(id: id, nftType: nftType): "locked duration has not been met"
304307
}
305308

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
310310
if let lockedTokens = &NFTLocker.lockedTokens[nftType] as auth(Remove) &{UInt64: NFTLocker.LockedData}? {
311311
lockedTokens.remove(key: id)
312312
}
313313

314-
// Decrement the total locked tokens
314+
// Decrement the locked tokens count
315315
NFTLocker.totalLockedTokens = NFTLocker.totalLockedTokens - 1
316316

317317
// 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)
320320

321-
return <-token
321+
return <- self.lockedNFTs[nftType]?.remove(key: id)!!
322322
}
323323

324324
/// 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 {
335335
let lockedTokenDetails = NFTLocker.getNFTLockerDetails(id: id, nftType: nftType)
336336
?? panic("No locked token found for the given id and NFT type")
337337

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
339339
let receiverCollector = NFTLocker.borrowAdminReceiverCollectorPublic()
340340
?? panic("No receiver collector found")
341341

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)
344344
?? panic("No authorized receiver for the given NFT type")
345345

346-
// Verify that the receiver is authorized to receive the NFT
346+
// Verify that the receiver with the given name is authorized
347347
assert(
348-
receiverNames[receiverName] == true,
348+
nftTypeReceivers[receiverName] == true,
349349
message: "Provided receiver does not exist or is not authorized for the given NFT type"
350350
)
351351

352-
// Expire the lock
352+
// Expire the NFT's lock
353353
NFTLocker.expireLock(id: id, nftType: nftType)
354354

355355
// Unlock and deposit the NFT using the receiver's deposit method
@@ -360,28 +360,34 @@ access(all) contract NFTLocker {
360360
)
361361
}
362362

363-
/// Lock an NFT of a given type
363+
/// Lock the given NFT for the specified duration
364364
///
365365
access(Operate) fun lock(token: @{NonFungibleToken.NFT}, duration: UInt64) {
366+
// Get the NFT's id and type
366367
let nftId: UInt64 = token.id
367368
let nftType: Type = token.getType()
368369

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
373371
if self.lockedNFTs[nftType] == nil {
374372
self.lockedNFTs[nftType] <-! {}
375373
}
376374

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+
}
379379

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}}?
381382

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)
383385

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
385391
let lockedData = NFTLocker.LockedData(
386392
id: nftId,
387393
owner: self.owner!.address,
@@ -390,7 +396,7 @@ access(all) contract NFTLocker {
390396
)
391397

392398
// Insert the locked data
393-
nestedLockRef!.insert(key: nftId, lockedData)
399+
lockedTokensDataRef.insert(key: nftId, lockedData)
394400

395401
// Increment the total locked tokens
396402
NFTLocker.totalLockedTokens = NFTLocker.totalLockedTokens + 1
@@ -406,8 +412,6 @@ access(all) contract NFTLocker {
406412
)
407413

408414
emit Deposit(id: nftId, to: self.owner?.address)
409-
410-
destroy oldToken
411415
}
412416

413417
/// Get the ids of NFTs locked for a given type
@@ -437,7 +441,7 @@ access(all) contract NFTLocker {
437441
self.CollectionPublicPath = /public/NFTLockerCollection
438442

439443
// 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())
441445

442446
// Set contract variables
443447
self.totalLockedTokens = 0

locked-nft/transactions/admin_add_escrow_receiver.cdc

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ import NFTLocker from "NFTLocker"
33
import Escrow from "Escrow"
44
import ExampleNFT from "ExampleNFT"
55

6-
/// This transaction creates a new ReceiverCollector resource and adds a new receiver to it with a deposit method that adds an NFT to an escrow leaderboard.
6+
/// This transaction creates a ReceiverCollector resource and adds an escrow leaderboard deposit handler to it.
77
///
88
transaction() {
99
// Authorized reference to the NFTLocker ReceiverCollector resource
1010
let receiverCollectorRef: auth(NFTLocker.Operate) &NFTLocker.ReceiverCollector
1111

1212
prepare(admin: auth(SaveValue, BorrowValue) &Account) {
13-
// Check if the ReceiverCollector resource does not exist
13+
// Create a ReceiverCollector resource if it does not exist yet in admin storage
1414
if NFTLocker.borrowAdminReceiverCollectorPublic() == nil {
1515
// Borrow a reference to the NFTLocker Admin resource
16-
let adminRef = admin.storage.borrow<&NFTLocker.Admin>(from: NFTLocker.getAdminStoragePath())
16+
let adminRef = admin.storage.borrow<&NFTLocker.Admin>(from: NFTLocker.GetAdminStoragePath())
1717
?? panic("Could not borrow a reference to the owner's collection")
1818

19-
// Create a new ReceiverCollector resource and save it in storage
19+
// Create a ReceiverCollector resource and save it in storage
2020
admin.storage.save(<- adminRef.createReceiverCollector(), to: NFTLocker.getReceiverCollectorStoragePath())
2121
}
2222

23-
// Borrow an authorized reference to the NFTLocker ReceiverCollector resource
23+
// Borrow an authorized reference to the admin's ReceiverCollector resource
2424
self.receiverCollectorRef = admin.storage
2525
.borrow<auth(NFTLocker.Operate) &NFTLocker.ReceiverCollector>(from: NFTLocker.getReceiverCollectorStoragePath())
2626
?? panic("Could not borrow a reference to the owner's collection")
2727
}
2828

2929
execute {
30-
// Add a new receiver to the ReceiverCollector with the provided deposit wrapper and accepted NFT types
30+
// Add a receiver to the ReceiverCollector with the provided namw, deposit handler, and accepted NFT types
3131
self.receiverCollectorRef.addReceiver(
3232
name: "add-entry-to-escrow-leaderboard",
3333
authorizedDepositHandler: Escrow.DepositHandler(),

locked-nft/transactions/admin_unlock_nft.cdc

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ transaction(id: UInt64) {
66

77
prepare(signer: auth(BorrowValue) &Account) {
88
self.adminRef = signer.storage
9-
.borrow<&NFTLocker.Admin>(from: NFTLocker.getAdminStoragePath())
9+
.borrow<&NFTLocker.Admin>(from: NFTLocker.GetAdminStoragePath())
1010
?? panic("Could not borrow a reference to the owner's collection")
1111
}
1212

0 commit comments

Comments
 (0)