0
contract MyContract {
  uint public priceUSD = 1.70 * 10**8; // USD
  uint public priceEth;

   .........


  function setPrice(uint newPriceUSD) external ownerOnly() returns(uint, uint) {
    uint priceUSDInt = newPriceUSD * 10**8;
    // make a small number like 2 equal 200,000,000.

    /**
    I'm hard coding the price here just to debug it
    but eventually, I'd be calling Chainlink 
    like this...
    (int price,) = getLatestPrice();
    **/

    int price = 132556000000;

    // this like evaluates to 
    // 150879.62823259603
    // I'm attempting to set it to the state variable priceEth
    priceEth = priceUSDInt / uint(price) * 10**8;

    // storing the number to state variable
    priceUSD = txtPriceUSDInt;

    // return both new prices
    return (newTxtPriceUSD, priceEth);
  }

  function getLatestPrice() public view returns (int, uint8) {
    (
      ,int price,,,
    ) = priceFeed.latestRoundData(); // Chainlink call

    uint8 decimals = priceFeed.decimals();
    return (price, decimals);
  }

  .........
}

Whenever setPrice() is called with e.g. setPrice(2), the intention is to set a new price in USD and in Eth. But I can't seem to ever set priceEth. It's always 0.

I'm really new to fixed-point math. It could be I'm doing something totally wrong here. But, when I use the Remix debugger and type of the result of txtPriceUSDInt / uint(price) * 10**8; I get a non zero number. But when I check the value of priceEth, it's always 0.

I'm not sure I'm doing something wrong with fixed-point arithmetic or something else. If anyone has any pointers?

5
  • What is setTxtPrice??? Jan 10, 2021 at 8:09
  • Plus, you obviously seem to be missing the fact that division in solidity is integer-division, meaning that if x < y then x / y == 0. Jan 10, 2021 at 8:12
  • "I'm not sure I'm doing something wrong with fixed-point arithmetic" - in fixed-point arithmetic, 2 / 132556000000 == 0. Jan 10, 2021 at 8:38
  • Oops. It's suppose to be setPrice. I changed it. And yes, still figuring out solidity-math hence why I asked the question :)
    – GN.
    Jan 10, 2021 at 9:43
  • You seem to be aware of the term 'fixed-point', so where exactly is the problem? Do you understand what integer-division means? Jan 10, 2021 at 9:45

0

Your Answer

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

Browse other questions tagged or ask your own question.