Fix Binance trade synchronization

This commit is contained in:
Tanguy Herbron 2018-04-29 19:59:06 +02:00
parent 009dc09eca
commit 6d3e5b7bfd
4 changed files with 86 additions and 28 deletions

View File

@ -24,6 +24,7 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MotionEvent; import android.view.MotionEvent;
@ -54,6 +55,7 @@ import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener; import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.nauk.moodl.DataManagers.CurrencyData.Currency; import com.nauk.moodl.DataManagers.CurrencyData.Currency;
import com.nauk.moodl.DataManagers.CurrencyData.CurrencyDataChart; import com.nauk.moodl.DataManagers.CurrencyData.CurrencyDataChart;
import com.nauk.moodl.DataManagers.CurrencyData.Trade;
import com.nauk.moodl.DataManagers.CurrencyData.Transaction; import com.nauk.moodl.DataManagers.CurrencyData.Transaction;
import com.nauk.moodl.DataManagers.DatabaseManager; import com.nauk.moodl.DataManagers.DatabaseManager;
import com.nauk.moodl.DataManagers.ExchangeManager.BinanceManager; import com.nauk.moodl.DataManagers.ExchangeManager.BinanceManager;
@ -63,6 +65,7 @@ import com.nauk.moodl.LayoutManagers.TransactionListAdapter;
import com.nauk.moodl.PlaceholderManager; import com.nauk.moodl.PlaceholderManager;
import com.nauk.moodl.R; import com.nauk.moodl.R;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -105,6 +108,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
private Button lineChartButton; private Button lineChartButton;
private Button candleStickChartButton; private Button candleStickChartButton;
private ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> returnedTrades;
private View loadingFooter; private View loadingFooter;
@ -918,26 +922,29 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
private void drawTradeList(ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades) private void drawTradeList(ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades)
{ {
tradeLayout.setOnScrollListener(new AbsListView.OnScrollListener() { if(returnedTrades.size() > 20)
@Override {
public void onScrollStateChanged(AbsListView absListView, int i) { tradeLayout.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
} }
@Override @Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0) if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(!flag_loading)
{ {
flag_loading = true; if(!flag_loading && tradeLayout.getCount() != returnedTrades.size() - 1)
{
flag_loading = true;
TradeAdder tradeAdder = new TradeAdder(); TradeAdder tradeAdder = new TradeAdder();
tradeAdder.execute(); tradeAdder.execute();
}
} }
} }
} });
}); }
tradeListAdapter = new TradeListAdapter(this, trades); tradeListAdapter = new TradeListAdapter(this, trades);
@ -987,8 +994,13 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
Looper.prepare(); Looper.prepare();
} }
ArrayList<Transaction> transactionList = databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol()); final ArrayList<Transaction> transactionList = databaseManager.getCurrencyTransactionsForSymbol(currency.getSymbol());
drawTransactionList(transactionList); runOnUiThread(new Runnable() {
@Override
public void run() {
drawTransactionList(transactionList);
}
});
return null; return null;
} }
@ -1019,7 +1031,23 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
binanceManager.updateTrades(new BinanceManager.BinanceCallBack() { final ArrayList<Trade> trades = new ArrayList<>();
for(int i = tradeLayout.getCount(); i < tradeLayout.getCount() + 20 && i < returnedTrades.size(); i++)
{
trades.add(returnedTrades.get(i));
}
runOnUiThread(new Runnable() {
@Override
public void run() {
tradeListAdapter.addAll(trades);
tradeListAdapter.notifyDataSetChanged();
flag_loading = false;
tradeLayout.removeFooterView(loadingFooter);
}
});
/*binanceManager.updateTrades(new BinanceManager.BinanceCallBack() {
@Override @Override
public void onSuccess() { public void onSuccess() {
ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades = binanceManager.getTrades(); ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades = binanceManager.getTrades();
@ -1046,7 +1074,7 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
public void onError(String error) { public void onError(String error) {
} }
}, currency.getSymbol(), tradeListAdapter.getItem(tradeListAdapter.getCount() - 1).getId()); }, currency.getSymbol(), tradeListAdapter.getItem(tradeListAdapter.getCount() - 1).getId());*/
return null; return null;
} }
@ -1073,9 +1101,9 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
@Override @Override
public void onSuccess() { public void onSuccess() {
ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades = binanceManager.getTrades(); ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> trades = binanceManager.getTrades();
final ArrayList<com.nauk.moodl.DataManagers.CurrencyData.Trade> returnedTrades = new ArrayList<>(); returnedTrades = new ArrayList<>();
for(int i = trades.size() - 1; i > 0 ; i--) for(int i = trades.size() - 1; i >= 0 ; i--)
{ {
returnedTrades.add(trades.get(i)); returnedTrades.add(trades.get(i));
} }
@ -1083,7 +1111,14 @@ public class CurrencyDetailsActivity extends AppCompatActivity {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
drawTradeList(returnedTrades); ArrayList<Trade> trades = new ArrayList<>();
for(int i = 0; i < 20 && i < returnedTrades.size(); i++)
{
trades.add(returnedTrades.get(i));
}
drawTradeList(trades);
} }
}); });
} }

