{"id":667,"date":"2022-03-30T08:19:00","date_gmt":"2022-03-30T13:19:00","guid":{"rendered":"https:\/\/flolio.com\/resources\/?p=667"},"modified":"2022-08-04T06:25:03","modified_gmt":"2022-08-04T11:25:03","slug":"proxy-smart-contract","status":"publish","type":"post","link":"https:\/\/flolio.com\/resources\/proxy-smart-contract","title":{"rendered":"Proxy Smart Contract &#8211; The New Frontier"},"content":{"rendered":"\n<p>Smart contracts are often considered to be a more secure and permanent form of agreement. However, because their source code cannot be changed or updated without causing an issue with the blockchain itself &#8211; which would lead to potential vulnerabilities- there needs to exist some degree of mutability for bug fixations as well as new features and improvements.<\/p>\n\n\n\n<p>The solution? Proxy smart contracts. A proxy is an object that refers to another object. The proxy can be used to simplify complex contracts or to store data more efficiently. Essentially, a proxy contract stores an address of the latest deployed contract and redirects calls to that, currently valid, logic. If contract logic is upgraded, deploying a new smart contract, the creator would just need to update the reference variable in a proxy contract with that new contract address.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69f4e97a0de1c\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69f4e97a0de1c\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Smart_Contracts\" >Smart Contracts<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Types_of_Smart_Contracts\" >Types of Smart Contracts<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Smart_Legal_Contract\" >Smart Legal Contract<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Decentralized_Autonomous_Organizations\" >Decentralized Autonomous Organizations&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Application_Logic_Contract\" >Application Logic Contract<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Why_Upgrade_a_Smart_Contract\" >Why Upgrade a Smart Contract?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Creating_Upgradeable_Contracts\" >Creating Upgradeable Contracts<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Obstacles_while_writing_Upgradeable_Contracts\" >Obstacles while writing Upgradeable Contracts<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Techniques_for_Creating_Upgradeable_Smart_Contracts\" >Techniques for Creating Upgradeable Smart Contracts<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Separate_Logic_Contract\" >Separate Logic Contract<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Master-Slave_Contracts\" >Master-Slave Contracts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Eternal_Storage_Contract\" >Eternal Storage Contract<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Proxy_Contract_Methodology\" >Proxy Contract Methodology<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Delegate_Call\" >Delegate Call<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Proxy_Forwarding\" >Proxy Forwarding<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Proxy_Patterns\" >Proxy Patterns<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Inherited_Storage\" >Inherited Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Eternal_Storage\" >Eternal Storage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#Storage\" >Storage<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/flolio.com\/resources\/proxy-smart-contract\/#To_Summarize\" >To Summarize<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Smart_Contracts\"><\/span>Smart Contracts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Smart contracts are computer codes that automatically execute all or parts of an agreement without the need for a third party. They can be stored on blockchains and executed when certain conditions are met, such as transferring funds from Party A to B<\/p>\n\n\n\n<p>The code itself is replicated across multiple nodes so it benefits greatly with security, permanence &amp; immutability offered by blockchain technology. Once the replication occurs, the code is executed as a new block is added to the chain. By initiating a transaction, both parties are indicating the parameters outlined by the contract have been met. If no transaction is initiated, the code will not take any steps. A smart contract is typically written in one of the specialized programming languages suitable for such computer programs, such as Solidity.<\/p>\n\n\n\n<p>Currently, the input parameters and execution steps for a smart contract have to be specific and objective, meaning, if &#8220;x&#8221; occurs, then execute step &#8220;y&#8221; which are pretty simple tasks. In essence, the smart contract is automatically moving an amount of cryptocurrency from one party&#8217;s wallet to another when certain criteria are met. But, as things change and blockchain becomes widespread causing more assets to become tokenized or go \u201con-chain&#8221;, smart contracts will need to meet the complex demands and handle more sophisticated transactions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Types_of_Smart_Contracts\"><\/span>Types of Smart Contracts<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Smart_Legal_Contract\"><\/span><strong>Smart Legal Contract<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>A smart legal contract is among the most common types of <a href=\"https:\/\/flolio.com\/resources\/smart-contracts\">smart contracts<\/a> in use today. They&#8217;re put into place to hold concerned parties accountable for fulfilling their end agreement, When set up properly, they can be legally enforceable. If one party breaches an obligation then legal action can come action against them which is automatically triggered by the smart contract.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Decentralized_Autonomous_Organizations\"><\/span><strong>Decentralized Autonomous Organizations<\/strong>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>The powers of a decentralized autonomous organization, or DAO, are in its rules. This community exists on the blockchain and can be defined by agreed-upon smart contracts which enforce their set code, subjecting each member&#8217;s activities to it with a task at hand.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Application_Logic_Contract\"><\/span><strong>Application Logic Contract<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>In the world of blockchain technology, ALCs are a key element. Application logic contracts contain application-based code that remains in step with other blockchain contracts and enables communication across different devices with blockchain technology. ALCs work under management by way of their programming language allowing them more flexibility when it comes down to deciding how they want to operate within certain parameters while also giving others options on what type of functionality could take place.<\/p>\n\n\n\n<p>Smart contracts are a powerful new way to bring transparency, efficiency, and precision into transactions that have traditionally lacked these qualities. The need for Smart Contracts comes from their ability to provide visibility in all aspects &#8211; financial or otherwise-of any business deal while also bringing an unprecedented level of safety through consensus mechanisms that enforce terms using blockchain technology; this makes them uniquely suited for industries with high risk, due diligence requirements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Upgrade_a_Smart_Contract\"><\/span>Why Upgrade a Smart Contract?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Smart contracts are immutable and blockchain technology profits highly off this benefit however, not being able to update, change or even withdraw the terms of a transaction can create a few challenges.<\/p>\n\n\n\n<p>The immutable nature of Smart Contracts ensures that they are reliable and secure. However, the ability to upgrade smart contracts allows for utmost protection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Upgradeable_Contracts\"><\/span>Creating Upgradeable Contracts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The basic idea of a proxy is to utilize it for upgrades to smart contracts. Two different contracts will work together to execute the logic. The first is a simple wrapper or &#8220;proxy&#8221; which users interact with directly and is in charge of forwarding transactions from one party on behalf of another- for example, if I send you 1 ETH then your account will be credited accordingly instead after receiving 2 confirmations (which could take hours). However these aren&#8217;t just any old backups; rather than replacing the entire codebase like most updates, only certain sections are swapped out leaving the contracts immutable still since the code itself cannot be changed.<\/p>\n\n\n\n<p>The use of a proxy contract is to delegate calls to another contract (the caller) and have access through that point. To interact with the actual contract, you have to go through the proxy which delegates the call to the target contract.<\/p>\n\n\n\n<p>A proxy pattern is used when you want upgradability for your contracts. This way the proxy contract stays immutable, but you can deploy a new contract behind the proxy contract &#8211; simply change the target address inside the proxy contract.<\/p>\n\n\n\n<p>It can be somewhat of a risk to use a proxy contract since there are no guarantees that the underlying target contract hasn&#8217;t been changed to a malicious one. There is no strict definition on how to detect a proxy contract, but it&#8217;s anything that delegates the functionality to another contract. You have to analyze the source code to decide.<\/p>\n\n\n\n<p><br>Smart contracts have evolved into being more than just basic contracts. Now we have whole ecosystems powered by Smart Contracts! No matter how careful we are or how well tested our code is, if we are creating a complex system, there is a good chance that we will need to update the logic to patch a bug, fix an exploit or add a necessary missing feature. Sometimes, we may even need to upgrade our smart contracts due to changes in EVM or newly found vulnerabilities.<\/p>\n\n\n\n<p>Generally, developers can easily upgrade their software but blockchains are different as they are immutable. If we deploy a contract then it is out there with turning back no longer an option. However, if we use proper techniques, we can deploy a new contract at a different address and render the old contract useless. Following are some of the most common techniques for creating upgradable smart contracts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Obstacles_while_writing_Upgradeable_Contracts\"><\/span><strong>Obstacles while writing Upgradeable Contracts<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>An upgradeable contract will have immense complexities that need to be considered during its development, even if it is just a basic upgrade.<\/p>\n\n\n\n<p>For instance, the creation and deployment of the new version with modified code will require developers not only to create an upgraded version but also to migrate all imperative states over from the old contract. Handling Unpreserved Contract Address by deploying a new contract will completely change the address of the contract that was previously used for contract interaction.<\/p>\n\n\n\n<p>Because this procedure lacks effectiveness, the community has taken notice of other approaches to develop upgradeable smart contracts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Techniques_for_Creating_Upgradeable_Smart_Contracts\"><\/span>Techniques for Creating Upgradeable Smart Contracts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Separate_Logic_Contract\"><\/span>Separate Logic Contract<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>This involves separating the smart contract into a data contract which contains the data (variables, structures, mappings, etc) with appropriate getters and setters, and a logic contract which contains all of the business logic of how to update this data. The logic contract updates the data through the setters and the data contract only allows the logic contract to call the setters. This allows the logic to be replaced while keeping the data in the same place, allowing for a fully upgradeable system.<\/p>\n\n\n\n<p>The contract can be updated by pointing users to use the new logic contract (through a resolver such as ENS) and updating the data contract permissions to allow the new logic contract to be able to execute the setters.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Master-Slave_Contracts\"><\/span>Master-Slave Contracts<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Master-Slave technique is one of the most basic and easy-to-understand techniques for making smart contracts upgradeable. In this technique, a master contract is deployed along with all of the other contracts. The master contract stores the addresses of all other contracts and returns the required address whenever needed. The contracts fetch the latest address of other contracts from the master whenever they need to communicate with other contracts. To upgrade a smart contract, it is simply deployed onto the network and the address in the master contract is changed.<\/p>\n\n\n\n<p>This isn&#8217;t the best method to develop upgradable contracts, but it is the simplest. One of the many limitations of this method is that the data or assets can&#8217;t complete the migration of the contract to a new contract easily.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Eternal_Storage_Contract\"><\/span>Eternal Storage Contract<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Eternal Storage without proxy causes loss of data in deployed contracts. To fix this issue, one would have to separate logic from the storage contract.<\/p>\n\n\n\n<p>In the Eternal Storage pattern, move the storage with setters and getters to a separate smart contract and let only read\/write the logic smart contract from it.<\/p>\n\n\n\n<p>Using a voting contract as an example to call vote() and increase a number;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Deploy the Eternal Storage; this contract remains a constant and isn&#8217;t changed at all.<\/li><li>Then deploy the Ballot Smart Contract, which will take the library and the Ballot Contract to do the actual logic.<\/li><li>Under the hood, a library does a delegate call, which executes the library&#8217;s code in the context of the Ballot Smart Contract. If you were to use msg. the sender in the library, then it has the same value as in the Ballot Smart Contract itself.<\/li><\/ul>\n\n\n\n<p>If a bug was present allowing everyone to vote as many times as they wanted, you would fix it and re-deploy <em>only the Ballot<\/em> Smart Contract (neglecting that the old version still runs and that there is no way to stop it without extra code). In this case, only the library changed, the storage is the same as before.<\/p>\n\n\n\n<p>To deploy the update, re-deploy the &#8220;Ballot&#8221; Smart Contract and give it the address of the Storage Contract.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Proxy_Contract_Methodology\"><\/span><strong>Proxy Contract Methodology<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>By making the eternal storage contracts act as a proxy to our logic contract, we can prevent having extra gas spent on unnecessary transactions as only one delegate call is needed no matter how many changes are made in the data.<\/p>\n\n\n\n<p>The two will inherit each other\u2019s values so that both are aligned in essence \u2013 this way nothing needs changing when it comes down to deciding who gets modified.<\/p>\n\n\n\n<p>With one fallback function in the proxy contract, shared by all three parties involved (the EVM+two others), there won&#8217;t be much work left unknown about how these smart contracts operate<\/p>\n\n\n\n<p>There are three components of this technique:<\/p>\n\n\n\n<p><strong>Proxy contract<\/strong>: It will act as eternal storage and delegate call the logic contract.<\/p>\n\n\n\n<p><strong>Logic contract<\/strong>: It will do all the processing of the data.<\/p>\n\n\n\n<p><strong>Storage structure<\/strong>: It contains the storage structure and is inherited by both proxy and logic contracts so that their storage pointers remain in sync on the blockchain.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Delegate_Call\"><\/span><strong>Delegate Call<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>An incredibly important concept to understand in the proxy contract approach is the delegate call. It is an opcode provided by the EVM that enables us to execute the code at the target contract address but in the context of the calling contract.<\/p>\n\n\n\n<p>Since the logic contract is separated, the logic can be changed while keeping the proxy contract the same for the user.<\/p>\n\n\n\n<p>Thus, the proxy contract in this procedure acts as immutable storage while the logic contract will include all the functionalities.<\/p>\n\n\n\n<p>To upgrade the logic of the contract, one would simply need to make the proxy contract aware of the address of the new delegate contract. Therefore, whenever a particular function in the proxy contract is invoked, it simply delegate calls the logic contract(which contains the function logic).<\/p>\n\n\n\n<p>Thus, enabling the modified code to be used while interacting with the same old proxy contract with the state and address of the contract completely preserved.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Proxy_Forwarding\"><\/span>Proxy Forwarding<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Proxies are a way to solve the problem of exposing the interface of the logic contract without needing one-to-one mapping. This can be difficult, prone to errors, and makes upgrading interfaces very challenging. That&#8217;s where dynamic forwarding comes into play; it allows you to update certain parts or all depending on what best suits each situation.<\/p>\n\n\n\n<p>The code can be put in the fallback function of a proxy and will forward any call to any function with any set of parameters to the logic contract without it <a href=\"https:\/\/flolio.com\/resources\/best-blockchains-you-need-to-know\">needing to know<\/a> anything in particular of the logic contract\u2019s interface. In essence, (1) the call data is copied to memory, (2) the call is forwarded to the logic contract, (3) the return data from the call to the logic contract is retrieved, and (4) the returned data is forwarded back to the caller.<\/p>\n\n\n\n<p>The EVM&#8217;s delegate call opcode allows for a proxy to be executed in the context of its caller. The logic contract controls this new state and therefore cannot know anything about what is happening outside of it, but by executing through these proxies we can still have some control over our original implementation while being able to execute transactions directly on top of an addressable interface which has many utilities not present before such as tracing or viewing how much gas was spent when submitting orders.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Proxy_Patterns\"><\/span>Proxy Patterns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Shared storage is a high concern in proxy contracts. When discussing storage in a smart contract, solidity will be the term most utilized as its memory is comparable to that of a computer&#8217;s RAM. Solidity is an object-oriented, high-level language for implementing smart contracts.<\/p>\n\n\n\n<p>During execution, memory for the solidity smart contract is unlimited but once the execution is complete, the memory is wiped clean for the next execution. However, storage is persistent throughout executions and previously stored data is readily available.<\/p>\n\n\n\n<p>Storage layout begins at position 0 and increments for each new state variable. So the first state variable is stored at position 0, the second state variable is stored at position 1, and the third is stored at position 2, etc. Each struct or array element uses the next storage position as if each one was defined separately on its own.<\/p>\n\n\n\n<p><strong>*A proxy contract and its delegate\/logic contract share the same storage layout.<\/strong><\/p>\n\n\n\n<p>The three main proxy patterns each attempt to answer the same question: how to ensure that the logic contract does not overwrite state variables that are used in the proxy for upgradeability.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Inherited Storage<\/li><li>Eternal Storage<\/li><li>Unstructured Storage<\/li><\/ol>\n\n\n\n<p>If a Proxy contract has state variables to keep track of the latest logic contract address at some storage slots, and neither party knows about it then there&#8217;s potential for error. The proxy may store other data in these same spots which could overwrite critical information related directly to their function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Inherited_Storage\"><\/span><strong>Inherited Storage<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The idea behind inherited storage is to start with one storage structure and each version will follow the previous one. Each new version of the upgrade cannot change the storage structure of the previous implementations but can add new state variables on top of that storage inheriting it.<\/p>\n\n\n\n<p>Both the proxy and the logic contract inherit the same storage structure to ensure that both adhere to storing the necessary proxy state variables.<\/p>\n\n\n\n<p>A few drawbacks to this would be a new version would need to inherit storage contracts that may contain many state variables not being used as well as becoming closely coupled to specific proxy contracts keeping them from being used by other proxy contracts that declare state variables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Eternal_Storage\"><\/span><strong>Eternal Storage<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The goal of this approach is to have the same generic, immutable storage structure for any contract. This is a set of solidity mappings for each type variable and cannot be changed.<\/p>\n\n\n\n<p>All versions of the logic contract must always use the <em>eternal<\/em> storage structure.<\/p>\n\n\n\n<p>Its issues are a clumsy syntax for state variables, working directly for simple values and arrays but not working in a generic framework for mapping and values. Also, state variables are not declared together anywhere so they wouldn&#8217;t be easily recognizable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Storage\"><\/span><strong>Storage<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The Unstructured Storage pattern is similar to Inherited Storage but doesn\u2019t require the logic contract to inherit any state variables associated with upgradeability. This pattern uses an unstructured storage slot defined in the proxy contract to save the data required for upgradeability.<\/p>\n\n\n\n<p>Instead of storing the implementation address at the proxy\u2019s first storage slot, the unstructured storage pattern chooses a pseudo-random slot instead. This principle is the same as for any other variable that a proxy contract may have.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"To_Summarize\"><\/span>To Summarize<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Proxies are a valuable tool for smart contracts. They can help to reduce transaction costs by reducing the number of transactions that need to be made by storing data locally.<\/p>\n\n\n\n<p>They simplify complex contracts by eliminating the need to repeat different parts of a contract since data is stored in a proxy object. This can make a contract easier to understand and less likely for errors.<\/p>\n\n\n\n<p>Finally, proxies can be used to improve the efficiency of <a href=\"https:\/\/flolio.com\/resources\/decentralized-storage\">data storage<\/a>. By storing data in a proxy object, you can reduce the amount of storage that is required by your contract. This can be helpful in situations where storage is limited, or when data needs to be accessed frequently.<\/p>\n\n\n\n<p>All in all, being familiar with proxies is important to understand why and how to upgrade your contract. Although this concept is still fairly new, the possibilities of change make it worth all the effort.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Smart contracts are often considered to be a more secure and permanent form of agreement. The proxy can be used to simplify complex contracts or to store data more efficiently. <\/p>\n","protected":false},"author":4,"featured_media":669,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"Proxy Smart Contract - The New Frontier | FLOLiO","_seopress_titles_desc":"Smart contracts are often considered to be a more secure and permanent form of agreement. The proxy can be used to simplify complex contracts or to store data more efficiently. ","_seopress_robots_index":"","_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","footnotes":""},"categories":[4],"tags":[],"class_list":{"0":"post-667","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-learn"},"taxonomy_info":{"category":[{"value":4,"label":"Learn"}]},"featured_image_src_large":["https:\/\/flolio.com\/resources\/wp-content\/uploads\/proxy-smart-contract-1024x683.png",770,514,true],"author_info":{"display_name":"FLOLiO","author_link":"https:\/\/flolio.com\/resources\/author\/flolio"},"comment_info":437,"category_info":[{"term_id":4,"name":"Learn","slug":"learn","term_group":0,"term_taxonomy_id":4,"taxonomy":"category","description":"Discover the latest articles about Learn published on FLOLiO","parent":0,"count":178,"filter":"raw","cat_ID":4,"category_count":178,"category_description":"Discover the latest articles about Learn published on FLOLiO","cat_name":"Learn","category_nicename":"learn","category_parent":0}],"tag_info":false,"_links":{"self":[{"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/posts\/667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/comments?post=667"}],"version-history":[{"count":0,"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/posts\/667\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/media\/669"}],"wp:attachment":[{"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/media?parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/categories?post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flolio.com\/resources\/wp-json\/wp\/v2\/tags?post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}