Hi I am currently at lecture 5 of the Plutus Pioneers Programm and I was wondering which part of the code is on chain and which is offchain. I know that the valididation script is onchain but the whole code is in one file, so how can later on some of that run offchain? And can maybe someone explain how this is handled in Ethereum?


I know that the valididation script is onchain but the whole code is in one file, so how can later on some of that run offchain?

Technically, everything you see in the file is off-chain code.

It is specifically the code that gets compiled into Plutus code that will be executed on-chain. And, yes this includes Validators and Policies. You can see how the monetary policy is compiled in the NFT code for lesson 5 here.

Everything else is used to either

  1. Submit the compiled code on-chain, or
  2. Interact with the submitted code

It's worth noting, people could write whatever off-chain code they want to interact with the on-chain code as long as the hashed policy address is the same here. It's the job of the policy writer to keep people from abusing that freedom.

It's nice having everything in one file/project so things can share types. It all starts as Haskell after all :).


Mitchel Turner is right but for myself in the beginning i tried to think of things a little "simpler"

The validators basically run "on chain" and the contracts (everything that result in the type -> Contract) runs in your(the users wallet) wallet.

So what that i took away from that.

  1. the contracts just create/build a transaction from ground up in your wallet or "hidden" in the user interface that you create ... and will the be submitted to the chain

  2. "the chain" will then use your validator to well validate this created transaction

  3. the offchain code does the "heavy lifting" in setting every thing up ...

  4. the validator is just there to say okay this transaction is okay or not

this "mindset" helped me in the beginning to start understand things ... and im still in the process of getting the whole picture ;)


The on chain part is basically every piece of code that runs when the nodes perform validations on whether you have the rights to spend UTOXs or you can mint or burn currencies (validator script and monetary policies). The term "On chain" I guess refers to the execution context of said code and since this validations are performed by the blockchain, that "on chain code" term was coined.

However, if I understand it correctly, these scripts do not actually live on the blockchain, but only the hash of the validator script does (along with the value and the current Datum of the UTXO), and is the spending transaction the one that must provide the right script for the nodes to validate (along with the new Datum and the Redeemer). Of course this script must match the script hash on the UTXO.

It works similar to the pay to script hash transactions in bitcoin, but adds the concepts of Datum and Redeemer and provide access to all inputs and outputs of the transaction so more sophisticated validations can be made.

So to summarize:

On chain code: All code that is executed by the nodes at validation time (UTXO validator script or monetary policy).

Off chain code: All code that is run by the wallet before sending the transaction. This code will construct the valid (or invalid) transaction to be send.

  • Thanks I think this helps Jul 21 at 7:24

On-chain part refers to all things compiled to the plutus core. This compilation is driven by haskell template engine and the code is wrapped in a haskell data structure.

Off-chain part interacts indireclty with the ledger. In fact this part runs in a sandbox called PAB (plutus application backend) which is capable of interacting with the cardano passive node (entry point to the cardano blockchain)

Off-chain part can lock (e)utxos or spend them.

When it comes to spending the transaction created by the off-chain part besides providing inputs and outputs needs only to provide hashes (hash for script and hash for datum). Those hashes can be computed by taking before compiled plutus script (wrapped by some haskell data structure) and performing some computation (calling function)

For spending however the transction being submited needs to contain the script, all required datum and of course redeemers as a wittneses.

To summarize the whole code related to the smart contract is sitting in a PAB. The offchain part is generated during compilation by haskell template engine and wrapped in haskell data structue. Off-chain part when needed unwraps the plutus from underlying data structure and submit it as a part of a transaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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