Testing Proxy with Truffle and Solidity 0.5

Hello people,

I’ve managed to setup the OZ Proxy, I have a question now that I am testing it.

In the 1st test, I change the proxy admin from accounts[0] to accounts[1] (the whole test passes.
The 2nd test fails because accounts[0] seems still to be the proxy admin although it is not (as per screenshot).

const Protocol = artifacts.require("Protocol.sol");
const AdminUpgradeabilityProxy = artifacts.require("AdminUpgradeabilityProxy");
const truffleAssert = require("truffle-assertions");

contract("Proxy", accounts => {

  var proxy;
  var proxyControl;
  var protocol;

  before('Setup proxy', async() => {
    await Protocol.new();
    protocol = await Protocol.deployed();
    proxy = await AdminUpgradeabilityProxy.new(protocol.address,accounts[0],'0x0f59f83a'); // 0x0f59f83a is the signature of our function go() > artifact constructor.
    proxyControl = await Protocol.at(AdminUpgradeabilityProxy.address);
  });

  it("Should transfer the proxy ownership to accounts[1]", async ()=> {
    // Becase only the proxy owner can call the function admin() I test the ownership by assering.
    truffleAssert.passes(proxy.admin({from:accounts[0]}));
    truffleAssert.fails(proxy.admin({from:accounts[1]}),truffleAssert.ErrorType.REVERT);
    await proxy.changeAdmin(accounts[1]);
    truffleAssert.passes(proxy.admin({from:accounts[1]}));
  })

  it("Should have triggered the function go() when deployed", async () => {
    let res = await proxyControl.getTestVar({from:accounts[0]});
    console.log("The test var is set to: " + res.toNumber());
  })
})

1 Like

Hi @dani69654,

If you want to deploy upgradeable contracts with Truffle then I recommend using OpenZeppelin Upgrades Plugins: https://docs.openzeppelin.com/upgrades-plugins/1.x/truffle-upgrades

I created a guide walking through the process of testing, deploying, transferring control of upgrades, testing upgrades and upgrading: OpenZeppelin Truffle Upgrades

The plugins use a Proxy Admin, as well as providing an easy to use API. I would recommend trying it out.


The proxy contracts have been moved to OpenZeppelin Contracts (OpenZeppelin Contracts 3.2 - Release Candidate) if you really wanted to deploy directly.


As for your tests, can you check that your go function is actually being called.
Can you add a check for checking the variable?

1 Like

Hey @abcoathup and thanks for your answer.
I am building in Solidity 0.5.17 at the moment and, as far as I understand this is a release for sol 0.7;

I have tested the contract and I do confirm that go() is called (I do have a variable inside that gets the value 5 when go is called).

Still cannot wrap my mind around the admin revert.
In the proxy contract I have created a getter method (view) that returns the admin of the proxy.

  function getAdmin () public view returns (address){
    return _admin();
  }

  it("Should not allow the proxy admin to call this function", async () => {
    let admin = await proxy.getAdmin();
    console.log('calling a fallback function from: ' + admin)
    let res = await proxyControl.getTestVar({from:admin});
    assert.equal(res.toNumber(),5);
  })
1 Like

My bad I was deploying the proxy twice. All fixed :slight_smile:

1 Like

Hi @dani69654,

I hadn’t spotted the proxy being deployed twice. Glad you were able to resolve. Thanks for letting me know.

There isn’t a restriction on the Solidity version of your implementation contracts so you can use OpenZeppelin Upgrades Plugins with Solidity 0.5 projects.

I recommend trying out OpenZeppelin Truffle Upgrades
I wrote the guide using Solidity 0.7 to use the latest version of the compiler. I really like the way deployment and testing works (I am biased :smile:). Feedback is greatly appreciated.

Out of interest why are you using Solidity 0.5?

Hi @dani69654,

Just wanted to follow up to see why you were using Solidity 0.5? Also to see if you needed any more information on using OpenZeppelin Upgrades Plugins, see the documentation for more details: https://docs.openzeppelin.com/upgrades-plugins/

1 Like

Hey @abcoathup

I ended up using OpenZeppelin Truffle Upgrades :slight_smile:
There is not a real reason why I am using 0.5, i guess it’s habit.

Thanks a lot for your help,
Dani

1 Like

Hi @dani69654,

Feedback on OpenZeppelin Upgrades Plugins and the documentation (https://docs.openzeppelin.com/upgrades-plugins/1.x/) is appreciated :pray:

I would suggest using Solidity 0.6 or potentially Solidity 0.7.

OpenZeppelin Truffle Upgrades it is really useful and the whole process of implementing a proxy is smooth.

Thanks for the suggestion again !

1 Like