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.