4

Since I just started to write Plutus code, I want to make some tests of Plutus.

For example, I'd like to know what exactly my redeemer :: BuiltinData value is. However I can't convert BuiltinData to BuiltinString which means I can't use trace :: BuiltinString -> a -> a to log it.

Is it possible to convert BuiltinData to BuiltinString or there is different approach to log redeemer?

2 Answers 2

2

Since BuiltinData derives Show:

newtype BuiltinData = BuiltinData PLC.Data
    deriving newtype (Show, Eq, Ord)

and there's the function:

stringToBuiltinString :: String -> BuiltinString found here in PlutusTx/Builtins/Class.hs

Assuming your "variable name" is dummyBuiltinData, you can convert BuiltinData to BuiltinString using:

stringToBuiltinString $ show dummyBuiltinData

EDIT:

As noted by kolam, show isn't INLINABLE and isn't applicable on-chain. Here is a version that should work:

decodeUtf8 $ unsafeDataAsB dummyBuiltinData

both decodeUtf8 and unsafeDataAsB can be found in module PlutusTx.Builtins.Internal

2
  • That won't work as show is not an inlinable function and thus cannot be used in the validation (on-chain) part of Plutus. Those functions you mentioned are meant to be used in the off-chain part of Plutus.
    – kolam
    Aug 24, 2021 at 14:14
  • Yes, as @kolam says, you can't use show onchain to print out strings that are built at runtime from onchain code. I think this is because the script's address is derived from the hash of the validator function/onchain code, so it can only use hardcoded strings in traceError or traceIfFalse. -> I did not have any luck getting decodeUtf8 to run on chain either. I'm guessing that's because the hash I was attempting to print out didn't resolve to utf8.
    – marcel_g
    May 2 at 4:45
0

You can't print out strings that are built at runtime in onchain code with Plutus, my guess is that's because the script's address is generated by hashing the validator function, so it only allows hardcoded strings so the hash of that function is always the same.

My solution is to trigger traceIfFalse with a function that gives me a clue about the variable I'm investigating:

... 
mkValidator = traceIfFalse getTraceString False 

getTraceString :: BuiltinString
getTraceString = case someVar of 
                   a -> "it's a"
                   b -> "it's b"
                   _ -> "it's something else"
...

A bit kludgy, but it's working for me.

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.