Fixed a bug in the OrderBook.calculateRequiredBase method that made the calculation of the required asset amount to fail some times

This commit is contained in:
Nelson R. Perez 2017-10-12 18:22:29 -05:00
parent 6ac84f9a52
commit 61dc72724e
5 changed files with 64 additions and 8 deletions

View file

@ -17,7 +17,7 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
VERSION_NAME=0.4.5
VERSION_NAME=0.4.5-SNAPSHOT
VERSION_CODE=7
GROUP=com.github.bilthon

View file

@ -22,7 +22,7 @@ android {
minSdkVersion 9
targetSdkVersion 24
versionCode 7
versionName "0.4.5"
versionName "0.4.5-SNAPSHOT"
vectorDrawables.useSupportLibrary = true
}

View file

@ -89,7 +89,13 @@ public class OrderBook {
}else{
// If the offered amount is less than what we need, we exchange the whole order
totalBought += orderAmount;
totalSold += order.getSellPrice().quote.getAmount().longValue();
// The amount specified in the price ratio is not always all for sale. So in order to calculate
// the amount actually sold we have to do:
// actually_sold = for_sale * quote / base
double sellRatio = ((double) orderAmount) / ((double) order.getSellPrice().base.getAmount().longValue());
totalSold += Math.floor(order.getSellPrice().quote.getAmount().doubleValue() * sellRatio);
}
}
}

View file

@ -10,11 +10,11 @@ import javax.net.ssl.SSLContext;
import cy.agorise.graphenej.test.NaiveSSLContext;
/**
* Created by nelson on 4/14/17.
* Base class that every test that involves any communication with the API must extend
*/
public class BaseApiTest {
protected String BLOCK_PAY_DE = System.getenv("OPENLEDGER_EU");
protected String NODE_URL = System.getenv("NODE_URL");
protected SSLContext context;
protected WebSocket mWebSocket;
@ -27,7 +27,7 @@ public class BaseApiTest {
// Set the custom SSL context.
factory.setSSLContext(context);
mWebSocket = factory.createSocket(BLOCK_PAY_DE);
mWebSocket = factory.createSocket(NODE_URL);
}
}

View file

@ -5,6 +5,7 @@ import com.neovisionaries.ws.client.WebSocketException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
@ -23,13 +24,18 @@ import cy.agorise.graphenej.operations.LimitOrderCreateOperation;
import static org.hamcrest.CoreMatchers.is;
/**
* Created by nelson on 3/24/17.
* Class used to test the 'get_limit_order' API wrapper
*/
public class GetLimitOrdersTest extends BaseApiTest {
private UserAccount seller = new UserAccount("1.2.143563");
private final Asset base = new Asset("1.3.121", "USD", 4);
private final Asset quote = new Asset("1.3.0", "BTS", 5);
@Before
public void setup(){
System.out.println("Connecting to node: "+NODE_URL);
}
@Test
public void testGetLimitOrders(){
try {
@ -104,7 +110,7 @@ public class GetLimitOrdersTest extends BaseApiTest {
int expiration = (int) ((System.currentTimeMillis() + 60000) / 1000);
LimitOrderCreateOperation operation = orderBook.exchange(seller, base, toBuy, expiration);
// Testing the successfull creation of a limit order create operation
// Testing the successful creation of a limit order create operation
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);
@ -135,6 +141,50 @@ public class GetLimitOrdersTest extends BaseApiTest {
}
}
@Test
public void testRequiredBase(){
try {
final Asset _quote = new Asset("1.3.121", "USD", 4);
final Asset _base = new Asset("1.3.0", "BTS", 5);
mWebSocket.addListener(new GetLimitOrders(_base.getObjectId(), _quote.getObjectId(), 100, new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
List<LimitOrder> orders = (List<LimitOrder>) response.result;
OrderBook orderBook = new OrderBook(orders);
long _totalQuote = 1000;
long _totalBase = orderBook.calculateRequiredBase(new AssetAmount(UnsignedLong.valueOf(_totalQuote), _quote));
System.out.println(String.format("Base: %s, Quote: %s", _base.getObjectId(), _quote.getObjectId()));
System.out.println(String.format("_totalQuote: %d, _totalBase: %d", _totalQuote, _totalBase));
synchronized (GetLimitOrdersTest.this){
GetLimitOrdersTest.this.notifyAll();
}
}
@Override
public void onError(BaseResponse.Error error) {
System.out.println("onError. Msg: "+error.message);
synchronized (GetLimitOrdersTest.this){
GetLimitOrdersTest.this.notifyAll();
}
}
}));
mWebSocket.connect();
synchronized (this){
wait();
}
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (InterruptedException e) {
System.out.println("InterruptedException. Msg: "+e.getMessage());
}
}
@After
public void tearDown() throws Exception {
mWebSocket.disconnect();