Skip to content

Commit 6df5cf8

Browse files
authored
Update buy_packs_primary_sale.cdc (#88)
* Update buy_packs_primary_sale.cdc * Update buy_packs_primary_sale.cdc * Update buy_packs_primary_sale.cdc * Update buy_packs_primary_sale.cdc
1 parent af239c6 commit 6df5cf8

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

atlas/transactions/user/buy_packs_primary_sale.cdc

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import FungibleToken from 0x{{.FungibleTokenContractAddress}}
22
import NonFungibleToken from 0x{{.NonFungibleTokenContractAddress}}
33
import DapperUtilityCoin from 0x{{.DapperUtilityCoinContractAddress}}
4-
import PackNFT from 0x{{.NFTContractAddress}}
4+
import PackNFT, {{.NFTProductName}} from 0x{{.NFTContractAddress}}
55
import NFTStorefront from 0x{{.NFTStorefrontV1ContractAddress}}
66

77
/// Transaction facilitates the purchase of listed NFT.
@@ -14,17 +14,43 @@ import NFTStorefront from 0x{{.NFTStorefrontV1ContractAddress}}
1414

1515
transaction() {
1616
let paymentVault: @{FungibleToken.Vault}
17-
let PackNFTCollection: &{NonFungibleToken.CollectionPublic}
17+
let PackNFTCollection: &PackNFT.Collection
1818
let storefront: &NFTStorefront.Storefront
1919
let listings: [&{NFTStorefront.ListingPublic}]
2020

21-
prepare(universalDucPayer: auth(Storage, Capabilities) &Account) {
21+
prepare(universalDucPayer: auth(Storage, Capabilities) &Account, user: auth(Storage, Capabilities) &Account) {
2222
let sellerAddress: Address = 0x{{.StorefrontAddress}}
2323
let buyerAddress: Address = 0x{{.RecipientAddress}}
2424
let listingIds: [UInt64] = [{{.ListingIds}}]
2525
let buyer = getAccount(buyerAddress)
2626

2727
assert(sellerAddress != buyerAddress, message : "Buyer and seller can not be same")
28+
29+
// Ensure the user has an {{.NFTProductName}} collection set up
30+
if user.storage.borrow<&{{.NFTProductName}}.Collection>(from: {{.NFTProductName}}.CollectionStoragePath) == nil {
31+
// Create a new collection and save it to the account storage
32+
user.storage.save(<- {{.NFTProductName}}.createEmptyCollection(nftType: Type<@{{.NFTProductName}}.NFT>()), to: {{.NFTProductName}}.CollectionStoragePath)
33+
34+
// Create a public capability for the collection
35+
user.capabilities.unpublish({{.NFTProductName}}.CollectionPublicPath)
36+
user.capabilities.publish(
37+
user.capabilities.storage.issue<&{{.NFTProductName}}.Collection>({{.NFTProductName}}.CollectionStoragePath),
38+
at: {{.NFTProductName}}.CollectionPublicPath
39+
)
40+
}
41+
// Ensure the user has a PackNFT collection set up
42+
if user.storage.borrow<&PackNFT.Collection>(from: PackNFT.CollectionStoragePath) == nil {
43+
// Create a new collection and save it to the account storage
44+
user.storage.save(<- PackNFT.createEmptyCollection(nftType: Type<@PackNFT.NFT>()), to: PackNFT.CollectionStoragePath)
45+
46+
// Create a public capability for the collection
47+
user.capabilities.unpublish(PackNFT.CollectionPublicPath)
48+
user.capabilities.publish(
49+
user.capabilities.storage.issue<&PackNFT.Collection>(PackNFT.CollectionStoragePath),
50+
at: PackNFT.CollectionPublicPath
51+
)
52+
}
53+
2854
// Access the storefront public resource of the seller to purchase the listing.
2955
self.storefront = getAccount(sellerAddress)
3056
.capabilities.borrow<&NFTStorefront.Storefront>(
@@ -51,7 +77,7 @@ transaction() {
5177
self.paymentVault <- provider.withdraw(amount: salePrice)
5278

5379
// Access the buyer's NFT collection to store the purchased NFT.
54-
self.PackNFTCollection = buyer.capabilities.borrow<&{NonFungibleToken.CollectionPublic}>(PackNFT.CollectionPublicPath)
80+
self.PackNFTCollection = buyer.capabilities.borrow<&PackNFT.Collection>(PackNFT.CollectionPublicPath)
5581
?? panic("Could not borrow Storefront from provided address")
5682

5783
}

0 commit comments

Comments
 (0)