X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=api%2Fmarket_config.go;h=09eb8a9c00ea2aa22a4521bea2f382afd4c9442a;hb=c6aa553f48d1014f651441bbd9e023508d0a819c;hp=d85af4de9e3c5f3a7e6f18c65d162ce130aa1d1e;hpb=2f91f20a8645339385ada602684f4957f20f4da4;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FFront.git diff --git a/api/market_config.go b/api/market_config.go index d85af4d..09eb8a9 100644 --- a/api/market_config.go +++ b/api/market_config.go @@ -5,11 +5,9 @@ import ( "strings" "time" - "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets" - "github.com/jloup/utils" - "github.com/shopspring/decimal" "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db" + "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets" ) type MarketConfigQuery struct { @@ -45,74 +43,65 @@ 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 } -type MarketBalanceQuery struct { +type TestMarketCredentialsQuery struct { In struct { - User db.User - Market string - Currency string + User db.User + Market string } } -func (q MarketBalanceQuery) ValidateParams() *Error { +func (q TestMarketCredentialsQuery) 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" && q.In.Currency != "ETH" { - return &Error{BadRequest, "invalid currency, accept [BTC, USDT, ETH]", fmt.Errorf("'%v' is not a valid currency", q.In.Currency)} - } - return nil } -func (q MarketBalanceQuery) Run() (interface{}, *Error) { +func (q TestMarketCredentialsQuery) 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)} + if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" { + return nil, &Error{MarketCredentialsNotConfigured, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)} } - result := struct { - Value decimal.Decimal `json:"value"` - ValueCurrency string `json:"valueCurrency"` - Balance map[string]decimal.Decimal `json:"balance"` - }{} - - resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error { - balance, err := Poloniex.GetBalance(config.Config["key"], config.Config["secret"]) + resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error { + err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"]) if utils.ErrIs(err, markets.InvalidCredentials) { 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) - return nil }) if resultErr != nil { - return nil, resultErr + return nil, NewInternalError(resultErr) } - return &result, nil + return nil, nil } type UpdateMarketConfigQuery struct { @@ -144,10 +133,74 @@ func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) { configMap["secret"] = q.In.Secret } - _, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) + marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) + if err != nil { + return nil, NewInternalError(err) + } + + resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error { + err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"]) + + if utils.ErrIs(err, markets.InvalidCredentials) { + return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)} + } + + if utils.ErrIs(err, markets.IPRestricted) { + return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)} + } + + if err != nil { + return NewInternalError(err) + } + + return nil + }) + + var newStatus db.MarketConfigStatus = db.MarketConfigEnabled + + if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) { + newStatus = db.MarketConfigInvalidCredentials + } else if resultErr != nil { + return nil, NewInternalError(resultErr) + } + + marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus) if err != nil { return nil, NewInternalError(err) } return nil, nil } + +type GetPortfolioQuery struct { + In struct { + User db.User + Market string + } +} + +func (q GetPortfolioQuery) 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)} + } + + return nil +} + +func (q GetPortfolioQuery) Run() (interface{}, *Error) { + marketConfig, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) + if err != nil { + return nil, NewInternalError(err) + } + + report, err := GetWeekPortfolio(*marketConfig) + if ErrorIs(err, NotFound) { + return nil, err.(*Error) + } + + if err != nil { + return nil, NewInternalError(err) + } + + return report.Round(), nil +}