-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from wukongcheng/silei/software-upgrade
Silei/software upgrade: sync with develop
- Loading branch information
Showing
4 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters