3

Within my smartpy contract, I want to convert from one type into another using sp.pack() and sp.unpack.

Below is an example of the method I am trying to use, converting a string to bytes.

        myString = sp.local("myString", "dummyString")
        packedString = sp.pack(myString.value)
        myBytes = sp.unpack(packedString, t = sp.TBytes)

The problem is the type of the output I get is

sp.TOption(sp.TBytes)

when what I wanted was just

sp.TBytes

What else should I do to make sure the type isn't sp.TOption?

1 Answer 1

4

sp.unpack gives you an option (because Michelson gives an option when you call UNPACK) that you need to open.

You can do it like this

        myBytes = sp.unpack(packedString, t = sp.TBytes).open_some()

if you're sure that packedString is indeed a packed sp.TBytes.

If you're not 100% sure, you can add an optional error message to open_some().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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