]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - api/market_config.go
Account page.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / market_config.go
index d85af4de9e3c5f3a7e6f18c65d162ce130aa1d1e..c2248b354e876403361f7a67169ae845ab7e2e90 100644 (file)
@@ -45,6 +45,14 @@ func (q MarketConfigQuery) Run() (interface{}, *Error) {
 
        }
 
+       if _, ok := config.Config["key"]; !ok {
+               config.Config["key"] = ""
+       }
+
+       if _, ok := config.Config["secret"]; !ok {
+               config.Config["secret"] = ""
+       }
+
        return config.Config, nil
 }
 
@@ -76,13 +84,13 @@ func (q MarketBalanceQuery) Run() (interface{}, *Error) {
        }
 
        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)}
+               return nil, &Error{InvalidMarketCredentials, "your credentials for this market are not setup", fmt.Errorf("'%v' credentials are not setup", q.In.Market)}
        }
 
        result := struct {
                Value         decimal.Decimal            `json:"value"`
                ValueCurrency string                     `json:"valueCurrency"`
-               Balance       map[string]decimal.Decimal `json:"balance"`
+               Balance       map[string]markets.Balance `json:"balance"`
        }{}
 
        resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error {
@@ -92,18 +100,23 @@ func (q MarketBalanceQuery) Run() (interface{}, *Error) {
                        return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
                }
 
-               if err != nil {
-                       return NewInternalError(err)
+               if utils.ErrIs(err, markets.IPRestricted) {
+                       return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
                }
 
-               value, err := Poloniex.ComputeAccountBalanceValue(balance, q.In.Currency)
                if err != nil {
                        return NewInternalError(err)
                }
 
-               result.Balance = balance
-               result.ValueCurrency = q.In.Currency
-               result.Value = value.Round(8)
+               for currency, value := range balance.Balances {
+                       if value.BTCValue.Abs().LessThan(decimal.NewFromFloat(0.0001)) {
+                               delete(balance.Balances, currency)
+                       }
+               }
+
+               result.Balance = balance.Balances
+               result.ValueCurrency = "BTC"
+               result.Value = balance.BTCValue.Round(8)
 
                return nil
        })