Create a Marketplace
Create a Marketplace
14 Oct 2022
Contributed by Flow Blockchain
Create a secondary marketplace for your NFTS using the NFT Storefront Contract.
Smart Contract Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
pub contract NFTStorefront {
.....
// Storefront
// A resource that allows its owner to manage a list of Listings, and anyone to interact with them
// in order to query their details and purchase the NFTs that they represent.
//
pub resource Storefront : StorefrontManager, StorefrontPublic {
// The dictionary of Listing uuids to Listing resources.
access(self) var listings: @{UInt64: Listing}
// insert
// Create and publish a Listing for an NFT.
//
pub fun createListing(
nftProviderCapability: Capability<&{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>,
nftType: Type,
nftID: UInt64,
salePaymentVaultType: Type,
saleCuts: [SaleCut]
): UInt64 {
let listing <- create Listing(
nftProviderCapability: nftProviderCapability,
nftType: nftType,
nftID: nftID,
salePaymentVaultType: salePaymentVaultType,
saleCuts: saleCuts,
storefrontID: self.uuid
)
let listingResourceID = listing.uuid
let listingPrice = listing.getDetails().salePrice
// Add the new listing to the dictionary.
let oldListing <- self.listings[listingResourceID] <- listing
// Note that oldListing will always be nil, but we have to handle it.
destroy oldListing
emit ListingAvailable(
storefrontAddress: self.owner?.address!,
listingResourceID: listingResourceID,
nftType: nftType,
nftID: nftID,
ftVaultType: salePaymentVaultType,
price: listingPrice
)
return listingResourceID
}
// removeListing
// Remove a Listing that has not yet been purchased from the collection and destroy it.
//
pub fun removeListing(listingResourceID: UInt64) {
let listing <- self.listings.remove(key: listingResourceID)
?? panic("missing Listing")
// This will emit a ListingCompleted event.
destroy listing
}
// getListingIDs
// Returns an array of the Listing resource IDs that are in the collection
//
pub fun getListingIDs(): [UInt64] {
return self.listings.keys
}
// borrowSaleItem
// Returns a read-only view of the SaleItem for the given listingID if it is contained by this collection.
//
pub fun borrowListing(listingResourceID: UInt64): &Listing{ListingPublic}? {
if self.listings[listingResourceID] != nil {
return &self.listings[listingResourceID] as! &Listing{ListingPublic}
} else {
return nil
}
}
// cleanup
// Remove an listing *if* it has been purchased.
// Anyone can call, but at present it only benefits the account owner to do so.
// Kind purchasers can however call it if they like.
//
pub fun cleanup(listingResourceID: UInt64) {
pre {
self.listings[listingResourceID] != nil: "could not find listing with given id"
}
let listing <- self.listings.remove(key: listingResourceID)!
assert(listing.getDetails().purchased == true, message: "listing is not purchased, only admin can remove")
destroy listing
}
// destructor
//
destroy () {
destroy self.listings
// Let event consumers know that this storefront will no longer exist
emit StorefrontDestroyed(storefrontResourceID: self.uuid)
}
// constructor
//
init () {
self.listings <- {}
// Let event consumers know that this storefront exists
emit StorefrontInitialized(storefrontResourceID: self.uuid)
}
}
pub fun createStorefront(): @Storefront {
return <-create Storefront()
}
....
}
The NFT Storefront contract is used to be able to list NFTS that a user owns for sale on your marketplace. Users can also purchase other NFTS, borrow listings, and do tons of other things related to the Storefront contract.
Before doing any of this, you need to create a Storefront resource from the NFTStorefront contract and save it to your account. Once that resource is saved you then have access to the Storefront resource which will give you the ability to create listings, remove listings, getListings, borrowListings, cleanupListings, and destroy your own Storefront.
The createStorefront function allows anyone to create a storefront and save it to their account.
Transaction Example
1
2
3
4
5
6
7
8
9
10
11
12
13
import NFTStorefront from 0x03
// This transaction sets up account 0x01 for the marketplace tutorial
// by publishing a Vault reference and creating an empty NFT Collection.
transaction {
prepare(acct: AuthAccount) {
acct.save<@NFTStorefront.Storefront>(<- NFTStorefront.createStorefront() , to: NFTStorefront.StorefrontStoragePath)
log("storefront created")
}
}
Here you are simply calling the createStorefront function to save a storefront to your account.
Up Next: Create an NFT Listing
33%