1 package org.oxerr.okcoin.xchange.service.fix;
2
3 import java.math.BigDecimal;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.knowm.xchange.currency.Currency;
8 import org.knowm.xchange.currency.CurrencyPair;
9 import org.knowm.xchange.dto.account.AccountInfo;
10 import org.knowm.xchange.dto.account.Balance;
11 import org.knowm.xchange.dto.account.Wallet;
12 import org.oxerr.okcoin.fix.fix44.AccountInfoResponse;
13
14 import quickfix.FieldNotFound;
15 import quickfix.Message;
16
17
18
19
20 public final class OKCoinFIXAdapters {
21
22 private OKCoinFIXAdapters() {
23 }
24
25 public static String adaptSymbol(CurrencyPair currencyPair) {
26 return String.format("%s/%s", currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode());
27 }
28
29 public static CurrencyPair adaptCurrencyPair(String symbol) {
30 String[] symbols = symbol.split("/");
31 CurrencyPair currencyPair = new CurrencyPair(symbols[0], symbols[1]);
32 return currencyPair;
33 }
34
35 public static AccountInfo adaptAccountInfo(AccountInfoResponse message)
36 throws FieldNotFound {
37 final String[] currencies = message.getCurrency().getValue().split("/");
38 final String[] balances = message.getBalance().getValue().split("/");
39
40 final int walletCount = currencies.length;
41 final List<Balance> balanceList = new ArrayList<>(walletCount);
42
43 for (int i = 0; i < walletCount; i++) {
44 final String currency = currencies[i];
45 final BigDecimal available = new BigDecimal(balances[i]);
46 final Balance balance = new Balance(Currency.getInstance(currency), available, available);
47 balanceList.add(balance);
48 }
49
50 final Wallet wallet = new Wallet(balanceList);
51 return new AccountInfo(wallet);
52 }
53
54 }