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:
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 :)