Search in sources :

Example 1 with WalletManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager in project crypto-bot by jnidzwetzki.

the class TestCapitalAllocation method buildPortfolioManager.

/**
 * Build the portfolio manager
 * @return
 * @throws APIException
 */
private PortfolioManager buildPortfolioManager() throws APIException {
    final Collection<Wallet> wallets = new ArrayList<>();
    wallets.add(new Wallet(Wallet.WALLET_TYPE_EXCHANGE, "USD", 1000, 0, 1000));
    final BitfinexApiBroker apiBroker = Mockito.mock(BitfinexApiBroker.class);
    final WalletManager walletManager = Mockito.mock(WalletManager.class);
    Mockito.when(walletManager.getWallets()).thenReturn(wallets);
    Mockito.when(apiBroker.getWalletManager()).thenReturn(walletManager);
    return new BasePortfolioManager(apiBroker, 0.05);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) BasePortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) ArrayList(java.util.ArrayList)

Example 2 with WalletManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletHandlerTest method testWalletSnapshot.

/**
 * Test the wallet parsing
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 20000)
public void testWalletSnapshot() throws APIException, InterruptedException {
    final String callbackValue = "[0,\"ws\",[[\"exchange\",\"ETH\",9,0,null],[\"exchange\",\"USD\",1826.56468323,0,null],[\"margin\",\"USD\",0,0,null],[\"exchange\",\"XRP\",0,0,null],[\"exchange\",\"EOS\",0,0,null],[\"exchange\",\"NEO\",0,0,null],[\"exchange\",\"LTC\",0,0,null],[\"exchange\",\"IOT\",0,0,null],[\"exchange\",\"BTC\",0,0,null]]]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final CountDownLatch walletLatch = new CountDownLatch(1);
    final Table<String, String, Wallet> walletTable = HashBasedTable.create();
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    final WalletManager walletManager = Mockito.mock(WalletManager.class);
    Mockito.when(bitfinexApiBroker.getWalletManager()).thenReturn(walletManager);
    Mockito.when(walletManager.getWalletTable()).thenReturn(walletTable);
    Mockito.when(bitfinexApiBroker.getConnectionReadyLatch()).thenReturn(walletLatch);
    Assert.assertTrue(walletTable.isEmpty());
    final WalletHandler walletHandler = new WalletHandler();
    walletHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    walletLatch.await();
    Assert.assertEquals(9, walletTable.size());
    Assert.assertEquals(9, walletTable.get("exchange", "ETH").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("exchange", "ETH").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("exchange", "ETH").getUnsettledInterest().doubleValue(), DELTA);
    Assert.assertEquals(1826.56468323, walletTable.get("exchange", "USD").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("exchange", "ETH").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("exchange", "ETH").getUnsettledInterest().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("margin", "USD").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("margin", "USD").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("margin", "USD").getUnsettledInterest().doubleValue(), DELTA);
    Assert.assertTrue(walletTable.get("margin", "USD").toString().length() > 0);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) JSONArray(org.json.JSONArray) WalletHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with WalletManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletHandlerTest method testWalletUpdate.

/**
 * Test the wallet parsing
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 20000)
public void testWalletUpdate() throws APIException, InterruptedException {
    final String callbackValue = "[0,\"ws\",[\"exchange\",\"ETH\",9,0,null]]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final CountDownLatch walletLatch = new CountDownLatch(1);
    final Table<String, String, Wallet> walletTable = HashBasedTable.create();
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    final WalletManager walletManager = Mockito.mock(WalletManager.class);
    Mockito.when(bitfinexApiBroker.getWalletManager()).thenReturn(walletManager);
    Mockito.when(walletManager.getWalletTable()).thenReturn(walletTable);
    Mockito.when(bitfinexApiBroker.getConnectionReadyLatch()).thenReturn(walletLatch);
    Assert.assertTrue(walletTable.isEmpty());
    final WalletHandler walletHandler = new WalletHandler();
    walletHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    walletLatch.await();
    Assert.assertEquals(1, walletTable.size());
    Assert.assertEquals(9, walletTable.get("exchange", "ETH").getBalance().doubleValue(), DELTA);
    Assert.assertEquals(-1, walletTable.get("exchange", "ETH").getBalanceAvailable().doubleValue(), DELTA);
    Assert.assertEquals(0, walletTable.get("exchange", "ETH").getUnsettledInterest().doubleValue(), DELTA);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) JSONArray(org.json.JSONArray) WalletHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with WalletManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class WalletHandler method handleWalletcallback.

/**
 * Handle the callback for a single wallet
 * @param bitfinexApiBroker
 * @param walletArray
 * @throws APIException
 */
private void handleWalletcallback(final BitfinexApiBroker bitfinexApiBroker, final JSONArray walletArray) throws APIException {
    final String walletType = walletArray.getString(0);
    final String currency = walletArray.getString(1);
    final BigDecimal balance = walletArray.getBigDecimal(2);
    final BigDecimal unsettledInterest = walletArray.getBigDecimal(3);
    final BigDecimal balanceAvailable = walletArray.optBigDecimal(4, BigDecimal.valueOf(-1));
    final Wallet wallet = new Wallet(walletType, currency, balance, unsettledInterest, balanceAvailable);
    final WalletManager walletManager = bitfinexApiBroker.getWalletManager();
    final Table<String, String, Wallet> walletTable = walletManager.getWalletTable();
    synchronized (walletTable) {
        walletTable.put(walletType, currency, wallet);
        walletTable.notifyAll();
    }
}
Also used : WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) BigDecimal(java.math.BigDecimal)

Aggregations

Wallet (com.github.jnidzwetzki.bitfinex.v2.entity.Wallet)4 WalletManager (com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager)4 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)3 WalletHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 JSONArray (org.json.JSONArray)2 Test (org.junit.Test)2 BasePortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1