Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Multiple Initializers in Upgradable Smart Contract?
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require("@nomiclabs/hardhat-waffle");
require('@openzeppelin/hardhat-upgrades');
module.exports = {
defaultNetwork: "localhost",
networks: {
localhost: {
url: "http://127.0.0.1:8545"
},
},
solidity: {
compilers: [
{
version: "0.8.0",
settings: {
optimizer: {
enabled: true,
runs: 1000,
},
},
},
{
version: "0.8.3",
settings: {
optimizer: {
enabled: true,
runs: 1000,
},
},
},
],
},
paths: {
sources: "./contracts",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
},
mocha: {
timeout: 20000
}
};
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
contract MultiIntializers is Initializable {
//Constant not subject to upgrade logic
string public constant _aConstant =
"John 3 vs 16(KJV): For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.";
// Subject to upgrade logic
string public _FirstintializerValue;
uint256 public _SecondintializerValue;
//The first Initializer
function initializeFirst() public initializer {
_FirstintializerValue = "xyz";
}
//The second Initializer
function initializeSecond() public initializer {
_SecondintializerValue = 777;
}
function getConstantValue() public pure returns (string memory) {
return _aConstant;
}
function getFirstValue() public view returns (string memory) {
return _FirstintializerValue;
}
function getSecondValue() public view returns (uint256) {
return _SecondintializerValue;
}
}
{
"name": "hardhat-project",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@openzeppelin/hardhat-upgrades": "^1.9.0",
"chai": "^4.3.4",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.4.4",
"hardhat": "2.6.0"
},
"dependencies": {
"@openzeppelin/contracts-upgradeable": "^4.2.0"
}
}
const { expect } = require("chai");
//Check package.json to see for what current versions this behaviour is present
describe("MultiIntializers (Proxy)", function () {
beforeEach(async function () {
MultiIntializers = await ethers.getContractFactory("MultiIntializers");
//Using this syntax one would expect both intializers to initialize
multiintializers = await upgrades.deployProxy(MultiIntializers, { initializer: 'initializeFirst()', initializer: 'initializeSecond()' });
});
//This test will pass
it("returns the value for the constant", async function () {
await multiintializers.getConstantValue();
expect(await multiintializers.getConstantValue()).to.equal("John 3 vs 16(KJV): For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life.");
});
/***
This test will fail when setting { initializer: 'initializeFirst()', initializer: 'initializeSecond()' });
AND
This test will pass when setting { initializer: 'initializeSecond()', initializer: 'initializeFirst()' });
***/
it("returns the value for the first initialzier", async function () {
await multiintializers.getFirstValue();
expect(await multiintializers.getFirstValue()).to.equal("xyz");
});
/***
This test will pass when setting { initializer: 'initializeFirst()', initializer: 'initializeSecond()' });
AND
This test will fail when setting { initializer: 'initializeSecond()', initializer: 'initializeFirst()' });
***/
it("returns the value for the second initializer", async function () {
await multiintializers.getSecondValue();
expect(await multiintializers.getSecondValue()).to.equal(777);
});
//Given that - for this current version - the test for the intializer specified fails with the default compiler value for the specified type
//i.e string sets to '' - uint256 sets to 0
//.viz the value specified in the code was not set and thus it follows to say that the initializer was not initialized
//Therefore - how do you use multiple-initializers-in-upgradable-smart-contract?
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment