Getting Started
This guide will help you set up your development environment and get your first Scalus Validator.
Install Scala on your computer
We recommend using Scala Installer - Coursier , that ensures that a JVM and standard Scala tools are installed on your system.
macOS - Brew
Homebrew based installation:
brew install coursier && coursier setup
Coursier will install Scala compiler, Command line tool: Scala CLI , Build tool: sbt , REPL: Ammonite and Code formatter: Scalafmt .
Get your first Scalus Validator
sbt new scalus3/hello.g8
This ran the template scalus3/hello.g8 using Giter8 . Let’s take a look at what just got generated:
validator/
├── HelloCardano.scala # Simple validator
├── HelloCardano.test.scala # Simple tests
├── project.scala # Project configuration
└── README.md
Test
Run unit tests (MUnit, ScalaTest and ScalaCheck):
scala-cli test hello
All is good if you see the following output:
validator.HelloCardanoSpec:
+ Hello Cardano 0.385s
Configure IDE
Setting up a productive development environment will significantly improve your Scala/Scalus development experience. Scala offers wide range of IDE support, the most popular are IntelliJ and VSCode.
IntelliJ
- Install IntelliJ IDEA (Community or Ultimate edition) from the JetBrains websiteÂ
- Install the Scala plugin:
- Go to Settings/Preferences → Plugins → Marketplace
- Search for “Scala” and install the plugin
- Restart IntelliJ IDEA when prompted
- Open your Scalus project:
- Select File → Open and navigate to your project directory
- Choose Import as sbt project when prompted
You are good to go! Start exploring.
import scalus.*
import scalus.builtin.Data
import scalus.ledger.api.v3.{PubKeyHash, TxInfo, TxOutRef}
import scalus.prelude.*
import scalus.prelude.Option.Some
import scalus.prelude.Prelude.*
/** This validator demonstrates two key validation checks:
* 1. It verifies that the transaction is signed by the owner's public key hash (stored in the datum)
* 2. It confirms that the redeemer contains the exact string "Hello, Cardano!"
*
* Both conditions must be met for the validator to approve spending the UTxO.
*/
@Compile
object HelloCardano extends Validator:
override def spend(
datum: Option[Data],
redeemer: Data,
tx: TxInfo,
sourceTxOutRef: TxOutRef
): Unit =
val Some(ownerData) = datum: @unchecked
val owner = ownerData.to[PubKeyHash]
val signed = tx.signatories.contains(owner)
require(signed, "Must be signed")
val saysHello = redeemer.to[String] == "Hello, Cardano!"
require(saysHello, "Invalid redeemer")