Getting Constructor call issue while testing using OpenZeppelin Test Environment

Hi Guys,

I am trying to test my contracts but getting a constructor call revert issue the call code seems fine to me. here is the test file

const { accounts, contract, web3 } = require('@openzeppelin/test-environment');
const { expectEvent, ether } = require('@openzeppelin/test-helpers');
const timeMachine = require("ganache-time-traveler");

const NewfiAdvisor = contract.fromArtifact('NewfiAdvisor');
const StablePoolProxy = contract.fromArtifact('StablePoolProxy');
const VolatilePoolProxy = contract.fromArtifact('VolatilePoolProxy');
const MockToken = contract.fromArtifact('MockToken');
const IERC20 = contract.fromArtifact("IERC20")


describe('NewfiAdvisor', () => {
    const [ mainAdvisor, secondAdvisor, investor ] = accounts;
    let contract;
    let mockToken;
    let stableProxy;
    let volatileProxy;
    // mimicking mainnet scenario
    const USDC = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
    const usdcInstance = IERC20.at(USDC);
    // Account with mainnet usdc
    const unlockAddress = "0x2a549b4af9ec39b03142da6dc32221fc390b5533";

    beforeEach(async () => {
        // Proxy pools are initialized in the NewfiAdvisor init function.
        stableProxy = await StablePoolProxy.new({ from: mainAdvisor });
        volatileProxy = await VolatilePoolProxy.new({ from: mainAdvisor });
        contract = await NewfiAdvisor.new(stableProxy.address, volatileProxy.address, { from: mainAdvisor });
        mockToken = await MockToken.new({ from: mainAdvisor });

        // Onboard a main advisor
        await contract.initialize(
            'Mock Advisor',
            60,
            40,
          { from: mainAdvisor });

        // Onboard second advisor
        await contract.initialize(
            'Second Advisor',
            50,
            50,
            { from: secondAdvisor }
        )
    });

    describe('advisor', () => {
        it('can get name', async () => {
            const name = await contract.advisorName(mainAdvisor);
            expect(name).toEqual('Mock Advisor');
        });
    });

    describe('investors', () => {
        beforeEach(async () => {
            await mockToken.mintTokens(10000, { from: investor });
            await mockToken.increaseAllowance(contract.address, 10000, { from: investor });
        });

        it('can add stable pool liquidity', async () => {
            const receipt = await contract.invest(
              mockToken.address,
              1000,
              mainAdvisor,
              100,
              0,
              { from: investor },
            );

            expectEvent(receipt, 'Investment', {
                investor: investor,
                _stablecoinAmount: '1000',
                _volatileAmount: '0',
                _advisor: mainAdvisor
            });
        });

        it('can add volatile pool liquidity', async () => {
            const receipt = await contract.invest(
                mockToken.address,
                1000,
                mainAdvisor,
                0,
                100,
                { from: investor },
            );

            expectEvent(receipt, 'Investment', {
                investor: investor,
                _stablecoinAmount: '0',
                _volatileAmount: '1000',
                _advisor: mainAdvisor
            });
        });

        it('can add both volatile and stable pool liquidity', async () => {
            const receipt = await contract.invest(
                mockToken.address,
                1000,
                mainAdvisor,
                60,
                40,
                { from: investor },
            );

            expectEvent(receipt, 'Investment', {
                investor: investor,
                _stablecoinAmount: '600',
                _volatileAmount: '400',
                _advisor: mainAdvisor
            });
        });

        it('can invest eth into advisor', async () => {
            const receipt = await contract.invest(
                mockToken.address,
                0,
                mainAdvisor,
                0,
                100,
                { from: investor, value: ether('25') }
            );

            const volatilePool = await contract.advisorVolatilePool(mainAdvisor);
            const balance = await web3.eth.getBalance(volatilePool);

            expect(balance / (10 ** 18)).toEqual(25);
            expectEvent(receipt, 'Investment', {
                investor: investor,
                _stablecoinAmount: '0',
                _volatileAmount: ether('25'),
                _advisor: mainAdvisor
            });
        });

        it('can invest more into the same advisor', async () => {
            await contract.invest(
                mockToken.address,
                1000,
                mainAdvisor,
                60,
                40,
                { from: investor },
            );
            const receipt = await contract.invest(
                mockToken.address,
                1000,
                mainAdvisor,
                50,
                50,
                { from: investor },
            );
            const  stablePoolLiquidity = await contract.investorStableLiquidity(investor);
            const volatileLiquidity = await contract.investorVolatileLiquidity(investor);

            expect(stablePoolLiquidity.toString()).toEqual('1100');
            expect(volatileLiquidity.toString()).toEqual('900');

            expectEvent(receipt, 'Investment', {
                investor: investor,
                _stablecoinAmount: '500',
                _volatileAmount: '500',
                _advisor: mainAdvisor
            });
        });

        it('can invest into two different advisors', async () => {
            const firstReceipt = await contract.invest(
                mockToken.address,
                1000,
                mainAdvisor,
                60,
                40,
                { from: investor },
            );
            const secondReceipt = await contract.invest(
                mockToken.address,
                1000,
                secondAdvisor,
                50,
                50,
                { from: investor },
            );
            const  stablePoolLiquidity = await contract.investorStableLiquidity(investor);
            const volatileLiquidity = await contract.investorVolatileLiquidity(investor);
            const advisors = await contract.getAdvisors(investor);

            expect(stablePoolLiquidity.toString()).toEqual('1100');
            expect(volatileLiquidity.toString()).toEqual('900');
            expect(advisors).toEqual([mainAdvisor, secondAdvisor]);

            expectEvent(firstReceipt, 'Investment', {
                investor: investor,
                _stablecoinAmount: '600',
                _volatileAmount: '400',
                _advisor: mainAdvisor
            });
            expectEvent(secondReceipt, 'Investment', {
                investor: investor,
                _stablecoinAmount: '500',
                _volatileAmount: '500',
                _advisor: secondAdvisor
            });
        })

        it('can invest the funds into protocol', async () => {
            await usdcInstance.approve(contract.address, 10000000, {from : unlockAddress});
            let receipt = await contract.invest(
                usdcInstance.address,
                1000,
                mainAdvisor,
                0,
                100,
                { from: unlockAddress },
            );

            expectEvent(receipt, 'Investment', {
                investor: unlockAddress,
                _stablecoinAmount: '0',
                _volatileAmount: '1000',
                _advisor: mainAdvisor
            });
            receipt = await contract.protocolInvestment(usdcInstance.address, {from : mainAdvisor})

            expectEvent(receipt, 'ProtocolInvestment', {
                advisor: mainAdvisor,
                mstableShare: '0',
                yearnShare: '1000'
            });
        });

        it('can invest the funds into protocol and unwind the position', async () => {
            await usdcInstance.approve(contract.address, 10000000, {from : unlockAddress});
            let receipt = await contract.invest(
                usdcInstance.address,
                1000,
                mainAdvisor,
                0,
                100,
                { from: unlockAddress },
            );

            expectEvent(receipt, 'Investment', {
                investor: unlockAddress,
                _stablecoinAmount: '0',
                _volatileAmount: '1000',
                _advisor: mainAdvisor
            });
            receipt = await contract.protocolInvestment(usdcInstance.address, {from : mainAdvisor})

            expectEvent(receipt, 'ProtocolInvestment', {
                advisor: mainAdvisor,
                mstableShare: '0',
                yearnShare: '1000'
            });
            // advancing 1 month for yield accural
            await timeMachine.advanceTimeAndBlock(2592000);
            receipt = await contract.unwind(mainAdvisor, usdcInstance.address, {from : unlockAddress})
        });
    });
});

getting an issue on this line -> contract = await NewfiAdvisor.new(stableProxy.address, volatileProxy.address, { from: mainAdvisor });
Let me know what am I doing wrong

1 Like

Hi @viraj124,

OpenZeppelin Test Environment spins up a local ganache instance.

For testing with fork, see the following: How to use mainnet forking in solidity tests

For simulating the passing of time with OpenZeppelin Test Environment you can use OpenZeppelin Test Helpers: https://docs.openzeppelin.com/test-helpers/0.5/api#time rather than ganache-time-traveler.

I don’t see anything else obvious in your test. Are you able to share a snippet of the contract that you are trying to test so I can try to reproduce?