user1161: 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?

user2053: 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.

user1113: 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.

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

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

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

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 ;)

user1052: 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.

user1130: 
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

Submit the compiled code on-chain, or
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 :).

1161: <p>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?</p>
 2053: <p>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.</p>
<p>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)</p>
<p>Off-chain part can lock (e)utxos or spend them.</p>
<p>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)</p>
<p>For spending however the transction being submited needs to contain the script, all required datum and of course redeemers as a wittneses.</p>
<p>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.</p>
 1113: <p>Mitchel Turner is right but for myself in the beginning i tried to think of things a little &quot;simpler&quot;</p>
<p>The validators basically run &quot;on chain&quot;
and the contracts (everything that result in the type -&gt; Contract) runs in your(the users wallet) wallet.</p>
<p>So what that i took away from that.</p>
<ol>
<li><p>the contracts just create/build a transaction from ground up in your wallet or &quot;hidden&quot; in the user interface that you create ... and will the be submitted to the chain</p>
</li>
<li><p>&quot;the chain&quot; will then use your validator to well validate this created transaction</p>
</li>
<li><p>the offchain code does the &quot;heavy lifting&quot; in setting every thing up ...</p>
</li>
<li><p>the validator is just there to say okay this transaction is okay or not</p>
</li>
</ol>
<p>this &quot;mindset&quot; helped me in the beginning to start understand things ... and im still in the process of getting the whole picture ;)</p>
 1052: <p>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). <strong>The term &quot;On chain&quot; I guess refers to the execution context of said code</strong> and since this validations are performed by the blockchain, that &quot;on chain code&quot; term was coined.</p>
<p>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.</p>
<p>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.</p>
<p>So to summarize:</p>
<p>On chain code: All code that is executed by the nodes at validation time (UTXO validator script or monetary policy).</p>
<p>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.</p>
 1130: <blockquote>
<p>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?</p>
</blockquote>
<p>Technically, everything you see in the file is off-chain code.</p>
<p>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 <a href="https://github.com/input-output-hk/plutus-pioneer-program/blob/47e0accd1710af598999cb0d5303dde76e826a23/code/week05/src/Week05/NFT.hs#L52" rel="noreferrer">here</a>.</p>
<p>Everything else is used to either</p>
<ol>
<li>Submit the compiled code on-chain, or</li>
<li>Interact with the submitted code</li>
</ol>
<p>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 <a href="https://github.com/input-output-hk/plutus-pioneer-program/blob/47e0accd1710af598999cb0d5303dde76e826a23/code/week05/src/Week05/NFT.hs#L59" rel="noreferrer">here</a>. It's the job of the policy writer to keep people from abusing that freedom.</p>
<p>It's nice having everything in one file/project so things can share types. It all starts as Haskell after all :).</p>
