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??
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