I am currently on tag: v2022-04-06 of plutus-apps and was wondering how to create Lookup and Constraints for a typedValidator that checks whether the input was returned to the script with the correct staking credential. I was hoping to find someone here that could tell me if that is currently possible or not.

In processConstraint it also looks like it just uses Address.scriptHashAddress to generate the address for this constraint, which does not include any staking credentials. (on tag v2022-04-06)

I have seen a few commit messages in plutus-apps for a new constraint via mustPayToOtherScriptAddress for a later release, but maybe it is already possible and I am just looking in the wrong place.

This is what I currently have written: (it pays to the script but validation fails because it doesn't pay to an address with staking credentials)

      l         = Constraints.typedValidatorLookups factoryV <>
                  Constraints.otherScript (Scripts.validatorScript factoryV) <>
                  Constraints.mintingPolicy policy <>
                  Constraints.unspentOutputs utxos

      tx        = Constraints.mustPayToTheScript datum (nftValue) <>
                  -- ?? Constraints.mustPayWithDatumToPubKeyAddress (scriptAddress factoryV) (Nothing) datum (nftValue) <>
                  Constraints.mustMintValue chunkValue <>
                  mconcat [Constraints.mustPayToOtherScript chunkVHash (Datum $ PlutusTx.toBuiltinData (ChunkDatum x)) singleCVal | x <- [start..(start+n-1)]] <>
                  Constraints.mustSpendScriptOutput ((Map.keys utxos) !! 0) r
  unbalanced <- adjustUnbalancedTx <$> mkTxConstraints l tx

In factoryV I just check for the existance

    checkStakingCredential :: Bool
    checkStakingCredential = case addressStakingCredential . txOutAddress $ factoryOutput of
      Just _ -> True
      _       -> False

1 Answer 1


I figured out a way to do this.

After looking at the implementation of adjustUnbalancedTx I came up with the following:

-- adjust the contracts address to use the given staking credential in the given UnbalancedTx
adjustContractAddress ::
  ValidatorHash ->
  StakingCredential ->
  UnbalancedTx -> UnbalancedTx
adjustContractAddress vH sK =
    over (tx . Tx.outputs . traverse) adjust
    adjust :: TxOut -> TxOut
    adjust txOut = txOut
      { txOutAddress=
        if (addressCredential . txOutAddress) txOut == ScriptCredential vH
        then Address (ScriptCredential vH) (Just sK)
        else txOutAddress txOut

using the function above I can now adjust my transaction by changing how I construct it as follows:

unbalanced <- adjustContractAddress (Scripts.validatorHash factoryV) (StakingHash $ PubKeyCredential somePubKeyHash) <$> adjustUnbalancedTx <$> mkTxConstraints l tx

I hope this will be useful for some of you ;)

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.