]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - api/market_config.go
poloniex balance draft
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / market_config.go
index 3fd10ae000d19fba89f6e86b4911a1dde21bf294..ce7918495cac8d9cb5c06144a13e82aab04e86f7 100644 (file)
@@ -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