<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: bnabi.eth</title>
    <description>The latest articles on DEV Community by bnabi.eth (@td_bn).</description>
    <link>https://dev.to/td_bn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F732519%2Fc3fa2e97-23d1-452b-9c98-3eb7446e15f9.jpg</url>
      <title>DEV Community: bnabi.eth</title>
      <link>https://dev.to/td_bn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/td_bn"/>
    <language>en</language>
    <item>
      <title>Playing with OpenZeppelin proxy contracts</title>
      <dc:creator>bnabi.eth</dc:creator>
      <pubDate>Tue, 02 Nov 2021 18:10:29 +0000</pubDate>
      <link>https://dev.to/td_bn/playing-with-openzeppelin-proxy-contracts-4khl</link>
      <guid>https://dev.to/td_bn/playing-with-openzeppelin-proxy-contracts-4khl</guid>
      <description>&lt;p&gt;The code in this post is based on the popular OpenZeppelin contract implementation for a proxy pattern. The original code can be found &lt;a href="https://github.com/OpenZeppelin/openzeppelin-contracts/tree/v3.4.0/contracts/proxy" rel="noopener noreferrer"&gt;here&lt;/a&gt;. This post will help to understand a bit more about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;proxy contract implementation&lt;/li&gt;
&lt;li&gt;how to deploy with a proxy pattern&lt;/li&gt;
&lt;li&gt;how to change implementation with proxy patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we begin there are some things that might help to grasp the subject matter better. &lt;a href="https://blog.openzeppelin.com/proxy-patterns/" rel="noopener noreferrer"&gt;This&lt;/a&gt; article is a great starting point. Learning about the &lt;a href="https://medium.com/aigang-network/how-to-read-ethereum-contract-storage-44252c8af925" rel="noopener noreferrer"&gt;layout of storage&lt;/a&gt; is also a good idea. Finally an understanding of &lt;a href="https://medium.com/coinmonks/delegatecall-calling-another-contract-function-in-solidity-b579f804178c" rel="noopener noreferrer"&gt;delegatecall&lt;/a&gt; is essential to understand what is going on. Armed with an idea of these, we can go ahead and try to tackle the implementation itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fez4g6lg1jo0w7dp2pg6l.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fez4g6lg1jo0w7dp2pg6l.gif" alt="war"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;We are going to deploy a simple contract, &lt;code&gt;Logic&lt;/code&gt;, that adds two numbers. Then we are going to upgrade it to &lt;code&gt;UpgradedLogic&lt;/code&gt;, an upgraded implementation that does the same thing, except that it uses the SafeMath library.&lt;/p&gt;

&lt;p&gt;Click &lt;a href="https://remix.ethereum.org/?#gist=2849d9a09aa997989e838e3b269144e9&amp;amp;optimize=false&amp;amp;runs=200&amp;amp;evmVersion=null&amp;amp;version=soljson-v0.7.6+commit.7338295f.js" rel="noopener noreferrer"&gt;here&lt;/a&gt; to open the contracts we'll be using in Remix.&lt;/p&gt;

&lt;p&gt;The code that we will be using is based on &lt;a href="https://gist.github.com/td-bn/2849d9a09aa997989e838e3b269144e9" rel="noopener noreferrer"&gt;this gist&lt;/a&gt;. It is almost a carbon copy of the OpenZeppelin implementation but leaves out a few bits to makes things simpler.&lt;br&gt;
It consists of the following contracts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Proxy, UpgradableProxy, TransparentUpgradableProxy and ProxyAdmin : These are slightly modified versions of OpenZeppelin's implementation of these contracts.&lt;/li&gt;
&lt;li&gt;Logic, UpgradedLogic: Contracts that hold the logic of our application&lt;/li&gt;
&lt;li&gt;Test: Contract that interacts with the proxy. Think of it like a user interacting with our application.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Proxy and UpgradableProxy contracts
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Overview:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fallback&lt;/code&gt; function that calls &lt;code&gt;_fallback()&lt;/code&gt; internally which in turn calls &lt;code&gt;_delegate()&lt;/code&gt;. This is the code that delegates our call to the implementation. &lt;/li&gt;
&lt;li&gt;Proxy is an abstract contract which does not have the implementation logic. That's where UpgradableProxy comes in.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Overview:

&lt;ul&gt;
&lt;li&gt;constructor take in an address which is the contract that houses our logic&lt;/li&gt;
&lt;li&gt;implementation/logic contract address is stored at a random storage location called &lt;code&gt;IMPLEMENTATION_SLOT&lt;/code&gt;. This is done to avoid clashes with default storage slots while managing the proxy. Understanding how storage works in Solidity will help drive this point home. &lt;/li&gt;
&lt;li&gt;methods to read and write to &lt;code&gt;IMPLEMENTATION_SLOT&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;public method to &lt;code&gt;getImplementation()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
This is a simple contract which takes the address of a proxy and makes a low level call to add two numbers. 

