0

I have a smart contract that returns the price of ETH using Chainlink. When i call getPrice() from geth i get a very large HEX number. How can i get the decimal number out of the hex value?

constructor (address _manager) ReentrancyGuard() {
        manager = _manager;

        priceFeed = AggregatorV3Interface(ETH_USD_RINKENBY);
    }

function getPrice() public view returns (int) 
{        
    (
        uint80 roundID,
        int price,
        uint startedAt,
        uint timeStamp,
        uint80 answeredInRound
    ) = priceFeed.latestRoundData();

    //uint8 decimals = priceFeed.decimals();

    return price;
}

returned values example

"0xfd918bdff54b7c2d1b639fe258ea80d8f00c68e096cce9cef69b42ff6b19522d" "0x480f2a54bec768ccc4e97983b62925d62075fe374d2daaadfd9d686f6a2757a4" "0x0d30772eaf09ac3ceb1e2b80e74757ae10db37a33d9aa0cdb8df3eb228981ae5" "0xfb0d591bca5c552ff4ecea8c5ff52da7f7df7925ec5a1bc95ecf77d8ddd3b9d1" "0x0d0abf0bdeb72dd3de217d6663993c0704fe20bb124ca1caad3ba74543a23894" "0xfd4f9d6f754414702a7c30cc9eda7c89472c0dea1f5881557687eafef3317db4" "0xdb8102933cff94ee14c46d58af60e534c8eb1da3f5d1c1f9a8b18b7dacb65e56" "0xdc8b57ffcc0c338b912f21241fd1f4451f079dd3229ad529d798eea2b3d29350

"

1 Answer 1

1

Using javascript you can use parseInt! Not only does it take regular string numbers, it also takes hex strings!

You can read about it here.

The interface is parseInt(string, radix) The second argument is the "radix" - which is a math term for essentially "base"

For your example above (taking the first hex string) parseInt('0xfd918bdff54b7c2d1b639fe258ea80d8f00c68e096cce9cef69b42ff6b19522d', 16)

gives me 1.1469230889660214e+77

2
  • Usually it is not a good idea to use javascript numbers because they don't have enough precision to hold a uint256. Either use a library like BN.js, BigNumber.js, etc. or BigInt if your js stack support them.
    – Ismael
    Jun 14 at 15:25
  • Great point! I will add this to the answer in a min. Jun 15 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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