Using Views to Improve SEXP Contracts

The next Tezos protocol upgrade, called Hangzhou, is expected to go live on the Tezos mainnet in December. It features several improvements, of which perhaps the most interesting to smart contract developers are the views. In  this article, we look at how views can help improve and simplify the design of SEXP Synth Swap.

Currently, if you want your contract to work with values from another contract, you need to make a call asking the other contract to send you the values. Then you have a separate callback handler that receives the values and processes them however you need. This means you have to split your logic into multiple entrypoints and make sure that the handler is only called after the entrypoint meant for external users, etc.

Views provide another option. Instead of making a transfer to request some values, you can use a special view entrypoint. The target contract has to support this. Calling the view entrypoint doesn't modify the target contract's storage, but instead of needing a separate callback handler you get the requested values immediately. Then you can continue executing your business logic. So you can remove the callback handlers and your code gets much easier to read.

Reducing complexity of contracts makes them easier to audit and check for unexpected behavior. Another benefit is that you can now provide read-only entrypoints meant for wallets, indexers and other consumers. Instead of having to use an agreed upon storage format for your tokens, you can provide a view that presents the interesting information to the caller. For example, a token standard interface doesn't need to dictate how your contract storage should look anymore, since consumers can simply rely on views provided by your contract. Tooling supporting the standard will become more robust.

After upgrading our contracts to use views, we expect a significant reduction in complexity and improvements in auditability. More information about views and the Hangzhou upgrade is available on the Tezos documentation page.

Try SEXP Synth Swap with test funds here and if you like it, don't forget to contribute in our token sale! Contributions are accepted until 31st October 18:00 UTC.