&lt;p&gt;At this point we can deploy our basic proxy contract. &lt;/p&gt;

&lt;p&gt;Steps to Deploy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the gist in Remix(Click &lt;a href="https://remix.ethereum.org/?#gist=2849d9a09aa997989e838e3b269144e9&amp;amp;optimize=false&amp;amp;runs=200&amp;amp;evmVersion=null&amp;amp;version=soljson-v0.7.6+commit.7338295f.js" rel="noopener noreferrer"&gt;here&lt;/a&gt; to open these contracts and others we'll be using in Remix, if you haven't already)&lt;/li&gt;
&lt;li&gt;Compile and deploy this &lt;code&gt;Logic.sol&lt;/code&gt; contract. Test it by passing in two integers and verifying that the sum is correct in the Remix console.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu9i4pakxaean7d67mf97.png" alt="Deploy our implementation"&gt;
&lt;/li&gt;
&lt;li&gt;To deploy the UpgradableProxy contract we need the address of our implementation/logic. Copy the address of the Logic contract deployed above and pass it into the constructor.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjuxh6fo3a5u71su64szi.png" alt="deploy Upgradable proxy"&gt;
&lt;/li&gt;
&lt;li&gt;Deploy the &lt;code&gt;Test.sol&lt;/code&gt; contract and pass it the address of the UpgradedProxy we just deployed. 
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fin83w41rz7pxm7pae20l.png" alt="deploy Test contract"&gt;
&lt;/li&gt;
&lt;li&gt;Call the &lt;code&gt;test&lt;/code&gt; function in the contract. It makes a low level call to the UpgradedProxy which in turn calls our Logic contract. If all goes well, you should see 0x2a as the output of the test call.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9h834tdgid3c5pi11zq0.png" alt="verify result"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Wait.. how do we upgrade?
&lt;/h3&gt;

&lt;p&gt;You might have noticed that the &lt;code&gt;_upgradeTo&lt;/code&gt; function is internal, which means that we can't really call is from external contracts or accounts.&lt;/p&gt;

&lt;p&gt;This is where the &lt;code&gt;TransparentUpgradableProxy&lt;/code&gt; comes in which handles the admin logic for us. This contract exposes functions for upgrading implementations, as well as owners, who handle those changes. See the OpenZeppelin contract implementation of TransparentUpgradableProxy for more details. They have some great documentation. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Overview:

&lt;ul&gt;
&lt;li&gt;Admin slot to store the admin who can deploy and upgrade proxies. Again, to avoid collision with default storage.&lt;/li&gt;
&lt;li&gt;Note that it inherits from the UpgradableProxy contract which already has the proxy logic baked in.&lt;/li&gt;
&lt;li&gt;Methods for changing implementation and admin.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Deploying and testing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Follow the steps above to deploy the Logic contract. &lt;/li&gt;
&lt;li&gt;Just like we deployed the Upgradable proxy in the step 3 above, we can deploy &lt;code&gt;TransparentUpgradableProxy&lt;/code&gt; with the address of the implementation(Logic contract) and admin(use Remix account address)&lt;/li&gt;
&lt;li&gt;Deploy the &lt;code&gt;Test.sol&lt;/code&gt; contract and pass it the address of the TransparentUpgradableProxy we just deployed. &lt;/li&gt;
&lt;li&gt;Test that the proxy works. Also verify the event that is triggered.&lt;/li&gt;
&lt;li&gt;Deploy UpgradedLogic and pass its address to the &lt;code&gt;upgradeTo&lt;/code&gt; method of the TransparentUpgradableProxy(with the admin account, of course)&lt;/li&gt;
&lt;li&gt;Use test contract again to verify the result. You should see a AddedSafely event in the console reflecting that the implementation was upgraded in the background, while the address you used to interact with remained the same.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6ocg35endpmpjsmi4pi.png" alt="AddedSafely"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it for this post. I hope you have a better understanding of proxy contracts after reading and playing with the attached Remix gist.&lt;br&gt;
A good exercise to understand these contracts might be to go through the OZ contract implementation and the related comments in the code. One could also learn about ProxyAdmin and the use cases that addresses.&lt;/p&gt;

&lt;p&gt;Might post more about storage and its layout or OpenZeppelin's upgrade plugins in the future, both of which would build on top of this post.    &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
