<p>I figured out a way to do this.</p>
<p>After looking at the implementation of <code>adjustUnbalancedTx</code> I came up with the following:</p>
<pre><code>-- adjust the contracts address to use the given staking credential in the given UnbalancedTx
adjustContractAddress ::
  ValidatorHash -&gt;
  StakingCredential -&gt;
  UnbalancedTx -&gt; UnbalancedTx
adjustContractAddress vH sK =
    over (tx . Tx.outputs . traverse) adjust
  where
    adjust :: TxOut -&gt; TxOut
    adjust txOut = txOut
      { txOutAddress=
        if (addressCredential . txOutAddress) txOut == ScriptCredential vH
        then Address (ScriptCredential vH) (Just sK)
        else txOutAddress txOut
      }
</code></pre>
<p>using the function above I can now adjust my transaction by changing how I construct it as follows:</p>
<pre><code>unbalanced &lt;- adjustContractAddress (Scripts.validatorHash factoryV) (StakingHash $ PubKeyCredential somePubKeyHash) &lt;$&gt; adjustUnbalancedTx &lt;$&gt; mkTxConstraints l tx
</code></pre>
<p>I hope this will be useful for some of you ;)</p>

CC BY-SA 4.0
2022-06-09T17:23:54.227Z
2022-06-09T19:46:19.193Z
2022-06-09T19:46:19.193Z
CantTouchDis
<p>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.</p>
<p>In <code>processConstraint</code> it also looks like it just uses <code>Address.scriptHashAddress</code> to generate the address for this constraint, which does not include any staking credentials. (on tag v2022-04-06)</p>
<p>I have seen a few commit messages in plutus-apps for a new constraint via
<code>mustPayToOtherScriptAddress</code> for a later release, but maybe it is already possible and I am just looking in the wrong place.</p>
<p>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)</p>
<pre><code>      l         = Constraints.typedValidatorLookups factoryV &lt;&gt;
                  Constraints.otherScript (Scripts.validatorScript factoryV) &lt;&gt;
                  Constraints.mintingPolicy policy &lt;&gt;
                  Constraints.unspentOutputs utxos

      tx        = Constraints.mustPayToTheScript datum (nftValue) &lt;&gt;
                  -- ?? Constraints.mustPayWithDatumToPubKeyAddress (scriptAddress factoryV) (Nothing) datum (nftValue) &lt;&gt;
                  Constraints.mustMintValue chunkValue &lt;&gt;
                  mconcat [Constraints.mustPayToOtherScript chunkVHash (Datum $ PlutusTx.toBuiltinData (ChunkDatum x)) singleCVal | x &lt;- [start..(start+n-1)]] &lt;&gt;
                  Constraints.mustSpendScriptOutput ((Map.keys utxos) !! 0) r
  unbalanced &lt;- adjustUnbalancedTx &lt;$&gt; mkTxConstraints l tx
</code></pre>
<p>In <code>factoryV</code> I just check for the existance</p>
<pre><code>    checkStakingCredential :: Bool
    checkStakingCredential = case addressStakingCredential . txOutAddress $ factoryOutput of
      Just _ -&gt; True
      _       -&gt; False
</code></pre>

<plutus><staking><address><offchain>
How do I add the staking part of an address in OffChain code
# How do I add the staking part of an address in OffChain code

(Asked by: username_0 on 2022-06-06)

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

```

**Tags:** plutus, staking, address, offchain


---

## 1 answers

### Answer by username_0 on 2022-06-09. Score: 0 | Accepted answer

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
  where
    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 ;)


---

