# Proxy Contracts Inheritance Hierarchy

**URL:** <https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174>\
**Category:** Upgrades\
**Created:** [October 18, 2020, 1:23pm UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174 "2020-10-18T13:23:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjd9s](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cjd9s](https://forum.openzeppelin.com/u/cjd9s)\
**Post date:** [October 18, 2020, 1:23pm UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/1 "2020-10-18T13:23:23Z")

</div>

Hi everyone,

I trust everyone is well.

I’ve been studying the proxy contracts to get a better understanding of how they work. I’ve come across a few things that I don’t understand along the way. I’ve listed some questions below.

- The BaseAdminUpgradeabilityProxy contract inherits the BaseUpgradeabilityProxy contract, but only imports the UpgradeabilityProxy.sol file. Why does this work? Does this work because the UpgradeabilityProxy contract imports and inherits the BaseUpgradeabilityProxy? Why wouldn’t you just import the BaseUpgradeabilityProxy contract into the BaseAdminUpgradeabilityProxy contract?

![BaseAdminUpgradeabilityProxy Inheritance Issue](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/a/a78690be4fa526afebe77d65288cc21159632923.png)

- Similarly, the AdminUpgradeabilityProxy contract inherits the UpgradeabilityProxy contract and BaseAdminUpgradeabilityProxy contract, but only imports the BaseAdminUpgradeabilityProxy.sol file. Does it have access to UpgradeabilityProxy.sol through the BaseAdminUpgradeabilityProxy contract?

 ![AdminUpgradeabilityProxy Inheritance Issue](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/6/6c5f30672abaed3315314ebfc283bcb4ee92492e.png)

- There is a \_willFallback() function in the BaseAdminUpgradeabilityProxy contract. In that function, it invokes super.\_willFallback(). I thought that super only accessed functions from the immediate parent contract level, but the \_willFallback() function is found in the Proxy contract which is two levels above. If you use super, does it just keep going up levels until it finds the referenced function? Is there a limit to how my inheritance levels it will go up?

 ![Proxy](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/2X/9/98f42b41e753bae6346da665bf87cc1eb9139bf0.jpeg)

The rectangles mean inherited contracts and the ovals mean imported, but not inherited.

I would appreciate any advice or comments. Cheers.

Sincerely,  
Craig

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [October 19, 2020, 9:23am UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/2 "2020-10-19T09:23:50Z")

</div>

Hi @cjd9s,

You may want to look at the proxy contracts that were migrated to OpenZeppelin Contracts 3.x which could be simpler to follow the hierarchy:

> <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.2.0/contracts/proxy/TransparentUpgradeableProxy.sol>

Currently the OpenZeppelin Upgrades Plugins use contracts in Upgrades Core (though this will change to use the proxy contracts in OpenZeppelin Contracts). So recommend studying these versions as this is what you would use when deploying an upgradeable project today with the Upgrades Plugins.

> <https://github.com/OpenZeppelin/openzeppelin-upgrades/tree/%40openzeppelin/upgrades-core%401.2.0/packages/core/contracts/proxy>
>
> //github.com/OpenZeppelin/openzeppelin-upgrades/tree/%40openzeppelin/upgrades-core%401.2.0/packages/core/contracts/proxy

The proxy contracts used in the OpenZeppelin CLI, as you found, have a more complex hierarchy.  
Please see: [Building for interoperability: why we’re focusing on Upgrades Plugins](https://forum.openzeppelin.com/t/building-for-interoperability-why-we-re-focusing-on-upgrades-plugins/4088) as we are focusing our upgradeability efforts on the Upgrades Plugins exclusively.  
[https://github.com/OpenZeppelin/openzeppelin-sdk/tree/v2.8.2/packages/lib/contracts/upgradeability](https://github.com/OpenZeppelin/openzeppelin-sdk/tree/v2.8.2/packages/lib/contracts/upgradeability)

* * *

> [@cjd9s](#):
>
> The BaseAdminUpgradeabilityProxy contract inherits the BaseUpgradeabilityProxy contract, but only imports the UpgradeabilityProxy.sol file. Why does this work? Does this work because the UpgradeabilityProxy contract imports and inherits the BaseUpgradeabilityProxy? Why wouldn’t you just import the BaseUpgradeabilityProxy contract into the BaseAdminUpgradeabilityProxy contract?

I assume this works because an import of a parent allows inheriting from a grand parent.  
It would be simpler if the imports matched the inheritance.

> [@cjd9s](#):
>
> Similarly, the AdminUpgradeabilityProxy contract inherits the UpgradeabilityProxy contract and BaseAdminUpgradeabilityProxy contract, but only imports the BaseAdminUpgradeabilityProxy.sol file.

I assume the same applies.

> [@cjd9s](#):
>
> If you use super, does it just keep going up levels until it finds the referenced function? Is there a limit to how my inheritance levels it will go up?

I assume that `super` calls up the inheritance tree: [https://solidity.readthedocs.io/en/latest/contracts.html#inheritance](https://solidity.readthedocs.io/en/latest/contracts.html#inheritance)

I suggest trying it out with some simple contracts to see it for yourself.

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [October 26, 2020, 12:59am UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/3 "2020-10-26T00:59:59Z")

</div>

Hi @cjd9s,

Let me know if you need more information.

---

<div class="post-metadata">

**Author:** ![cjd9s](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cjd9s](https://forum.openzeppelin.com/u/cjd9s)\
**Post date:** [October 26, 2020, 2:57pm UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/4 "2020-10-26T14:57:41Z")

</div>

Hi Andrew @abcoathup,

If you ever stop hearing from me, it’s because I’m dead. I will never not need more information.

I prefer to download and inherit the proxy contracts directly. I have gone through and mostly understand what is going on. It has been interesting to learn Yul. I do have a few questions, however.

1. My previous post had a question about super. I noted that the \_willFallback() function in BaseAdminUpgradeabilityProxy contract invokes super.\_willFallback(). However, there is no \_willFallback() in the immediate parent contract. It’s actually two levels up in the Proxy contract.

As you suggested, I created some simple contracts in Remix. And, lo, even though there was no function in the parent contract, there was one in the grandparent contract and it worked.

However, you copied a link to Solidity docs about inheritance. I noted the following:  
“It is possible to call functions further up in the inheritance hierarchy internally by … using super.functionName() if you want to call the function one level higher up in the flattened inheritance hierarchy (see below).”

So, if I’m reading this right, it says that super can be used to call a function one level higher up in the inheritance hierarchy. However, it seems to work at least two levels up. Am I reading this wrong, do I not understand the specific order in the directed acyclic graph (DAG) of base classes, or is Solidity Docs wrong?

1. In the ProxyFactory contract there is a deployMinimal() function.

// store next available memory address in clone

let clone := mload(0x40)

// Set the bytes of memory from the next available memory address (clone) to clone+32  
// equal to  
// 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000  
// which is the constructor of the clone contract

mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)

// Set the bytes of memory from clone+20 to clone+52 equal to targetBytes  
// which is the address of the implementation (logic) contract

mstore(add(clone, 0x14), targetBytes)

// Set the bytes of memory from clone+40 to clone+72 is set to equal  
// 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000

mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)

So, am I correct that the first store operation is the clone constructor? What is the signifiance of the final (third) store operation?

1. If I want to use the Proxy Factory contract, do I need to inherit the InitializableAdminUpgradeabilityProxy into my own contract and then change the import in the Proxy Contract from InitializableAdminUpgradeabilityProxy to my contract? And then replace the InitializableAdminUpgradeabilityProxy reference to my contract in the constructor function?

As always, any insight you could provide would be great. Cheers.

Sincerely,  
Craig

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [October 27, 2020, 2:09am UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/5 "2020-10-27T02:09:01Z")

</div>

Hi @cjd9s,

> [@cjd9s](#):
>
> I prefer to download and inherit the proxy contracts directly.

If you are deploying from Truffle or Buidler I recommend using the proxy contracts via OpenZeppelin Upgrades Plugins as the plugins will check upgrade safety when deploying and upgrading.

> [@cjd9s](#):
>
> However, you copied a link to Solidity docs about inheritance. I noted the following:  
> “It is possible to call functions further up in the inheritance hierarchy internally by … using super.functionName() if you want to call the function one level higher up in the flattened inheritance hierarchy (see below).”

I assume the documentation needs updating. I suggest creating an issue and if the Solidity team agree, a PR to update the docs: [https://github.com/ethereum/solidity/blob/develop/docs/contracts/inheritance.rst](https://github.com/ethereum/solidity/blob/develop/docs/contracts/inheritance.rst)

> [@cjd9s](#):
>
> So, am I correct that the first store operation is the clone constructor? What is the signifiance of the final (third) store operation?

For information on minimal proxies, I recommend reading:

> **[Deep dive into the Minimal Proxy contract - OpenZeppelin blog](https://blog.openzeppelin.com/deep-dive-into-the-minimal-proxy-contract)**
>
> In this deep dive into low-level EVM code, you will learn how to code a Minimal Proxy (EIP 1167) from scratch, no Solidity involved. Finally, we see how to easily deploy the proxy using OpenZeppelin's SDK.

> [@cjd9s](#):
>
> If I want to use the Proxy Factory contract, do I need to inherit the InitializableAdminUpgradeabilityProxy into my own contract and then change the import in the Proxy Contract from InitializableAdminUpgradeabilityProxy to my contract? And then replace the InitializableAdminUpgradeabilityProxy reference to my contract in the constructor function?

Yes, that sounds right. You could modify the ProxyFactory to meet your use case.

There is an open issue and a PR in OpenZeppelin Contracts to create a factory to deploy upgradeable contracts

> <https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2339>
>
> We should provide functionality to make it simple to create proxy instances at r…untime. Creating a single proxy instance is simple using the proxy contracts provided, but it's worth providing something on top of that that implements a common pattern when the implementation contract needs to be reused. This pattern should be simple and not opinionated.
> 
> The proposed design is a library with a \`ProxyFactory\` "object" that would be used as in the following snippet.
> 
> \`\`\`solidity
> contract MyFactory {
> using Proxies for Proxies.ProxyFactory;
> 
> // This struct contains the address of the implementation that is
> // used for the proxies it creates.
> struct Proxies.ProxyFactory factory;
> 
> constructor() public {
> // Initializes the factory struct by deploying the implementation
> // and storing its address.
> factory = Proxies.newFactory(FooUpgradeable.creationCode);
> }
> 
> // initializerData is the encoded call to the initializer function.
> function deployInstance(address admin, bytes initializerData) public {
> factory.deploy(admin, initializerData);
> }
> }
> \`\`\`
> 
> In this case the factory creates instances of \`TransparentUpgradeableProxy\`, but the same pattern could be offered for other kinds of proxies. We should think about this as it will affect naming.

> <https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2371>
>
> 2339
> \<!-- Keep in mind that new features have a better chance of being merged f…ast if
> they were first discussed and designed with the maintainers. If there is no
> corresponding issue, please consider opening one for discussion first! --\>
> 
> Fixes #2339 
> 
> 
> 
> 
> Implements a Proxy Factory Library names \`Proxies\` which allows to deploy multiple proxy instances when the implementation contract needs to be reused. 
> 
> This library can be utilized from a \`Factory\` contract which should provide the \`creationCode\` for the Implementation contract to deploy, the proxy \`admin\` address and the encoded \`initializerData\`. 
> 
> This PR is in DRAFT but wanted to check if its going in the right direction and what additional functionality we should include.
> 
> Pending tasks:
> \* Mockup Factory Contract
> \* Tests
> \* Docs

---

<div class="post-metadata">

**Author:** ![cjd9s](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cjd9s](https://forum.openzeppelin.com/u/cjd9s)\
**Post date:** [November 2, 2020, 12:14pm UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/6 "2020-11-02T12:14:28Z")

</div>

Hi Andrew @abcoathup,

I appreciate your help.

I submitted an issue in Github referencing the Solidity docs explanation of super. We’ll see what happens. I suspect that it’s probably a misunderstanding on my part in some way.

I appreciate the link to the Minimal Proxy contract blog post. Very helpful. And I learned some things about how the EVM stack works.

I have some more questions, but I’m going to create a new post. Thanks again for your help. Cheers.

Sincerely,  
Craig

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 3, 2020, 10:56pm UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/7 "2020-11-03T22:56:48Z")

</div>

Hi @cjd9s,

Thanks for creating an issue for the docs, as it would be great to get that cleared up.  
Can you share the link to the Solidity docs issue?

Keep the questions coming. Appreciate you creating a new topic for each type of question. 🙏

---

<div class="post-metadata">

**Author:** ![cjd9s](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cjd9s](https://forum.openzeppelin.com/u/cjd9s)\
**Post date:** [November 4, 2020, 11:11am UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/8 "2020-11-04T11:11:29Z")

</div>

Hi Andrew @abcoathup,

The link for the issue that I created is [https://github.com/ethereum/solidity/issues/10131](https://github.com/ethereum/solidity/issues/10131). Cheers.

Sincerely,  
Craig

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 5, 2020, 4:03am UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/9 "2020-11-05T04:03:44Z")

</div>

Hi @cjd9s,

In case you didn’t know, in GitHub you can use Markdown to improve the readability of your post.

It may be worth adding this to your contract examples to make your Issue easier to read

```solidity  
`Code goes here`  
```

You can also add a quote for the line from the documentation.  
\> Quote some text

---

<div class="post-metadata">

**Author:** ![cjd9s](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cjd9s](https://forum.openzeppelin.com/u/cjd9s)\
**Post date:** [November 5, 2020, 11:01am UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/10 "2020-11-05T11:01:07Z")

</div>

Hi Andrew,

I didn’t know that. I have used your suggestions to clean up the issue. Cheers.

Sincerely,  
Craig

---

<div class="post-metadata">

**Author:** ![cjd9s](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cjd9s](https://forum.openzeppelin.com/u/cjd9s)\
**Post date:** [November 5, 2020, 11:43am UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/11 "2020-11-05T11:43:45Z")

</div>

Hi Andrew @abcoathup,

I received a reply on the Solidity docs inheritance issue that I created. As the person points out, because superParent is inheriting from superGrandparent, the setNumber() function is also defined in that the superParent contract. So using super.setNumber() in the superChild contract is accessing the setNumber() function in the superParent contract which is one level above in the inheritance hierarchy. It seems obvious now, but I guess this is how you learn.

The person goes on to note that the keyword `super` is mainly useful when multiple base classes implement the same function type. Cheers.

Sincerely,  
Craig

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 5, 2020, 7:24pm UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/12 "2020-11-05T19:24:21Z")

</div>

> [@cjd9s](#):
>
> I didn’t know that. I have used your suggestions to clean up the issue. Cheers.

It looks so much better. Thank you.

> [@cjd9s](#):
>
> It seems obvious now, but I guess this is how you learn.

Whilst obvious in retrospect I still found it confusing, though haven’t found a succinct way to express it in the documentation.

---

<div class="post-metadata">

**Author:** ![cjd9s](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@cjd9s](https://forum.openzeppelin.com/u/cjd9s)\
**Post date:** [November 8, 2020, 1:31pm UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/13 "2020-11-08T13:31:01Z")

</div>

Hi Andrew @abcoathup,

After mulling over the issue, I became confused again. However, after studying the documentation closely, I think I understand.

I believe that part of my confusion is my misunderstanding of the word ‘level’ in this context. The example given in the section on super in the Github link that you sent me has an inheritance order that goes Owned =\> Destructible =\> B1 =\> B2 =\> Final. ([https://github.com/ethereum/solidity/blob/develop/docs/contracts/inheritance.rst](https://github.com/ethereum/solidity/blob/develop/docs/contracts/inheritance.rst)). Owned is inherited by Destructible which is inherited by both B1 and B2 which are then both inherited by Final.

My previous understanding is that there would be four levels in this inheritance hierarchy (owned, destructible, B1 & B2, and final). However, it appears that super references the next contract in the sequence Owned =\> Destructible =\> B1 =\> B2 =\> Final. So, in a sense, this contract system has five levels.

This is important in cases of multiple inheritance where you need to ensure that the overridden function in both B1 and B2 is called. The reason in the example is that there is different clean up code that needs to be run in B1 AND B2 in advance of a selfdestruct command. If you use super.overriddenFunction() in final it calls the overriddenFunction() in B2. If you use super.overriddenFunction() in B2 it calls the overriddenFunction() in B1. If you use super.overriddenFunction() in B1 it calls the overriddenFunction() in Destructible.

So, super really doesn’t have much affect on contracts unless there is multiple inheritance involved. This is because without multiple inheritance (i.e. a straight line down of inheriting functions) the overridden function will always be in the contract just above the contract in which it has been overridden. And that’s why the example I gave was straightforward to answer.

Essentially, this comes down to understanding what is meant by a level in a ‘flattened’ inheritance hierarchy. Cheers.

Sincerely,  
Craig

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 11, 2020, 3:53am UTC](https://forum.openzeppelin.com/t/proxy-contracts-inheritance-hierarchy/4174/14 "2020-11-11T03:53:30Z")

</div>

Hi @cjd9s,

You could also look at:

> [@Solidity Diamond Inheritance](https://forum.openzeppelin.com/t/solidity-diamond-inheritance/2694):
>
> Not related to [Diamond (Upgrade) Standard](https://github.com/ethereum/EIPs/issues/2535). [Diamonds Are a Girl’s Best Friend](https://www.youtube.com/watch?v=knLd8bfeWtI), but that might not be the case for Solidity developers. Solidity allows [for multiple inheritance](https://solidity.readthedocs.io/en/v0.6.4/contracts.html#multiple-inheritance-and-linearization) and uses [C3 linearization](https://en.wikipedia.org/wiki/C3_linearization) to resolve it. Let’s take a quick dive into C3 linearization and how it works. Its first major appearance was when it was chosen as the default algorithm for method resolution in [Python](https://en.wikipedia.org/wiki/Python_(programming_language)) 2.3, followed by [Raku](https://en.wikipedia.org/wiki/Raku_(programming_language)), [Parrot](https://en.wikipedia.org/wiki/Parrot_virtual_machine), [Solidity](https://en.wikipedia.org/wiki/Solidity), and [PGF/TikZ](https://en.wikipedia.org/wiki/PGF/TikZ)’s Object-Or…
