Can't "initialize" in tests

#1

Consider the following contract and respective test

pragma solidity ^0.5.0;
import "zos-lib/contracts/Initializable.sol";
import "openzeppelin-eth/contracts/ownership/Ownable.sol";
contract Whitelist is Ownable {
    mapping(address => bool) internal governors;
    function initialize(address _owner) public initializer {
        Ownable.initialize(_owner);
        governors[_owner] = true;
    }
    function isGovernor(address _userAddress) public view returns (bool)
    {
        return governors[_userAddress];
    }
}

.

const { TestHelper } = require('zos');
const { Contracts, ZWeb3 } = require('zos-lib');

ZWeb3.initialize(web3.currentProvider);

const Whitelist = Contracts.getFromLocal('Whitelist');

require('chai').should();

contract('Sample', (accounts) => {
    const owner = accounts[0];
    const governor = accounts[2];

    beforeEach(async () => {
        this.project = await TestHelper();
    });

    it('should create a proxy', async () => {
        const proxy = await this.project.createProxy(Whitelist);
        await proxy.methods.initialize(owner).send({ from: owner });
        const result = await proxy.methods.isGovernor(owner).call();
        console.log(result);
    });

    it('should create a proxy', async () => {
        const proxy = await this.project.createProxy(Whitelist);
        const result = await proxy.methods.isGovernor(owner).call();
        console.log(result);
    });
});

Running these tests I get an “Error: Returned error: VM Exception while processing transaction: revert” for the first test. What makes it happen is actually the “_owner = sender;” in “initialize” function in “Ownable”. If I comment that line it works. This is my first question, can anyone help me, please?

My second question is related to the “createProxy” method. If I try to initialize in the create proxy like

const proxy = await this.project.createProxy(Sample, {
  initMethod: 'initialize',
  initArgs: [42]
});

as shown here https://docs.zeppelinos.org/docs/testing.html, it also throws.
Does anyone know the reason?

Thank you so much.

0 Likes

#2

As for your first question, I have tested in the Remix, it looks like everything is in order, I can deploy the contract, and call the function initialize(address).

0 Likes

#3

Thanks, but the test doesn’t work

0 Likes

#4

Hello once again,

Today, following this tutorial https://docs.zeppelinos.org/docs/zos_lib.html I’m having problems at const proxy = await project.createProxy(MyContractV0, { initArgs: [42] }) step where I get a

Creating an upgradeable instance of V0...
Error: Returned error: VM Exception while processing transaction: out of gas
at Object.ErrorResponse (some-path/node_modules/web3-core-helpers/src/errors.js:29:16)
at some-path/node_modules/web3-core-requestmanager/src/index.js:140:36
at some-path/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:112:1
at XMLHttpRequest.request.onreadystatechange (some-path/node_modules/truffle/build/webpack:/~/web3/~/web3-providers-http/src/index.js:
96:1)
at XMLHttpRequestEventTarget.dispatchEvent (some-path/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request-event-t
arget.js:34:1)
at XMLHttpRequest._setReadyState (some-path/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:208:1)
at XMLHttpRequest._onHttpResponseEnd (some-path/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:318:1)
at IncomingMessage.<anonymous> (some-path/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:289:47)
at IncomingMessage.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Truffle v5.0.13 (core: 5.0.13)
Node v10.12.0

Can anybody help? Is it a known problem? Is there any workaround? Ive tried to do something like await proxy.methods.initialize(42) but then the value is zero.

Cheers.

0 Likes

#5

Hello,

quick solution was

const proxy = await project.createProxy(MyContractV0/* , { initMethod: 'initialize', initArgs: ['42'] } */);
await proxy.methods.initialize(42).send({ from: userAddress });
0 Likes