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

CC BY-SA 4.0
2021-06-04T00:35:50.390Z
2021-08-02T16:38:46.840Z
2021-08-02T16:38:46.840Z
Mitchell Turner
<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>

CC BY-SA 4.0
2021-07-21T07:24:18.350Z
Thanks I think this helps
Reddspark
CC BY-SA 4.0
2021-07-01T07:05:40.867Z
2021-07-02T02:08:55.567Z
2021-07-02T02:08:55.567Z
AngelCastillo
<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>

CC BY-SA 4.0
2021-06-04T09:43:17.503Z
2021-06-04T09:43:17.503Z
SaIG
<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 on-chain 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>

CC BY-SA 4.0
2021-08-04T23:21:59.617Z
2024-09-11T06:56:54.760Z
2024-09-11T06:56:54.760Z
slavik
<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>

CC BY-SA 4.0
2021-11-03T14:54:51.627Z
I'm noob when it comes to plutus but i was curious to one thing: Where is the off-chain code stored? is this stored on-chain to be executed off-chain? or does it not work in that way?
Guidok
<plutus><transaction><plutus-pioneer-program><smart-contract>
On-chain and off-chain for Plutus SCs
# On-chain and off-chain for Plutus SCs

(Asked by: username_0 on 2021-06-03)

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?

**Tags:** plutus, transaction, plutus-pioneer-program, smart-contract
> **Comments:**
> **username_1**: I'm noob when it comes to plutus but i was curious to one thing: Where is the off-chain code stored? is this stored on-chain to be executed off-chain? or does it not work in that way?  

---

## 4 answers

### Answer by username_2 on 2021-06-04. Score: 13

> 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](https://github.com/input-output-hk/plutus-pioneer-program/blob/47e0accd1710af598999cb0d5303dde76e826a23/code/week05/src/Week05/NFT.hs#L52).

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](https://github.com/input-output-hk/plutus-pioneer-program/blob/47e0accd1710af598999cb0d5303dde76e826a23/code/week05/src/Week05/NFT.hs#L59). 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 :).


---

### Answer by username_3 on 2021-07-01. Score: 7

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.

> **Comments:**
> **username_4**: Thanks I think this helps  

---

### Answer by username_5 on 2021-06-04. Score: 5

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


---

### Answer by username_6 on 2021-08-04. Score: 3

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 on-chain 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.


---

