Tutorial - Marketplace - marketplace.cdc

Hello !

I am trying to complete the Marketplace tutorial, however one piece of codecopied from the tutorial and the playground does not work in the VS Code extension ::

        // purchase lets a user send tokens to purchase an NFT that is for sale

            pub fun purchase(tokenID: UInt64, recipient: &AnyResource{NonFungibleToken.NFTReceiver}, buyTokens: @FungibleToken.Vault) {

                pre {

                    self.forSale[tokenID] != nil && self.prices[tokenID] != nil:

                        "No token matching this ID for sale!"

                    buyTokens.balance >= (self.prices[tokenID] ?? UFix64(0)):

                        "Not enough tokens to by the NFT!"

                }

The console says loss of ressources.
I am not sure what to do here.
Also in the Playground, this part works. However the transaction1 never finishes. So i’m kinda stuck. :confused:

Thank you

@robinbonnin I’m looking into it now. I’ll get back to you when I’ve found out what is happening. We think some of the recent updates to Cadence might have broke some of the tutorials so we are working on fixing them.

@robinbonnin I’m seeing the same error. This is because we use the optional chaining if let to get the price of the token. If the price isn’t found, then the execution would reach the end of the function and the buyTokens resource would be lost. we had mistakenly allowed this previously, but have fixed this bug.

To fix this in your code, you can remove the if let block and instead force unwrap the optional price.

    pub fun purchase(tokenID: UInt64, recipient: &AnyResource{NonFungibleToken.NFTReceiver}, buyTokens: @FungibleToken.Vault) {
        pre {
            self.forSale[tokenID] != nil && self.prices[tokenID] != nil:
                "No token matching this ID for sale!"
            buyTokens.balance >= (self.prices[tokenID] ?? UFix64(0)):
                "Not enough tokens to by the NFT!"
        }

        // get the value out of the optional
        let price = self.prices[tokenID]! 

        self.prices[tokenID] = nil

        // deposit the purchasing tokens into the owners vault
        self.ownerVault.deposit(from: <-buyTokens)

        // deposit the NFT into the buyers collection
        recipient.deposit(token: <-self.withdraw(tokenID: tokenID))

        emit TokenPurchased(id: tokenID, price: price)
        
    }

I apologize for the inconvenience. I’m working through the tutorials today to make sure they are correct