3

I want to use String or Text in a Datum, like this:

import PlutusTx.Prelude

newtype MyDatum = MyDatum {userReadableStuff :: BuiltinString}
PlutusTx.unstableMakeIsData ''MyDatum

but the complier complains:

• No instance for (PlutusTx.IsData BuiltinString)
    arising from a use of ‘PlutusTx.fromBuiltinData’
• In the second argument of ‘(<*>)’, namely
    ‘PlutusTx.fromBuiltinData arg_a2lNn’
  In the second argument of ‘PlutusTx.Builtins.matchList’, namely
    ‘(pure MyDatum <*> PlutusTx.fromBuiltinData arg_a2lNn)’
  In the expression:
    ((PlutusTx.Builtins.matchList t_a2lNq)
       (pure MyDatum <*> PlutusTx.fromBuiltinData arg_a2lNn))
      (\ _ _ -> Nothing)typecheck(-Wdeferred-type-errors)
• No instance for (PlutusTx.IsData BuiltinString)
    arising from a use of ‘PlutusTx.toBuiltinData’
• In the first argument of ‘PlutusTx.Builtins.Internal.mkCons’, namely
    ‘(PlutusTx.toBuiltinData arg_a2lNi)’
  In the second argument of ‘PlutusTx.Builtins.Internal.mkConstr’, namely
    ‘((PlutusTx.Builtins.Internal.mkCons
         (PlutusTx.toBuiltinData arg_a2lNi))
        (PlutusTx.Builtins.Internal.mkNilData
           PlutusTx.Builtins.Internal.unitval))’
  In the expression:
    (PlutusTx.Builtins.Internal.mkConstr 0)
      ((PlutusTx.Builtins.Internal.mkCons
          (PlutusTx.toBuiltinData arg_a2lNi))
         (PlutusTx.Builtins.Internal.mkNilData
            PlutusTx.Builtins.Internal.unitval))typecheck(-Wdeferred-type-errors)
• No instance for (PlutusTx.IsData BuiltinString)
    arising from a use of ‘PlutusTx.unsafeFromBuiltinData’
• In the first argument of ‘MyDatum’, namely
    ‘(PlutusTx.unsafeFromBuiltinData arg_a2lNv)’
  In the expression:
    MyDatum (PlutusTx.unsafeFromBuiltinData arg_a2lNv)
  In the expression:
    let
      t_a2lNy = PlutusTx.Builtins.Internal.snd tup_a2lNu
      arg_a2lNv = PlutusTx.Builtins.Internal.head t_a2lNy
    in MyDatum (PlutusTx.unsafeFromBuiltinData arg_a2lNv)typecheck(-Wdeferred-type-errors)

I could use ByteString -- about which the compiler has no complaints, and then use helper functions for Text <-> ByteString, as shown at end of this. But what I really want is for end user to see the datum in the script, and be able to read directly from the datum.

2

Not really sure what you mean by "end user to see the datum in the script" and how having a Text or String in the datum would make any difference to this. Datums are not easily visible to everyone and certainly not to "end users".

However, if you want to use BuiltinString in MyDatum, you could write your own instances for ToData, FromData and UnsafeFromData via BuiltinByteString and encodeUtf8/decodeUtf8 from PlutusTx.Builtins.

Something like the following could work:

instance ToData MyDatum where
  toBuiltinData = toBuiltinData . encodeUtf8 . userReadableStuff

instance FromData MyDatum where
  fromBuiltinData = MyDatum . decodeUtf8 <$> fromBuiltinData

instance UnsafeFromData MyDatum where
  unsafeFromBuiltinData = MyDatum . decodeUtf8 . unsafeFromBuiltinData

Note: I had a quick look at the implementation of the builtin encodeUtf8, this might not be what you expect (it seems to be currently ASCII encoding and truncating any code points outside of the 8bit range).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.