Fixed a rounding error in the OrderBook class' exchange method
This commit is contained in:
parent
aa35925718
commit
80358bc086
2 changed files with 11 additions and 5 deletions
|
@ -1,7 +1,9 @@
|
||||||
package de.bitsharesmunich.graphenej;
|
package de.bitsharesmunich.graphenej;
|
||||||
|
|
||||||
|
import com.google.common.math.DoubleMath;
|
||||||
import com.google.common.primitives.UnsignedLong;
|
import com.google.common.primitives.UnsignedLong;
|
||||||
|
|
||||||
|
import java.math.RoundingMode;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.bitsharesmunich.graphenej.operations.LimitOrderCreateOperation;
|
import de.bitsharesmunich.graphenej.operations.LimitOrderCreateOperation;
|
||||||
|
@ -80,7 +82,10 @@ public class OrderBook {
|
||||||
// If the offered amount is greater than what we still need, we exchange just what we need
|
// If the offered amount is greater than what we still need, we exchange just what we need
|
||||||
if(orderAmount >= stillNeed) {
|
if(orderAmount >= stillNeed) {
|
||||||
totalBought += stillNeed;
|
totalBought += stillNeed;
|
||||||
totalSold += (order.getSellPrice().quote.getAmount().longValue() * stillNeed) / order.getSellPrice().base.getAmount().longValue();;
|
double additionalRatio = (double) stillNeed / (double) order.getSellPrice().base.getAmount().longValue();
|
||||||
|
double additionalAmount = order.getSellPrice().quote.getAmount().longValue() * additionalRatio;
|
||||||
|
long longAdditional = DoubleMath.roundToLong(additionalAmount, RoundingMode.HALF_UP);
|
||||||
|
totalSold += longAdditional;
|
||||||
}else{
|
}else{
|
||||||
// If the offered amount is less than what we need, we exchange the whole order
|
// If the offered amount is less than what we need, we exchange the whole order
|
||||||
totalBought += orderAmount;
|
totalBought += orderAmount;
|
||||||
|
|
|
@ -34,8 +34,8 @@ import static org.hamcrest.CoreMatchers.is;
|
||||||
public class GetLimitOrdersTest {
|
public class GetLimitOrdersTest {
|
||||||
private String BLOCK_PAY_DE = System.getenv("BLOCKPAY_DE");
|
private String BLOCK_PAY_DE = System.getenv("BLOCKPAY_DE");
|
||||||
private UserAccount seller = new UserAccount("1.2.143563");
|
private UserAccount seller = new UserAccount("1.2.143563");
|
||||||
private final Asset base = new Asset("1.3.0", "BTS", 5);
|
private final Asset base = new Asset("1.3.121", "USD", 4);
|
||||||
private final Asset quote = new Asset("1.3.120", "EUR", 4);
|
private final Asset quote = new Asset("1.3.0", "BTS", 5);
|
||||||
|
|
||||||
private SSLContext context;
|
private SSLContext context;
|
||||||
private WebSocket mWebSocket;
|
private WebSocket mWebSocket;
|
||||||
|
@ -121,13 +121,14 @@ public class GetLimitOrdersTest {
|
||||||
List<LimitOrder> orders = (List<LimitOrder>) response.result;
|
List<LimitOrder> orders = (List<LimitOrder>) response.result;
|
||||||
OrderBook orderBook = new OrderBook(orders);
|
OrderBook orderBook = new OrderBook(orders);
|
||||||
|
|
||||||
AssetAmount toBuy = new AssetAmount(UnsignedLong.valueOf(9850000), quote);
|
AssetAmount toBuy = new AssetAmount(UnsignedLong.valueOf(100000), quote);
|
||||||
int expiration = (int) ((System.currentTimeMillis() + 60000) / 1000);
|
int expiration = (int) ((System.currentTimeMillis() + 60000) / 1000);
|
||||||
LimitOrderCreateOperation operation = orderBook.exchange(seller, base, toBuy, expiration);
|
LimitOrderCreateOperation operation = orderBook.exchange(seller, base, toBuy, expiration);
|
||||||
|
|
||||||
// Testing the successfull creation of a limit order create operation
|
// Testing the successfull creation of a limit order create operation
|
||||||
Assert.assertTrue(operation != null);
|
Assert.assertTrue(operation != null);
|
||||||
|
double price = (double) Math.pow(10, base.getPrecision() - quote.getPrecision()) * operation.getMinToReceive().getAmount().longValue() / operation.getAmountToSell().getAmount().longValue();
|
||||||
|
System.out.println("price: "+price);
|
||||||
synchronized (GetLimitOrdersTest.this){
|
synchronized (GetLimitOrdersTest.this){
|
||||||
GetLimitOrdersTest.this.notifyAll();
|
GetLimitOrdersTest.this.notifyAll();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue