<p>If it does not match, it means your off-chain code is using a different address than what you expect on-chain.</p>
<p>An address is comprised of two parts, the payment and the staking part. You seem to be calculating the address of the payment part being the <code>ValidatorHash</code> of your game script and the staking part to be <code>Nothing</code>.</p>
<p>Maybe your off-chain code is creating the address slightly differently?</p>
<p>There is a function <code>scriptOutputsAt</code> in V1/V2 <code>plutus</code> (<a href="https://github.com/input-output-hk/plutus/blob/5cc518f1202930ad52b8ba838af32af084c0e754/plutus-ledger-api/src/Plutus/V1/Ledger/Contexts.hs#L251-L258" rel="nofollow noreferrer">https://github.com/input-output-hk/plutus/blob/5cc518f1202930ad52b8ba838af32af084c0e754/plutus-ledger-api/src/Plutus/V1/Ledger/Contexts.hs#L251-L258</a>) which you could also use. It explicitly only looks at the payment part and ignores the staking part.</p>

CC BY-SA 4.0
2022-08-02T07:36:17.600Z
good point. managed to make it working using scriptOutputsAt function. Nothing else worked, even extracting ValidatorHash from each output and comparing it to ValidatorHash parameter from contract (gameValidatorHash = validatorHash $ Validator $ mkGameScript). Thanks!
serx
CC BY-SA 4.0
2022-08-03T06:32:46.697Z
If it worked, you can accept the answer (I guess there is a button for that).
Sebastian Nagel
CC BY-SA 4.0
2022-08-02T06:36:49.497Z
2022-08-02T06:36:49.497Z
Sebastian Nagel
<p>In Haskell <code>&quot;f1befc070bbc16ea613cfc0f0a5c0f856063f1ecbe640556136568f8&quot;</code> is a string (because its enclosed in double quotes), not a <code>PubKeyHash</code>.</p>
<p>I think all you need to add the constructor like this (it compiles, but not 100% sure it does the right thing):</p>
<pre><code>hardcodedPubKeyHash :: PubKeyHash
hardcodedPubKeyHash = PubKeyHash &quot;f1befc070bbc16ea613cfc0f0a5c0f856063f1ecbe640556136568f8&quot;
</code></pre>

CC BY-SA 4.0
2022-08-02T03:57:04.270Z
2022-08-02T04:24:24.290Z
2022-08-02T04:24:24.290Z
Erik de Castro Lopo
<p>1st way I tried to do it was to get validator address using this function:</p>
<pre><code>{-# INLINABLE scriptAddress #-}
-- | The address that should be used by a transaction output locked by the given validator script.
scriptAddress :: Validator -&gt; Address
scriptAddress validator = Address (ScriptCredential (validatorHash validator)) Nothing
</code></pre>
<p>For example:</p>
<pre><code>-- | The validator script of the game.
gameValidator :: ValidatorScript
gameValidator =
    ValidatorScript ($$(Ledger.compileScript [|| validateGuess ||]))

gameAddress :: Address
gameAddress = Ledger.scriptAddress gameValidator
</code></pre>
<p>Then use this gameAddress variable as parameter in minting policy, which compiles and works fine.</p>
<p>But real contract address never matches the address parameter, script fails on cardano-cli submit:</p>
<pre><code>txOutputs :: [TxOut]
txOutputs = txInfoOutputs info

ownOutput :: TxOut
ownOutput = case [o | o &lt;- txOutputs, gameAddress == txOutAddress o] of
[o] -&gt; o
_ -&gt; error ()
</code></pre>
<p>We always get an error here (I am sure that there is an output to the same smart contract).</p>
<p>This gameAddress has no awareness of mainnet/testnet, maybe that's the reason why it does not match? Maybe there is a way to fix this?</p>
<p>Also, is there a way to simply hardcode smart contract address into Plutus code? Like we can do with PubKeyHash?:</p>
<pre><code>hardcodedPubKeyHash :: PubKeyHash
hardcodedPubKeyHash = &quot;f1befc070bbc16ea613cfc0f0a5c0f856063f1ecbe640556136568f8&quot;
</code></pre>

<plutus>
How to hardcode smart contract address in minting policy in Plutus?
# How to hardcode smart contract address in minting policy in Plutus?

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

1st way I tried to do it was to get validator address using this function:
{-# INLINABLE scriptAddress #-}
-- | The address that should be used by a transaction output locked by the given validator script.
scriptAddress :: Validator -> Address
scriptAddress validator = Address (ScriptCredential (validatorHash validator)) Nothing

For example:
-- | The validator script of the game.
gameValidator :: ValidatorScript
gameValidator =
    ValidatorScript ($$(Ledger.compileScript [|| validateGuess ||]))

gameAddress :: Address
gameAddress = Ledger.scriptAddress gameValidator

Then use this gameAddress variable as parameter in minting policy, which compiles and works fine.
But real contract address never matches the address parameter, script fails on cardano-cli submit:
txOutputs :: [TxOut]
txOutputs = txInfoOutputs info

ownOutput :: TxOut
ownOutput = case [o | o <- txOutputs, gameAddress == txOutAddress o] of
[o] -> o
_ -> error ()

We always get an error here (I am sure that there is an output to the same smart contract).
This gameAddress has no awareness of mainnet/testnet, maybe that's the reason why it does not match? Maybe there is a way to fix this?
Also, is there a way to simply hardcode smart contract address into Plutus code? Like we can do with PubKeyHash?:
hardcodedPubKeyHash :: PubKeyHash
hardcodedPubKeyHash = "f1befc070bbc16ea613cfc0f0a5c0f856063f1ecbe640556136568f8"

**Tags:** plutus

---

## 2 answers

### Answer by username_1 on 2022-08-02. Score: 3 | Accepted answer

If it does not match, it means your off-chain code is using a different address than what you expect on-chain.

An address is comprised of two parts, the payment and the staking part. You seem to be calculating the address of the payment part being the `ValidatorHash` of your game script and the staking part to be `Nothing`.

Maybe your off-chain code is creating the address slightly differently?

There is a function `scriptOutputsAt` in V1/V2 `plutus` (<https://github.com/input-output-hk/plutus/blob/5cc518f1202930ad52b8ba838af32af084c0e754/plutus-ledger-api/src/Plutus/V1/Ledger/Contexts.hs#L251-L258>) which you could also use. It explicitly only looks at the payment part and ignores the staking part.

> **Comments:**
> **username_0**: good point. managed to make it working using scriptOutputsAt function. Nothing else worked, even extracting ValidatorHash from each output and comparing it to ValidatorHash parameter from contract (gameValidatorHash = validatorHash $ Validator $ mkGameScript). Thanks!  
> **username_1**: If it worked, you can accept the answer (I guess there is a button for that).  

---

### Answer by username_2 on 2022-08-02. Score: 0

In Haskell "f1befc070bbc16ea613cfc0f0a5c0f856063f1ecbe640556136568f8" is a string (because its enclosed in double quotes), not a PubKeyHash.
I think all you need to add the constructor like this (it compiles, but not 100% sure it does the right thing):
hardcodedPubKeyHash :: PubKeyHash
hardcodedPubKeyHash = PubKeyHash "f1befc070bbc16ea613cfc0f0a5c0f856063f1ecbe640556136568f8"


---

