2

I was following the example from the basic split app and changing it a bit to try to make a simple sell/buy NFT transaction.

For now I'm just trying to transfer the NFT from one wallet to another.

My idea is basically that a user has to call the 'Offer' endpoint to send the NFT to the script and then the other wallet calls the 'Buy' endpoint to get the NFT.

doOffer :: OfferArgs -> Contract () MarketplaceSchema T.Text ()
doOffer offerData@OfferArgs{..} = do
    pkh <- pubKeyHash <$> Plutus.Contract.ownPubKey
    let offerData = OfferData
                { seller         = pkh
                , price          = aPrice
                , nftCurrencySymbol = aCurrencySymbol
                , nftTokenName      = aTokenName
                }
    logInfo @Haskell.String $ printf "creating offer %s" (Haskell.show offerData)
    let tx = Constraints.mustPayToTheScript offerData (Value.singleton aCurrencySymbol aTokenName 1)
    void $ submitTxConstraints offerValidator tx

doBuy :: OfferArgs -> Contract () MarketplaceSchema T.Text ()
doBuy offer@OfferArgs{..} = do
    logInfo @Haskell.String $ printf "buying %s" (Haskell.show offer)
    pkh <- pubKeyHash <$> Plutus.Contract.ownPubKey
    let contractAddress = Scripts.validatorAddress offerValidator
    utxos <- utxosAt contractAddress
    let tx =
            collectFromScript utxos Buy
            <> Constraints.mustPayToPubKey pkh (Value.singleton aCurrencySymbol aTokenName 1) -- Send the NFT to buyer
    void $ submitTxConstraintsSpending offerValidator utxos tx

The offer part seems to work well, and the NFT shows up at the script output. However when I try to send it to the buyer in mustPayToPubKey I get an error:

Contract instance stopped with error: "WalletError (InsufficientFunds \"Total: Value (Map [(,Map [(\\\"\\\",100)])]) expected: Value (Map [(,Map [(\\\"\\\",3242)])])\")" ]

I don't know but it looks like it's trying to pay the token using the Buyer's wallet, but I thought the collectFromScript part was supposed to use script utxo?

Here is how it looks in the simulator when the error occurs: Simulator

I believe the rest of the code is not necessary as I'm not doing any kind of complex validation yet, I'm just trying to make a simple transfer but if you need the validator piece let me know :)

2 Answers 2

3

expected: Value (Map [(,Map [(\\\"\\\",3242)])])\")"

Try adding a few zeros to the starting balances. It's saying it doesn't have enough to pay the fees.

I'm surprised that the first transaction worked, but without seeing all of your code it's hard to diagnose why.

1
  • 1
    Yeah that was exactly it, thank you!!
    – glneto
    Commented Oct 25, 2021 at 17:26
2

Transaction should be done by someone, in this case by the Buyer. In order to do that, buyer should pay the fees of the transaction.

Actually, this error is explaining that buyer has less lovelace than the required to pay the fees.

"WalletError (InsufficientFunds \"Total: Value (Map [(,Map [(\\\"\\\",100)])]) expected: Value (Map [(,Map [(\\\"\\\",3242)])])\")" ]

Buyer has 100 lovelace and it's required to pay 3242.

Function collectFromScript has nothing to do with it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.