Staking Protocol Architecture
If we abstract from the simplified representation of the Staking Protocol provided in the About Staking Protocol topic, we must define an additional component called the diamond proxy smart contract, which plays a significant role in managing the staking platform.
As mentioned previously, facets (smart contracts) are based on the EIP-2535 standard and contain the necessary business logic for constructing custom staking platforms. These facets have already been deployed to the Ethereum blockchain by Nexera. Hence, all new staking platforms simply utilize the existing functionality of the facets to run staking campaigns.
The only component that is deployed by an administrator is the diamond smart contract, which acts as a proxy between users and the selected facets. The diamond proxy contract contains storage layouts and a list of functions, which can be invoked on the facets using the delegate function call. When a user wants to interact with the platform, they must call a function on the diamond smart contract, which then makes a delegate call to the specific facet.
When the delegate call is succesfully invoked, the result of that operation changes the state of the diamond proxy smart contract and the storage gets updated. Therefore, facets only provide functions and the storage layouts, but the diamond proxy stores the state associated with staking campaigns and also a list of target functions.
A list of target functions in the diamond proxy contract represents a mapping of function selectors to facet addresses that must be called by a user to interact with the staking platform. The diamond proxy is designed to dynamically route the function invocation, via a delegate call, to the appropriate facet based on the function signature. Therefore, the proxy contract heavily relies on the fallback function, which acts in the following manner:
- When a user calls a facet function that the diamond proxy in fact doesn't have, an error occurs, and the fallback function is triggered.
- The fallback function is written in a way that allows it to read the signature of the facet function that was called.
- The program searches for the signature in the mapping of function selectors to facet addresses.
- If it finds the signature name in the list, it makes a delegate call, the facet function is executed, and the state of the diamond proxy gets updated.