1+ import FungibleToken from 0x{{.FungibleTokenContractAddress }}
2+ import NonFungibleToken from 0x{{.NonFungibleTokenContractAddress }}
3+ import DapperUtilityCoin from 0x{{.DapperUtilityCoinContractAddress }}
4+ import {{.NFTProductName }}, AllDay from 0x{{.NFTContractAddress }}
5+ import NFTStorefront from 0x{{.NFTStorefrontV1ContractAddress }}
6+
7+ /// Transaction facilitates the purchase of listed NFT.
8+ /// It takes the storefront address, listing resource that need
9+ /// to be purchased & a address that will takeaway the commission.
10+ ///
11+ /// Buyer of the listing (,i.e. underling NFT) would authorize and sign the
12+ /// transaction and if purchase happens then transacted NFT would store in
13+ /// buyer's collection.
14+
15+ transaction () {
16+ let paymentVault : @{FungibleToken .Vault }
17+ let {{.NFTProductName }}Collection : &{NonFungibleToken .CollectionPublic }
18+ let storefront : &NFTStorefront.Storefront
19+ let listings : [&{NFTStorefront .ListingPublic }]
20+
21+ prepare (universalDucPayer : auth (Storage , Capabilities ) &Account ) {
22+ let sellerAddress : Address = 0x{{.StorefrontAddress }}
23+ let buyerAddress : Address = 0x{{.RecipientAddress }}
24+ let listingIds : [UInt64 ] = [{{.ListingIds }}]
25+ let buyer = getAccount (buyerAddress )
26+
27+ assert (sellerAddress ! = buyerAddress , message : " Buyer and seller can not be same" )
28+ // Access the storefront public resource of the seller to purchase the listing.
29+ self .storefront = getAccount (sellerAddress )
30+ .capabilities .borrow <&NFTStorefront.Storefront >(
31+ NFTStorefront .StorefrontPublicPath
32+ )
33+ ?? panic (" Could not borrow Storefront from provided address" )
34+
35+ var salePrice : UFix64 = 0.0
36+ self .listings = []
37+ for listingID in listingIds {
38+ let listing = self .storefront .borrowListing (listingResourceID : listingID )
39+ ?? panic (" No Listing with that ID in Storefront" )
40+ self .listings .append (
41+ listing
42+ )
43+ salePrice = salePrice + listing .getDetails ().salePrice
44+ }
45+
46+ // Borrow a provider reference to the buyers vault
47+ let provider = universalDucPayer .storage .borrow <auth (FungibleToken.Withdraw ) &DapperUtilityCoin.Vault >(from : /storage/dapperUtilityCoinVault )
48+ ?? panic (" Cannot borrow DUC vault from buyer account storage" )
49+
50+ // withdraw the purchase tokens from the vault
51+ self .paymentVault <- provider .withdraw (amount : salePrice )
52+
53+ // Access the buyer's NFT collection to store the purchased NFT.
54+ self .{{.NFTProductName }}Collection = buyer .capabilities .borrow <&{NonFungibleToken .CollectionPublic }>({{.NFTProductName }}.CollectionPublicPath )
55+ ?? panic (" Could not borrow Storefront from provided address" )
56+
57+ }
58+
59+ execute {
60+ // Purchase the NFTs
61+ for listing in self .listings {
62+ let listingSalePrice : UFix64 = listing .getDetails ().salePrice
63+ let listingPaymentVault <-self .paymentVault .withdraw (amount : listingSalePrice )
64+ let item <- listing .purchase (
65+ payment : <- listingPaymentVault
66+ )
67+
68+ self .{{.NFTProductName }}Collection .deposit (token : <- item )
69+ }
70+ destroy self .paymentVault
71+ }
72+ }
0 commit comments