From ebe4089c48a445e4246d49d75883a28c5cb24933 Mon Sep 17 00:00:00 2001 From: avalkov Date: Thu, 18 Aug 2022 15:18:41 +0300 Subject: [PATCH] Added cudos network total supply endpoint --- cmd/stats-service/main.go | 1 + config.yaml | 1 + internal/config/config.go | 17 +++++++++-------- internal/handlers/handlers.go | 24 ++++++++++++++++++++++++ internal/tasks/inflation.go | 9 ++++++++- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/cmd/stats-service/main.go b/cmd/stats-service/main.go index be12c90..f08dff7 100644 --- a/cmd/stats-service/main.go +++ b/cmd/stats-service/main.go @@ -73,6 +73,7 @@ func main() { http.HandleFunc("/cosmos/bank/v1beta1/supply", handlers.GetSupplyHandler(cfg, keyValueStorage)) http.HandleFunc("/circulating-supply", handlers.GetCircSupplyTextHandler(cfg, keyValueStorage)) http.HandleFunc("/json/circulating-supply", handlers.GetCircSupplyJSONHandler(cfg, keyValueStorage)) + http.HandleFunc("/total-supply", handlers.GetCudosNetworkTotalSupply(cfg, keyValueStorage)) http.HandleFunc("/stats", handlers.GetStatsHandler(cfg, keyValueStorage)) log.Info().Msg(fmt.Sprintf("Listening on port: %d", cfg.Port)) diff --git a/config.yaml b/config.yaml index 70f3fc0..305ccb9 100644 --- a/config.yaml +++ b/config.yaml @@ -37,4 +37,5 @@ storage: all_tokens_supply_key: all_tokens_supply supply_key: supply supply_height_key: supply_height + cudos_network_total_supply_key: cudos_network_total_supply diff --git a/internal/config/config.go b/internal/config/config.go index 50c8beb..e491b70 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -49,13 +49,14 @@ type Config struct { InflationSinceDays int64 `yaml:"inflation_since_days"` } `yaml:"calculation"` Storage struct { - APRKey string `yaml:"apr_key"` - APRHeightKey string `yaml:"apr_height_key"` - AnnualProvisionsKey string `yaml:"annual_provisions"` - InflationKey string `yaml:"inflation_key"` - InflationHeightKey string `yaml:"inflation_height_key"` - AllTokensSupplyKey string `yaml:"all_tokens_supply_key"` - SupplyKey string `yaml:"supply_key"` - SupplyHeightKey string `yaml:"supply_height_key"` + APRKey string `yaml:"apr_key"` + APRHeightKey string `yaml:"apr_height_key"` + AnnualProvisionsKey string `yaml:"annual_provisions"` + InflationKey string `yaml:"inflation_key"` + InflationHeightKey string `yaml:"inflation_height_key"` + AllTokensSupplyKey string `yaml:"all_tokens_supply_key"` + SupplyKey string `yaml:"supply_key"` + SupplyHeightKey string `yaml:"supply_height_key"` + CudosNetworkTotalSupplyKey string `yaml:"cudos_network_total_supply_key"` } `yaml:"storage"` } diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 515ab8a..9c59efb 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -195,6 +195,30 @@ func GetParamsHandler(cfg config.Config) func(http.ResponseWriter, *http.Request } } +func GetCudosNetworkTotalSupply(cfg config.Config, storage keyValueStorage) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + supply, err := storage.GetValue(cfg.Storage.CudosNetworkTotalSupplyKey) + if err != nil { + badRequest(w, err) + return + } + + formattedSupply, err := formatSupply(supply) + if err != nil { + badRequest(w, err) + return + } + + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(http.StatusOK) + + if _, err := w.Write([]byte(formattedSupply)); err != nil { + badRequest(w, err) + return + } + } +} + func formatSupply(supply string) (string, error) { bigSupply, ok := new(big.Int).SetString(supply, 10) if !ok || bigSupply == nil { diff --git a/internal/tasks/inflation.go b/internal/tasks/inflation.go index 739325f..5e8bfa2 100644 --- a/internal/tasks/inflation.go +++ b/internal/tasks/inflation.go @@ -85,8 +85,11 @@ func getCalculateInflationHandler(genesisState cudoMintTypes.GenesisState, cfg c return fmt.Errorf("error while getting total supply: %s", err) } + var cudosNetworkTotalSupply sdk.Int + for i := 0; i < len(totalSupply.Supply); i++ { if totalSupply.Supply[i].Denom == cfg.Genesis.MintDenom { + cudosNetworkTotalSupply = totalSupply.Supply[i].Amount totalSupply.Supply[i].Amount = currentTotalSupply } } @@ -97,7 +100,7 @@ func getCalculateInflationHandler(genesisState cudoMintTypes.GenesisState, cfg c } if err := storage.SetValue(cfg.Storage.AllTokensSupplyKey, string(totalSupplyJSON)); err != nil { - return fmt.Errorf("failed to set value %s for key %s", currentTotalSupply.String(), cfg.Storage.AllTokensSupplyKey) + return fmt.Errorf("failed to set value %s for key %s", string(totalSupplyJSON), cfg.Storage.AllTokensSupplyKey) } if err := storage.SetValue(cfg.Storage.SupplyKey, currentTotalSupply.String()); err != nil { @@ -108,6 +111,10 @@ func getCalculateInflationHandler(genesisState cudoMintTypes.GenesisState, cfg c return fmt.Errorf("failed to set value %d for key %s", latestCudosBlock, cfg.Storage.SupplyHeightKey) } + if err := storage.SetValue(cfg.Storage.CudosNetworkTotalSupplyKey, cudosNetworkTotalSupply.String()); err != nil { + return fmt.Errorf("failed to set value %s for key %s", cudosNetworkTotalSupply.String(), cfg.Storage.CudosNetworkTotalSupplyKey) + } + return nil } }