X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=api%2Fmarket_config.go;h=ce7918495cac8d9cb5c06144a13e82aab04e86f7;hb=refs%2Fheads%2Fpoloniex_balance;hp=3fd10ae000d19fba89f6e86b4911a1dde21bf294;hpb=3602fbf8412d69900d793a963c8e774f487f5e45;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FFront.git diff --git a/api/market_config.go b/api/market_config.go index 3fd10ae..ce79184 100644 --- a/api/market_config.go +++ b/api/market_config.go @@ -3,7 +3,9 @@ package api import ( "fmt" + "github.com/shopspring/decimal" "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db" + "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets" ) type MarketConfigQuery struct { @@ -42,6 +44,56 @@ func (q MarketConfigQuery) Run() (interface{}, *Error) { return config.Config, nil } +type MarketBalanceQuery struct { + In struct { + User db.User + Market string + Currency string + } +} + +var Poloniex *markets.Poloniex + +func init() { + Poloniex = markets.NewPoloniex() + Poloniex.StartTicker() +} + +func (q MarketBalanceQuery) ValidateParams() *Error { + if q.In.Market != "poloniex" { + return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} + } + + // TODO: we should request market for available currencies. + if q.In.Currency != "BTC" || q.In.Currency != "USDT" { + return &Error{BadRequest, "invalid currency, accept [BTC, USDT]", fmt.Errorf("'%v' is not a valid currency", q.In.Currency)} + } + + return nil +} + +func (q MarketBalanceQuery) Run() (interface{}, *Error) { + config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) + if err != nil { + return nil, NewInternalError(err) + } + + if config.Config["key"] == "" || config.Config["secret"] == "" { + return nil, &Error{BadRequest, "your credentials for this market are not setup", fmt.Errorf("'%v' credentials are not setup", q.In.Market)} + } + + balance, err := Poloniex.GetBalance(config.Config["key"], config.Config["secret"], q.In.Currency) + if err != nil { + return nil, NewInternalError(err) + } + + result := struct { + Balance decimal.Decimal `json:balance` + }{balance} + + return &result, nil +} + type UpdateMarketConfigQuery struct { In struct { User db.User