0

openzeppelin-test-environment is quite a new package, it doesn't tell how to initialize the upgradeable contract in the information below:

https://github.com/OpenZeppelin/openzeppelin-test-environment

Just a basic example from the link above:

const { accounts, contract } = require('@openzeppelin/test-environment');
const [ owner ] = accounts;

const { expect } = require('chai');

const MyContract = contract.fromArtifact('MyContract'); // Loads a compiled contract

describe('MyContract', function () {
  it('deployer is owner', async function () {
    const myContract = await MyContract.new({ from: owner });
    expect(await myContract.owner()).to.equal(owner);
  });
});

How to add an upgradeable contract which doesn't have constructor but initialize method?

2 Answers 2

0

What about using deployProxy?

// migrations/NN_deploy_upgradeable_box.js
const { deployProxy } = require('@openzeppelin/truffle-upgrades');

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

module.exports = async function (deployer) {
  const instance = await deployProxy(Box, [42], { deployer });
  console.log('Deployed', instance.address);
};
0

To initialize the contract you have to deploy it as stated in the OpenZeppelin-upgrades official doc.

If you're using hardhat you should have :

const { accounts, contract } = require('@openzeppelin/test-environment');
const [ owner ] = accounts;
const { expect } = require('chai');

const { ethers, upgrades } = require("hardhat");

describe('MyContract', function () {
  it('deployer is owner', async function () {
    const myContract = await ethers.getContractFactory('MyContract');
    const mycontract = await upgrades.deployProxy(myContract, [{ from: owner}]);

    expect(await mycontract.owner()).to.equal(owner);
  });
});

If you're using Truffle you should have :

const { accounts, contract } = require('@openzeppelin/test-environment');
const [ owner ] = accounts;
const { expect } = require('chai');

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

describe('MyContract', function () {
  it('deployer is owner', async function () {
    const myContract = artifacts.require('MyContract');
    const mycontract = await deployProxy(myContract, [{ from: owner}]);

    expect(await mycontract.owner()).to.equal(owner);
  });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.