Will Chainlink's AggregatorV3Interface.sol work with an OpenZeppelin upgradable contract? Do I place the
"priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);"
inside the "initializer{}"?
I would like the address "0x9326BFA02ADD2366b30bacB125260Af641031331" to be upgradable in the next version of the smart contract.
Is there already a way to do so?
Thank you!
Motivation & Justification
I hope that it makes sense to want to "getLatestPrice()" within the smart contract of a new token.
Before deployment, there is no way of knowing the new token's address.
I would like to change the address using the OpenZeppelin upgradable contract under the UUPS proxy pattern.
Is there any example online to update
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331); // Kovan Testnet
on version 2 of the smart contract?
Thank you!
Important Links:
OpenZeppelin Contracts Wizard
get-the-latest-price using Chainlink's AggregatorV3Interface
Deploying an UUPS Upgradable Contract
I have no idea how to work on version 2 of an UUPS Upgradable Contract.
This is where I got stuck using Chainlink's AggregatorV3Interface.sol with every feature selected on OpenZeppelin Contracts Wizard:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.4;
import "./@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol"; import "./@openzeppelin/contracts- upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol"; import "./@openzeppelin/contracts- upgradeable/token/ERC20/extensions/ERC20SnapshotUpgradeable.sol"; import "./@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; import "./@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol"; import "./@openzeppelin/contracts-upgradeable/token/ERC20/extensions/draft- ERC20PermitUpgradeable.sol"; import "./@openzeppelin/contracts- upgradeable/token/ERC20/extensions/ERC20VotesUpgradeable.sol"; import "./@openzeppelin/contracts- upgradeable/token/ERC20/extensions/ERC20FlashMintUpgradeable.sol"; import "./@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; import "./@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract UpToken is Initializable, ERC20Upgradeable, ERC20BurnableUpgradeable, ERC20SnapshotUpgradeable, OwnableUpgradeable, PausableUpgradeable, ERC20PermitUpgradeable, ERC20VotesUpgradeable, ERC20FlashMintUpgradeable, UUPSUpgradeable { /// @custom:oz-upgrades-unsafe-allow constructor AggregatorV3Interface internal priceFeed; constructor() initializer {
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331); //
Kovan Testnet }
function initialize() initializer public { __ERC20_init("UpToken", "UPT"); __ERC20Burnable_init(); __ERC20Snapshot_init(); __Ownable_init(); __Pausable_init(); __ERC20Permit_init("UpToken"); __ERC20FlashMint_init(); __UUPSUpgradeable_init(); }
function snapshot() public onlyOwner { _snapshot(); }
function pause() public onlyOwner { _pause(); }
function unpause() public onlyOwner { _unpause(); }
function mint(address to, uint256 amount) public onlyOwner { _mint(to, amount); }
function _beforeTokenTransfer(address from, address to, uint256 amount) internal whenNotPaused override(ERC20Upgradeable, ERC20SnapshotUpgradeable) { super._beforeTokenTransfer(from, to, amount); }
function _authorizeUpgrade(address newImplementation) internal onlyOwner override {}
// The following functions are overrides required by Solidity.
function _afterTokenTransfer(address from, address to, uint256 amount) internal override(ERC20Upgradeable, ERC20VotesUpgradeable) { super._afterTokenTransfer(from, to, amount); }
function _mint(address to, uint256 amount) internal override(ERC20Upgradeable, ERC20VotesUpgradeable) { super._mint(to, amount); }
function _burn(address account, uint256 amount) internal override(ERC20Upgradeable, ERC20VotesUpgradeable) { super._burn(account, amount); }
function getLatestPrice() public view returns (int) { ( /uint80 roundID/, int price, /uint startedAt/, /uint timeStamp/, /uint80 answeredInRound/ ) = priceFeed.latestRoundData(); return price; }
}