1
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts-upgradeable/token/ERC1155/ERC1155Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC1155/extensions/ERC1155BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "hardhat/console.sol";

contract MyToken is
    Initializable,
    ERC1155Upgradeable,
    OwnableUpgradeable,
    PausableUpgradeable,
    ERC1155BurnableUpgradeable,
    UUPSUpgradeable
{
    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() {
        _disableInitializers();
    }

    string public _typeOfSC;
    string public _role;

    function initialize(string memory typeOfSC_, string memory role_)
        public 
        initializer       
    {
        console.log("log 1");
        __ERC1155_init("");
        __Ownable_init();
        __Pausable_init();
        __ERC1155Burnable_init();
        __UUPSUpgradeable_init();
        _typeOfSC = typeOfSC_;
        console.log("_typeOfSC : ");
        console.log(_typeOfSC);
        console.log("typeOfSC_ : ");
        console.log(typeOfSC_);
        _role = role_;
        setType(typeOfSC_);
    }

    function setType(string memory newuri) public {
        _typeOfSC = newuri;
    }
    function typeOfSC() public view virtual  returns (string memory) {
        console.log("_typeOfSC");
        console.log(_typeOfSC);
        return _typeOfSC;
    }

    function role() public view virtual  returns (string memory) {
        return _role;
    }

    function setURI(string memory newuri) public onlyOwner {
        _setURI(newuri);
    }

    function pause() public onlyOwner {
        _pause();
    }

    function unpause() public onlyOwner {
        _unpause();
    }

    function mint(
        address account,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) public onlyOwner {
        _mint(account, id, amount, data);
    }

    function mintBatch(
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) public onlyOwner {
        _mintBatch(to, ids, amounts, data);
    }

    function _beforeTokenTransfer(
        address operator,
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) internal override whenNotPaused {
        super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
    }

    function _authorizeUpgrade(address newImplementation)
        internal
        override
        onlyOwner
    {}
}


Using this smart contract I want to initialize two state variable, but when I try to get this two variable after smart contract deployment, their value is empty.

2 Answers 2

0

So, the way the proxy contracts work is that you need to deploy two instances of contracts: one for the implementation (your contract) and the other for the ERC1967 proxy contract.

To interact with your implementation contract DO NOT use the instance of your contract. Instead, you should use the ERC1967 Proxy contract. The proxy will have all the functions of your implementation and will route to the current version of the implementation.


Previous answer:

The following code is working, but it's a simplified version of what you're trying to do:

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.7;

import "hardhat/console.sol";

contract MyContract {
    bool isInitialized = false;

    string public x;

    
    function initialize(string calldata _x) public {
        require(!isInitialized, 'Contract already initialized');
        isInitialized = true;

        x = _x;
    }

    function setX(string calldata _x) public {
        x = _x;
    }

    function getX() public view returns (string memory) {
        console.log("x");
        console.log(x);
        return x;
    }
}

Maybe you can provide the complete code of the smart contract for better answers =)

1

The was just because we should use the ERC1967 Proxy implementation to interact with smart contract functions that will have all the functions of the Token smart contractinstead of using the Token contract implementation to interact.

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.