0

I am getting this very strange error message --DeclarationError:undefined identifier.

And it's giving this in all of my Functions, I don't know why. Can anyone please tell me what's is going on ?

I am using Ethereum, Solidity and Openzepellinn to do this, but to be able to do the next step I need my interface to work without any of these error.

pls see the code below

pragma solidity ^0.8.0;



interface AggregatorV3Interface {
  function decimals() external view returns (uint8);

  function description() external view returns (string memory);

  function version() external view returns (uint256);

  // getRoundData and latestRoundData should both raise "No data present"
  // if they do not have data to report, instead of returning unset values
  // which could be misinterpreted as actual reported values.
  function getRoundData(uint80 _roundId)
    external
    view
    returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );

  function latestRoundData()
    external
    view
    returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );
}
/**
 * THIS EXAMPLE USES UN-AUDITED CODE.
 * Network: Kovan
 * Base: BTC/USD
 * Base Address: 0x6135b13325bfC4B00278B4abC5e20bbce2D6580e
 * Quote: EUR/USD
 * Quote Address: 0x0c15Ab9A0DB086e062194c273CC79f41597Bbf13
 * Decimals: 8
 */

contract PriceConverter {

        address public priceSource;
        address public quote;
        uint8 private decimals;
       
        


        uint256 public fallbackPrice;

    event FallbackPrice(
         uint80 roundId, 
         int256 answer,
         uint256 startedAt,
         uint256 updatedAt, 
         uint80 answeredInRound
         );

 constructor(address _priceSource, address _quote, uint8 _decimals) public {
        priceSource = _priceSource;
        quote  = _quote;
        decimals =  uint8(10 ** uint8(_decimals));
        
    }

   function latestRoundData() public view
        returns 
            (uint80 roundId,
             int256 answer,
             uint256 startedAt,
             uint256 updatedAt, 
             uint80 answeredInRound
            )
            
    {
       

        require(decimals > uint8(0) && decimals <= uint8(18), "Invalid _decimals");
       
        ( , int256 basePrice, , , ) = address(priceSource).latestRoundData();
        uint8 baseDecimals = address(priceSource).decimals();
        basePrice = scalePrice(basePrice, baseDecimals, decimals);

        ( , int256 quotePrice, , , )  = address(quote).latestRoundData();
        uint8 quoteDecimals = address(quote).decimals();
        quotePrice = scalePrice(quotePrice, quoteDecimals, decimals);

         return basePrice * decimals / quotePrice;
         

        
    }



function updateFallbackPrice() public {
        (
         uint80 roundId, 
         int256 answer,
         uint256 startedAt,
         uint256 updatedAt, 
         uint80 answeredInRound
         ) = priceSource.latestRoundData();

        if (answer > 0) {
            fallbackPrice = uint256(answer);
            emit FallbackPrice(roundId,answer,startedAt,updatedAt,answeredInRound);
        }
    }


    function scalePrice(int256 _price, uint8 _priceDecimals, uint8 _decimals)
        internal
        pure
        returns (int256)
    {
        if (_priceDecimals < _decimals) {
            return _price * int256(10 ** uint256(_decimals - _priceDecimals));
        } else if (_priceDecimals > _decimals) {
            return _price / int256(10 ** uint256(_priceDecimals - _decimals));
        }
        return _price;
    }
} ```


Also i added screenshot of what it looks like on remix IDE




  [1]: https://i.stack.imgur.com/e00dC.png
calibre24
  • 359
  • 1
  • 3
  • 7

1 Answers1

0

first mistake, this is your constructor

constructor(address _priceSource, address _quote, uint8 _decimals) public {
        priceSource = AggregatorV3Interface(_priceSource);
        quote  = ERC20(_quote);
        decimals = uint8 (_decimals);
        
    }

you initialize _decimals with type uint8, then convert it again to uint8, it is not necessary

then in your functions you are using _decimals which is initialized only inside the construct, but the value you passed it to decimals, you have to use that instead of _decimals

Jacopo Mosconi
  • 972
  • 8
  • 22
  • i have applied your comment on the code, i however have some errors you can see it on the screenshot attached. – calibre24 Dec 10 '21 at 08:19
  • you are trying to implicit convert quote (that is an erc20 type) to aggregatorv3interface, that's not possible. in line 93, to find the decimals of quote you only need to write uint8 quoteDecimals = quote.decimals(), what are you trying to do in line 92? – Jacopo Mosconi Dec 10 '21 at 09:57
  • ok, what i am trying to achieve here is get the price feed in another currency which EUR..by using a constructor.. this is the doc on chainlink https://docs.chain.link/docs/get-the-latest-price/#getting-a-different-price-denomination – calibre24 Dec 10 '21 at 10:48
  • so there are several thing wrong, you initialized quote and priceSource with wrong type, they are both address, not erc20 and aggregatorv3interface, initialize them like addresses, then edit your code like before my last comment, with implicit conversion on line 92/93, now you can do the implicit conversion from address to aggregatorv3 – Jacopo Mosconi Dec 10 '21 at 11:59
  • i have updated the code and i get this error now, ```TypeError: Member "latestRoundData" not found or not visible after argument-dependent lookup in address. --> contracts/priceConverter.sol:90:39: | 90 | ( , int256 basePrice, , , ) = address(priceSource).latestRoundData(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` – calibre24 Dec 11 '21 at 18:33
  • why you wrote `address(priceSource).latestRoundData();`? what address is price source? you have to convert it to the interface of the contract. exemple: if it is an aggregatorv3, you have to convert it to aggregatorv3interface, not address – Jacopo Mosconi Dec 12 '21 at 20:12