Skip to content

Commit

Permalink
Debug tx
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Dec 12, 2024
1 parent 4b3cf84 commit 5837708
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions cadence/transactions/purchase_nft.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ transaction {
let exampleNFTCollection: &ExampleNFT.Collection
let storefront: auth(NFTStorefront.CreateListing) &NFTStorefront.Storefront
let listing: &{NFTStorefront.ListingPublic}
var listingResourceId: UInt64

prepare(acct: auth(Storage, Capabilities, NFTStorefront.CreateListing) &Account) {
prepare(acct: auth(Storage, Capabilities, NFTStorefront.CreateListing) &Account) {

// Create and save the storefront
let storefront <- NFTStorefront.createStorefront()
Expand Down Expand Up @@ -65,7 +66,7 @@ transaction {
)

// List the NFT
let listingResourceId = self.storefront.createListing(
self.listingResourceId = self.storefront.createListing(
nftProviderCapability: nftProviderCapability,
nftType: Type<@ExampleNFT.NFT>(),
nftID: nftID,
Expand All @@ -82,20 +83,22 @@ transaction {
log("Listing created successfully")

// Borrow the listing reference
self.listing = self.storefront.borrowListing(listingResourceID: listingResourceId)
self.listing = self.storefront.borrowListing(listingResourceID: self.listingResourceId)
?? panic("No Offer with that ID in Storefront")

// Fetch the sale price
let price = self.listing.getDetails().salePrice

// Borrow FlowToken vault with proper authorization to withdraw
let flowTokenCap = acct.capabilities.borrow<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>(
/public/flowTokenReceiver
) ?? panic("Cannot borrow FlowToken vault with Withdraw entitlement from account")
// Issue the capability for the FlowToken vault with Withdraw entitlement
let flowTokenCap = acct.capabilities.storage.issue<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>(
/storage/flowTokenVault
)

// Withdraw the payment
self.paymentVault <- flowTokenCap.withdraw(amount: price)
let flowTokenVault = flowTokenCap.borrow()
?? panic("Failed to borrow FungibleToken.Vault from capability")

self.paymentVault <- flowTokenVault.withdraw(amount: price)

// Borrow the NFT collection receiver reference
let nftCollectionCap = acct.capabilities.borrow<&ExampleNFT.Collection>(
Expand All @@ -115,7 +118,7 @@ transaction {
self.exampleNFTCollection.deposit(token: <-nft)

// Cleanup the listing from the storefront
self.storefront.cleanup(listingResourceID: 10)
self.storefront.cleanup(listingResourceID: self.listingResourceId)

log("Transaction completed successfully")
}
Expand Down

0 comments on commit 5837708

Please sign in to comment.