use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class OrderManagerTest method testOrderChannelHandler3.
/**
* Test the order channel handler - posclose order
* @throws APIException
*/
@Test
public void testOrderChannelHandler3() throws APIException {
final String jsonString = "[0,\"on\",[6827301913,null,null,\"tXRPUSD\",1515069803530,1515069803530,-60,-60,\"MARKET\",null,null,null,0,\"ACTIVE (note:POSCLOSE)\",null,null,0,3.2041,null,null,null,null,null,0,0,0]]";
final JSONArray jsonArray = new JSONArray(jsonString);
final OrderHandler orderHandler = new OrderHandler();
final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
Assert.assertTrue(orderManager.getOrders().isEmpty());
orderHandler.handleChannelData(bitfinexApiBroker, jsonArray);
Assert.assertEquals(1, orderManager.getOrders().size());
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class OrderManagerTest method testPlaceOrderUnauth.
/**
* Test the placement of an order
* @throws InterruptedException
* @throws APIException
*/
@Test(expected = APIException.class)
public void testPlaceOrderUnauth() throws APIException, InterruptedException {
final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
Mockito.when(bitfinexApiBroker.getCapabilities()).thenReturn(ConnectionCapabilities.NO_CAPABILITIES);
final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.MARKET, 12).build();
orderManager.placeOrderAndWaitUntilActive(order);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class OrderManagerTest method testPlaceOrder.
/**
* Test the placement of an order
* @throws InterruptedException
* @throws APIException
*/
@Test(timeout = 60000)
public void testPlaceOrder() throws APIException, InterruptedException {
final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
Mockito.when(bitfinexApiBroker.isAuthenticated()).thenReturn(true);
final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.MARKET, 1).build();
final Runnable r = () -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
return;
}
final ExchangeOrder exchangeOrder = new ExchangeOrder();
exchangeOrder.setCid(order.getCid());
exchangeOrder.setState(ExchangeOrderState.STATE_ACTIVE);
orderManager.updateOrder(exchangeOrder);
};
// Cancel event
(new Thread(r)).start();
orderManager.placeOrderAndWaitUntilActive(order);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class CommandsCallbackTest method testAuthCommandCallback1.
/**
* Test the auth callback
* @throws APIException
*/
@Test
public void testAuthCommandCallback1() throws APIException {
final String authCallback = "{\"event\":\"auth\",\"status\":\"OK\",\"chanId\":0,\"userId\":1015301,\"auth_id\":\"d5c6a71c-6164-40dd-b57a-92fb59d42975\",\"caps\":{\"orders\":{\"read\":1,\"write\":1},\"account\":{\"read\":1,\"write\":0},\"funding\":{\"read\":1,\"write\":1},\"history\":{\"read\":1,\"write\":0},\"wallets\":{\"read\":1,\"write\":1},\"withdraw\":{\"read\":0,\"write\":0},\"positions\":{\"read\":1,\"write\":1}}}";
final BitfinexApiBroker bitfinexApiBroker = new BitfinexApiBroker();
final JSONObject jsonObject = new JSONObject(authCallback);
final AuthCallbackHandler authCallbackHandler = new AuthCallbackHandler();
Assert.assertFalse(bitfinexApiBroker.isAuthenticated());
Assert.assertEquals(ConnectionCapabilities.NO_CAPABILITIES, bitfinexApiBroker.getCapabilities());
authCallbackHandler.handleChannelData(bitfinexApiBroker, jsonObject);
Assert.assertTrue(bitfinexApiBroker.isAuthenticated());
final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
Assert.assertTrue(capabilities.isHavingOrdersReadCapability());
Assert.assertTrue(capabilities.isHavingOrdersWriteCapability());
Assert.assertTrue(capabilities.isHavingAccountReadCapability());
Assert.assertFalse(capabilities.isHavingAccountWriteCapability());
Assert.assertTrue(capabilities.isHavingFundingReadCapability());
Assert.assertTrue(capabilities.isHavingFundingWriteCapability());
Assert.assertTrue(capabilities.isHavingHistoryReadCapability());
Assert.assertFalse(capabilities.isHavingHistoryWriteCapability());
Assert.assertTrue(capabilities.isHavingWalletsReadCapability());
Assert.assertTrue(capabilities.isHavingWalletsWriteCapability());
Assert.assertFalse(capabilities.isHavingWithdrawReadCapability());
Assert.assertFalse(capabilities.isHavingWithdrawWriteCapability());
Assert.assertTrue(capabilities.isHavingPositionReadCapability());
Assert.assertTrue(capabilities.isHavingPositionWriteCapability());
Assert.assertTrue(capabilities.toString().length() > 10);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class OrderManager method placeOrderOrderOnAPI.
/**
* Execute a new Order
* @param order
* @return
* @throws Exception
*/
private boolean placeOrderOrderOnAPI(final BitfinexOrder order) throws Exception {
final CountDownLatch waitLatch = new CountDownLatch(1);
final Consumer<ExchangeOrder> ordercallback = (o) -> {
if (o.getCid() == order.getCid()) {
waitLatch.countDown();
}
};
registerCallback(ordercallback);
try {
placeOrder(order);
waitLatch.await(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
if (waitLatch.getCount() != 0) {
throw new APIException("Timeout while waiting for order");
}
// Check for order error
final boolean orderInErrorState = bitfinexApiBroker.getOrderManager().getOrders().stream().filter(o -> o.getCid() == order.getCid()).anyMatch(o -> o.getState() == ExchangeOrderState.STATE_ERROR);
if (orderInErrorState) {
throw new APIException("Unable to place order " + order);
}
return true;
} catch (Exception e) {
throw e;
} finally {
removeCallback(ordercallback);
}
}
Aggregations