-1

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; }

}

  • hi [@johnnybegoode](https://stackoverflow.com/users/17943288/johnnybegoode), can you please review and edit your question (i think there are a couple of dupes in there?) and also ensure the code properly formatted. It's not easy to read at the moment. I tried to edit your post but the edit queue is full. With that done I can try and review what you've done and offer some thoughts. – ZeusLawyer May 25 '22 at 04:35
  • Thank you! I added 3 important links to get started. – johnnybegoode May 26 '22 at 16:45
  • [@johnnybegoode](https://stackoverflow.com/users/17943288/johnnybegoode) what are you trying to achieve? That contract address you pass in is ETH/USD. But what is UpToken trying to achieve? can it not have a setter to update the address? – ZeusLawyer May 31 '22 at 02:18
  • **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. – johnnybegoode Jun 01 '22 at 03:38
  • What I am trying to achieve: The first step of upgrading a token I would like to do is to getLatestPrice() of the very token created. – johnnybegoode Jun 01 '22 at 18:40

1 Answers1

1

Recommend you use hardhat with the upgrades plugin (https://docs.openzeppelin.com/contracts/4.x/upgradeable) to achieve this. When you deploy it the first time using hardhat, it will deploy the proxy, the implementation contract (i.e. your token contract) and an admin contract.

See 20:23 onwards in this video: https://www.youtube.com/watch?v=bdXJmWajZRY&t=15s . In fact maybe watch that entire video to get a sense of how to use hardhat with OZ upgradeable contracts.

Then when you get your token contract address, update the token contract as V2, add the contract address as a state variable and (optionally) add a setter function that gives you the ability to update that state variable in future, and deploy V2. You can see how to deploy V2 in that same video I linked above, at [24:30] or so.

ZeusLawyer
  • 274
  • 1
  • 8
  • I had seen that video numerous times, I'm trying to upgrade using the UUPS proxy pattern and there isn't any example online. The goal is to getLatestPrice() of the very token created in V2. – johnnybegoode Jun 01 '22 at 18:41
  • getLatestPrice() is a function of Chainlink's AggregatorV3Interface.sol – johnnybegoode Jun 01 '22 at 18:49