Skip to content

Commit

Permalink
added first stab at porting giefts
Browse files Browse the repository at this point in the history
  • Loading branch information
bjartek committed Aug 20, 2024
1 parent 4288a2b commit b85f24f
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 98 deletions.
4 changes: 2 additions & 2 deletions contracts/FindUserStatus.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ access(all) contract FindUserStatus {
access(all) fun getFlowtyListing(user: Address, id: UInt64, type: Type) : FlowtyListing? {
var flowty : FlowtyListing? = nil
let account = getAccount(user)
let flowtyCap = account.capabilities.get<&Flowty.FlowtyStorefront{Flowty.FlowtyStorefrontPublic}>(Flowty.FlowtyStorefrontPublicPath)
let flowtyCap = account.capabilities.get<&Flowty.FlowtyStorefront>(Flowty.FlowtyStorefrontPublicPath)!

if flowtyCap.check() {
let storefrontRef=flowtyCap.borrow()!
Expand Down Expand Up @@ -340,7 +340,7 @@ access(all) contract FindUserStatus {

var flowtyRental : FlowtyRental? = nil
let account = getAccount(user)
let flowtyRentalCap = account.getCapability<&FlowtyRentals.FlowtyRentalsStorefront{FlowtyRentals.FlowtyRentalsStorefrontPublic}>(FlowtyRentals.FlowtyRentalsStorefrontPublicPath)
let flowtyRentalCap = account.capabilities.get<&FlowtyRentals.FlowtyRentalsStorefront>(FlowtyRentals.FlowtyRentalsStorefrontPublicPath)!

if flowtyRentalCap.check() {
let storefrontRef=flowtyRentalCap.borrow()!
Expand Down
243 changes: 149 additions & 94 deletions contracts/Giefts.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "NonFungibleToken"
import "MetadataViews"
import "ViewResolver"

// ___ __
// __ /'___\/\ \__
Expand All @@ -14,108 +15,165 @@ import "MetadataViews"
// Giefts - wrap NFT gifts in a box and send them to your friends.
// The gifts can be claimed by passing the correct password.
//
pub contract Giefts {

/**//////////////////////////////////////////////////////////////
// PATHS //
/////////////////////////////////////////////////////////////**/
access(all)
contract Giefts{

pub let GieftsStoragePath: StoragePath
pub let GieftsPublicPath: PublicPath
pub let GieftsPrivatePath: PrivatePath
access(all) entitlement Owner
/**/
/////////////////////////////////////////////////////////////
/**//////////////////////////////////////////////////////////////
// EVENTS //
// PATHS //
/////////////////////////////////////////////////////////////**/
access(all)
let GieftsStoragePath: StoragePath

pub event Packed(gieft: UInt64, nfts: [UInt64])
pub event Added(gieft: UInt64, nft: UInt64, type: String, name: String, thumbnail: String)
pub event Removed(gieft: UInt64, nft: UInt64, type: String, name: String, thumbnail: String)
pub event Claimed(gieft: UInt64, nft: UInt64, type: String, name: String, thumbnail: String, gifter: Address?, giftee: Address?)
access(all)
let GieftsPublicPath: PublicPath

/**//////////////////////////////////////////////////////////////
// INTERFACES //
// EVENTS //
/////////////////////////////////////////////////////////////**/
access(all)
event Packed(gieft: UInt64, nfts: [UInt64])

/// Gieft
access(all)
event Added(gieft: UInt64, nft: UInt64, type: String, name: String, thumbnail: String)

access(all)
event Removed(gieft: UInt64, nft: UInt64, type: String, name: String, thumbnail: String)

pub resource interface GieftPublic {
pub let password: [UInt8]
pub fun borrowClaimableNFT(): &NonFungibleToken.NFT?
pub fun claimNft(password: String, collection: &AnyResource{NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection})
pub fun getNftIDs(): [UInt64]
access(all)
event Claimed(
gieft: UInt64,
nft: UInt64,
type: String,
name: String,
thumbnail: String,
gifter: Address?,
giftee: Address?
) /**/
/////////////////////////////////////////////////////////////

// INTERFACES //
/////////////////////////////////////////////////////////////**/
/// Gieft
access(all)
resource interface GieftPublic{
access(all)
let password: [UInt8]

access(all)
fun borrowClaimableNFT(): &{NonFungibleToken.NFT}?

access(all)
fun claimNft(
password: String,
collection: &{NonFungibleToken.CollectionPublic, ViewResolver.ResolverCollection}
)

access(all)
fun getNftIDs(): [UInt64]
}

/// GieftCollection
access(all)
resource interface GieftCollectionPublic{
access(all)
fun borrowGieft(_ gieft: UInt64): &Giefts.Gieft?

pub resource interface GieftCollectionPublic {
pub fun borrowGieft(_ gieft: UInt64): &Gieft{GieftPublic}?
pub fun getGieftIDs(): [UInt64]
access(all)
fun getGieftIDs(): [UInt64]
}

pub resource interface GieftCollectionPrivate {
pub fun packGieft(name: String, password: [UInt8], nfts: @{UInt64: NonFungibleToken.NFT})
pub fun addNftToGieft(gieft: UInt64, nft: @NonFungibleToken.NFT)
pub fun unpackGieft(gieft: UInt64): @{UInt64: NonFungibleToken.NFT}
}
access(all)
resource interface GieftCollectionPrivate{
access(Owner)
fun packGieft(
name: String,
password: [
UInt8
],
nfts: @{
UInt64:{ NonFungibleToken.NFT}
}
): Void

access(Owner)
fun addNftToGieft(gieft: UInt64, nft: @{NonFungibleToken.NFT})

access(Owner)
fun unpackGieft(gieft: UInt64): @{UInt64:{ NonFungibleToken.NFT}}
} /**/
/////////////////////////////////////////////////////////////
/**//////////////////////////////////////////////////////////////
// RESOURCES //
/////////////////////////////////////////////////////////////**/

// RESOURCES //
/////////////////////////////////////////////////////////////**/
/// Gieft
/// A collection of NFTs that can be claimed by passing the correct password
pub resource Gieft: GieftPublic {
access(all)
resource Gieft: GieftPublic{
/// The name of the gieft
pub let name: String
access(all)
let name: String

/// A collection of NFTs
/// nfts are stored as a map of uuids to NFTs
access(contract) var nfts: @{UInt64: NonFungibleToken.NFT}
access(contract)
var nfts: @{UInt64:{ NonFungibleToken.NFT}}

/// The hashed password to claim an nft
pub let password: [UInt8]
access(all)
let password: [UInt8]

/// add an NFT to the gieft
access(contract) fun addNft(nft: @NonFungibleToken.NFT) {
pre {
!self.nfts.keys.contains(nft.uuid) : "NFT uuid already added"
access(contract)
fun addNft(nft: @{NonFungibleToken.NFT}){
pre{
!self.nfts.keys.contains(nft.uuid):
"NFT uuid already added"
}
let display: MetadataViews.Display = nft.resolveView(Type<MetadataViews.Display>())! as! MetadataViews.Display
emit Added(gieft: self.uuid, nft: nft.uuid, type: nft.getType().identifier, name: display.name, thumbnail: display.thumbnail.uri())
let oldNft <- self.nfts[nft.uuid] <-nft
let oldNft <- self.nfts[nft.uuid] <- nft
destroy oldNft
}

/// borrwClaimableNFT
/// get a reference to the first NFT that can be claimed
/// @returns the first NFT that can be claimed
pub fun borrowClaimableNFT(): &NonFungibleToken.NFT? {
if self.nfts.length > 0 {
return &self.nfts[self.nfts.keys[0]] as &NonFungibleToken.NFT?
} else {
access(all)
fun borrowClaimableNFT(): &{NonFungibleToken.NFT}?{
if self.nfts.length > 0{
return &self.nfts[self.nfts.keys[0]]
} else{
return nil
}
}

/// claim an NFT from the gieft
/// @params password: the password to claim the NFT
pub fun claimNft(password: String, collection: &AnyResource{NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection}) {
pre {
self.password == HashAlgorithm.KECCAK_256.hash(password.utf8) : "Incorrect password"
self.nfts.length > 0 : "No NFTs to claim"
access(all)
fun claimNft(password: String, collection: &{NonFungibleToken.CollectionPublic, ViewResolver.ResolverCollection}){
pre{
self.password == HashAlgorithm.KECCAK_256.hash(password.utf8):
"Incorrect password"
self.nfts.length > 0:
"No NFTs to claim"
}
let nft <- self.nfts.remove(key: self.nfts.keys[0])!
let display: MetadataViews.Display = nft.resolveView(Type<MetadataViews.Display>())! as! MetadataViews.Display
emit Claimed(gieft: self.uuid, nft: nft.uuid, type: nft.getType().identifier, name: display.name, thumbnail: display.thumbnail.uri(), gifter: self.owner?.address, giftee: collection.owner?.address)
collection.deposit(token: <- nft)
collection.deposit(token: <-nft)
}

/// unpack, a function to unpack an NFT from the gieft, this function is only callable by the owner
/// @params nft: the uuid of the NFT to claim
access(contract) fun unpack(nft: UInt64): @NonFungibleToken.NFT {
pre {
self.nfts.keys.contains(nft) : "NFT does not exist"
access(contract)
fun unpack(nft: UInt64): @{NonFungibleToken.NFT}{
pre{
self.nfts.keys.contains(nft):
"NFT does not exist"
}
let nft <- self.nfts.remove(key: nft)!
let display: MetadataViews.Display = nft.resolveView(Type<MetadataViews.Display>())! as! MetadataViews.Display
Expand All @@ -124,62 +182,61 @@ pub contract Giefts {
}

/// get all NFT ids
pub fun getNftIDs(): [UInt64] {
access(all)
fun getNftIDs(): [UInt64]{
return self.nfts.keys
}

init (name: String, password: [UInt8], nfts: @{UInt64: NonFungibleToken.NFT}) {
init(name: String, password: [UInt8], nfts: @{UInt64:{ NonFungibleToken.NFT}}){
self.name = name
self.nfts <- nfts
self.password = password
emit Packed(gieft: self.uuid, nfts: self.nfts.keys)
}

destroy () {
pre {
self.nfts.length == 0 : "All NFTs must be claimed before destroying the gieft"
}
destroy self.nfts
}
}

/// GieftCollection
/// A collection of giefts
pub resource GieftCollection: GieftCollectionPublic, GieftCollectionPrivate {
access(all)
resource GieftCollection: GieftCollectionPublic, GieftCollectionPrivate{
/// a collection of giefts
pub var giefts: @{UInt64: Gieft}
access(all)
var giefts: @{UInt64: Gieft}

/// create a new gieft
/// @params password: the hashed password to claim an NFT from the Gieft
/// @params nfts: the NFTs to add to the gieft
pub fun packGieft(name: String, password: [UInt8], nfts: @{UInt64: NonFungibleToken.NFT}) {
let gieft <- create Gieft(name: name, password: password, nfts: <- nfts)
access(Owner)
fun packGieft(name: String, password: [UInt8], nfts: @{UInt64:{ NonFungibleToken.NFT}}){
let gieft <- create Gieft(name: name, password: password, nfts: <-nfts)
let oldGieft <- self.giefts[gieft.uuid] <- gieft
destroy oldGieft
}

/// add an NFT to a gieft
/// @params gieft: the uuid of the gieft to add the NFT to
/// @params nft: the NFT to add to the gieft
pub fun addNftToGieft(gieft: UInt64, nft: @NonFungibleToken.NFT) {
pre {
self.giefts.keys.contains(gieft) : "Gieft does not exist"
access(Owner)
fun addNftToGieft(gieft: UInt64, nft: @{NonFungibleToken.NFT}){
pre{
self.giefts.keys.contains(gieft):
"Gieft does not exist"
}
self.borrowGieft(gieft)!.addNft(nft: <-nft)
(self.borrowGieft(gieft)!).addNft(nft: <-nft)
}

/// unpack a gieft
/// @params gieft: the uuid of the gieft to unpack
pub fun unpackGieft(gieft: UInt64): @{UInt64: NonFungibleToken.NFT} {
pre {
self.giefts.keys.contains(gieft) : "Gieft does not exist"
access(Owner)
fun unpackGieft(gieft: UInt64): @{UInt64:{ NonFungibleToken.NFT}}{
pre{
self.giefts.keys.contains(gieft):
"Gieft does not exist"
}
var nfts: @{UInt64: NonFungibleToken.NFT} <- {}

var nfts: @{UInt64:{ NonFungibleToken.NFT}} <-{}
let gieft = self.borrowGieft(gieft)!
let nftIDs = gieft.getNftIDs()
for nftID in nftIDs {
for nftID in nftIDs{
let nft <- gieft.unpack(nft: nftID)
let oldNft <- nfts[nftID] <- nft
destroy oldNft
Expand All @@ -189,37 +246,35 @@ pub contract Giefts {

/// borrow a gieft reference
/// @params gieft: the uuid of the gieft to borrow
pub fun borrowGieft(_ gieft: UInt64): &Gieft? {
return &self.giefts[gieft] as &Gieft?
access(all)
fun borrowGieft(_ gieft: UInt64): &Gieft?{
return &self.giefts[gieft]
}

/// get all gieft ids
pub fun getGieftIDs(): [UInt64] {
access(all)
fun getGieftIDs(): [UInt64]{
return self.giefts.keys
}

init () {
self.giefts <- {}
init(){
self.giefts <-{}
}
} /**/
/////////////////////////////////////////////////////////////
destroy () {
destroy self.giefts
}
}

/**//////////////////////////////////////////////////////////////
// FUNCTIONS //
// FUNCTIONS //
/////////////////////////////////////////////////////////////**/
/// create a new gieft collection resource
pub fun createGieftCollection (): @GieftCollection {
access(all)
fun createGieftCollection(): @GieftCollection{
return <-create GieftCollection()
}

init () {
init(){
/// paths
self.GieftsStoragePath = /storage/Giefts
self.GieftsPublicPath = /public/Giefts
self.GieftsPrivatePath = /private/Giefts
}
}
}
Loading

0 comments on commit b85f24f

Please sign in to comment.