View File

@ -77,7 +77,10 @@ public class BinanceManager {
for(int i = 0; i < pairSymbolList.size(); i++) for(int i = 0; i < pairSymbolList.size(); i++)
{ {
trades.addAll(updateTrades(symbol, pairSymbolList.get(i))); if(!pairSymbolList.get(i).equals(symbol))
{
trades.addAll(updateTrades(symbol, pairSymbolList.get(i)));
}
} }
callBack.onSuccess(); callBack.onSuccess();
@ -89,7 +92,10 @@ public class BinanceManager {
for(int i = 0; i < pairSymbolList.size(); i++) for(int i = 0; i < pairSymbolList.size(); i++)
{ {
trades.addAll(updateTrades(symbol, pairSymbolList.get(i), fromId)); if(!pairSymbolList.get(i).equals(symbol))
{
trades.addAll(updateTrades(symbol, pairSymbolList.get(i), fromId));
}
} }
callBack.onSuccess(); callBack.onSuccess();
@ -106,16 +112,16 @@ public class BinanceManager {
if(!symbol.equals(pairSymbol)) if(!symbol.equals(pairSymbol))
{ {
try { try {
presentTrades = client.getMyTrades(symbol + pairSymbol, 20); presentTrades = client.getMyTrades(symbol + pairSymbol);
} catch (BinanceApiException e) { } catch (BinanceApiException e) {
try { /*try {
presentTrades = client.getMyTrades(pairSymbol + symbol, 20); presentTrades = client.getMyTrades(pairSymbol + symbol);
} catch (BinanceApiException f) { } catch (BinanceApiException f) {
f.printStackTrace(); f.printStackTrace();
} }*/
} }
} }

View File

@ -31,7 +31,9 @@ public class HitBtcManager {
private String publicKey; private String publicKey;
private String privateKey; private String privateKey;
final private String hitBalanceUrl = "https://api.hitbtc.com/api/2/trading/balance"; final private String hitBalanceUrl = "https://api.hitbtc.com/api/2/trading/balance";
final private String tradeHistoryUrl = "https://api.hitbtc.com/api/2/history/trades?";
private RequestQueue requestQueue; private RequestQueue requestQueue;
private List<String> pairSymbolList;
private List<Currency> balance; private List<Currency> balance;
private android.content.Context context; private android.content.Context context;
@ -45,6 +47,21 @@ public class HitBtcManager {
this.privateKey = privateKey; this.privateKey = privateKey;
} }
private void createPairSymbolList()
{
pairSymbolList = new ArrayList<>();
pairSymbolList.add("BTC");
pairSymbolList.add("ETH");
pairSymbolList.add("BNB");
pairSymbolList.add("USDT");
}
public void updateTrades(final HitBtcCallBack callBack, String symbol, String pairSymbol)
{
}
public void updateBalance(final HitBtcCallBack callBack) public void updateBalance(final HitBtcCallBack callBack)
{ {
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, hitBalanceUrl JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, hitBalanceUrl

View File

@ -647,7 +647,7 @@
<ListView <ListView
android:id="@+id/listTrades" android:id="@+id/listTrades"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:orientation="vertical"/> android:orientation="vertical"/>
</LinearLayout> </LinearLayout>