aboutsummaryrefslogblamecommitdiff
path: root/api/market_config.go
blob: d5f05d9743df29c178d3eddd325003e3588697a3 (plain) (tree)
1
2
3
4
5
6
7
8
9



             

                 
 

                                                        
                                


































                                                                                                                            







                                                  


                                 
                                        
                   

                              


         
                                                             



                                                                                                                            


                  
                                                                 




                                                                        
                                                                                         
                                                                                                                                                                      

         
                                                                                                                                                       
                                                                                                                                




                                                                                                                                                     

                                                                                                                                 

                 



                                                    


                          






                                                                                                                                                         
                                                       

                                                  

         






                                                                          
                       

 













                                                                                                                            


                                                    











                                                              





                                                                                                                                                       
                                                                                                                                            

















                                                                                                                                                     
                                                                                                                                                         





                                                                              





                                                 
































                                                                                                                            
package api

import (
	"fmt"
	"strings"
	"time"

	"git.immae.eu/Cryptoportfolio/Front.git/db"
	"git.immae.eu/Cryptoportfolio/Front.git/markets"
	"github.com/jloup/utils"
)

type MarketConfigQuery struct {
	In struct {
		User   db.User
		Market string
	}
}

func (q MarketConfigQuery) 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 MarketConfigQuery) Run() (interface{}, *Error) {
	config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
	if err != nil {
		return nil, NewInternalError(err)
	}

	if config == nil {
		configMap := make(map[string]string)
		configMap["key"] = ""
		configMap["secret"] = ""

		config, err = db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
		if err != nil {
			return nil, NewInternalError(err)
		}

	}

	if _, ok := config.Config["key"]; !ok {
		config.Config["key"] = ""
	}

	if _, ok := config.Config["secret"]; !ok {
		config.Config["secret"] = ""
	}

	return config.Config, nil
}

type TestMarketCredentialsQuery struct {
	In struct {
		User   db.User
		Market string
	}
}

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)}
	}

	return nil
}

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 == 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)}
	}

	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"], EXTERNAL_SERVICE_TIMEOUT_SECONDS)

		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 = config.Status

	if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) || ErrorIs(resultErr, MarketCredentialsNotConfigured) {
		newStatus = db.MarketConfigInvalidCredentials
	} else if resultErr != nil {
		return nil, NewInternalError(resultErr)
	} else if resultErr != nil {
		return nil, NewInternalError(resultErr)
	} else if resultErr == nil {
		newStatus = db.MarketConfigEnabled
	}

	if newStatus != config.Status {
		config, err = db.SetMarketConfigStatus(*config, newStatus)
		if err != nil {
			return nil, NewInternalError(err)
		}
	}

	return nil, nil
}

type UpdateMarketConfigQuery struct {
	In struct {
		User   db.User
		Market string
		Key    string
		Secret string
	}
}

func (q UpdateMarketConfigQuery) ValidateParams() *Error {
	if q.In.Market == "" {
		return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
	}

	q.In.Secret = strings.TrimSpace(q.In.Secret)
	q.In.Key = strings.TrimSpace(q.In.Key)

	return nil
}

func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
	configMap := make(map[string]string)
	if q.In.Key != "" {
		configMap["key"] = q.In.Key
	}
	if q.In.Secret != "" {
		configMap["secret"] = q.In.Secret
	}

	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"], EXTERNAL_SERVICE_TIMEOUT_SECONDS)

		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) || ErrorIs(resultErr, MarketCredentialsNotConfigured) {
		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
}