Skip to content

Commit 3f69b8e

Browse files
committed
update with some better practise syntax
1 parent d560e27 commit 3f69b8e

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

contracts/AllDay.cdc

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ pub contract AllDay: NonFungibleToken {
131131
// initializer
132132
//
133133
init (id: UInt64) {
134-
let series = (&AllDay.seriesByID[id] as &AllDay.Series?)!
135-
self.id = series.id
136-
self.name = series.name
137-
self.active = series.active
134+
if let series = &AllDay.seriesByID[id] as &AllDay.Series? {
135+
self.id = series.id
136+
self.name = series.name
137+
self.active = series.active
138+
} else {
139+
panic("series does not exist")
140+
}
138141
}
139142
}
140143

@@ -229,10 +232,13 @@ pub contract AllDay: NonFungibleToken {
229232
// initializer
230233
//
231234
init (id: UInt64) {
232-
let set = (&AllDay.setByID[id] as &AllDay.Set?)!
235+
if let set = &AllDay.setByID[id] as &AllDay.Set? {
233236
self.id = id
234237
self.name = set.name
235238
self.setPlaysInEditions = set.setPlaysInEditions
239+
} else {
240+
panic("set does not exist")
241+
}
236242
}
237243
}
238244

@@ -312,10 +318,13 @@ pub contract AllDay: NonFungibleToken {
312318
// initializer
313319
//
314320
init (id: UInt64) {
315-
let play = (&AllDay.playByID[id] as &AllDay.Play?)!
321+
if let play = &AllDay.playByID[id] as &AllDay.Play? {
316322
self.id = id
317323
self.classification = play.classification
318324
self.metadata = play.metadata
325+
} else {
326+
panic("play does not exist")
327+
}
319328
}
320329
}
321330

@@ -374,14 +383,17 @@ pub contract AllDay: NonFungibleToken {
374383
// initializer
375384
//
376385
init (id: UInt64) {
377-
let edition = (&AllDay.editionByID[id] as &AllDay.Edition?)!
386+
if let edition = &AllDay.editionByID[id] as &AllDay.Edition? {
378387
self.id = id
379388
self.seriesID = edition.seriesID
380389
self.playID = edition.playID
381390
self.setID = edition.setID
382391
self.maxMintSize = edition.maxMintSize
383392
self.tier = edition.tier
384393
self.numMinted = edition.numMinted
394+
} else {
395+
panic("edition does not exist")
396+
}
385397
}
386398
}
387399

@@ -611,15 +623,21 @@ pub contract AllDay: NonFungibleToken {
611623
// borrowNFT gets a reference to an NFT in the collection
612624
//
613625
pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
626+
pre {
627+
self.ownedNFTs[id] != nil: "Cannot borrow NFT, no such id"
628+
}
629+
614630
return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
615631
}
616632

617633
// borrowMomentNFT gets a reference to an NFT in the collection
618634
//
619635
pub fun borrowMomentNFT(id: UInt64): &AllDay.NFT? {
620636
if self.ownedNFTs[id] != nil {
621-
let ref = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
622-
return ref as! &AllDay.NFT
637+
if let ref = &self.ownedNFTs[id] as auth &NonFungibleToken.NFT? {
638+
return ref! as! &AllDay.NFT
639+
}
640+
return nil
623641
} else {
624642
return nil
625643
}
@@ -717,9 +735,11 @@ pub contract AllDay: NonFungibleToken {
717735
// Close a Series
718736
//
719737
pub fun closeSeries(id: UInt64): UInt64 {
720-
let series = (&AllDay.seriesByID[id] as &AllDay.Series?)!
721-
series.close()
722-
return series.id
738+
if let series = &AllDay.seriesByID[id] as &AllDay.Series? {
739+
series.close()
740+
return series.id
741+
}
742+
panic("series does not exist")
723743
}
724744

725745
// Create a Set
@@ -775,9 +795,11 @@ pub contract AllDay: NonFungibleToken {
775795
// Close an Edition
776796
//
777797
pub fun closeEdition(id: UInt64): UInt64 {
778-
let edition = (&AllDay.editionByID[id] as &AllDay.Edition?)!
779-
edition.close()
780-
return edition.id
798+
if let edition = &AllDay.editionByID[id] as &AllDay.Edition? {
799+
edition.close()
800+
return edition.id
801+
}
802+
panic("edition does not exist")
781803
}
782804

783805
// Mint a single NFT
@@ -788,7 +810,6 @@ pub contract AllDay: NonFungibleToken {
788810
// Make sure the edition we are creating this NFT in exists
789811
AllDay.editionByID.containsKey(editionID): "No such EditionID"
790812
}
791-
792813
return <- self.borrowEdition(id: editionID).mint()
793814
}
794815
}

0 commit comments

Comments
 (0)