From 78d25a27059e097fe4812ee732bf4558dd71c816 Mon Sep 17 00:00:00 2001 From: kaifei Date: Mon, 6 Aug 2018 16:21:15 +0800 Subject: [PATCH 1/3] add getValidatorExRate API --- Gopkg.lock | 3 +- client/ex_rate.go | 80 ++++++++++++++++++++++++++++++++++++++++++ client/ex_rate_test.go | 50 ++++++++++++++++++++++++++ client/lcd.go | 1 + 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 client/ex_rate.go create mode 100644 client/ex_rate_test.go diff --git a/Gopkg.lock b/Gopkg.lock index 622cc6fae..c418aa931 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -198,7 +198,6 @@ ".", "hcl/ast", "hcl/parser", - "hcl/printer", "hcl/scanner", "hcl/strconv", "hcl/token", @@ -596,6 +595,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "faea5e0943a8cfc0fd6185c9c6d45fb254f81e5c5d0f67553b185e5e7e60bf40" + inputs-digest = "6656d188f89610f47c383af201af4c47dffbdf8241b372720fe66aa614a5a426" solver-name = "gps-cdcl" solver-version = 1 diff --git a/client/ex_rate.go b/client/ex_rate.go new file mode 100644 index 000000000..9a8531702 --- /dev/null +++ b/client/ex_rate.go @@ -0,0 +1,80 @@ +package client + +import ( + "github.com/cosmos/cosmos-sdk/x/stake" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/wire" + "github.com/gorilla/mux" + "net/http" + "encoding/json" + sdk "github.com/cosmos/cosmos-sdk/types" + cmn "github.com/tendermint/tendermint/libs/common" + "github.com/cosmos/cosmos-sdk/x/stake/types" +) + +type ExRateResponse struct { + ExRate float64 `json:"token_shares_rate"` +} + +func RegisterStakeExRate(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec) { + r.HandleFunc("/stake/validator/{valAddr}/exRate", GetValidatorExRate(ctx, cdc)).Methods("GET") +} + +func GetValidatorExRate(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + valAddr := vars["valAddr"] + validatorAddr, err := sdk.AccAddressFromBech32(valAddr) + + // get validator + validator, err := getValidator(validatorAddr, ctx, cdc) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + if validator.Owner == nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("validator not exist")) + return + } + + // validator exRate + valExRate := validator.DelegatorShareExRate() + + floatExRate, _ := valExRate.Float64() + res := ExRateResponse{ + ExRate: floatExRate, + } + + resRaw, err := json.Marshal(res) + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(resRaw) + } +} + +func getValidator(address sdk.AccAddress, ctx context.CoreContext, cdc *wire.Codec) (stake.Validator, error) { + var ( + res []byte + validator stake.Validator + ) + res, err := query(ctx, stake.GetValidatorKey(address)) + if err != nil { + return validator, err + } + + validator = types.MustUnmarshalValidator(cdc, address, res) + + return validator, err +} + +func query(ctx context.CoreContext, key cmn.HexBytes) ([]byte, error) { + res, err := ctx.QueryStore(key, "stake") + return res, err +} \ No newline at end of file diff --git a/client/ex_rate_test.go b/client/ex_rate_test.go new file mode 100644 index 000000000..76d08b0b3 --- /dev/null +++ b/client/ex_rate_test.go @@ -0,0 +1,50 @@ +package client + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/client/context" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" + "fmt" + "encoding/json" +) + +func Test_getValidator(t *testing.T) { + address, _ := sdk.AccAddressFromBech32("faa1r4dnf8lnakw743dwhd4nnpxatcx5v40n0vntc6") + //ctx := app.NewContext().Ctx + //cdc := app.MakeCodec() + + + type args struct { + address sdk.AccAddress + ctx context.CoreContext + cdc *wire.Codec + } + tests := []struct { + name string + args args + }{ + { + name: "test get validator", + args: args{ + address: address, + //ctx: ctx, + //cdc: cdc, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + //res, err := getValidator(tt.args.address, tt.args.ctx, tt.args.cdc) + res := ExRateResponse{ + ExRate: 1.0, + } + resStr, err := json.Marshal(res) + if err != nil { + panic(err) + } + fmt.Println(string(resStr)) + }) + } +} diff --git a/client/lcd.go b/client/lcd.go index 3f915c1cc..b633227fb 100644 --- a/client/lcd.go +++ b/client/lcd.go @@ -88,6 +88,7 @@ func createHandler(cdc *wire.Codec) http.Handler { ibc.RegisterRoutes(ctx.Ctx, r, cdc, kb) stake.RegisterRoutes(ctx.Ctx, r, cdc, kb) RegisterRoutes(ctx, r, cdc, kb) + RegisterStakeExRate(ctx.Ctx, r, cdc) gov.RegisterRoutes(ctx.Ctx, r, cdc) slashing.RegisterRoutes(ctx.Ctx, r, cdc, kb) return r From cb9ca3d6c6651d0ae9b8adc689e9d25d62b53c93 Mon Sep 17 00:00:00 2001 From: Yelong Zhang Date: Mon, 6 Aug 2018 17:19:33 +0800 Subject: [PATCH 2/3] Update version to 0.4.0 --- version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version/version.go b/version/version.go index d994d64f7..36232deb2 100644 --- a/version/version.go +++ b/version/version.go @@ -8,7 +8,7 @@ import ( ) // Version - Iris Version -const Version = "0.3.1" +const Version = "0.4.0" // VersionCmd - The version of gaia var VersionCmd = &cobra.Command{ From 2a9ce5f338471615027c1bcc1582f3e062b02c45 Mon Sep 17 00:00:00 2001 From: zhangyelong Date: Mon, 6 Aug 2018 18:44:23 +0800 Subject: [PATCH 3/3] Update version to 0.3.2 --- version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version/version.go b/version/version.go index d994d64f7..4637de842 100644 --- a/version/version.go +++ b/version/version.go @@ -8,7 +8,7 @@ import ( ) // Version - Iris Version -const Version = "0.3.1" +const Version = "0.3.2" // VersionCmd - The version of gaia var VersionCmd = &cobra.Command{