We like the (EIP-2535) Diamonds

Let’s talk about the Premia smart contract architecture

Premia Finance
5 min readNov 7, 2021

The Premia team has been hard at work over the last several months reimagining and rebuilding our decentralized options platform, from the ground up. What we have come up with is a system that not only functions as a self-contained options marketplace, but also fits well into the wider Ethereum blockchain ecosystem. A true “money lego”, one might say.

Today, we want discuss in particular our decisions in designing the Premia smart contracts, specifically the choice to utilize EIP-2535 Diamond Proxies as part of the core Premia architecture, both in terms of technical implications and user experience.

Why did we choose to use Diamond Proxies? The EIP itself lays out a rationale, and Premia is the perfect use-case. We can break it down into a few points.

Organization

Option markets are chaotic — a refined contract architecture helps to maintain order. Like standard proxies, Diamond Proxies work by separating a contract deployment into two parts: the proxy itself, and the implementation code. However, one key innovation sets Diamond Proxies apart — the ability to define any number of distinct but interoperable implementation contracts, called “facets.” Premia’s codebase is highly readable due to extensive use of this pattern.

Flexibility

Option markets are complex — our smart contracts reflect this. Between building core functionality, integrating with Chainlink, and guarding against potential exploits, our development team continually ran into the EIP-170 contract size limit of 24 KB. Because a Diamond Proxy can be configured to use multiple implementation contracts, we were able to split the code into manageable pieces, staying safely below this limit without sacrificing quality or security.

Upgradeability

Option markets are hard to predict — indeed, the future is hard to predict. For this reason, the Premia contracts, including the liquidity pools, are fully upgradeable. New third-party integrations, gas optimizations resulting from new EVM features, and other unforeseen opportunities are all on the table. The upgrade process will be subject to extensive audit and approval by the Premia DAO (more on that later).

The SolidState Development Library

It’s standard (and best) practice in software development to make use of tested, peer-reviewed, third-party development libraries whenever possible. Existing options didn’t offer much support for upgradeable contracts, however, so the Premia development team built its own — the SolidState open-source library. A few fundamental design principals were adopted throughout, most notably testability and upgradeability.

Comprehensive testing is particularly important in the world of DeFi, where a single erroneous line of code can cost users millions of dollars. However, development speed is equally important, and developers are known to cut corners in order to keep up with evolving market conditions. SolidState helps to close this gap, by providing portable specifications — test suites that can be imported and run against custom implementations of contract standards, ensuring compliance with those standards.

Upgradeability is a newer trend in the Ethereum ecosystem, and not without its controversies, but it is our belief that widespread DeFi interoperability will not scale without it. With SolidState, developers can decide what’s best for their projects on a case-by-case basis while only ever integrating a single library. We achieved this through the use of the “Diamond Storage” pattern, which is described in EIP-2535.

SolidState is live on GitHub, and a pre-release version is available on NPM. The code has been audited, and no known security issues are outstanding.

The Premia Contract Suite

With the pieces in place, let’s turn our attention to our primary focus: the Premia core contracts.

Premia contract architecture diagram
Premia contract architecture overview

Premia Core Diamond

Naturally, a Diamond Proxy forms the foundation of the Premia platform. It uses only one facet for now, the ProxyManager, which handles the deployment of new option pool proxies. The Diamond itself includes an implementation of an Ownable contract — ownership of the Diamond constitutes ownership of the protocol. For now, this ownership role belongs to the Premia Multisig; however, the stage is set for the formation of an on-chain DAO and the transition to full decentralization. More on this in a future article.

Premia Pool Diamond

Premia’s pools, where most of the platform’s complexity lies, also utilize a Diamond Proxy. It’s a bit more elaborate than the core Diamond, so let’s go through the facets one-by-one.

PoolBase includes the basic pool functions, particularly those required for compliance with the ERC1155 standard.

PoolExercise allows holders to exercise their options before or after maturity, and is also used to process expired options in batches, freeing up liquidity for future underwriting.

PoolSettings gives the Premia team the ability to fine-tune several parameters of the system, to ensure uninterrupted smooth operation.

PoolView is used to read pool state. This is useful for integrations by third-party contracts and dapps, as well as by our own GRT subgraph.

PoolIO provides an interface for underwriters to deposit and withdraw liquidity from the pool. Holders of short option tokens who wish to exit can reassign their positions.

PoolWrite defines the most important features of all — the pricing and minting of option tokens. This includes integrations with Sushiswap and Uniswap, enabling users to buy options with any input currency.

PoolInternal and PoolSwap are used internally — they are not facets themselves, but contain code that is inherited by the facets. This might sound like a lot of code duplication, but the Solidity compiler automatically detects which internal functions are left unused, and removes them from the final compiled bytecode. This was instrumental in keeping each of our contracts below the aforementioned size limit of 24 KB.

These contracts work to form the technically elegant system that is the Premia protocol. We believe this contract architecture is the best possible for the future growth and development of the Premia ecosystem, and we are happy to finally get to share all of our hard work with you!

If you’d like to read more about the ins and outs of the protocol, check out our in-depth documentation. You can find our contract code and deployment addresses in our contract repository.

--

--

Premia Finance
Premia Finance

Written by Premia Finance

Decentralized options protocol revolutionizing market-driven pricing and capital efficient returns for all. Trade American style options, earn yield on crypto.

No responses yet