Skip to main content

Crash course: Cardano Plutus Smart-Contracts

1. What is a Cardano Transaction?

In Cardano, transactions consume one or more UTXOs and generate one or more new UTXOs.

UTXO Consumed: A transaction’s input UTXOs are considered "spent" once they are consumed by a transaction. It's important to note that a UTXO must be used in its entirety.

Transaction Processing: The transaction then processes these inputs according to the instructions embedded in the transaction such as a simple transfer of ADA, or a complex smart contract operation. In the case of a smart contract the instructions for how to process the UTXO are contained within the Plutus scripts.

UTXO Created: The transaction generates new UTXOs as outputs. These outputs are then available to be consumed by future transactions. The total value of the new UTXOs (plus transaction fees) must be equal to the total value of the consumed UTXOs, ensuring the conservation of value across the transaction.

2. What is a Cardano EUTxO?

The Extended Unspent Transaction Output (EUTxO) model in Cardano extends the traditional UTxO model by providing the ability to define arbitrary logic for determining a transaction is valid or not, thereby enabling smart contracts.

Value: The amount of ADA or other native tokens held in the UTxO.

Address: The destination of the UTxO. This can be a public key address or a more complex script address with additional spending conditions.

Datum: A unique feature of eUTxO, representing arbitrary data attached to a UTxO. It can be used to signify the state of a smart contract, enabling complex, stateful applications in an otherwise stateless model.

Each EUTxO includes value, address, and datum, making it a self-contained entity carrying its own data payload, facilitating the implementation of smart contracts and decentralized applications. These smart contracts are defined via a programming framework called Plutus.

3. What is a Cardano Smart-Contract Plutus Script?

Cradano’s EUTxO model extends on Bitcoin’s UTxO model. The Validator, Datum, Redeemer, and Context are the key new components that enable the creation of complex smart contracts. One way to look at a script validator is as a parameterised function that returns True or False.

Validator(datum, redeemer, context) = True or False

Validator: In Plutus, a validator script is a piece of code that determines whether a transaction is allowed to consume a particular UTXO. It does this by checking whether the transaction meets certain conditions, which are specified in the script. Validator scripts have access to three pieces of information: the Datum, the Redeemer, and the Context.

Datum: The Datum is a piece of data that's associated with a particular UTXO and can be used to represent the state of a smart contract. This data is stored on the blockchain and can be accessed by validator scripts. The Datum is chosen when the UTXO is created and can't be changed afterwards.

Redeemer: The Redeemer is another piece of data that's provided by the transaction that's trying to consume a UTXO. The Redeemer is intended to represent the action that the transaction is trying to perform on the smart contract.

Context: The Context represents the current state of the blockchain at the time when a validator script is run. This includes information about the current transaction, such as its inputs, outputs, and the amount of ADA it's transferring. Validator scripts can also use this information to make more complex decisions about whether a transaction should be allowed to consume a UTXO.

4. Representing Smart-contracts with State-machine Diagrams

State machine diagrams can give useful representations of Cardano Plutus smart contract's operations in terms of states and state transitions.

Smart-Contract States: In transactions UTXOs can carry more data than just the amount of ADA being transferred; they can also contain information representing the "state" of a smart contract.

Smart-Contract State Transitions: Each transaction that consumes and produces UTXOs can be seen as causing a transition from one state to another.

A state machine diagram clearly illustrates these states and their respective transitions. Each state can be represented as a node in the diagram, and each transition can be represented as an edge or arrow connecting the nodes. These diagrams can help developers reason about the behavior of their smart contracts and can be particularly useful when designing and debugging contracts.

tip

Where to next?