Skip to content

Commit 6488b13

Browse files
committed
update buy_item to use generic types
1 parent 12225e5 commit 6488b13

File tree

5 files changed

+47
-35
lines changed

5 files changed

+47
-35
lines changed

flow.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
"dependencies": {
4747
"Burner": {
48-
"source": "mainnet://f233dcee88fe0abe.Burner",
48+
"source": "testnet://9a0766d93b6608b7.Burner",
4949
"hash": "71af18e227984cd434a3ad00bb2f3618b76482842bae920ee55662c37c8bf331",
5050
"aliases": {
5151
"emulator": "f8d6e0586b0a20c7",
@@ -134,8 +134,8 @@
134134
}
135135
},
136136
"FungibleToken": {
137-
"source": "mainnet://f233dcee88fe0abe.FungibleToken",
138-
"hash": "23c1159cf99b2b039b6b868d782d57ae39b8d784045d81597f100a4782f0285b",
137+
"source": "testnet://9a0766d93b6608b7.FungibleToken",
138+
"hash": "d84e41a86bd27806367b89c75a2a9570f7713480ab113cc214c4576466888958",
139139
"aliases": {
140140
"emulator": "ee82856bf20e2aa6",
141141
"mainnet": "f233dcee88fe0abe",
@@ -170,8 +170,8 @@
170170
}
171171
},
172172
"MetadataViews": {
173-
"source": "mainnet://1d7e57aa55817448.MetadataViews",
174-
"hash": "10a239cc26e825077de6c8b424409ae173e78e8391df62750b6ba19ffd048f51",
173+
"source": "testnet://631e88ae7f1d7c20.MetadataViews",
174+
"hash": "0b746cabba668c39de9dc47b94fc458f4119cbabb6883616e4a8750518310ccb",
175175
"aliases": {
176176
"emulator": "f8d6e0586b0a20c7",
177177
"mainnet": "1d7e57aa55817448",
@@ -222,16 +222,16 @@
222222
}
223223
},
224224
"NonFungibleToken": {
225-
"source": "mainnet://1d7e57aa55817448.NonFungibleToken",
226-
"hash": "b63f10e00d1a814492822652dac7c0574428a200e4c26cb3c832c4829e2778f0",
225+
"source": "testnet://631e88ae7f1d7c20.NonFungibleToken",
226+
"hash": "ac40c5a3ec05884ae48cb52ebf680deebb21e8a0143cd7d9b1dc88b0f107e088",
227227
"aliases": {
228228
"emulator": "f8d6e0586b0a20c7",
229229
"mainnet": "1d7e57aa55817448",
230230
"testnet": "631e88ae7f1d7c20"
231231
}
232232
},
233233
"ViewResolver": {
234-
"source": "mainnet://1d7e57aa55817448.ViewResolver",
234+
"source": "testnet://631e88ae7f1d7c20.ViewResolver",
235235
"hash": "374a1994046bac9f6228b4843cb32393ef40554df9bd9907a702d098a2987bde",
236236
"aliases": {
237237
"emulator": "f8d6e0586b0a20c7",

transactions/buy_item.cdc

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
1-
import "ExampleToken"
21
import "FungibleToken"
2+
import "FungibleTokenMetadataViews"
33
import "NonFungibleToken"
4-
import "ExampleNFT"
54
import "NFTStorefrontV2"
65
import "MetadataViews"
76

8-
/// Transaction facilitates the purcahse of listed NFT. It takes the storefront address, listing resource that need to be
7+
/// Transaction facilitates the purchase of listed NFT.
8+
/// It takes the storefront address, listing resource that need to be
99
/// purchased & a address that will takeaway the commission.
1010
///
11-
/// Buyer of the listing (,i.e. underling NFT) would authorize and sign the transaction and if purchase happens then
11+
/// Buyer of the listing (,i.e. underling NFT) would authorize
12+
/// and sign the transaction and if purchase happens then
1213
/// transacted NFT would store in buyer's collection.
1314
///
1415
transaction(listingResourceID: UInt64,
1516
storefrontAddress: Address,
16-
commissionRecipient: Address?) {
17+
commissionRecipient: Address?,
18+
nftTypeIdentifier: String,
19+
ftTypeIdentifier: String) {
1720

1821
let paymentVault: @{FungibleToken.Vault}
19-
let exampleNFTReceiver: &{NonFungibleToken.Receiver}
22+
let NFTReceiver: &{NonFungibleToken.Receiver}
2023
let storefront: &{NFTStorefrontV2.StorefrontPublic}
2124
let listing: &{NFTStorefrontV2.ListingPublic}
2225
var commissionRecipientCap: Capability<&{FungibleToken.Receiver}>?
2326

2427
prepare(acct: auth(BorrowValue) &Account) {
28+
29+
let collectionData = MetadataViews.resolveContractViewFromTypeIdentifier(
30+
resourceTypeIdentifier: nftTypeIdentifier,
31+
viewType: Type<MetadataViews.NFTCollectionData>()
32+
) as? MetadataViews.NFTCollectionData
33+
?? panic("Could not construct valid NFT type and view from identifier \(nftTypeIdentifier)")
34+
35+
let vaultData = MetadataViews.resolveContractViewFromTypeIdentifier(
36+
resourceTypeIdentifier: ftTypeIdentifier,
37+
viewType: Type<FungibleTokenMetadataViews.FTVaultData>()
38+
) as? FungibleTokenMetadataViews.FTVaultData
39+
?? panic("Could not construct valid FT type and view from identifier \(ftTypeIdentifier)")
40+
2541
self.commissionRecipientCap = nil
2642
// Access the storefront public resource of the seller to purchase the listing.
2743
self.storefront = getAccount(storefrontAddress).capabilities.borrow<&{NFTStorefrontV2.StorefrontPublic}>(
@@ -34,16 +50,12 @@ transaction(listingResourceID: UInt64,
3450
let price = self.listing.getDetails().salePrice
3551

3652
// Access the vault of the buyer to pay the sale price of the listing.
37-
let mainVault = acct.storage.borrow<auth(FungibleToken.Withdraw) &ExampleToken.Vault>(from: /storage/exampleTokenVault)
38-
?? panic("The signer does not store an ExampleToken.Vault object at the path /storage/exampleTokenVault "
53+
let mainVault = acct.storage.borrow<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>(from: vaultData.storagePath)
54+
?? panic("The signer does not store an Vault object at the path \(vaultData.storagePath)"
3955
.concat(". The signer must initialize their account with this vault first!"))
4056
self.paymentVault <- mainVault.withdraw(amount: price)
4157

42-
// Access the buyer's NFT collection to store the purchased NFT.
43-
let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData?
44-
?? panic("Could not resolve NFTCollectionData view. The ExampleNFT contract needs to implement the NFTCollectionData Metadata view in order to execute this transaction")
45-
46-
self.exampleNFTReceiver = acct.capabilities.borrow<&{NonFungibleToken.Receiver}>(collectionData.publicPath)
58+
self.NFTReceiver = acct.capabilities.borrow<&{NonFungibleToken.Receiver}>(collectionData.publicPath)
4759
?? panic("Cannot borrow an NFT collection receiver from the signer's account at path \(collectionData.publicPath).")
4860

4961
// Fetch the commission amt.
@@ -52,9 +64,9 @@ transaction(listingResourceID: UInt64,
5264
if commissionRecipient != nil && commissionAmount != 0.0 {
5365
// Access the capability to receive the commission.
5466
let _commissionRecipientCap = getAccount(commissionRecipient!).capabilities.get<&{FungibleToken.Receiver}>(
55-
/public/exampleTokenReceiver
67+
vaultData.receiverPath
5668
)
57-
assert(_commissionRecipientCap.check(), message: "Commission Recipient doesn't have ExampleToken receiving capability")
69+
assert(_commissionRecipientCap.check(), message: "Commission Recipient doesn't have a receiving capability at \(vaultData.receiverPath)")
5870
self.commissionRecipientCap = _commissionRecipientCap
5971
} else if commissionAmount == 0.0 {
6072
self.commissionRecipientCap = nil
@@ -70,6 +82,6 @@ transaction(listingResourceID: UInt64,
7082
commissionRecipient: self.commissionRecipientCap
7183
)
7284
// Deposit the NFT in the buyer's collection.
73-
self.exampleNFTReceiver.deposit(token: <-item)
85+
self.NFTReceiver.deposit(token: <-item)
7486
}
7587
}

transactions/sell_item.cdc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ transaction(
2727
) {
2828

2929
let tokenReceiver: Capability<&{FungibleToken.Receiver}>
30-
let exampleNFTProvider: Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>
30+
let NFTProvider: Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>
3131
let storefront: auth(NFTStorefrontV2.CreateListing) &NFTStorefrontV2.Storefront
3232
var saleCuts: [NFTStorefrontV2.SaleCut]
3333
var marketplacesCapability: [Capability<&{FungibleToken.Receiver}>]
@@ -79,7 +79,7 @@ transaction(
7979
}
8080
assert(nftProviderCap?.check() ?? false, message: "Could not assign Provider Capability")
8181

82-
self.exampleNFTProvider = nftProviderCap!
82+
self.NFTProvider = nftProviderCap!
8383

8484
let collection = acct.capabilities.borrow<&{NonFungibleToken.Collection}>(
8585
collectionData.publicPath
@@ -128,7 +128,7 @@ transaction(
128128
execute {
129129
// Create listing
130130
self.storefront.createListing(
131-
nftProviderCapability: self.exampleNFTProvider,
131+
nftProviderCapability: self.NFTProvider,
132132
nftType: Type<@ExampleNFT.NFT>(),
133133
nftID: saleItemID,
134134
salePaymentVaultType: Type<@ExampleToken.Vault>(),

transactions/sell_item_and_replace_current_listing.cdc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ transaction(
2727
) {
2828

2929
let tokenReceiver: Capability<&{FungibleToken.Receiver}>
30-
let exampleNFTProvider: Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>
30+
let NFTProvider: Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>
3131
let storefront: auth(NFTStorefrontV2.CreateListing, NFTStorefrontV2.RemoveListing) &NFTStorefrontV2.Storefront
3232
var saleCuts: [NFTStorefrontV2.SaleCut]
3333
var marketplacesCapability: [Capability<&{FungibleToken.Receiver}>]
@@ -82,7 +82,7 @@ transaction(
8282
}
8383
assert(nftProviderCap?.check() ?? false, message: "Could not assign Provider Capability")
8484

85-
self.exampleNFTProvider = nftProviderCap!
85+
self.NFTProvider = nftProviderCap!
8686

8787
let collection = acct.capabilities.borrow<&{NonFungibleToken.Collection}>(
8888
collectionData.publicPath
@@ -115,7 +115,7 @@ transaction(
115115
amount: effectiveSaleItemPrice - totalRoyaltyCut
116116
)
117117
)
118-
assert(self.exampleNFTProvider.borrow() != nil, message: "Missing or mis-typed ExampleNFT.Collection provider")
118+
assert(self.NFTProvider.borrow() != nil, message: "Missing or mis-typed ExampleNFT.Collection provider")
119119

120120
self.storefront = acct.storage.borrow<auth(NFTStorefrontV2.CreateListing, NFTStorefrontV2.RemoveListing) &NFTStorefrontV2.Storefront>(
121121
from: NFTStorefrontV2.StorefrontStoragePath
@@ -143,7 +143,7 @@ transaction(
143143
}
144144
// Create listing
145145
self.storefront.createListing(
146-
nftProviderCapability: self.exampleNFTProvider,
146+
nftProviderCapability: self.NFTProvider,
147147
nftType: Type<@ExampleNFT.NFT>(),
148148
nftID: saleItemID,
149149
salePaymentVaultType: Type<@ExampleToken.Vault>(),

transactions/sell_item_with_marketplace_cut.cdc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ transaction(
2626
marketPlaceSaleCutPercentage: UFix64
2727
) {
2828
let flowReceiver: Capability<&{FungibleToken.Receiver}>
29-
let exampleNFTProvider: Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>
29+
let NFTProvider: Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>
3030
let storefront: auth(NFTStorefrontV2.CreateListing) &NFTStorefrontV2.Storefront
3131
var saleCuts: [NFTStorefrontV2.SaleCut]
3232
var marketplacesCapability: [Capability<&{FungibleToken.Receiver}>]
@@ -81,7 +81,7 @@ transaction(
8181
}
8282
assert(nftProviderCap?.check() ?? false, message: "Could not assign Provider Capability")
8383

84-
self.exampleNFTProvider = nftProviderCap!
84+
self.NFTProvider = nftProviderCap!
8585

8686
let collection = acct.capabilities.borrow<&{NonFungibleToken.Collection}>(
8787
collectionData.publicPath
@@ -111,7 +111,7 @@ transaction(
111111
amount: saleItemPrice - totalRoyaltyCut - saleItemPrice * marketPlaceSaleCutPercentage
112112
)
113113
)
114-
assert(self.exampleNFTProvider.borrow() != nil, message: "Missing or mis-typed ExampleNFT.Collection provider")
114+
assert(self.NFTProvider.borrow() != nil, message: "Missing or mis-typed ExampleNFT.Collection provider")
115115

116116
self.storefront = acct.storage.borrow<auth(NFTStorefrontV2.CreateListing) &NFTStorefrontV2.Storefront>(
117117
from: NFTStorefrontV2.StorefrontStoragePath
@@ -134,7 +134,7 @@ transaction(
134134
execute {
135135
// Create listing
136136
self.storefront.createListing(
137-
nftProviderCapability: self.exampleNFTProvider,
137+
nftProviderCapability: self.NFTProvider,
138138
nftType: Type<@ExampleNFT.NFT>(),
139139
nftID: saleItemID,
140140
salePaymentVaultType: Type<@FlowToken.Vault>(),

0 commit comments

Comments
 (0)