Skip to content

Draft: [#346] `deriveView` prototype

Diogo Castro requested to merge diogo/#346-th-poc into master

WIP

Description

This prototype exposes a new type family:

type family AsView a = b | b -> a

This type family maps a storage type S to the type that will be returned from the Tezos RPC when we query that storage, AsView S.

type instance AsView (BigMap k v) = BigMapId (AsView k) (AsView v)
type instance AsView Integer = Integer
type instance AsView Natural = Natural
type instance AsView [a] = [AsView a]

Given a user-defined custom type like this:

data Storage a = Storage
  { fa :: Integer
  , fb :: a
  , fc :: BigMap Integer Integer
  }
  deriving stock Generic
  deriving anyclass IsoValue

Then the deriveView TemplateHaskell function will generate the following code:

deriveView "Storage"
  ======>

data StorageV a = StorageV
  { faV :: AsView Integer
  , fbV :: AsView a
  , fcV :: AsView (BigMap Integer Integer)
  }

instance Generic (StorageV a) where
  type Rep (StorageV (a :: Type) :: Type) = ...
  from (StorageV fa fb fc) = ...
  to ... = ...

deriving anyclass instance IsoValue (AsView a) =>
                           IsoValue (StorageV a)

type instance AsView (Storage a) = StorageV a

Note: You can test this yourself by adding {-# OPTIONS_GHC -ddump-splices #-} to your module.

If the Generic instance for Storage was constructed using customGeneric, then the Generic instance for StorageV will have the same "shape".

This MR also exposes a (temporarily named) getStorageNew function. It takes @Storage as a type parameter, and returns m StorageV:

niGetStorageNew
  :: forall s. (HasCallStack, UnpackedValScope (ToT (AsView s)), IsoValue (AsView s))
  => AddressOrAlias -> m (AsView s)

I've added some network tests (see the Nettest.GetStorage module), everything seems to work as expected so far.

Things I'd like to discuss:

  1. For a lack of a better term, I've been calling this concept (storage types with BigMapIds instead of BigMap) "views", but I really want to avoid this term. "View" is already understood to refer TZIP-4's view entrypoints, and I'd like to avoid overloading the term. To be honest, I'm having a hard time coming up with a clear and intuitive name, so suggestions are very much welcome!
  2. The AsView/TH logic/BigMapId are all in morley:lib at the moment (for my convenience), but I was thinking whether it would be better to move it morley-client, what do you think?

Related issue(s)

Resolves part of #346 (closed)

Checklist for your Merge Request

Related changes (conditional)

  • Tests (see short guidelines)

    • If I added new functionality, I added tests covering it.
    • If I fixed a bug, I added a regression test to prevent the bug from silently reappearing again.
  • Documentation

    • I checked whether I should update the docs and did so if necessary:
    • I updated changelog files of all affected packages released to Hackage if my changes are externally visible.

Stylistic guide (mandatory)

Merge request reports

Loading