Skip to content

Sadie/AllDay Secure Cadence upgrade #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 42 additions & 21 deletions contracts/AllDay.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,13 @@ pub contract AllDay: NonFungibleToken {
// initializer
//
init (id: UInt64) {
let series = &AllDay.seriesByID[id] as! &AllDay.Series
self.id = series.id
self.name = series.name
self.active = series.active
if let series = &AllDay.seriesByID[id] as &AllDay.Series? {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked to Bastian about best practise syntax for this. Slack thread here for context: https://axiomzen.slack.com/archives/CTGAW0TM2/p1653430599342029

self.id = series.id
self.name = series.name
self.active = series.active
} else {
panic("series does not exist")
}
}
}

Expand Down Expand Up @@ -229,10 +232,13 @@ pub contract AllDay: NonFungibleToken {
// initializer
//
init (id: UInt64) {
let set = &AllDay.setByID[id] as! &AllDay.Set
if let set = &AllDay.setByID[id] as &AllDay.Set? {
self.id = id
self.name = set.name
self.setPlaysInEditions = set.setPlaysInEditions
} else {
panic("set does not exist")
}
}
}

Expand Down Expand Up @@ -312,10 +318,13 @@ pub contract AllDay: NonFungibleToken {
// initializer
//
init (id: UInt64) {
let play = &AllDay.playByID[id] as! &AllDay.Play
if let play = &AllDay.playByID[id] as &AllDay.Play? {
self.id = id
self.classification = play.classification
self.metadata = play.metadata
} else {
panic("play does not exist")
}
}
}

Expand Down Expand Up @@ -374,14 +383,17 @@ pub contract AllDay: NonFungibleToken {
// initializer
//
init (id: UInt64) {
let edition = &AllDay.editionByID[id] as! &AllDay.Edition
if let edition = &AllDay.editionByID[id] as &AllDay.Edition? {
self.id = id
self.seriesID = edition.seriesID
self.playID = edition.playID
self.setID = edition.setID
self.maxMintSize = edition.maxMintSize
self.tier = edition.tier
self.numMinted = edition.numMinted
} else {
panic("edition does not exist")
}
}
}

Expand Down Expand Up @@ -611,15 +623,21 @@ pub contract AllDay: NonFungibleToken {
// borrowNFT gets a reference to an NFT in the collection
//
pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
return &self.ownedNFTs[id] as &NonFungibleToken.NFT
pre {
self.ownedNFTs[id] != nil: "Cannot borrow NFT, no such id"
}

return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
Copy link
Contributor Author

@sadief sadief May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used the force unwrap here because we have already nil checked in the precondition and I figured it would be easier than changing the function signature to limit what we need to change on our transactions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Open to other suggestions here though!)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I think in an ideal world, we would return an optional, but that would require changing the standard, which would be a breaking change unfortunately

}

// borrowMomentNFT gets a reference to an NFT in the collection
//
pub fun borrowMomentNFT(id: UInt64): &AllDay.NFT? {
if self.ownedNFTs[id] != nil {
let ref = &self.ownedNFTs[id] as auth &NonFungibleToken.NFT
return ref as! &AllDay.NFT
if let ref = &self.ownedNFTs[id] as auth &NonFungibleToken.NFT? {
return ref! as! &AllDay.NFT
}
return nil
} else {
return nil
}
Expand Down Expand Up @@ -667,7 +685,7 @@ pub contract AllDay: NonFungibleToken {
AllDay.seriesByID[id] != nil: "Cannot borrow series, no such id"
}

return &AllDay.seriesByID[id] as &AllDay.Series
return (&AllDay.seriesByID[id] as &AllDay.Series?)!
}

// Borrow a Set
Expand All @@ -677,7 +695,7 @@ pub contract AllDay: NonFungibleToken {
AllDay.setByID[id] != nil: "Cannot borrow Set, no such id"
}

return &AllDay.setByID[id] as &AllDay.Set
return (&AllDay.setByID[id] as &AllDay.Set?)!
}

// Borrow a Play
Expand All @@ -687,7 +705,7 @@ pub contract AllDay: NonFungibleToken {
AllDay.playByID[id] != nil: "Cannot borrow Play, no such id"
}

return &AllDay.playByID[id] as &AllDay.Play
return (&AllDay.playByID[id] as &AllDay.Play?)!
}

// Borrow an Edition
Expand All @@ -697,7 +715,7 @@ pub contract AllDay: NonFungibleToken {
AllDay.editionByID[id] != nil: "Cannot borrow edition, no such id"
}

return &AllDay.editionByID[id] as &AllDay.Edition
return (&AllDay.editionByID[id] as &AllDay.Edition?)!
}

// Create a Series
Expand All @@ -717,9 +735,11 @@ pub contract AllDay: NonFungibleToken {
// Close a Series
//
pub fun closeSeries(id: UInt64): UInt64 {
let series = &AllDay.seriesByID[id] as &AllDay.Series
series.close()
return series.id
if let series = &AllDay.seriesByID[id] as &AllDay.Series? {
series.close()
return series.id
}
panic("series does not exist")
}

// Create a Set
Expand Down Expand Up @@ -775,9 +795,11 @@ pub contract AllDay: NonFungibleToken {
// Close an Edition
//
pub fun closeEdition(id: UInt64): UInt64 {
let edition = &AllDay.editionByID[id] as &AllDay.Edition
edition.close()
return edition.id
if let edition = &AllDay.editionByID[id] as &AllDay.Edition? {
edition.close()
return edition.id
}
panic("edition does not exist")
}

// Mint a single NFT
Expand All @@ -788,7 +810,6 @@ pub contract AllDay: NonFungibleToken {
// Make sure the edition we are creating this NFT in exists
AllDay.editionByID.containsKey(editionID): "No such EditionID"
}

return <- self.borrowEdition(id: editionID).mint()
}
}
Expand Down
13 changes: 7 additions & 6 deletions lib/go/test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ module github.com/dapperlabs/nfl-smart-contracts/lib/go/test
go 1.16

require (
github.com/onflow/cadence v0.18.0
github.com/onflow/flow-emulator v0.22.0
github.com/onflow/cadence v0.21.3-0.20220419065337-d5202c162010
github.com/onflow/flow-emulator v0.31.2-0.20220422152808-6c116a9d88b7
github.com/onflow/flow-ft/lib/go/contracts v0.5.0
github.com/onflow/flow-ft/lib/go/templates v0.2.0
github.com/onflow/flow-go-sdk v0.20.1-0.20210623043139-533a95abf071
github.com/onflow/flow-nft/lib/go/contracts v0.0.0-20210603230546-76c44712d829
github.com/onflow/flow-go v0.25.13-0.20220422145107-5a9458db1f1d // indirect
github.com/onflow/flow-go-sdk v0.24.1-0.20220421152843-9ce4d554036e
github.com/onflow/flow-nft/lib/go/contracts v0.0.0-20210915191154-12ee8c507a0e
github.com/onflow/flow-nft/lib/go/templates v0.0.0-20210603230546-76c44712d829
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.7.1-0.20210824115523-ab6dc3262822
gotest.tools v2.2.0+incompatible // indirect
)
)
Loading