title_body: Referencing onchain contract as 3rd party 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 &lt;- 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?
upvoted_answer: 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 -&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 @()
  
  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 &lt;- 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 =&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
  
  
  use it by providing the right redeemer for the wanted endpoint (in this case &quot;Use&quot;, see &quot;Redeemer&quot;):
  
  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
  
  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.