<p><strong>In short</strong>:<br>
In my understanding, the actual smart contracts are always shipped with the transaction, so you need all code that you want to execute. Merely the script hash ensures that actually the right script code is shipped with the transaction. And this hash will also be the location (the public key address) of potential funds or information.</p>
<p><strong>In long</strong>:<br>
A good example is given in the <a href="https://youtu.be/Tr2VBm8vOhw" rel="noreferrer">oracle lecture of the Plutus Pioneer Program</a>:<br>
from <code>Swap.hs</code>:</p>
<pre><code>swapInst :: Oracle -&gt; Scripts.ScriptInstance Swapping
swapInst oracle = Scripts.validator @Swapping
    ($$(PlutusTx.compile [|| mkSwapValidator ||])
        `PlutusTx.applyCode` PlutusTx.liftCode oracle
        `PlutusTx.applyCode` PlutusTx.liftCode (oracleAddress oracle))
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @PubKeyHash @()
</code></pre>
<p>Here the oracle contract is passed to swap contract as a parameter. If somebody wants to use it, he has to provide an instance:</p>
<pre><code>    h3 &lt;- activateContractWallet (Wallet 3) $ swap oracle
</code></pre>
<p>And as far as I understand the code, this is only possible if you have access to the source code. But please correct me if I'm wrong.</p>
<p>The oracle API is used in the sense that there are multiple possibilities for valid redeemers, which define the endpoints: <code>data OracleRedeemer = Update | Use</code></p>
<p>Later somebody can</p>
<ol>
<li>search for the oracle (this is in a sense library code coming from the oracle provider):</li>
</ol>
<pre><code>findOracle :: forall w s. HasBlockchainActions s =&gt; Oracle -&gt; Contract w s Text (Maybe (TxOutRef, TxOutTx, Integer))
findOracle oracle = do
    utxos &lt;- Map.filter f &lt;$&gt; utxoAt (oracleAddress oracle)
    return $ case Map.toList utxos of
        [(oref, o)] -&gt; do
            x &lt;- oracleValue (txOutTxOut o) $ \dh -&gt; Map.lookup dh $ txData $ txOutTxTx o
            return (oref, o, x)
        _           -&gt; Nothing
  where
    f :: TxOutTx -&gt; Bool
    f o = assetClassValueOf (txOutValue $ txOutTxOut o) (oracleAsset oracle) == 1
