8

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?

1
  • Please clarify "Or is the original source code always necessary to reference the correct instance?". May 26, 2021 at 16:19

2 Answers 2

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

  1. 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
  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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.