Verify an initializer upgradeable contract on Etherscan

Fatory.sol

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.9;

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

contract Factory is IFactory, Initializable {
    mapping(address => mapping(address => address)) public override getPair;
    address[] public override allPairs;

    uint32 public override feeArg;
    address public override feeTo;
    address public override feeToSetter;
    address public override twammAdd;

    constructor(address _feeToSetter) {
        feeToSetter = _feeToSetter;
    }

    function allPairsLength() external view override returns (uint256) {
        return allPairs.length;
    }

    function initialize(address _twammAdd) external override initializer {
        twammAdd = _twammAdd;
    }

    function createPair(
        address token0,
        address token1
    ) external override returns (address pair) {
        require(
            msg.sender == twammAdd,
            "Invalid User, Only TWAMM Can Create Pair"
        );
        require(twammAdd != address(0), "Factory Not Initialized By TWAMM Yet");
        require(token0 != token1, "Factory: Identical Addresses");

        (address tokenA, address tokenB) = token0 < token1
            ? (token0, token1)
            : (token1, token0);
        require(tokenA != address(0), "Factory: Zero Address");
        require(getPair[tokenA][tokenB] == address(0), "Factory: Pair Exists"); // single check is sufficient

        bytes memory bytecode = type(Pair).creationCode;
        bytes memory bytecodeArg = abi.encodePacked(
            bytecode,
            abi.encode(tokenA, tokenB, twammAdd)
        );
        bytes32 salt = keccak256(abi.encodePacked(tokenA, tokenB));
        assembly {
            pair := create2(0, add(bytecodeArg, 0x20), mload(bytecodeArg), salt)
        }
        require(pair != address(0), "Create2: Failed On Deploy");
        getPair[tokenA][tokenB] = pair;
        getPair[tokenB][tokenA] = pair; // populate mapping in the reverse direction
        allPairs.push(pair);
        emit PairCreated(tokenA, tokenB, pair, allPairs.length);
    }

    function setFeeArg(uint32 _feeArg) external override {
        require(msg.sender == feeToSetter, "Factory: Forbidden");
        feeArg = _feeArg;
    }

    function setFeeTo(address _feeTo) external override {
        require(msg.sender == feeToSetter, "Factory: Forbidden");
        feeTo = _feeTo;
    }

    function setFeeToSetter(address _feeToSetter) external override {
        require(msg.sender == feeToSetter, "Factory: Forbidden");
        feeToSetter = _feeToSetter;
    }
}

Hello everyone,

I managed to create an upgradable contract (Factory.sol) and deploy it to the mainnet (https://etherscan.io/address/0x408f66057163d829a30D4d466092c6B0eebb692f) with hardhat.

I then did a fine job of upgrading Factory.sol using the @openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol contract. The twammAdd is https://etherscan.io/address/0xcd43AbA971bEF65555D877657F83093dDFB885b8

function initialize(address _twammAdd) external override initializer {
        twammAdd = _twammAdd;
    }

Now, Iā€™m now trying to verify it manually on etherscan with the command: npx hardhat verify --contract "contracts/Factory.sol: Factory" --network mainnet 0x408f66057163d829a30D4d466092c6B0eebb692f "0xCB3A9BFC9f99E75E922a516EED04D9F62e83a28E", but I got the error:

Error in plugin @nomiclabs/hardhat-etherscan: The address provided as an argument contains a contract, but its bytecode doesn't match the contract contracts/Factory.sol: Factory.

Who can help verify this contract to etherscan? Thanks!

You need to provide the parameter for constructor rather than initialize, it seems like _feeToSetter address is 0xCB3A9BFC9f99E75E922a516EED04D9F62e83a28E.

1 Like

Hi,I wrote the wrong address before, npx hardhat verify --contract "contracts/Factory.sol: Factory" --network mainnet 0x408f66057163d829a30D4d466092c6B0eebb692f "0xCB3A9BFC9f99E75E922a516EED04D9F62e83a28E" is also useless.

I do not have the source code, so I can not verify for you, maybe you can have a look at the hardhat docs: hardhat-etherscan | Hardhat

https://github.com/PulsarSwap/TWAMM-Contracts/blob/master/contracts/Factory.sol
This is our repo, thanks!

Have a try, but failed.

npx hardhat verify --network mainnet DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1"

I called the initialize() function after deploying the Factory.sol contract and was unable to verify

Can you help me with this problem? I can pay you for it.

I tried to verify contracts at the mater branch, but it failed, so maybe this is not the source contract to deploy.