</code></pre>
<ol start="2">
<li>use it by providing the right redeemer for the wanted endpoint (in this case &quot;Use&quot;, see &quot;Redeemer&quot;):</li>
</ol>
<pre><code>useSwap :: forall w s. HasBlockchainActions s =&gt; Oracle -&gt; Contract w s Text ()
useSwap oracle = do
    funds &lt;- ownFunds
    let amt = assetClassValueOf funds $ oAsset oracle
    logInfo @String $ &quot;available assets: &quot; ++ show amt

    m &lt;- findOracle oracle
    case m of
        Nothing           -&gt; logInfo @String &quot;oracle not found&quot;
        Just (oref, o, x) -&gt; do
            logInfo @String $ &quot;found oracle, exchange rate &quot; ++ show x
            pkh   &lt;- pubKeyHash &lt;$&gt; Contract.ownPubKey
            swaps &lt;- findSwaps oracle (/= pkh)
            case find (f amt x) swaps of
                Nothing                -&gt; logInfo @String &quot;no suitable swap found&quot;
                Just (oref', o', pkh') -&gt; do
                    let v       = txOutValue (txOutTxOut o) &lt;&gt; lovelaceValueOf (oFee oracle)
                        p       = assetClassValue (oAsset oracle) $ price (lovelaces $ txOutValue $ txOutTxOut o') x
                        lookups = Constraints.otherScript (swapValidator oracle)                     &lt;&gt;
                                  Constraints.otherScript (oracleValidator oracle)                   &lt;&gt;
                                  Constraints.unspentOutputs (Map.fromList [(oref, o), (oref', o')])
                        tx      = Constraints.mustSpendScriptOutput oref  (Redeemer $ PlutusTx.toData Use) &lt;&gt;
                                  Constraints.mustSpendScriptOutput oref' (Redeemer $ PlutusTx.toData ())  &lt;&gt;
                                  Constraints.mustPayToOtherScript
                                    (validatorHash $ oracleValidator oracle)
                                    (Datum $ PlutusTx.toData x)
                                    v                                                                      &lt;&gt;
                                  Constraints.mustPayToPubKey pkh' p
                    ledgerTx &lt;- submitTxConstraintsWith @Swapping lookups tx
                    awaitTxConfirmed $ txId ledgerTx
                    logInfo @String $ &quot;made swap with price &quot; ++ show (Value.flattenValue p)
  where
    getPrice :: Integer -&gt; TxOutTx -&gt; Integer
    getPrice x o = price (lovelaces $ txOutValue $ txOutTxOut o) x

    f :: Integer -&gt; Integer -&gt; (TxOutRef, TxOutTx, PubKeyHash) -&gt; Bool
    f amt x (_, o, _) = getPrice x o &lt;= amt
</code></pre>
<p><strong>So I suspect that you always need the third-party code.</strong> But that would actually be a good thing since you want to make sure anyway that the third party can't fool you.<br>
Additionally, I don't see a way how it could be possible to execute another contract without the code since it isn't stored on the blockchain itself. The blockchain just stores the hash of the code (the <strong>public key address</strong>) and all funds (<strong>eUTxO</strong>s) located at that address.</p>

CC BY-SA 4.0
2021-05-26T17:16:06.450Z
2021-05-26T17:16:06.450Z
Johannes Sirsch
<p>Johannes is correct. One will need the source code of the contracts in order to interface with it.</p>
<p>Source: <a href="https://github.com/Emurgo/Emurgo-Research/blob/master/smart-contracts/Unlocking%20The%20Potential%20Of%20The%20UTXO%20Model.md" rel="nofollow noreferrer">https://github.com/Emurgo/Emurgo-Research/blob/master/smart-contracts/Unlocking%20The%20Potential%20Of%20The%20UTXO%20Model.md</a></p>

CC BY-SA 4.0
2021-06-10T08:51:32.983Z
2021-06-10T08:51:32.983Z
Shannon Goh
<p>In solidity and EVM, it is possible to reference external contracts by providing the address and interface to the contract:</p>
<pre><code>  address ckAddress =  0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
  KittyInterface kittyContract = KittyInterface(ckAddress);
</code></pre>
<p>In Plutus, however, the onchain code are referenced with</p>
<pre><code>    ledgerTx &lt;- submitTxConstraints inst tx
</code></pre>
<p>where <code>inst</code> is the <code>ScriptInstance</code> of the validator code. Is there some example that shows how to reference a 3rd party smart contract with exposed API? Or is the original source code always necessary to reference the correct instance?</p>

CC BY-SA 4.0
2021-05-26T16:19:35.413Z
Please clarify "Or is the original source code always necessary to reference the correct instance?".
Johannes Sirsch
<plutus><smart-contract><plutus-pioneer-program>
Referencing onchain contract as 3rd party
# Referencing onchain contract as 3rd party

(Asked by: username_0 on 2021-05-26)

In solidity and EVM, it is possible to reference external contracts by providing the address and interface to the contract:
  address ckAddress =  0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
  KittyInterface kittyContract = KittyInterface(ckAddress);

In Plutus, however, the onchain code are referenced with
    ledgerTx <- submitTxConstraints inst tx

where inst is the ScriptInstance of the validator code. Is there some example that shows how to reference a 3rd party smart contract with exposed API? Or is the original source code always necessary to reference the correct instance?

**Tags:** plutus, smart-contract, plutus-pioneer-program
> **Comments:**
> **username_1**: Please clarify "Or is the original source code always necessary to reference the correct instance?".  

---

## 2 answers

### Answer by username_1 on 2021-05-26. Score: 6

In short:
In my understanding, the actual smart contracts are always shipped with the transaction, so you need all code that you want to execute. Merely the script hash ensures that actually the right script code is shipped with the transaction. And this hash will also be the location (the public key address) of potential funds or information.
In long:
A good example is given in the oracle lecture of the Plutus Pioneer Program:
from Swap.hs:
swapInst :: Oracle -> Scripts.ScriptInstance Swapping
swapInst oracle = Scripts.validator @Swapping
    ($$(PlutusTx.compile [|| mkSwapValidator ||])
        `PlutusTx.applyCode` PlutusTx.liftCode oracle
        `PlutusTx.applyCode` PlutusTx.liftCode (oracleAddress oracle))
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @PubKeyHash @()

Here the oracle contract is passed to swap contract as a parameter. If somebody wants to use it, he has to provide an instance:
    h3 <- activateContractWallet (Wallet 3) $ swap oracle

And as far as I understand the code, this is only possible if you have access to the source code. But please correct me if I'm wrong.
The oracle API is used in the sense that there are multiple possibilities for valid redeemers, which define the endpoints: data OracleRedeemer = Update | Use
Later somebody can

search for the oracle (this is in a sense library code coming from the oracle provider):

findOracle :: forall w s. HasBlockchainActions s => Oracle -> Contract w s Text (Maybe (TxOutRef, TxOutTx, Integer))
findOracle oracle = do
    utxos <- Map.filter f <$> utxoAt (oracleAddress oracle)
    return $ case Map.toList utxos of
        [(oref, o)] -> do
            x <- oracleValue (txOutTxOut o) $ \dh -> Map.lookup dh $ txData $ txOutTxTx o
            return (oref, o, x)
        _           -> Nothing
  where
    f :: TxOutTx -> Bool
    f o = assetClassValueOf (txOutValue $ txOutTxOut o) (oracleAsset oracle) == 1


use it by providing the right redeemer for the wanted endpoint (in this case "Use", see "Redeemer"):

useSwap :: forall w s. HasBlockchainActions s => Oracle -> Contract w s Text ()
useSwap oracle = do
    funds <- ownFunds
    let amt = assetClassValueOf funds $ oAsset oracle
    logInfo @String $ "available assets: " ++ show amt

    m <- findOracle oracle
    case m of
        Nothing           -> logInfo @String "oracle not found"
        Just (oref, o, x) -> do
            logInfo @String $ "found oracle, exchange rate " ++ show x
            pkh   <- pubKeyHash <$> Contract.ownPubKey
            swaps <- findSwaps oracle (/= pkh)
            case find (f amt x) swaps of
                Nothing                -> logInfo @String "no suitable swap found"
                Just (oref', o', pkh') -> do
                    let v       = txOutValue (txOutTxOut o) <> lovelaceValueOf (oFee oracle)
                        p       = assetClassValue (oAsset oracle) $ price (lovelaces $ txOutValue $ txOutTxOut o') x
                        lookups = Constraints.otherScript (swapValidator oracle)                     <>
                                  Constraints.otherScript (oracleValidator oracle)                   <>
                                  Constraints.unspentOutputs (Map.fromList [(oref, o), (oref', o')])
                        tx      = Constraints.mustSpendScriptOutput oref  (Redeemer $ PlutusTx.toData Use) <>
                                  Constraints.mustSpendScriptOutput oref' (Redeemer $ PlutusTx.toData ())  <>
                                  Constraints.mustPayToOtherScript
                                    (validatorHash $ oracleValidator oracle)
                                    (Datum $ PlutusTx.toData x)
                                    v                                                                      <>
                                  Constraints.mustPayToPubKey pkh' p
                    ledgerTx <- submitTxConstraintsWith @Swapping lookups tx
                    awaitTxConfirmed $ txId ledgerTx
                    logInfo @String $ "made swap with price " ++ show (Value.flattenValue p)
  where
    getPrice :: Integer -> TxOutTx -> Integer
    getPrice x o = price (lovelaces $ txOutValue $ txOutTxOut o) x

    f :: Integer -> Integer -> (TxOutRef, TxOutTx, PubKeyHash) -> Bool
    f amt x (_, o, _) = getPrice x o <= amt

So I suspect that you always need the third-party code. But that would actually be a good thing since you want to make sure anyway that the third party can't fool you.
Additionally, I don't see a way how it could be possible to execute another contract without the code since it isn't stored on the blockchain itself. The blockchain just stores the hash of the code (the public key address) and all funds (eUTxOs) located at that address.


---

### Answer by username_2 on 2021-06-10. Score: 0

Johannes is correct. One will need the source code of the contracts in order to interface with it.

Source: <https://github.com/Emurgo/Emurgo-Research/blob/master/smart-contracts/Unlocking%20The%20Potential%20Of%20The%20UTXO%20Model.md>


---

