Some progress on symbol config page

This commit is contained in:
Ziver Koc 2021-05-24 23:53:29 +02:00
parent 245a599b52
commit 0556882f4e
5 changed files with 52 additions and 29 deletions

View file

@ -7,7 +7,13 @@ import java.util.List;
public interface ExchangeMarket {
/**
* @return a List of all symbols supported by this exchange, symbols are defined by "{baseAsset}/{quoteAsset}"
* @return a List of all symbol names that are supported by this exchange, symbols are defined by "{baseAsset}/{quoteAsset}"
*/
List<Symbol> getSymbols();
List<String> getSymbols();
/**
* @param name one of the names provided by the {@link #getSymbol(String)} method.
* @return new Symbol instance representing the given symbol name on the exchange.
*/
Symbol getSymbol(String name);
}

View file

@ -21,8 +21,8 @@ public class ExchangeEndpoint implements WSInterface {
Exchange exchange = Exchange.getExchange(db, exchangeId);
DataNode symbols = root.set("symbols", DataNode.DataType.List);
for (Symbol symbol : exchange.getObject().getExchangeMarket().getSymbols()) {
symbols.add(symbol.getName());
for (String symbolName : exchange.getObject().getExchangeMarket().getSymbols()) {
symbols.add(symbolName);
}
return root;

View file

@ -3,14 +3,18 @@ package se.koc.trader.exchange.binance;
import se.koc.trader.api.ExchangeMarket;
import se.koc.trader.struct.Symbol;
import zutil.net.ws.rest.RESTClientFactory;
import zutil.parser.DataNode;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class BinanceMarket implements ExchangeMarket {
private BinanceRestAPI rest;
private transient BinanceRestAPI rest;
private transient List<String> symbolsCache;
public BinanceMarket() {
@ -27,10 +31,20 @@ public class BinanceMarket implements ExchangeMarket {
}
@Override
public List<Symbol> getSymbols() {
return null; // /api/v3/ticker/price
public List<String> getSymbols() {
if (symbolsCache == null) {
symbolsCache = new ArrayList<>();
DataNode symbols = rest.getExchangeInfo().get("symbols");
for (DataNode symbol : symbols) {
symbolsCache.add(symbol.getString("symbol"));
}
}
return symbolsCache;
}
@Override
public Symbol getSymbol(String symbol) {
return null; // /api/v3/ticker/price?symbol=xxx
}

View file

@ -10,6 +10,7 @@ import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.parser.Templator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
@ -37,7 +38,9 @@ public class SymbolConfigPage extends TraderPage {
// Save new input
if (request.containsKey("action")) {
int exchangeId = (ObjectUtil.isEmpty(request.get("exchange-id")) ? -1 : Integer.parseInt(request.get("exchange-id")));
int id = (ObjectUtil.isEmpty(request.get("id")) ? -1 : Integer.parseInt(request.get("id")));
Exchange exchange = Exchange.getExchange(db, exchangeId);
Symbol symbol = null;
if (id >= 0)
@ -46,22 +49,11 @@ public class SymbolConfigPage extends TraderPage {
switch(request.get("action")) {
case "create_symbol":
logger.info("Creating symbol: " + request.get("name"));
symbol = new Symbol();
/* FALLTHROUGH */
case "modify_symbol":
if (symbol != null) {
logger.info("Modifying sensor: " + symbol.getName());
symbol.setName(request.get("name"));
symbol.save(db);
symbol = exchange.getObject().getExchangeMarket().getSymbol(request.get("symbol-name"));
symbol.save(db);
TraderContext.getMessageManager().add(new UserMessage(
MessageLevel.SUCCESS, "Successfully saved symbol: " + symbol.getName(), MessageTTL.ONE_VIEW));
} else {
logger.warning("Unknown sensor id: " + id);
TraderContext.getMessageManager().add(new UserMessage(
MessageLevel.ERROR, "Unknown sensor id: " + id, MessageTTL.ONE_VIEW));
}
break;
TraderContext.getMessageManager().add(new UserMessage(
MessageLevel.SUCCESS, "Successfully saved symbol: " + symbol.getName(), MessageTTL.ONE_VIEW));
case "remove_symbol":
if (symbol != null) {
@ -81,11 +73,10 @@ public class SymbolConfigPage extends TraderPage {
// Output
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
tmpl.set("exchanges", Exchange.getExchanges(db));
tmpl.set("symbols", Symbol.getSymbols(db));
tmpl.set("exchanges", Exchange.getExchanges(db));
return tmpl;
}
}

View file

@ -63,11 +63,23 @@
<!------------- MODALS --------------->
<script>
var exchangeSymbols = {
{{#exchanges}}
{{.getId()}}: [
{{#.getObject().getExchangeMarket().getSymbols()}}"{{.}}",{{/.getObject().getExchangeMarket().getSymbols()}}
],
{{/exchanges}}
};
$(function() {
initDynamicModalForm("symbolModal");
$("#exchange").change(function() {
alert(this.value);
var options = $("#symbol-name");
options.empty();
$.each(exchangeSymbols[this.value], function(index, value) {
options.append($("<option />").val(value).text(value));
});
});
});
</script>
@ -85,8 +97,8 @@
<input type="hidden" id="id" name="id">
<div>
<label class="control-label" for="exchange">Exchange:</label>
<select id="exchange" name="exchange" class="form-control">
<label class="control-label" for="exchange-id">Exchange:</label>
<select id="exchange-id" name="exchange-id" class="form-control">
<option></option>
{{#exchanges}}
<option value="{{.getId()}}">{{.getName()}}</option>
@ -95,8 +107,8 @@
</div>
<div>
<label class="control-label" for="symbol">Symbol:</label>
<select id="symbol" name="symbol" class="form-control">
<label class="control-label" for="symbol-name">Symbol:</label>
<select id="symbol-name" name="symbol-name" class="form-control">
</select>
</div>
</div>