# How to deploy a Proxy Upgradeable Contract from a contract?

**URL:** <https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991>\
**Category:** Upgrades\
**Created:** [July 24, 2021, 5:05pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991 "2021-07-24T17:05:34Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [July 24, 2021, 5:05pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/1 "2021-07-24T17:05:34Z")

</div>

Greetings.

I would like to understand how I can upgrade a contract that was deployed from another contract.

```auto
 vault.logic = new CompoundVault(); <--- stores implementation address of upgradeable
        vault.logic.initializeIt(
            address(this),
            deployer,
            cToken,
            asset,
            fees,
            feeBase,
            mentissa,
            symbol,
            name
        );

```

I notice that the deployment is returning the implementation address directly.

How would I upgrade this contract from my deployer contract?

If I set myself as the owner of the implementation, would that restrict the proxy?  
If for instance, I find a bug in my contract. and want to upgrade them.  
I would probably need to upgrade the deployer contract with the (fix) So it would deploy fixed version in the future. But I want to upgrade the previously deployed as well.

Right now I only store the address of the implementation, I'm not sure if I can just call initialize again as the owner.

I would like a function that would upgrade the previously deployed contract.

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [July 25, 2021, 11:02pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/2 "2021-07-25T23:02:24Z")

</div>

> [@madeindreams](#):
>
> 1. How would I upgrade this contract from my deployer contract?

I am not sure which proxy do you use, do you use the plugin to deploy a proxy contract?  
For hardhat: [Upgrading smart contracts - OpenZeppelin Docs](https://docs.openzeppelin.com/learn/upgrading-smart-contracts#upgrading-a-contract-via-plugins)  
For Truffle:

```auto
const { deployProxy, upgradeProxy } = require('@openzeppelin/truffle-upgrades');

const Box = artifacts.require('Box');
const BoxV2 = artifacts.require('BoxV2');

module.exports = async function (deployer) {
  const instance = await deployProxy(Box, [], { deployer });
  const upgraded = await upgradeProxy(instance.address, BoxV2, { deployer });
}

```

> [@madeindreams](#):
>
> 1. If I set myself as the owner of the implementation, would that restrict the proxy?

Depends on your proxy contract.

> [@madeindreams](#):
>
> 1. I would probably need to upgrade the deployer contract with the (fix) So it would deploy fixed version in the future. But I want to upgrade the previously deployed as well.

If you find a bug in your implementation contract, you can fix the bug in the new implementation contract, and upgrade your original proxy contract with the new fixed contract.

> [@madeindreams](#):
>
> 1. Right now I only store the address of the implementation, I'm not sure if I can just call initialize again as the owner.

I think the function `initialize()` should only be called once.

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [July 26, 2021, 1:58am UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/3 "2021-07-26T01:58:28Z")

</div>

Hello @Skyge !

I am using  
"@openzeppelin/contracts-upgradeable": "^4.0.0",  
"@openzeppelin/hardhat-upgrades": "^1.6.0",

There is 2 thing I don't get. I deploy my main contract. This contract deploys other contract.

I store the contract in a library (struct). But I don't know what is the address of the proxy.  
I only get the address of my main contract (deployer). This contract, when it deploys another contract, only record the address of the implementation.

I understand how I can upgrade the main one with Hardhat. But not all the deployed contract. Etherscan is also not showing these as Proxy.

Are they all using the same proxy?

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [July 26, 2021, 11:42pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/4 "2021-07-26T23:42:07Z")

</div>

In Solidity, unless you explicit deploy a Proxy contract, your contracts will not be upgradeable. This is why you're only seeing implementation addresses.

If you want to deploy upgradeable contracts, you will have to import a proxy contract from OpenZeppelin Contracts and deploy it using `new` as well, where one of its parameters will be the logic address you get from `new CompoundVault`.

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [July 27, 2021, 12:05am UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/5 "2021-07-27T00:05:36Z")

</div>

Humm ok that is a bit confusing!

First my contract (CompoundVault) is already upgradeable when I use hardhat to deploy.  
But I deploy it from my main contract.

I'm not sure I understand.  
I have to pass the address of CompVault to the proxy? so already deployed? but deployed with the proxy? Which contract i have to deploy first.

Does the proxy take the deployment as the argument? Would it look like

Proxy.initialize(args...)??

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [July 27, 2021, 1:28am UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/6 "2021-07-27T01:28:11Z")

</div>

Have you read this tutorial? I think it can give you some idea.

- [OpenZeppelin Upgrades: Step by Step Tutorial for Hardhat - General / Guides and Tutorials - OpenZeppelin Community](https://forum.openzeppelin.com/t/openzeppelin-upgrades-step-by-step-tutorial-for-hardhat/3580)

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [July 27, 2021, 1:45am UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/7 "2021-07-27T01:45:40Z")

</div>

Yes this is my procedure for all my contract.

But im unable to do it like that because it's not the hardhat plugin that is deploying my contract.  
It's a contract.

```auto
import ".CompoundVault.sol" <---- is upgradeable 
import "./lib/CVault.sol"; <---- struct Lib where I store contract data
 
myConctract is initializable {

    using CVaults for CVaults.CompVault;

    initialize (some args) initializer {
    }

    fuction deployContract(some args){

         CVaults.CompVault storage vault = vaults[vaultCount];
                                                       <---- I guess I need a proxy here 
            vault.logic = new CompoundVault();
            vault.logic.initializeIt(
             someArg ...
          );
    }
}

```

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [July 27, 2021, 6:23am UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/8 "2021-07-27T06:23:05Z")

</div>

I found this!

> <https://github.com/OpenZeppelin/openzeppelin-sdk/blob/master/packages/lib/contracts/upgradeability/ProxyFactory.sol>

and also this

> **[How to build a Contract Factory that Creates Contract Clones](https://medium.com/upstate-interactive/how-to-build-a-contract-factory-that-creates-contract-clones-efcc9619be0b)**
>
> Last year we began working on a DAO for a commercial client comprised of nine smart contracts. We can’t get into the specifics per NDA…

However this contract seems a bit hold. Is there a Proxy Factory for solidity 0.8.0?

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [July 27, 2021, 5:19pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/9 "2021-07-27T17:19:35Z")

</div>

We don't have a ProxyFactory contract for Solidity 0.8 but you can take a look at this one that a user built recently:

> [@Deploying Upgradeable Proxies and Proxy Admin from Factory contract](https://forum.openzeppelin.com/t/deploying-upgradeable-proxies-and-proxy-admin-from-factory-contract/12132/3):
>
> @frangio Here is my initial attempt. Please let me know if its in the right direction. It looks too easy to be true. I created two contracts the UpgradeableToken and the Factory contract which deploys the Tokens. I realized I don’t need a ProxyAdmin for UUPS. One of the doubts I had was which type of “Proxy” should I use for UUPS. I went for ERC1967Proxy not sure if it’s the correct choice. MyTokenUpgradeable // SPDX-License-Identifier: MIT pragma solidity 0.8.4; import "@openzeppelin/cont…

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [July 27, 2021, 5:57pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/10 "2021-07-27T17:57:24Z")

</div>

This look like what I want! Reading trough!

Thank you @frangio And @Skyge

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [July 27, 2021, 6:14pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/11 "2021-07-27T18:14:31Z")

</div>

I forgot to mention, do note that the code I linked is using UUPS proxies. For transparent proxies you will need to use [`TransparentUpgradeableProxy`](https://docs.openzeppelin.com/contracts/4.x/api/proxy#TransparentUpgradeableProxy) rather than `ERC1967Proxy`, and refer to its documentation, particularly the constructor parameters.

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [July 27, 2021, 8:28pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/12 "2021-07-27T20:28:33Z")

</div>

I tried with UUPS

```auto
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.2;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract Vault is Initializable, UUPSUpgradeable, OwnableUpgradeable {
    string public name;
  
    function initialize() initializer public {
      __Ownable_init();
      transferOwnership(msg.sender);
      name = 'VaultV1';
    }

   function _authorizeUpgrade(address newImplementation) internal virtual override onlyOwner {}
   function upgradeTo(address newImplementation) external virtual override {}
}

```

So this vault contract is a proxy contract I assume, and the implementation pass to upgradeTo() do not need to be upgradeable? Do I need to make all subsequent implementation upgrade as UUPS contract as well?

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [July 28, 2021, 1:13pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/13 "2021-07-28T13:13:02Z")

</div>

> [@madeindreams](#):
>
> So this vault contract is a proxy contract I assume

No, your Vault contract by itself is not a proxy contract. This is the "implementation" contract. You would need to deploy an ERC1967Proxy and set its implementation to your Vault contract.

The implementation passed to `upgradeTo` needs to be "upgradeable" in the sense of using `@openzeppelin/contracts-upgradeable`, and it needs to inherit `UUPSUpgradeable` as well.

---

<div class="post-metadata">

**Author:** ![madeindreams](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/madeindreams/32/23461_2.png) [@madeindreams](https://forum.openzeppelin.com/u/madeindreams)\
**Post date:** [July 29, 2021, 3:59am UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/14 "2021-07-29T03:59:25Z")

</div>

Could I have multiple proxy with the same interface then? If i need to upgrade, all the vault will point to that new implementation but each proxy would have is own state? Or should i look into the beacon for that?

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [July 30, 2021, 10:31pm UTC](https://forum.openzeppelin.com/t/how-to-deploy-a-proxy-upgradeable-contract-from-a-contract/12991/15 "2021-07-30T22:31:59Z")

</div>

What do you mean by "interface"? If you want to upgrade all vaults simultaneously, you should use Beacon proxies. If you want the vaults to be upgraded independently, use either transparent or UUPS proxies.
