<p>I'm not sure how specific you want the answer to be, but essentially <code>on-chain</code> means that the contract is executed on the block-chain. Whereas, <code>off-chain</code> means that this part of the contract is executed off of the block-chain (ie. on the user's machine).</p>

CC BY-SA 4.0
2021-10-02T03:53:56.197Z
Are the following clarifications correct?  By "on the user's machine", you mean the code is executed using computation power provided by a 3rd-party/user interacting with the contract (the results of which will potentially not be verified in any way).  By "on the block-chain", you mean the code is executed using compute power provided by validator nodes (the results of which will be statistically verified).
mathandy
CC BY-SA 4.0
2021-05-01T04:35:29.083Z
2021-05-01T04:35:29.083Z
Cavenfish
<p>I think the <a href="https://hydra.iohk.io/build/5988492/download/1/plutus-core-specification.pdf" rel="noreferrer">Plutus core specification</a> gives pretty good explanation.</p>
<blockquote>
<p>Plutus Core code (and code intended for execution on a blockchain in general) can be executed in two different environments: <strong>off-chain</strong> and <strong>on-chain</strong>. As the name suggests, off-chain execution doesn’t happen on the blockchain itself, but in some other environment, such as in an electronic wallet on a smartphone or PC. In contrast, on-chain execution takes place on core nodes, machines which are actually maintaining the blockchain.</p>
</blockquote>
<p>Not only the environment is different though. The purpose is also different:</p>
<ul>
<li>On-chain code is used to validate transactions. It will be run every time somebody wants to submit a transaction to the blockchain. If validation succeeds, most likely the transaction will be written to the chain. Fees also should be paid for running the on-chain code to prevent DoS of the blockchain (if there are no fees, anybody can submit an infinite loop to the blockchain and then every node running this code will be stuck and thus blockchain won't be able to operate)</li>
<li>Off-chain code is used to build and submit transactions and also query the blockchain. Say, you want to participate in online auction running on Cardano. In order to do so, you need to make a bid. To make a bid, you need to send specific data to the auction smart contract, say the ID of the lot and amount of Ada. Off-chain code builds such a transaction, runs off-chain validation (so you can avoid spending fees for invalid transactions), calculates the fees and submits the transaction to the blockchain</li>
</ul>

CC BY-SA 4.0
2021-05-01T19:48:13.867Z
Since the off-chain part runs on the wallet, would be possible to hang it with an infinite loop? or there's a mechanism to prevent this behaviour?
Aron Neewart
CC BY-SA 4.0
2021-05-01T12:52:58.590Z
2021-05-03T05:05:46.500Z
2021-05-03T05:05:46.500Z
Tim P.
<p>The on-chain and off-chain code are written in Plutus language which is basically Haskell.
The on-chain part is compiled using PlutusTx to &quot;Plutus Core&quot; which are executed by the nodes on the blockchain.
The off-chain part is compiled using Haskell compiler to run on user's machine (wallet, pc, mobile ...)</p>

CC BY-SA 4.0
2021-05-02T22:00:36.867Z
2021-05-02T22:00:36.867Z
Jelil Akjaly
<p>Smart contracts written in Plutus can be divided in two: an <code>on-chain</code> part and an <code>off-chain</code> part. What's the main difference between these two?</p>

<smart-contract><plutus>
What's the difference between "on-chain" and "off-chain" in plutus smart contracts?
# What's the difference between "on-chain" and "off-chain" in plutus smart contracts?

(Asked by: username_0 on 2021-05-01)

Smart contracts written in Plutus can be divided in two: an `on-chain` part and an `off-chain` part. What's the main difference between these two?

**Tags:** smart-contract, plutus

---

## 3 answers

### Answer by username_3 on 2021-05-01. Score: 21 | Accepted answer

I think the [Plutus core specification](https://hydra.iohk.io/build/5988492/download/1/plutus-core-specification.pdf) gives pretty good explanation.

> Plutus Core code (and code intended for execution on a blockchain in general) can be executed in two different environments: **off-chain** and **on-chain**. As the name suggests, off-chain execution doesn’t happen on the blockchain itself, but in some other environment, such as in an electronic wallet on a smartphone or PC. In contrast, on-chain execution takes place on core nodes, machines which are actually maintaining the blockchain.

Not only the environment is different though. The purpose is also different:

* On-chain code is used to validate transactions. It will be run every time somebody wants to submit a transaction to the blockchain. If validation succeeds, most likely the transaction will be written to the chain. Fees also should be paid for running the on-chain code to prevent DoS of the blockchain (if there are no fees, anybody can submit an infinite loop to the blockchain and then every node running this code will be stuck and thus blockchain won't be able to operate)
* Off-chain code is used to build and submit transactions and also query the blockchain. Say, you want to participate in online auction running on Cardano. In order to do so, you need to make a bid. To make a bid, you need to send specific data to the auction smart contract, say the ID of the lot and amount of Ada. Off-chain code builds such a transaction, runs off-chain validation (so you can avoid spending fees for invalid transactions), calculates the fees and submits the transaction to the blockchain

> **Comments:**
> **username_0**: Since the off-chain part runs on the wallet, would be possible to hang it with an infinite loop? or there's a mechanism to prevent this behaviour?  

---

### Answer by username_1 on 2021-05-01. Score: 5

I'm not sure how specific you want the answer to be, but essentially `on-chain` means that the contract is executed on the block-chain. Whereas, `off-chain` means that this part of the contract is executed off of the block-chain (ie. on the user's machine).

> **Comments:**
> **username_2**: Are the following clarifications correct?  By "on the user's machine", you mean the code is executed using computation power provided by a 3rd-party/user interacting with the contract (the results of which will potentially not be verified in any way).  By "on the block-chain", you mean the code is executed using compute power provided by validator nodes (the results of which will be statistically verified).  

---

### Answer by username_4 on 2021-05-02. Score: 1

The on-chain and off-chain code are written in Plutus language which is basically Haskell.
The on-chain part is compiled using PlutusTx to "Plutus Core" which are executed by the nodes on the blockchain.
The off-chain part is compiled using Haskell compiler to run on user's machine (wallet, pc, mobile ...)


---

