Hey yall! I’m building a model that can simulate the trading that occurs across a liquidity pool and to experiment with how I can use a balancer contract account to tend the pool and keep a stable currency ratio. The issue I’m having is with my math in calculating the pool balances after a transaction, something weird is happening… I think it might have to do with the weird parsing of data from string to float I’m doing to generate numbers with decimals rounded to a specific decimal point. This is the first time I’m using the nf() function and I think I’m using it correctly, but now some of my math is rounding to weird decimal places where it should’nt. Anyways I’ve included the code below with some context:
The first part of the code works fine. It parses the string data from my accounts[] array from the corresponding account to a float and sets the variable BuyLimit equal to the max amount of money that the account can spend. Next I calculate a random float less than the BuyLimit and set it equal to the String buy.
The next chunk checks the values of the exchange pools before the transaction. When the software starts they are set to 100,000. The first print line confirms that in the console. My issue is when I go to change the pool values with " poolTokenOne = poolTokenOne - Float.parseFloat(buy); " , they end up as the wrong number when I println() again to check their value after the transaction.
Like I said maybe this has to do with the weird Float.parseFloat thing or my nf() rounding, but I tried checking just the output of the Float.parseFloat(buy) with a println() prior to the transaction and the value to be subtracted comes out equivalent to the original buy amount which is how it should be. To me this indicates the issue is occurring not in the Float.parseFloat as I have the correct value to subtract, but when the equation of subtraction occurs on that value from poolTokenOne.
I’m really confused here because poolTokenOne and poolTokenTwo are assigned as floats so when I parse the buy string into a float I’m subtracting or adding proper data types in a simple equation but the solution always comes out wrong…
For example if the buy amount was 328.153. When I call the transaction it should be doing:
Pool A
100,000 = 100,000 - 328.153 = 99,671.847
Pool B
100,000 = 100,000 + 328.153 = 100,328.153
Instead I end up with the final values of:
poolTokenOne = 99,671.844
poolTokenTwo = 100,328.16
It doesn’t make any sense to me there not even redistributing its like they round to weird values. If I took .003 from the difference in the wrong vs correct output for pool one and added it to pool two I don’t even get the correct pool two value, the ratio is off. In this case it creates .004 coins that don’t exist.
ex: 99.671.847 - 99,671.844 = .003 + 100,328,153 /= 100,328.16)
Seriously someone with more experience please help, what’s going on here? Why is processing failing to do basic subtraction and addition? Why am I an idiot?
if (tokenSelector <= 5) {
println("A Pool Selected");
println(" ");
// Call a random buy from Token A balance
float buyLimit = Float.parseFloat(accounts[accountSelector-1]);
println("Buy Limit: " + buyLimit);
String buy = nf(random(0,buyLimit),0,decimal);
println("Buy Amount: " + buy);
println(" ");
println("Token A Bal Pre Transaction: " + poolTokenOne);
println("Token B Bal Pre Transaction: " + poolTokenTwo);
println(Float.parseFloat(buy));
poolTokenOne = poolTokenOne - Float.parseFloat(buy);
poolTokenTwo = poolTokenTwo + Float.parseFloat(buy);
println(" ");
println("Token A Bal Post Transaction: " + nf(poolTokenOne,0,decimal));
println("Token B Bal Post Transaction: " + poolTokenTwo);
} else if (tokenSelector > 5) {