<p>You need to provide datum and redeemer that match the types your validator is expecting. Those funds cannot be unlocked now because the datum hash is for a value of a different type.</p>
<p>You're using datum value <code>123</code>, which is of type <code>Integer</code> but your validator expects <code>WithdrawalDatum</code>. You're passing <code>datum.json</code> for your redeemer, that must be using correct schema for type <code>StackingDetails</code>.</p>

CC BY-SA 4.0
2022-10-26T04:38:19.243Z
i used datum hash of 123 for locking funds should I try a different datum with unlocking
Hari Krishna
CC BY-SA 4.0
2022-11-11T21:59:47.803Z
is there no way to unlock a utxo that was instansiated with the wrong datum type?
Jonathan Fishbein
CC BY-SA 4.0
2022-11-12T09:11:55.833Z
There's currently no way to unlock utxo with a hash for the wrong datum type, same goes for the absence of hash.
james
CC BY-SA 4.0
2022-10-25T18:43:22.873Z
2022-10-25T18:43:22.873Z
james
<p>hi I am getting this error while trying to unlock tokens from my treasury contract it is already topp up with tokens when trying to unlock with the access token doesn't know the exact reason behind this pls help me out</p>
<p>StackingTreasury.hs</p>
<pre><code>{-# LANGUAGE DeriveAnyClass        #-}
{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DataKinds #-}
module Stake.StackingTreasury where

import              Ledger              hiding (singleton)
import qualified Ledger.Typed.Scripts as Scripts
import              Ledger.Value        as Value
import              Ledger.Ada
import qualified    PlutusTx
import              PlutusTx.Prelude    hiding (Semigroup (..), unless)

import              Stake.StackingTypes

{-# INLINEABLE mkValidator #-}
mkValidator :: TreasuryParam -&gt; WithdrawalDatum -&gt; StackingDetails -&gt; ScriptContext -&gt; Bool
mkValidator tp dat b ctx =    traceIfFalse &quot;Only Issuer can change Treasury&quot;              signedByIssuer ||
                              traceIfFalse &quot;Access token missing from input&quot;              inputHasAuthToken &amp;&amp;
                              traceIfFalse &quot;Access token missing from contract output&quot;    contractOutputHasAuthToken &amp;&amp;
                              traceIfFalse &quot;Output Value must match StackingDetails&quot;      checkValueToStackingContract &amp;&amp;
                              traceIfFalse &quot;Treasury must keep remaining tokens&quot;          treasuryGetsTokensBack &amp;&amp;
                              traceIfFalse &quot;redeemer is not datum&quot;                        checkReIsOutDat

  where
    info :: TxInfo
    info = scriptContextTxInfo ctx

    signedByIssuer :: Bool
    signedByIssuer = txSignedBy info $ tTreasuryIssuerPkh tp

    -- Create a list of all CurrencySymbol in tx input
    inVals :: [CurrencySymbol]
    inVals = symbols $ valueSpent info

    -- Check that list of CurrencySymbols includes Auth CurrencySymbol
    inputHasAuthToken :: Bool
    inputHasAuthToken = tAccessTokenPolicyId tp `elem` inVals

    -- The Value to be included in Stacking Contract UTXO
    toStackingContract :: Value
    toStackingContract = valueLockedBy info (bountyContractHash tp)

    -- Check that the Auth Token is sent to Bounty Contract UTXO
    contractOutputHasAuthToken :: Bool
    contractOutputHasAuthToken = tAccessTokenPolicyId tp `elem` symbols toStackingContract

    -- Check that the Value sent to Contract UTXO matches what is specified in the Redeemer
    -- Note: For now, we can just remember to match Treasury Redeemer to Bounty Datum
    -- when we build transactions
    checkValueToStackingContract :: Bool
    checkValueToStackingContract =  valueOf toStackingContract (tBountyTokenPolicyId tp) (tBountyTokenName tp) &gt;= tokenAmount b

    -- The UTXO input from Treasury
    ownInput :: TxOut
    ownInput = case findOwnInput ctx of
        Nothing -&gt; traceError &quot;treasury input missing&quot;
        Just i  -&gt; txInInfoResolved i

    -- The UTXO output back to Treasury
    ownOutput :: TxOut
    ownOutput = case getContinuingOutputs ctx of
        [o] -&gt; o -- There must be exactly ONE output UTXO
        _   -&gt; traceError &quot;expected exactly one treasury output&quot;

    -- Values of each
    treasuryInputValue :: Value
    treasuryInputValue = txOutValue ownInput

    treasuryOutputValue :: Value
    treasuryOutputValue = txOutValue ownOutput

    -- Compare Values from and to Treasury to make sure that Treasury gets the right value back.
   

    treasuryGetsTokensBack :: Bool
    treasuryGetsTokensBack = valueOf treasuryInputValue (tBountyTokenPolicyId tp) (tBountyTokenName tp) - valueOf treasuryOutputValue (tBountyTokenPolicyId tp) (tBountyTokenName tp) &lt;= valueOf toStackingContract (tBountyTokenPolicyId tp) (tBountyTokenName tp)

    getStackingDatum :: Maybe StackingDatum
    getStackingDatum = let os = [ o | o &lt;- txInfoOutputs info, txOutValue o == toStackingContract ] in
                  case os of
                    [o] -&gt; stackingDatum o (`findDatum` info)
                    _   -&gt; Nothing

    checkReIsOutDat :: Bool
    checkReIsOutDat = case getStackingDatum of
      Nothing -&gt; False
      Just ns -&gt; bedIssuerPkh   ns == issuerPkh b &amp;&amp;
                 bedContributorPkh ns == contributorPkh b &amp;&amp;
                 bedTokenAmount ns == tokenAmount b &amp;&amp;
                 bedExpirationTime ns == expirationTime b

-- typedValidator :: TreasuryParam -&gt; TypedValidator TreasuryTypes
-- typedValidator tp =
--   mkTypedValidator @TreasuryTypes
--     $$(PlutusTx.compile [||mkValidator||]) `PlutusTx.applyCode` PlutusTx.liftCode tp
--     $$(PlutusTx.compile [||wrap||])
--   where
--     wrap = wrapValidator @WithdrawalDatum @StackingDetails

-- validator :: TreasuryParam -&gt; Validator
-- validator = validatorScript . typedValidator

typedValidator :: TreasuryParam -&gt; Scripts.TypedValidator TreasuryTypes
typedValidator tp = Scripts.mkTypedValidator @TreasuryTypes
    ($$(PlutusTx.compile [|| mkValidator ||])
        `PlutusTx.applyCode` PlutusTx.liftCode tp)
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @WithdrawalDatum @StackingDetails

validator :: TreasuryParam -&gt; Validator
validator = Scripts.validatorScript . typedValidator
</code></pre>
<p>utxo at treasury address with datum hash of 123</p>
<pre><code>cardano-cli query utxo --address addr_test1wpcsng9wvph3yn6felqewnq9wwcvuwpwdumrax2dset0vecwhasah --testnet-magic 1

</code></pre>
<p>result</p>
<pre><code>                     TxHash                                 TxIx        Amount
--------------------------------------------------------------------------------------
0e4f0c3c372bb3c3d044f4ff72f200ad962f0f51190794dc43ca8f119b0faec2     1        2000000 lovelace + 99999999 d7d0b7e1195d3c51fefcf4e40146de39fe11162dcc56427f9e9db196.48616c + TxOutDatumHash ScriptDataInBabbageEra &quot;7cfec515f56d4413375aa9775f5de15ee60180861e9eaa954bcf9d015054857c&quot;

</code></pre>
<p>getting this error while trying to unlock funds from treasury conttract to stacking contract</p>
<pre><code>Command failed: transaction build  Error: The following scripts have execution failures:
the script for transaction input 0 (in the order of the TxIds) failed with: 
The Plutus script evaluation failed: An error has occurred:  User error:
The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Caused by: [ (builtin unConstrData) (con data #187b) ]
Script debugging logs: 

</code></pre>
<p>unlocking command</p>
<pre><code>CONTRACT_TXIN=&quot;0e4f0c3c372bb3c3d044f4ff72f200ad962f0f51190794dc43ca8f119b0faec2#1&quot;
TOKENS_BACK_TO_TREASURY=99999989
BOUNTY_ADDR=addr_test1wz0r05r5gmfykqzt6uavku4ncacxxd60sj2auadaefudntc3cyvrk
BOUNTY_ASSET=&quot;d7d0b7e1195d3c51fefcf4e40146de39fe11162dcc56427f9e9db196.48616c&quot;
CONTRIBUTOR_ASSET=&quot;e8abf29fc898134b44c1af4148ad15a6840c53d4d5e67483fba24ac3.6e696b6b69&quot;
TREASURY_ADDR=addr_test1wpcsng9wvph3yn6felqewnq9wwcvuwpwdumrax2dset0vecwhasah
TREASURY_PLUTUS_SCRIPT=&quot;output/treasury.plutus&quot;
BOUNTY_DATUM=&quot;datum.json&quot;
CONTRIBUTOR=addr_test1vr5uap3n4mu4kcetl0luz25re3x93yfjc3kdq0ud4c2xassvq0wsd
TXIN1=&quot;b61083800814c048d1c464d8306cc817a559565e6ca10d7f887eb23cfd3e5f1b#1&quot;
TXIN2=&quot;b72f8592ab7f740f46ba461d0302f82a69c0134c95df1622fb31ca7b5141f405#0&quot;
COLLATERAL=&quot;290fa91f253f968f20fa347c9a8371263c7e852e9bf611a7eb91c35818fd0edf#0&quot;
BOUNTY_AMOUNT=10

cardano-cli transaction build \
--babbage-era \
--tx-in $CONTRACT_TXIN \
--tx-in-script-file $TREASURY_PLUTUS_SCRIPT \
--tx-in-datum-value 123 \
--tx-in-redeemer-file $BOUNTY_DATUM \
--tx-in $TXIN1 \
--tx-in $TXIN2 \
--tx-in-collateral $COLLATERAL \
--tx-out $BOUNTY_ADDR+&quot;2000000 + 1 $CONTRIBUTOR_ASSET + $BOUNTY_AMOUNT $BOUNTY_ASSET&quot; \
--tx-out-datum-embed-file $BOUNTY_DATUM \
--tx-out $TREASURY_ADDR+&quot;2000000 + $TOKENS_BACK_TO_TREASURY $BOUNTY_ASSET&quot; \
--tx-out-datum-hash 7cfec515f56d4413375aa9775f5de15ee60180861e9eaa954bcf9d015054857c \
--change-address $CONTRIBUTOR \
--protocol-params-file protocol.json \
--testnet-magic 1 \
--out-file commitment-tx.draft

</code></pre>

<plutus><plutus-pioneer-program><cardano-cli><wallet>
User error: Caused by: [ (builtin unConstrData) (con data #187b) ] while trying to unlock tokens from contract
# User error: Caused by: [ (builtin unConstrData) (con data #187b) ] while trying to unlock tokens from contract

(Asked by: username_0 on 2022-10-25)

hi I am getting this error while trying to unlock tokens from my treasury contract it is already topp up with tokens when trying to unlock with the access token doesn't know the exact reason behind this pls help me out
StackingTreasury.hs
{-# LANGUAGE DeriveAnyClass        #-}
{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DataKinds #-}
module Stake.StackingTreasury where

import              Ledger              hiding (singleton)
import qualified Ledger.Typed.Scripts as Scripts
import              Ledger.Value        as Value
import              Ledger.Ada
import qualified    PlutusTx
import              PlutusTx.Prelude    hiding (Semigroup (..), unless)

import              Stake.StackingTypes

{-# INLINEABLE mkValidator #-}
mkValidator :: TreasuryParam -> WithdrawalDatum -> StackingDetails -> ScriptContext -> Bool
mkValidator tp dat b ctx =    traceIfFalse "Only Issuer can change Treasury"              signedByIssuer ||
                              traceIfFalse "Access token missing from input"              inputHasAuthToken &&
                              traceIfFalse "Access token missing from contract output"    contractOutputHasAuthToken &&
                              traceIfFalse "Output Value must match StackingDetails"      checkValueToStackingContract &&
                              traceIfFalse "Treasury must keep remaining tokens"          treasuryGetsTokensBack &&
                              traceIfFalse "redeemer is not datum"                        checkReIsOutDat

  where
    info :: TxInfo
    info = scriptContextTxInfo ctx

    signedByIssuer :: Bool
    signedByIssuer = txSignedBy info $ tTreasuryIssuerPkh tp

    -- Create a list of all CurrencySymbol in tx input
    inVals :: [CurrencySymbol]
    inVals = symbols $ valueSpent info

    -- Check that list of CurrencySymbols includes Auth CurrencySymbol
    inputHasAuthToken :: Bool
    inputHasAuthToken = tAccessTokenPolicyId tp `elem` inVals

    -- The Value to be included in Stacking Contract UTXO
    toStackingContract :: Value
    toStackingContract = valueLockedBy info (bountyContractHash tp)

    -- Check that the Auth Token is sent to Bounty Contract UTXO
    contractOutputHasAuthToken :: Bool
    contractOutputHasAuthToken = tAccessTokenPolicyId tp `elem` symbols toStackingContract

    -- Check that the Value sent to Contract UTXO matches what is specified in the Redeemer
    -- Note: For now, we can just remember to match Treasury Redeemer to Bounty Datum
    -- when we build transactions
    checkValueToStackingContract :: Bool
    checkValueToStackingContract =  valueOf toStackingContract (tBountyTokenPolicyId tp) (tBountyTokenName tp) >= tokenAmount b

    -- The UTXO input from Treasury
    ownInput :: TxOut
    ownInput = case findOwnInput ctx of
        Nothing -> traceError "treasury input missing"
        Just i  -> txInInfoResolved i

    -- The UTXO output back to Treasury
    ownOutput :: TxOut
    ownOutput = case getContinuingOutputs ctx of
        [o] -> o -- There must be exactly ONE output UTXO
        _   -> traceError "expected exactly one treasury output"

    -- Values of each
    treasuryInputValue :: Value
    treasuryInputValue = txOutValue ownInput

    treasuryOutputValue :: Value
    treasuryOutputValue = txOutValue ownOutput

    -- Compare Values from and to Treasury to make sure that Treasury gets the right value back.
   

    treasuryGetsTokensBack :: Bool
    treasuryGetsTokensBack = valueOf treasuryInputValue (tBountyTokenPolicyId tp) (tBountyTokenName tp) - valueOf treasuryOutputValue (tBountyTokenPolicyId tp) (tBountyTokenName tp) <= valueOf toStackingContract (tBountyTokenPolicyId tp) (tBountyTokenName tp)

    getStackingDatum :: Maybe StackingDatum
    getStackingDatum = let os = [ o | o <- txInfoOutputs info, txOutValue o == toStackingContract ] in
                  case os of
                    [o] -> stackingDatum o (`findDatum` info)
                    _   -> Nothing

    checkReIsOutDat :: Bool
    checkReIsOutDat = case getStackingDatum of
      Nothing -> False
      Just ns -> bedIssuerPkh   ns == issuerPkh b &&
                 bedContributorPkh ns == contributorPkh b &&
                 bedTokenAmount ns == tokenAmount b &&
                 bedExpirationTime ns == expirationTime b

-- typedValidator :: TreasuryParam -> TypedValidator TreasuryTypes
-- typedValidator tp =
--   mkTypedValidator @TreasuryTypes
--     $$(PlutusTx.compile [||mkValidator||]) `PlutusTx.applyCode` PlutusTx.liftCode tp
--     $$(PlutusTx.compile [||wrap||])
--   where
--     wrap = wrapValidator @WithdrawalDatum @StackingDetails

-- validator :: TreasuryParam -> Validator
-- validator = validatorScript . typedValidator

typedValidator :: TreasuryParam -> Scripts.TypedValidator TreasuryTypes
typedValidator tp = Scripts.mkTypedValidator @TreasuryTypes
    ($$(PlutusTx.compile [|| mkValidator ||])
        `PlutusTx.applyCode` PlutusTx.liftCode tp)
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @WithdrawalDatum @StackingDetails

validator :: TreasuryParam -> Validator
validator = Scripts.validatorScript . typedValidator

utxo at treasury address with datum hash of 123
cardano-cli query utxo --address addr_test1wpcsng9wvph3yn6felqewnq9wwcvuwpwdumrax2dset0vecwhasah --testnet-magic 1


result
                     TxHash                                 TxIx        Amount
--------------------------------------------------------------------------------------
0e4f0c3c372bb3c3d044f4ff72f200ad962f0f51190794dc43ca8f119b0faec2     1        2000000 lovelace + 99999999 d7d0b7e1195d3c51fefcf4e40146de39fe11162dcc56427f9e9db196.48616c + TxOutDatumHash ScriptDataInBabbageEra "7cfec515f56d4413375aa9775f5de15ee60180861e9eaa954bcf9d015054857c"


getting this error while trying to unlock funds from treasury conttract to stacking contract
Command failed: transaction build  Error: The following scripts have execution failures:
the script for transaction input 0 (in the order of the TxIds) failed with: 
The Plutus script evaluation failed: An error has occurred:  User error:
The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Caused by: [ (builtin unConstrData) (con data #187b) ]
Script debugging logs: 


unlocking command
CONTRACT_TXIN="0e4f0c3c372bb3c3d044f4ff72f200ad962f0f51190794dc43ca8f119b0faec2#1"
TOKENS_BACK_TO_TREASURY=99999989
BOUNTY_ADDR=addr_test1wz0r05r5gmfykqzt6uavku4ncacxxd60sj2auadaefudntc3cyvrk
BOUNTY_ASSET="d7d0b7e1195d3c51fefcf4e40146de39fe11162dcc56427f9e9db196.48616c"
CONTRIBUTOR_ASSET="e8abf29fc898134b44c1af4148ad15a6840c53d4d5e67483fba24ac3.6e696b6b69"
TREASURY_ADDR=addr_test1wpcsng9wvph3yn6felqewnq9wwcvuwpwdumrax2dset0vecwhasah
TREASURY_PLUTUS_SCRIPT="output/treasury.plutus"
BOUNTY_DATUM="datum.json"
CONTRIBUTOR=addr_test1vr5uap3n4mu4kcetl0luz25re3x93yfjc3kdq0ud4c2xassvq0wsd
TXIN1="b61083800814c048d1c464d8306cc817a559565e6ca10d7f887eb23cfd3e5f1b#1"
TXIN2="b72f8592ab7f740f46ba461d0302f82a69c0134c95df1622fb31ca7b5141f405#0"
COLLATERAL="290fa91f253f968f20fa347c9a8371263c7e852e9bf611a7eb91c35818fd0edf#0"
BOUNTY_AMOUNT=10

cardano-cli transaction build \
--babbage-era \
--tx-in $CONTRACT_TXIN \
--tx-in-script-file $TREASURY_PLUTUS_SCRIPT \
--tx-in-datum-value 123 \
--tx-in-redeemer-file $BOUNTY_DATUM \
--tx-in $TXIN1 \
--tx-in $TXIN2 \
--tx-in-collateral $COLLATERAL \
--tx-out $BOUNTY_ADDR+"2000000 + 1 $CONTRIBUTOR_ASSET + $BOUNTY_AMOUNT $BOUNTY_ASSET" \
--tx-out-datum-embed-file $BOUNTY_DATUM \
--tx-out $TREASURY_ADDR+"2000000 + $TOKENS_BACK_TO_TREASURY $BOUNTY_ASSET" \
--tx-out-datum-hash 7cfec515f56d4413375aa9775f5de15ee60180861e9eaa954bcf9d015054857c \
--change-address $CONTRIBUTOR \
--protocol-params-file protocol.json \
--testnet-magic 1 \
--out-file commitment-tx.draft

**Tags:** plutus, plutus-pioneer-program, cardano-cli, wallet

---

## 1 answers

### Answer by username_1 on 2022-10-25. Score: 0

You need to provide datum and redeemer that match the types your validator is expecting. Those funds cannot be unlocked now because the datum hash is for a value of a different type.

You're using datum value `123`, which is of type `Integer` but your validator expects `WithdrawalDatum`. You're passing `datum.json` for your redeemer, that must be using correct schema for type `StackingDetails`.

> **Comments:**
> **username_0**: i used datum hash of 123 for locking funds should I try a different datum with unlocking  
> **username_2**: is there no way to unlock a utxo that was instansiated with the wrong datum type?  
> **username_1**: There's currently no way to unlock utxo with a hash for the wrong datum type, same goes for the absence of hash.  

---

