1

so I have a function getPriceReserveBuySUForEth that is returning the incorrect value and I'm not sure why :| I guess what the function does and the purpose doesn't really matter so I won't waste the space here The function:

function getPriceReserveBuySUForEth(uint256 _value) public view returns (uint256) {
    require(getAvailableReserveUSD(0) > 0);                     // Need to have some reserve available in order to buy SU back

    uint256 maxReserveAvailable = address(this).balance * getEthPrice() * percentDropBeforeBonds / 100;
    uint256 m = 2 * maxReserveAvailable / ((maxReservePriceDeviationInCents * 10**24)**2);
    uint256 amountReserveUsedUp = maxReserveAvailable - uint(getAvailableReserveUSD(0));
    uint256 averageTradePrice = sqrt(2 * (amountReserveUsedUp + _value) / m);
    uint256 tempVal = 10**26 - averageTradePrice;
    return tempVal;
}

When using 0 as the input, it sgould return 10^26, however it returns 0.

averageTradePrice should = 0. Therefore 10^26 - 0 should = 10^26. I tested this with a function y:

function y(uint256 _value) public view returns (uint256) {
    return 10**26 - 0;
} // I know _value doesn't do anything here - I was just lazy in copy/pasting

This correctly returns 10^26. Also I created a function x to test that averageTradePrice does indeed = 0:

function x(uint256 _value) public view returns (uint256) {
    require(getAvailableReserveUSD(0) > 0);                     // Need to have some reserve available in order to buy SU back

    uint256 maxReserveAvailable = address(this).balance * getEthPrice() * percentDropBeforeBonds / 100;
    uint256 m = 2 * maxReserveAvailable / ((maxReservePriceDeviationInCents * 10**24)**2);
    uint256 amountReserveUsedUp = maxReserveAvailable - uint(getAvailableReserveUSD(0));
    return sqrt(2 * (amountReserveUsedUp + _value) / m);
}

This is the exact same function as getPriceReserveBuySUForEth, except it returns the value for averageTradePrice instead of 20^26 - averageTradePrice. x(0) does indeed return 0 like it should. So why does getPriceReserveBuySUForEth return 0 when it should return 10^26??

  • Can you give an small example we can test? The value of some variables do not appear in your question like balance or getEthPrice(). Are optimizations enabled during compilation? From the details it might be a compiler bug, or an overflow/underflow. – Ismael Oct 11 '18 at 14:36
  • 1
    Thanks for you response Ismael - I managed to figure it out though :) Turns out the issue was that m was a number much less than 1 (close to 0), so solidity interpreted it as 0, which messed up the rest of the calculation. Not having floats is sooooo frustrating! – user46871 Oct 12 '18 at 4:06
1

Turns out the issue was that m was a number much less than 1 (close to 0), so solidity interpreted it as 0, which messed up the rest of the calculation. Not having floats is sooooo frustrating!

  • Solidity will return 0 for any number between 0 and 1, not just ones "much less than 1". It uses integer division which always rounds toward zero. So even 9999/10000 will resolve to zero. – Shawn Tabrizi Oct 12 '18 at 4:22

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.