Closed ignaciodopazo closed 1 year ago
Enum is a bit more difficult and also not straightforward I have to admit. I can probably improve this over time.
But Credential
would look like this:
const Credential = Data.Enum([
Data.Object({
PublicKeyCredential: Data.Tuple([
Data.Bytes(),
]),
}),
Data.Object({
ScriptCredential: Data.Tuple([
Data.Bytes(),
]),
}),
]);
The rules for enums are the following. If the enum item doesn't have args then you use Data.Literal()
. If it does have args then you use a combination of Data.Object()
and Data.Tuple
. This is the only way to represent those enums properly in JavaScript. For example:
const MyEnum = Data.Enum([
Data.Literal("Buy"),
Data.Object({ Sell: Data.Tuple([Data.Bytes()]) }),
]);
Fair enough, it works perfectly! Thanks for the explanation.
I want to produce a Enum that maps to a sum type in Haskell, but Lucid crashes while building the enum in some cases. It follows the simplest example that I could make it fail with
which happens with
Data.Integer()
,Data.Any()
as well. The error in all cases is the followingI tried modifying this test (changing
Data.Literal("Left")
withData.Bytes()
) and it explodes as well.My particular use case was to represent the
Credential
Plutus type, and their constructorsPubKeyCredential
andScriptCredential
arePubKeyHash
andScriptHash
which both are newtypes wrappingPlutusTx.BuiltinByteString
that end up being stripped down to just bytes in Plutus Data format.So, the following code
does not work, while I believe it should.
My workaround was building a
ScriptCredential
by hand usingConstr