0

I have the following function with a tuple:

function getETHPrice() internal view returns (int) {
    (,int price,,,) = AggregatorV3Interface(ETH_USD_FEED).latestRoundData();
    return price;
}

How do I return the price without storing it in an intermediary variable?
In another language, I would do something like this:

return AggregatorV3Interface(ETH_USD_FEED).latestRoundData()[1]

How can i do that?
When I try that, I get the following error:

TypeError: Indexed expression has to be a type, mapping or array (is tuple(uint80,int256,uint256,uint256,uint80))

2
  • 1
    What's wrong with a local variable?
    – Mad Jackal
    Dec 22, 2021 at 6:38
  • @MadJackal Nothing, I am just learning.
    – Qwerty
    Nov 4, 2022 at 10:45

1 Answer 1

4

In the Solidity official documentation there is a part about tuples

Tuples are not proper types in Solidity, they can only be used to form syntactic groupings of expressions.

You can use it as a return value but can't access a certain element in it. The way you do it in your code is the way to go, unfortunately. You could simplify it a bit by declaring a return variable like this if you would like:

function getETHPrice() internal view returns (int price) {
    (, price,,,) = AggregatorV3Interface(ETH_USD_FEED).latestRoundData();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.