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.
Add a comment
|
1 Answer
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>
.
-
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
-
-
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– WillCommented May 7, 2022 at 10:06
-
I needed to add
Constraints.unspentOutputs utxos
andConstraints.typedValidatorLookups myValidator
to my lookups variable. Thanks for the help, I appreciate it. Commented May 7, 2022 at 15:56