TZIP-16: off-chain views implementation guideline
At the moment there is an ambiguity in how to properly implement michelson-storage-views, especially in high-level languages.
The common understanding is that off-chain view is basically a Michelson script with some restrictions, however TZIP-16 does not specify how this script should be constructed. This could potentially lead to non-compatibility across the ecosystem tooling.
Default implementation
Used in TZComet, Taquito.
Given the TZIP-16 specification of michelson-storage-view
{
parameter: $optional_view_args_type,
return-type: $view_return_type,
code: $view_code
}
The resulting Michelson script is:
parameter (pair ($optional_view_args_type) ($contract_storage_type)) ;
storage ($view_return_type) ;
code {
CAR ;
{ $view_code } ;
SOME ;
NIL operation ;
PAIR ;
}
There are several problems with such implementation:
- TZIP-16 spec is not enough to construct view parameter type, you have to make extra request to query the contract storage type.
- Currently, the simplest way to implement an off-chain view in high-level languages is introducing a new compilation target (i.e. another Michelson contract on the output). That means any tooling will have to extract $view_code and $optional_view_args_type from the compiled script, which is a non-trivial task (in some cases impossible due to compiler optimisations).
Alternative implementation
The suggested alternative is to copy parameter/storage types and instruction sequence as is:
{
parameter: $compiled_script_parameter_type,
return-type: $compiled_script_storage_type,
code: $compiled_script_code
}
Edited by Michael Zaikin