# Creating upgradeable smart contracts using contract factory

**URL:** <https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295>\
**Category:** SDK\
**Created:** [February 15, 2019, 7:14am UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295 "2019-02-15T07:14:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [February 15, 2019, 7:15am UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/1 "2019-02-15T07:15:00Z")

</div>

Hi, I’m playing around with ZeppelinOS and I’m trying do use it with simple contract factory (using App contract).  
I have MyContract.sol that works as contract factory and creates NewContract.sol

MyContract.sol:

```
pragma solidity ^0.4.24;

import "zos-lib/contracts/Initializable.sol";
import "zos-lib/contracts/application/App.sol";

contract MyContract is Initializable {
App private app;

function initialize(App _app) initializer public {
    app = _app;
}

function createNewContract() public returns(address) {
    return app.create("zos-fact", "NewContract", '0x69E3a4A7Ff6C1AEff4B096E4A6CF4d094D195a5A');
}

}

```

NewContarct.sol:

```
    pragma solidity ^0.4.24;

import "zos-lib/contracts/Initializable.sol";

contract NewContract is Initializable {

    uint256 public x;

    function initialize() initializer public {
        x = 10;
    }

    function setNumber(uint256 _x) public returns (bool) {
        x = _x;
    }

}

```

I have published my project to use App as admin of the proxies.  
When I try to create new upgradeable contract on the fly I got error:

```
truffle(ganache)> myContract.createNewContract()
Error: VM Exception while processing transaction: revert

```

Do you have any ideas what a problem might be? I would be grateful for your advices

---

<div class="post-metadata">

**Author:** ![IvanTheGreatDev](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/ivanthegreatdev/32/6647_2.png) [@IvanTheGreatDev](https://forum.openzeppelin.com/u/IvanTheGreatDev)\
**Post date:** [February 15, 2019, 7:41am UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/2 "2019-02-15T07:41:41Z")

</div>

Hello @paulinablaszk! Thanks again for posting here 🙂

Hmm, I’m not the most informed on ZeppelinOS so maybe @spalladino can help out? We’re at ETHDenver right now so apologies if it takes a moment for him to respond 😃

---

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [February 15, 2019, 7:50am UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/3 "2019-02-15T07:50:39Z")

</div>

Hi @IvanTheGreatDev , thank you, no prob 🙂

---

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [February 15, 2019, 7:57am UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/4 "2019-02-15T07:57:51Z")

</div>

@IvanTheGreatDev Now I see I’ve posted it in the wrong place so I will move it to “Smart contract development”

---

<div class="post-metadata">

**Author:** ![IvanTheGreatDev](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/ivanthegreatdev/32/6647_2.png) [@IvanTheGreatDev](https://forum.openzeppelin.com/u/IvanTheGreatDev)\
**Post date:** [February 15, 2019, 7:58am UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/5 "2019-02-15T07:58:57Z")

</div>

It’s fine here! The issue might be ZeppelinOS specific!

---

<div class="post-metadata">

**Author:** ![spalladino](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/spalladino/32/22_2.png) [@spalladino](https://forum.openzeppelin.com/u/spalladino)\
**Post date:** [February 15, 2019, 2:10pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/6 "2019-02-15T14:10:21Z")

</div>

Hey @paulinablaszk! Have you run `zos add NewContract` and `zos push` on the corresponding network, so the App has registered the NewContract implementation? Does it work fine if you run `zos create NewContract` directly? And if all of the above fails, can you upload your project somewhere, so we can help in troubleshooting it? Thanks!

---

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [February 15, 2019, 5:17pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/7 "2019-02-15T17:17:39Z")

</div>

Hi @spalladino. Thanks for your response. Yes, I’ve added and pushed NewContract. `zos create NewContract` also works fine. I uploaded my project here [https://github.com/paulinablaszk/zos-fact](https://github.com/paulinablaszk/zos-fact)

---

<div class="post-metadata">

**Author:** ![spalladino](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/spalladino/32/22_2.png) [@spalladino](https://forum.openzeppelin.com/u/spalladino)\
**Post date:** [February 16, 2019, 5:57pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/8 "2019-02-16T17:57:57Z")

</div>

Hey @paulinablaszk! The issue that is causing the REVERT is in [https://github.com/paulinablaszk/zos-fact/blob/master/contracts/MyContract.sol#L14](https://github.com/paulinablaszk/zos-fact/blob/master/contracts/MyContract.sol#L14). There, `App#create` is being called with 3 params (package name, contract name, and an admin address), which corresponds to the [App interface in v2.2](https://github.com/zeppelinos/zos/blob/v2.2.0/packages/lib/contracts/application/App.sol#L115). However, in your project you have v2.1 as a dependency, where the [App interface is slightly different](https://github.com/zeppelinos/zos/blob/v2.1.0/packages/lib/contracts/application/App.sol#L140). In 2.1, `create` takes 3 params: package name, contract name, and the `data` that will be forwarded to the contract upon initialization. The admin address you are supplying is actually being parsed as that `data`, is sent to the contract, and thus fails.

**TL;DR** : You should either upgrade to zos and zos-lib v2.2 (recommended) **or** change the call to `create` by removing the last `0x69E3a4A7Ff6C1AEff4B096E4A6CF4d094D195a5A` parameter.

Hope this helps! Cheers!

---

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [February 16, 2019, 9:43pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/9 "2019-02-16T21:43:42Z")

</div>

Thank you very much @spalladino!

---

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [February 18, 2019, 9:25am UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/10 "2019-02-18T09:25:34Z")

</div>

I have one more question connected with this topic. I upgraded my zos and zos-lib to 2.2 but now MyContract.sol does not compile because of the `create` function. I am getting an error:

```
TypeError: Wrong argument count for the function call: 3 arguments given but expected 4.

```

Is it correct behavior when the 4th param `data` is optional?

**Update:**  
I tried to put as `data` first 4 bytes of the Keccak hash of the signature `initialize()` and function `app.create()` has returned some address(?) but `NewContract` wasn’t deployed to the network  
`truffle(ganache)> contract.methods.createNewContract("zos-fact", "NewContract", "0x69E3a4A7Ff6C1AEff4B096E4A6CF4d094D195a5A", "0x8129fc1c").call() '0xD6C3A17f91D873352E882315269c1183278a96Ae'`

Thanks

---

<div class="post-metadata">

**Author:** ![IvanTheGreatDev](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/ivanthegreatdev/32/6647_2.png) [@IvanTheGreatDev](https://forum.openzeppelin.com/u/IvanTheGreatDev)\
**Post date:** [February 18, 2019, 9:21pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/11 "2019-02-18T21:21:50Z")

</div>

Hey Paulina! I’m gonna go ahead and tag @spalladino because he’ll know more to help you 🙂

---

<div class="post-metadata">

**Author:** ![jaureguino](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jaureguino/32/9820_2.png) [@jaureguino](https://forum.openzeppelin.com/u/jaureguino)\
**Post date:** [February 21, 2019, 2:05pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/12 "2019-02-21T14:05:38Z")

</div>

Greetings @spalladino,

The following is the content of my zos.json

```auto
{
  "zosversion": "2",
  "name": "TheFaustFlick",
  "version": "0.1.0",
  "contracts": {},
  "dependencies": {
    "openzeppelin-eth": "^2.1.3"
  }
}

```

Since I already initialized my project with `zos init TheFaustFlick`, could you please point to me the documentation on how do I properly upgrade zos and zos.lib to the latest version?

g.a.

---

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [February 21, 2019, 2:31pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/13 "2019-02-21T14:31:11Z")

</div>

Hi @jaureguino, I’ve used  
`npm install zos@latest`  
and  
`npm install zos-lib@latest`

Or you can choose any other from “Versions” tab here:

> **[zos](https://www.npmjs.com/package/zos)**
>
> Command-line interface for the ZeppelinOS smart contract platform

  

> **[zos-lib](https://www.npmjs.com/package/zos-lib)**
>
> JavaScript library for the ZeppelinOS smart contract platform

---

<div class="post-metadata">

**Author:** ![jaureguino](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jaureguino/32/9820_2.png) [@jaureguino](https://forum.openzeppelin.com/u/jaureguino)\
**Post date:** [February 21, 2019, 2:46pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/14 "2019-02-21T14:46:31Z")

</div>

Thanks a lot @paulinablaszk,

I just used both commands and they worked perfectly. However, the content of my zos.json file remains the same, is that ok? I am new on this, hence my basic questions… 😳

---

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [February 21, 2019, 6:04pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/15 "2019-02-21T18:04:09Z")

</div>

@jaureguino No problem, nice to help 🙂  
In zos.json you have:

> [@jaureguino](#):
>
> "zosversion": "2",

The documentation says: _The field `zosversion` indicates the **major** version of ZeppelinOS you are using_  
So it doesn't change because it can by only "1" or "2" (without detailed versions).

and `"openzeppelin-eth": "^2.1.3"` is OpenZeppelin EVM package - it is not the same as `zos-lib`. You should find info about `zos-lib` version in your package.json file.

---

<div class="post-metadata">

**Author:** ![jaureguino](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jaureguino/32/9820_2.png) [@jaureguino](https://forum.openzeppelin.com/u/jaureguino)\
**Post date:** [February 21, 2019, 7:45pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/16 "2019-02-21T19:45:17Z")

</div>

@paulinablaszk

Thank you so much for making it all clear for me, you are very kind. 👍

Cheers and keep on coding!

g.a.

---

<div class="post-metadata">

**Author:** ![imbenwolf](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/imbenwolf/32/337_2.png) [@imbenwolf](https://forum.openzeppelin.com/u/imbenwolf)\
**Post date:** [March 4, 2019, 5:58pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/17 "2019-03-04T17:58:53Z")

</div>

> [@paulinablaszk](#):
>
> I have published my project to use App as admin of the proxies.

How did you do that? After linking the openzeppelin-eth evm package, my app instance does not register the implementation addresses and thus i cannot use  
`app.create("openzeppelin-eth", "StandaloneERC20", msg.sender, abi.encode(...))`

`app.getImplementation("openzeppelin-eth", "StandaloneERC20")`  
returns '0x0000000000000000000000000000000000000000'

The documentation under [https://docs.zeppelinos.org/docs/writing\_contracts.html#creating-new-instances-from-your-contract-code](https://docs.zeppelinos.org/docs/writing_contracts.html#creating-new-instances-from-your-contract-code) states:

> An advanced alternative, if you need to create upgradeable contracts on the fly, is to keep an instance of your ZeppelinOS `App` in your contracts.

But how? I'm out of ideas ☹

---

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [March 4, 2019, 7:34pm UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/18 "2019-03-04T19:34:01Z")

</div>

Hi @imbenwolf, did you use:  
`zos push --deploy-dependencies`? (_–deploy dependencies_ is needed when you use openzeppelin-eth on your local network)  
and then `zos publish` ?

You can also check new example on zos github: [_Creating instances from solidity_](https://github.com/zeppelinos/zos/tree/master/examples/creating-instances-from-solidity). It was very helpful for me when I was trying to create an upgradeable smart contract using factory

---

<div class="post-metadata">

**Author:** ![imbenwolf](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/imbenwolf/32/337_2.png) [@imbenwolf](https://forum.openzeppelin.com/u/imbenwolf)\
**Post date:** [March 7, 2019, 10:02am UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/19 "2019-03-07T10:02:18Z")

</div>

Hi @paulinablaszk

Thank you!

That was the piece I was missing. The example also helped a lot 😃

---

<div class="post-metadata">

**Author:** ![paulinablaszk](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulinablaszk/32/233_2.png) [@paulinablaszk](https://forum.openzeppelin.com/u/paulinablaszk)\
**Post date:** [March 7, 2019, 11:22am UTC](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295/20 "2019-03-07T11:22:08Z")

</div>

@imbenwolf great 🙂 It was nice to help

[Next page](https://forum.openzeppelin.com/t/creating-upgradeable-smart-contracts-using-contract-factory/295.md?page=2)
