0

Which TxConstraints i o method should I use to create a transaction where the script pays a specified amount to the wallet? The mustSpendScriptOutput is the only one I can find that sends tokens from the script to the wallet, but it sends all the tokens at a utxo. I am looking to send a specified amount, and the change is returned to the script.

1 Answer 1

1

UTxOs can only be consumed completely which means you will have to create a new output at the script address that receives the change that shall stay with the script which can be done via mustPayToTheScript <datum> <value>.

5
  • When I added that to the constraints the transaction failed. Is there a way I can post my code in multiple lines in a comment? Commented May 2, 2022 at 19:31
  • meta.stackexchange.com/questions/207842/…
    – Will
    Commented May 3, 2022 at 6:59
  • I'm getting a ConstraintResolutionError TxOutRefNotFound during the ledgerTx line of code ```` utxos <- utxosAt $ scriptAddress payValidator let orefs = fst <$> Map.toList utxos change = _ciTxOutValue (snd (L.head (Map.toList utxos))) - (Ada.lovelaceValueOf bet) lookups = Constraints.typedValidatorLookups typedPayToScriptValidator tx = Constraints.mustSpendScriptOutput (last orefs) unitRedeemer <> Constraints.mustPayToTheScript bet change ledgerTx <- submitTxConstraintsWith lookups tx void $ awaitTxConfirmed $ getCardanoTxId ledgerTx ```` Commented May 6, 2022 at 17:14
  • Looks like your TxOutRef is invalid and does not exist. Check your orefs
    – Will
    Commented May 7, 2022 at 10:06
  • I needed to add Constraints.unspentOutputs utxos and Constraints.typedValidatorLookups myValidator to my lookups variable. Thanks for the help, I appreciate it. Commented May 7, 2022 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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