1- import " ExampleToken"
21import " FungibleToken"
2+ import " FungibleTokenMetadataViews"
33import " NonFungibleToken"
4- import " ExampleNFT"
54import " NFTStorefrontV2"
65import " 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///
1415transaction (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}
0 commit comments