<blockquote>
<p>A simple validator that insures that for every transaction, the amount of ADA being locked into the contract is greater than the amount being unlocked (spent) must be possible given how simple the premise is.</p>
</blockquote>
<p>Since we use a UTxO based ledger, indeed we have to track the inputs and outputs from and to the script address. So, we define the amount that is locked/unlocked here as the difference between the value send to the script and the value spent from the script. You are correct that the former (the value send to the address) can be extracted via the function <code>valueLockedBy</code> defined by,</p>
<pre><code>{-# INLINABLE valueLockedBy #-}
-- | Get the total value locked by the given validator in this transaction.
valueLockedBy :: TxInfo -&gt; ValidatorHash -&gt; Value
valueLockedBy ptx h =
    let outputs = map snd (scriptOutputsAt h ptx)
    in mconcat outputs
</code></pre>
<p>We see that this adds all the value found at the created outputs of the transaction that contain the <code>ValidatorHash</code>. Here, the <code>scriptOutputsAt</code> function is defined as,</p>
<pre><code>{-# INLINABLE scriptOutputsAt #-}
scriptOutputsAt h p =
    let flt TxOut{txOutDatumHash=Just ds, txOutAddress=Address (ScriptCredential s) _, txOutValue} | s == h = Just (ds, txOutValue)
        flt _ = Nothing
    in mapMaybe flt (txInfoOutputs p)
</code></pre>
<p>Your assessment that currently there is no dedicated function for grabbing the total value of the inputs at the script address is correct. Your list comprehension seems correct. But it is not the only way, we can rewrite the above function to work on the inputs as well,</p>
<pre><code>{-# INLINABLE scriptInputsAt #-}
scriptInputsAt :: ValidatorHash -&gt; TxInfo -&gt; [(OutputDatum, Value)]
scriptInputsAt h p = 
    let flt TxInInfo{txInInfoResolved=TxOut{txOutDatum=d, txOutAddress=Address (ScriptCredential s) _, txOutValue}} | s == h = Just (d, txOutValue) 
        flt _ = Nothing
    in mapMaybe flt (txInfoInputs p)

{-# INLINABLE valueUnlockedBy #-}
valueUnlockedBy :: TxInfo -&gt; ValidatorHash -&gt; Value
valueUnlockedBy ptx h =
    let inputs = map snd (scriptInputsAt h ptx)
    in mconcat inputs
</code></pre>
<p>Disclaimer: the above functions are for plutusV2 (contexts are handled differently between the two versions).</p>

CC BY-SA 4.0
2022-08-12T10:22:40.073Z
I made a PR for these functions to be included in the module https://github.com/input-output-hk/plutus/pull/4810
Fermat
CC BY-SA 4.0
2022-08-12T08:17:44.013Z
2022-08-12T09:44:33.840Z
2022-08-12T09:44:33.840Z
Fermat
<p>A simple validator that insures that for every transaction, the amount of ADA being locked into the contract is greater than the amount being unlocked (spent) must be possible given how simple the premise is. However, it seems that there is a gap in the provided context functions.</p>
<p>We can easily get the amount of ADA that is locked in the transaction with:
adaLockedBy ctx ownHash</p>
<p>But there is no equivalent function to get the amount of ADA unlocked in the transaction.</p>
<p>Am I correct in thinking that currently the only way to get the total amount of ADA unlocked in the transaction is to filter script inputs for ? ex:</p>
<pre><code>[txOutValue (txInInfoResolved i) | i &lt;- txInfoInputs (scriptContextInfoTx ctx), ((==) (txOutAddress (txInInfoResolved i)) (ScrptAddress))]
</code></pre>
<p>Is there no <code>valueUnlockedBy ctx ownHash</code>?</p>

<plutus>
Validator check for amount locked vs amount unlocked
# Validator check for amount locked vs amount unlocked

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

A simple validator that insures that for every transaction, the amount of ADA being locked into the contract is greater than the amount being unlocked (spent) must be possible given how simple the premise is. However, it seems that there is a gap in the provided context functions.
We can easily get the amount of ADA that is locked in the transaction with:
adaLockedBy ctx ownHash
But there is no equivalent function to get the amount of ADA unlocked in the transaction.
Am I correct in thinking that currently the only way to get the total amount of ADA unlocked in the transaction is to filter script inputs for ? ex:
[txOutValue (txInInfoResolved i) | i <- txInfoInputs (scriptContextInfoTx ctx), ((==) (txOutAddress (txInInfoResolved i)) (ScrptAddress))]

Is there no valueUnlockedBy ctx ownHash?

**Tags:** plutus

---

## 1 answers

### Answer by username_1 on 2022-08-12. Score: 1 | Accepted answer

A simple validator that insures that for every transaction, the amount of ADA being locked into the contract is greater than the amount being unlocked (spent) must be possible given how simple the premise is.

Since we use a UTxO based ledger, indeed we have to track the inputs and outputs from and to the script address. So, we define the amount that is locked/unlocked here as the difference between the value send to the script and the value spent from the script. You are correct that the former (the value send to the address) can be extracted via the function valueLockedBy defined by,
{-# INLINABLE valueLockedBy #-}
-- | Get the total value locked by the given validator in this transaction.
valueLockedBy :: TxInfo -> ValidatorHash -> Value
valueLockedBy ptx h =
    let outputs = map snd (scriptOutputsAt h ptx)
    in mconcat outputs

We see that this adds all the value found at the created outputs of the transaction that contain the ValidatorHash. Here, the scriptOutputsAt function is defined as,
{-# INLINABLE scriptOutputsAt #-}
scriptOutputsAt h p =
    let flt TxOut{txOutDatumHash=Just ds, txOutAddress=Address (ScriptCredential s) _, txOutValue} | s == h = Just (ds, txOutValue)
        flt _ = Nothing
    in mapMaybe flt (txInfoOutputs p)

Your assessment that currently there is no dedicated function for grabbing the total value of the inputs at the script address is correct. Your list comprehension seems correct. But it is not the only way, we can rewrite the above function to work on the inputs as well,
{-# INLINABLE scriptInputsAt #-}
scriptInputsAt :: ValidatorHash -> TxInfo -> [(OutputDatum, Value)]
scriptInputsAt h p = 
    let flt TxInInfo{txInInfoResolved=TxOut{txOutDatum=d, txOutAddress=Address (ScriptCredential s) _, txOutValue}} | s == h = Just (d, txOutValue) 
        flt _ = Nothing
    in mapMaybe flt (txInfoInputs p)

{-# INLINABLE valueUnlockedBy #-}
valueUnlockedBy :: TxInfo -> ValidatorHash -> Value
valueUnlockedBy ptx h =
    let inputs = map snd (scriptInputsAt h ptx)
    in mconcat inputs

Disclaimer: the above functions are for plutusV2 (contexts are handled differently between the two versions).

> **Comments:**
> **username_1**: I made a PR for these functions to be included in the module https://github.com/input-output-hk/plutus/pull/4810  

---

