3

I have a simple contract below Swap.sol that is upgradable.


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

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract Swap is Initializable {

    address public tokenA;
    address public tokenB;

    function initialize(
        address _tokenA, 
        address _tokenB, 
    ) public initializer {
        tokenA = _tokenA;
        tokenB = _tokenB;

    }

 function getTokenA() public view returns (address) {
        return tokenA;
    }

}

And a factory contract that deploys multiple Swap.sol on call of createSwap.

// SPDX-License-Identifier: MIT
pragma solidity >0.4.23 <0.9.0;
import "./Swap.sol";
contract SwapFactory {

    Swap[] private _swaps;

    function createSwap(address _tokenA, address _tokenB) public {
        Swap swap = new Swap();
        swap.initialize(_tokenA, _tokenB);
        _swaps.push(swap);
    }

    function getSwaps() public view returns (Swap[] memory)
    {
        return _swaps;
    }

}

When I deploy SwapFactory and call createSwap it says

Error: VM Exception while processing transaction: reverted with reason string 'Initializable: contract is already initialized' 

How do I solve that? Thanks in advance.

1
  • Contracts look fine.
    – anwar
    Commented Jul 20, 2022 at 17:56

1 Answer 1

0

public initializer means it will only run only once at deploying

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.