Plutus 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 that 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 check whether the spending transaction was signed by a particular key – this would exactly replicate the behavior of simple pay-to-pubkey outputs. However, with a bit of careful extension, we can use scripts to express 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 spending input. This is typically used to provide an input to the script from the spender.
  • Context: this is a piece of data that represents information about the spending transaction. 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:

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.height.
  • The context is the state of the world, at that point meaning: ferrisWheel.minimumHeight.
  • 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:

Cost model parameters

The cost model for Plutus Core scripts has a number of parameters, which are part of the Cardano protocol parameters. Developers can adjust those parameters individually.

See the following for more details:

© IOHK 2015 - 2021


Cardano Logo

Cardano is an open-source project.

Cardano is a software platform ONLY and does not conduct any independent diligence on, or substantive review of, any blockchain asset, digital currency, cryptocurrency or associated funds. You are fully and solely responsible for evaluating your investments, for determining whether you will exchange blockchain assets based on your own judgement, and for all your decisions as to whether to exchange blockchain assets with Cardano. In many cases, blockchain assets you exchange on the basis of your research may not increase in value, and may decrease in value. Similarly, blockchain assets you exchange on the basis of your research may fall or rise in value after your exchange.

Past performance is not indicative of future results. Any investment in blockchain assets involves the risk of loss of part or all of your investment. The value of the blockchain assets you exchange is subject to market and other investment risks