5

I feel like this should be pretty easy, but I'm stumped. How can I use a slot number as a token name?

I'm currently trying to encode the slot integer as a ByteString:

slotNumberTokenName :: TokenName
slotNumberTokenName = TokenName { unTokenName = encode $ getSlot slot }

but I get the error

    • Couldn't match expected type ‘ByteString’
                  with actual type ‘bytestring-0.10.12.0:Data.ByteString.Lazy.Internal.ByteString’
      NB: ‘bytestring-0.10.12.0:Data.ByteString.Lazy.Internal.ByteString’
            is defined in ‘Data.ByteString.Lazy.Internal’
          ‘ByteString’ is defined in ‘Data.ByteString.Internal’
    • In the ‘unTokenName’ field of a record
      In the expression: TokenName {unTokenName = encode $ getSlot slot}
      In an equation for ‘slotNumberTokenName’:
          slotNumberTokenName
            = TokenName {unTokenName = encode $ getSlot slot}
    |
153 |     slotNumberTokenName = TokenName { unTokenName = encode $ getSlot slot }
    | 

I am using Data.Aeson.encode, so maybe there is a different function to do that conversion.

Anyhow, any help would be appreciated. Been looking at this for longer than I'd like to admit.

3

I've just run this in the REPL with success. Not sure if it is the best way, though.

TokenName { unTokenName = Data.ByteString.Char8.pack $ show (toInteger (getSlot 1)) }
1
  • This worked. Thanks. I agree there might be a more direct way of doing it, but I'm not picky for now. Can always improve it. Jul 6 at 21:05
1

This can be done with Data.Binary. See https://stackoverflow.com/a/2284193/10069673

Another possibility would be to convert the int to a string and then convert the string to a ByteString.

1
  • 1
    Hmm. I got the same error with Data.Binary.encode as I got with Data.Aeson.encode. Seems like it would work :/ I got Chrismo's suggestion to work though. Thanks! Jul 6 at 21:04

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.