Extracting from Data
Data corresponds to Plutus Core Data and encodes arbitrary information.
When receiving ScriptContext, scripts get Datums and Redeemers as data.
Scalus provides type classes FromData and ToData to convert between user-defined types and Data. You can derive instances automatically using Scala 3’s derives syntax.
One caveat - you must always supply a companion object with @Compile annotation on it even if it is empty.
This is because Scalus needs to compile the companion object to include the FromData instance in the UPLC script.
import scalus.builtin.*, Builtins.*, Data.*
case class Account(hash: ByteString, balance: BigInt) derives FromData, ToData
@Compile
object Account
enum State derives FromData, ToData:
case Empty
case Active(account: Account)
@Compile
object State
Here’s an example of converting standard and user-defined types to and from Data.
import scalus.builtin.*, Builtins.*, Data.*
case class Account(hash: ByteString, balance: BigInt)
enum State:
case Empty
case Active(account: Account)
val fromDataExample = compile {
// The `fromData` function is used to convert a `Data` value to a typed Scala value.
val data = iData(123)
// fromData is a method that looks up the appropriate `FromData` instance for the type
// there are instances for all built-in types
val a = fromData[BigInt](data)
// also you can use extension method `to` on Data
val b = data.to[BigInt]
// you can define your own `FromData` instances
{
given FromData[Account] = (d: Data) => {
val args = unConstrData(d).snd
Account(args.head.to[ByteString], args.tail.head.to[BigInt])
}
val account = data.to[Account]
}
// or you can use a macro to derive the FromData instance
{
given FromData[Account] = FromData.derived
given FromData[State] = FromData.derived
}
}You can derive FromData instances for your case classes and enums via standard Scala 3 derives syntax.
Last updated on