I'm trying to upgrade a contract deployed by my factory contract using BeaconProxy. I'm able to get it compiled and deployed. I'm also able to successfully run the upgradeUpgradableContract function below without any errors. However the upgraded contract does not work when I try to use it. An "execution reverted" error occurs when I try to run a view function on the upgraded contract code. Perhaps my upgradeUpgradableContract is wrong? Any help would be appreciated! Thanks!
Portion of Code:
pragma solidity 0.8.7;
import '@openzeppelin/contracts/proxy/beacon/UpgradeableBeacon.sol';
import '@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol';
import '...UpgradableContract.sol';
contract FactoryContract{
address immutable beaconAddress;
constructor(
address upgrader
) public {
UpgradeableBeacon _beacon = new UpgradeableBeacon(address(new UpgradableContract()));
_beacon.transferOwnership(address(this));
beaconAddress = address(_beacon);
}
function upgradeUpgradableContract(
address addressToUpgrade,
address variableForUpgradableContract,
address admin
)
payable
public
{
BeaconProxy proxy = new BeaconProxy(
beaconAddress,
abi.encodeWithSelector(
UpgradableContract(payable(address(0))).initialize.selector,
adminAddress,
variableForUpgradableContract)
);
UUPSUpgradeable(addressToUpgrade).upgradeTo(address(proxy));
}
function newUpgradableContract(address variableForUpgradableContract, address adminAddress)
payable
public
{
BeaconProxy proxy = new BeaconProxy(
beaconAddress,
abi.encodeWithSelector(
UpgradableContract(payable(address(0))).initialize.selector,
adminAddress,
addressForContract));
}
emit Created(address(proxy), beaconAddress);
}
pragma solidity 0.8.7;
import '...uniswap.';
import '...IERC20.sol';
import "./FactoryContract.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {UUPSUpgradeable} from '@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol';
import {OwnableUpgradeable} from '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
contract UpgradableContract is Initializable, UUPSUpgradeable, OwnableUpgradeable {
function initialize(
address admin,
address VarForContract
) public initializer {
_admin = admin;
__Ownable_init();
transferOwnership(_admin);
//do something with VarForContract
}
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
}
}