I know that under the hood hardhat has helped my do a lots of things.
But my question is how can I deploy the proxy & logic contract and then setup them correctly?
I know I have to deploy the ERC1967UpgradeUpgradeable contract myself, but is that all?
How can I setup this proxy so that it can linked to the logic contract.
Thank for reading this question and I'll appreciate any suggestions! thank you
You have to deploy your mynft contract first, this is your logic contract. Then you have to deploy an instance of ERC1967Proxy from @openzeppelin/contracts, this is your proxy.
Hi @frangio thanks for the reply!
I've deployed an proxy contract instance using the following code:
// SPDX-License-Identifier: Apache2.0
pragma solidity ^0.8.4;
import "@openzeppelin/contracts-upgradeable/proxy/ERC1967/ERC1967UpgradeUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
contract Erc721NftProxy is Initializable, ERC1967UpgradeUpgradeable {
mapping(bytes4 => uint32) _sizes;
function initialize(address implementation) public virtual initializer {
ERC1967UpgradeUpgradeable.__ERC1967Upgrade_init();
_upgradeTo(implementation);
}
function getImplementation() public returns (address) {
return _getImplementation();
}
function upgradeTo(address newImplementation) public {
_upgradeTo(newImplementation);
}
}
After I called the upgradeTo and tried to call some functions in the implementation contract, like for example proxyContract.symbol(), it shows the function is undefined.
Can you shed some light on this for me please? thanks!!
The problem is that proxyContract is an Ethers instance of Proxy, so it doesn't expose the functions of Erc721NftLogic. To use the latter you have to get an instance like this:
// DEPLOY THE IMPLEMENTATION
const MyContract = await ethers.getContractFactory('MyContract');
const implementation = await MyContract.deploy();
await implementation.deployed();
// DEPLOY THE PROXY
const MyContractProxy = await ethers.getContractFactory('MyContractProxy');
const proxy = await MyContractProxy.deploy();
await proxy.deployed();
// CALL INITIALIZAION ON BOTH CONTRACTS
await implementation.initialize(contractOwnerAddress);
await proxy.initialize(implementation.address);
// EXPOSE THE MyContract FUNCTIONS
myContract = MyContract.attach(proxy.address);
await myContract.owner()
// this should return "contractOwnerAddress", but throws the error: Error: Transaction reverted: function selector was not recognized and there's no fallback function