Skip to content

Commit

Permalink
add burnCallback function to emit ListingCompleted events
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahannan committed Aug 15, 2024
1 parent b3d66b2 commit 4b1086b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 18 deletions.
26 changes: 22 additions & 4 deletions contracts/NFTStorefront.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "FungibleToken"
import "NonFungibleToken"
import "Burner"

/// NB: This contract is no longer supported. NFT Storefront V2 is recommended
///
Expand Down Expand Up @@ -202,7 +203,7 @@ access(all) contract NFTStorefront {
/// A resource that allows an NFT to be sold for an amount of a given FungibleToken,
/// and for the proceeds of that sale to be split between several recipients.
///
access(all) resource Listing : ListingPublic {
access(all) resource Listing: ListingPublic, Burner.Burnable {
// Event to be emitted when this listing is destroyed.
// If the listing has not been purchased, we regard it as completed here.
// There is a separate event in purchase for purchased listings
Expand All @@ -214,6 +215,23 @@ access(all) contract NFTStorefront {
nftID: UInt64 = self.details.nftID
)

access(contract) fun burnCallback() {
// If the listing has not been purchased, we regard it as completed here.
// Otherwise we regard it as completed in purchase().
// This is because we destroy the listing in Storefront.removeListing()
// or Storefront.cleanup() .
// If we change this destructor, revisit those functions.
if !self.details.purchased {
emit ListingCompleted(
listingResourceID: self.uuid,
storefrontResourceID: self.details.storefrontID,
purchased: self.details.purchased,
nftType: self.details.nftType,
nftID: self.details.nftID
)
}
}

/// The simple (non-Capability, non-complex) details of the sale
access(self) let details: ListingDetails

Expand Down Expand Up @@ -412,7 +430,7 @@ access(all) contract NFTStorefront {
let oldListing <- self.listings[listingResourceID] <- listing
// Note that oldListing will always be nil, but we have to handle it.
destroy oldListing
Burner.burn(<-oldListing)

emit ListingAvailable(
storefrontAddress: self.owner?.address!,
Expand All @@ -435,7 +453,7 @@ access(all) contract NFTStorefront {
?? panic("missing Listing")

// This will emit a ListingCompleted event.
destroy listing
Burner.burn(<-listing)
}

/// getListingIDs
Expand Down Expand Up @@ -468,7 +486,7 @@ access(all) contract NFTStorefront {

let listing <- self.listings.remove(key: listingResourceID)!
assert(listing.getDetails().purchased == true, message: "listing is not purchased, only admin can remove")
destroy listing
Burner.burn(<-listing)
}

/// constructor
Expand Down
37 changes: 31 additions & 6 deletions contracts/NFTStorefrontV2.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "FungibleToken"
import "NonFungibleToken"
import "Burner"

/// NFTStorefrontV2
///
Expand Down Expand Up @@ -254,7 +255,7 @@ access(all) contract NFTStorefrontV2 {
/// A resource that allows an NFT to be sold for an amount of a given FungibleToken,
/// and for the proceeds of that sale to be split between several recipients.
///
access(all) resource Listing: ListingPublic {
access(all) resource Listing: ListingPublic, Burner.Burnable {
/// The simple (non-Capability, non-complex) details of the sale
access(self) let details: ListingDetails

Expand All @@ -268,6 +269,30 @@ access(all) contract NFTStorefrontV2 {
/// to receive the marketplace commission.
access(contract) let marketplacesCapability: [Capability<&{FungibleToken.Receiver}>]?

access(contract) fun burnCallback() {
// If the listing has not been purchased, we regard it as completed here.
// Otherwise we regard it as completed in purchase().
// This is because we destroy the listing in Storefront.removeListing()
// or Storefront.cleanup() .
// If we change this destructor, revisit those functions.
if !self.details.purchased {
emit ListingCompleted(
listingResourceID: self.uuid,
storefrontResourceID: self.details.storefrontID,
purchased: self.details.purchased,
nftType: self.details.nftType,
nftUUID: self.details.nftUUID,
nftID: self.details.nftID,
salePaymentVaultType: self.details.salePaymentVaultType,
salePrice: self.details.salePrice,
customID: self.details.customID,
commissionAmount: self.details.commissionAmount,
commissionReceiver: nil,
expiry: self.details.expiry
)
}
}

/// borrowNFT
/// Return the reference of the NFT that is listed for sale.
/// if the NFT is absent, for example if it has been sold via another listing.
Expand Down Expand Up @@ -590,7 +615,7 @@ access(all) contract NFTStorefrontV2 {
let oldListing <- self.listings[listingResourceID] <- listing
// Note that oldListing will always be nil, but we have to handle it.
destroy oldListing
Burner.burn(<-oldListing)

// Add the `listingResourceID` in the tracked listings.
self.addDuplicateListing(nftIdentifier: nftType.identifier, nftID: nftID, listingResourceID: listingResourceID)
Expand Down Expand Up @@ -657,7 +682,7 @@ access(all) contract NFTStorefrontV2 {
let listingDetails = listing.getDetails()
self.removeDuplicateListing(nftIdentifier: listingDetails.nftType.identifier, nftID: listingDetails.nftID, listingResourceID: listingResourceID)
// This will emit a ListingCompleted event.
destroy listing
Burner.burn(<-listing)
}

/// getListingIDs
Expand Down Expand Up @@ -690,7 +715,7 @@ access(all) contract NFTStorefrontV2 {
let listingDetails = listing.getDetails()
self.removeDuplicateListing(nftIdentifier: listingDetails.nftType.identifier, nftID: listingDetails.nftID, listingResourceID: listingResourceID)

destroy listing
Burner.burn(<-listing)
}

/// getDuplicateListingIDs
Expand Down Expand Up @@ -758,7 +783,7 @@ access(all) contract NFTStorefrontV2 {
let listingDetails = listing.getDetails()
self.removeDuplicateListing(nftIdentifier: listingDetails.nftType.identifier, nftID: listingDetails.nftID, listingResourceID: listingResourceID)

destroy listing
Burner.burn(<-listing)
}

/// cleanupGhostListings
Expand All @@ -782,7 +807,7 @@ access(all) contract NFTStorefrontV2 {
for listingID in duplicateListings {
self.cleanup(listingResourceID: listingID)
}
destroy listing
Burner.burn(<-listing)
}

/// constructor
Expand Down
34 changes: 32 additions & 2 deletions flow.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
"mainnet": "0xf233dcee88fe0abe"
}
},
"Burner": {
"source": "./contracts/utility/Burner.cdc",
"aliases": {
"emulator": "f8d6e0586b0a20c7",
"previewnet": "b6763b4399a888c8",
"testing": "0000000000000001",
"testnet": "9a0766d93b6608b7",
"mainnet": "0xf233dcee88fe0abe"
}
},
"NonFungibleToken": {
"source": "./contracts/utility/NonFungibleToken.cdc",
"aliases": {
Expand Down Expand Up @@ -191,7 +201,21 @@
"emulator-account": {
"address": "f8d6e0586b0a20c7",
"key": "ca102a35963666f3b517a903b747da4eece5e0553523cb8e59a851e162c85db2"
}
},
"mainnet-account": {
"address": "0x4eb8a10cb9f87357",
"key": {
"type": "file",
"location": "./storefront-mainnet.pkey"
}
},
"testnet-storefront": {
"address": "0x94b06cfca1d8a476",
"key": {
"type": "file",
"location": "./storefront-testnet.pkey"
}
}
},
"deployments": {
"emulator": {
Expand All @@ -202,6 +226,12 @@
"NFTStorefront",
"NFTStorefrontV2"
]
}
},
"mainnet": {
"mainnet-account": [
"NFTStorefrontV2",
"NFTStorefront"
]
}
}
}
Loading

0 comments on commit 4b1086b

Please sign in to comment.