Skip to content

Commit

Permalink
Create 08-Plutus-validator-scripts.mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
nahern committed Sep 10, 2021
1 parent e0adf5f commit b9aac0c
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions content/10-plutus/08-Plutus-validator-scripts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: Using validator scripts
metaTitle: Using validator scripts
---

Cardano uses scripts to validate actions. These scripts, which are pieces of code, implement pure functions with True or False outputs. Script validation is the process of invoking the script interpreter to run a given script on appropriate arguments.

## What are scripts?
A script is a program that decides whether or not the transaction which spends the output is authorized to do so. Such a script is called a validator script, because it validates whether the spending is allowed.

A simple validator script would be one that checked whether the spending transaction was signed by a particular key - this would exactly replicate the behaviour of simple pay-to-pubkey outputs. However, with a bit of careful extension, we can use scripts to let us express a large amount of useful logic on the chain.

The way the EUTXO model works is that validator scripts are passed three arguments:
- Datum: this is a piece of data attached to the output that the script is locking (strictly, again, just the hash is present). This is typically used to carry state.
- Redeemer: this is a piece of data attached to the input that is doing the spending. This is typically used to provide an input to the script from the spender.
- Context: this is a piece of data which represents information about the transaction doing the spending. This is used to make assertions about the way the output is being sent (such as “Bob signed it”).

## Intuitive example
For example, a kid wants to go on a ferris wheel, but before getting on, they must be taller than the safety sign.

We could express that idea in pseudo code, like:

```python
if isTallEnough(attraction=ferrisWheel,passenger=michael):
getOnFerrisWheel()

def isTallEnough(attraction,kid):
return kid["height"] >= attraction["minimumHeight"]

def getOnFerrisWheel():
print ("get On the Ferris Wheel")

ferrisWheel = {"minimumHeight":120}
michael = {"height":135}
```

In this example the following applies:
- The datum is the information about this transaction: `michael.hegiht`.
- The context is the state of the world, at that point meaning: `ferrisWheel.minimumHeght`.
- The reedemer, is the action to perform: `getOnFerrisWheel()`

The validator script is the function that uses all that information `isTallEnough`

## Defi example
Now let’s look at an example from the DeFi domain.

We could implement an atomic swap, as follows:
- The datum contains the keys of the two parties in the swap, and a description of what they are swapping
- The redeemer is unused.
- The context contains a representation of the transaction.

The logic of the validator script is as follows: does the transaction make a payment from the second party to the first party, containing the value that they are supposed to send? If so, then they may spend this output and send it where they want (or we could insist that they send it to their key, but we might as well let them do what they like with it).

## Code examples
You can find real code examples of validator scripts on every smart contract, for example:

- [Plutus transaction tutorial](https://github.com/input-output-hk/Alonzo-testnet/blob/main/Alonzo-tutorials/Plutus_transactions_tutorial.md#transaction-to-lock-funds): On this validator, it always succeeds.
- [Plutus Hello World](https://github.com/input-output-hk/Alonzo-testnet/blob/e27563ec0c0c3723376f4d12881cd003a7a7157f/resources/plutus-sources/plutus-helloworld/src/Cardano/PlutusExample/HelloWorld.hs#L47): On this validator if the datum is equal to ‘Hello’ converted to Integer
- [Plutus Pioneers English Auction](https://github.com/input-output-hk/plutus-pioneer-program/blob/024ebd367bf6c4003b482bfb4c6db7d745ec85aa/code/week01/src/Week01/EnglishAuction.hs#L103): On this line the validator makes sure that the new bid (datum) is superior to the previous one and so on until time is up.

0 comments on commit b9aac0c

Please sign in to comment.