<p>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 <code>mustPayToTheScript &lt;datum&gt; &lt;value&gt;</code>.</p>

CC BY-SA 4.0
2022-05-02T19:31:48.920Z
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?
Kyle Murphy
CC BY-SA 4.0
2022-05-03T06:59:55.127Z
https://meta.stackexchange.com/questions/207842/how-to-easily-indent-multi-line-code
Will
CC BY-SA 4.0
2022-05-06T17:14:20.380Z
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
````
Kyle Murphy
CC BY-SA 4.0
2022-05-07T10:06:39.220Z
Looks like your TxOutRef is invalid and does not exist. Check your orefs
Will
CC BY-SA 4.0
2022-05-07T15:56:12.213Z
I needed to add `Constraints.unspentOutputs utxos` and `Constraints.typedValidatorLookups myValidator` to my lookups variable. Thanks for the help, I appreciate it.
Kyle Murphy
CC BY-SA 4.0
2022-05-02T19:03:50.443Z
2022-05-02T19:03:50.443Z
Will
<p>Which TxConstraints i o method should I use to create a transaction where the script pays a specified amount to the wallet? The <code>mustSpendScriptOutput</code> 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.</p>

<plutus>
Send Given Amount of Tokens From Script to Wallet
# Send Given Amount of Tokens From Script to Wallet

(Asked by: username_0 on 2022-05-02)

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.

**Tags:** plutus

---

## 1 answers

### Answer by username_1 on 2022-05-02. Score: 1 | Accepted 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` .

> **Comments:**
> **username_0**: 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?  
> **username_1**: https://meta.stackexchange.com/questions/207842/how-to-easily-indent-multi-line-code  
> **username_0**: 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 ````  
> **username_1**: Looks like your TxOutRef is invalid and does not exist. Check your orefs  
> **username_0**: I needed to add `Constraints.unspentOutputs utxos` and `Constraints.typedValidatorLookups myValidator` to my lookups variable. Thanks for the help, I appreciate it.  

---

