Skip to content

Commit

Permalink
Added cudos network total supply endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
avalkov committed Aug 18, 2022
1 parent 2f82cd6 commit ebe4089
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/stats-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

17 changes: 9 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
24 changes: 24 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion internal/tasks/inflation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand Down

0 comments on commit ebe4089

Please sign in to comment.