Commit e9d8d7e1 authored by Cranky Kernel's avatar Cranky Kernel

Highlight BNB balance when low.

Gitlab issue:
#44
parent 24c3dba3
Pipeline #55498312 passed with stage
in 13 minutes and 42 seconds
......@@ -29,6 +29,8 @@
- Add simple Binance balance view.
- Paginate trade history view. Too many trades can harm performance,
server side and in the browser.
- Highlight the BNB balance when
low. https://gitlab.com/crankykernel/maker/issues/44
[Full Changelog](https://gitlab.com/crankykernel/maker/compare/0.3.2...master)
......
......@@ -62,6 +62,12 @@ func Set(key string, val string) {
viper.Set(key, val)
}
func SetFloat64(key string, val float64) {
lock.Lock()
defer lock.Unlock()
viper.Set(key, val)
}
func GetString(key string) string {
return viper.GetString(key)
}
......@@ -20,17 +20,28 @@ import (
"github.com/crankykernel/binanceapi-go"
"gitlab.com/crankykernel/maker/go/config"
"gitlab.com/crankykernel/maker/go/log"
"io/ioutil"
"net/http"
)
func SavePreferencesHandler(w http.ResponseWriter, r *http.Request) {
type preferenceConfig struct {
BalancePercents string `json:"balancePercents"`
BalancePercents string `json:"preferences.balance.percents"`
BinanceBnbWarningLevel float64 `json:"preferences.binance.bnbwarninglevel"`
}
var request preferenceConfig
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&request); err != nil {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.WithFields(log.Fields{
"path": r.URL.Path,
"method": r.Method,
}).WithError(err).Errorf("Failed to decode Binance configuration.")
WriteJsonError(w, http.StatusBadRequest, err.Error())
return
}
err = json.Unmarshal(body, &request)
if err != nil {
log.WithFields(log.Fields{
"path": r.URL.Path,
"method": r.Method,
......@@ -39,7 +50,10 @@ func SavePreferencesHandler(w http.ResponseWriter, r *http.Request) {
return
}
log.Printf("%+v", request)
config.Set("preferences.balance.percents", request.BalancePercents)
config.SetFloat64("preferences.binance.bnbwarninglevel", request.BinanceBnbWarningLevel)
config.WriteConfig(ServerFlags.ConfigFilename)
}
......
......@@ -21,6 +21,8 @@ import {Logger, LoggerService} from "./logger.service";
export const DEFAULT_BALANCE_PERCENTS = "5,10,25,50,75,100";
export const DEFAULT_BINANCE_BNB_WARNING_LEVEL = 1.0;
@Injectable({
providedIn: "root"
})
......@@ -41,9 +43,13 @@ export class ConfigService {
this.logger.log("Returning cached configuration.");
return of(this.config);
}
return this.makerApi.getConfig().pipe(tap((config) => {
return this.makerApi.getConfig().pipe(map((config) => {
this.logger.log("Fetching configuration from server.");
this.config = config;
if (this.config[ConfigKey.PREFERENCE_BINANCE_BNB_WARNING_LEVEL] === undefined) {
this.config[ConfigKey.PREFERENCE_BINANCE_BNB_WARNING_LEVEL] = DEFAULT_BINANCE_BNB_WARNING_LEVEL;
}
return this.config;
}));
}
......@@ -85,4 +91,5 @@ export enum ConfigKey {
"BINANCE_API_KEY" = "binance.api.key",
"BINANCE_API_SECRET" = "binance.api.secret",
"PREFERENCE_BALANCE_PERCENTS" = "preferences.balance.percents",
"PREFERENCE_BINANCE_BNB_WARNING_LEVEL" = "preferences.binance.bnbwarninglevel",
}
......@@ -81,6 +81,13 @@
formControlName="balancePercents">
</div>
<div class="form-group">
<label>BNB Warning Level
</label>
<input type="number" class="form-control" step="0.1" min="0"
formControlName="binanceBnbWarningLevel">
</div>
<div class="row">
<div class="col">
<button type="button" class="btn btn-primary btn-block"
......
......@@ -19,7 +19,13 @@ import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {MakerApiService} from "../maker-api.service";
import {ToastrService} from "../toastr.service";
import {Router} from "@angular/router";
import {Config, ConfigKey, ConfigService, DEFAULT_BALANCE_PERCENTS} from "../config.service";
import {
Config,
ConfigKey,
ConfigService,
DEFAULT_BALANCE_PERCENTS,
DEFAULT_BINANCE_BNB_WARNING_LEVEL
} from "../config.service";
@Component({
selector: "app-config",
......@@ -72,8 +78,13 @@ export class ConfigComponent implements OnInit {
} else {
percents = DEFAULT_BALANCE_PERCENTS;
}
let binanceBnbWarningLevel: number = DEFAULT_BINANCE_BNB_WARNING_LEVEL;
if (this.configService.config[ConfigKey.PREFERENCE_BINANCE_BNB_WARNING_LEVEL]) {
binanceBnbWarningLevel = this.configService.config[ConfigKey.PREFERENCE_BINANCE_BNB_WARNING_LEVEL];
}
this.preferenceForm = this.fb.group({
balancePercents: [percents, Validators.pattern(/^[\d\., ]+$/)],
binanceBnbWarningLevel: [binanceBnbWarningLevel],
});
}
......@@ -122,7 +133,7 @@ export class ConfigComponent implements OnInit {
savePreferences() {
const prefs: PreferenceForm = <PreferenceForm>this.preferenceForm.value;
this.configService.set(ConfigKey.PREFERENCE_BALANCE_PERCENTS, prefs.balancePercents);
this.configService.config.balancePercents = prefs.balancePercents;
this.configService.set(ConfigKey.PREFERENCE_BINANCE_BNB_WARNING_LEVEL, prefs.binanceBnbWarningLevel);
this.configService.savePreferences().subscribe(() => {
this.toastr.success("Preferences saved.");
}, (error) => {
......@@ -143,4 +154,5 @@ interface BinanceForm {
interface PreferenceForm {
balancePercents: string;
binanceBnbWarningLevel: number;
}
......@@ -337,7 +337,10 @@
</tr>
<tr>
<th>BNB Balance</th>
<td *ngIf="balances['BNB']">
<td *ngIf="balances['BNB'] && balances['BNB'].free >= bnbWarningLevel">
{{balances['BNB'].free | number:".8-8"}}
</td>
<td *ngIf="balances['BNB'] && balances['BNB'].free < bnbWarningLevel" class="bg-warning">
{{balances['BNB'].free | number:".8-8"}}
</td>
</tr>
......
......@@ -22,7 +22,7 @@ import {Subscription} from "rxjs/Subscription";
import * as Mousetrap from "mousetrap";
import * as $ from "jquery";
import {round8, roundx} from "../utils";
import {ConfigService} from "../config.service";
import {ConfigKey, ConfigService, DEFAULT_BINANCE_BNB_WARNING_LEVEL} from "../config.service";
import {MakerService, TradeMap} from "../maker.service";
import {Logger, LoggerService} from "../logger.service";
import {FormBuilder, FormGroup} from "@angular/forms";
......@@ -129,6 +129,8 @@ export class TradeComponent implements OnInit, OnDestroy, AfterViewInit {
viewTrades = "all";
bnbWarningLevel: number = DEFAULT_BINANCE_BNB_WARNING_LEVEL;
@ViewChild(TradeTableComponent) private tradeTable: TradeTableComponent;
constructor(private api: BinanceApiService,
......@@ -159,6 +161,7 @@ export class TradeComponent implements OnInit, OnDestroy, AfterViewInit {
this.config.loadConfig().subscribe(() => {
this.balancePercents = this.config.getBalancePercents();
this.orderFormSettings.balancePercent = this.balancePercents[0];
this.bnbWarningLevel = this.config.config[ConfigKey.PREFERENCE_BINANCE_BNB_WARNING_LEVEL];
this.reloadState();
// Setup the trailing stop reactive form. It might be more work
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment