|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gorilla/mux" |
| 5 | + "net/http" |
| 6 | + "github.com/cosmos/cosmos-sdk/x/stake" |
| 7 | + "github.com/cosmos/cosmos-sdk/client/context" |
| 8 | + cmn "github.com/tendermint/tmlibs/common" |
| 9 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 10 | + "github.com/cosmos/cosmos-sdk/wire" |
| 11 | + "encoding/json" |
| 12 | +) |
| 13 | + |
| 14 | +type ExRateResponse struct { |
| 15 | + ExRate float64 `json:"token_shares_rate"` |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +func RegisterStakeExRate(ctx context.CoreContext, r *mux.Router, cdc *wire.Codec) { |
| 20 | + r.HandleFunc("/stake/validator/{valAddr}/exRate", GetValidatorExRate(ctx, cdc)).Methods("GET") |
| 21 | +} |
| 22 | + |
| 23 | +func GetValidatorExRate(ctx context.CoreContext, cdc *wire.Codec) http.HandlerFunc { |
| 24 | + return func(w http.ResponseWriter, r *http.Request) { |
| 25 | + vars := mux.Vars(r) |
| 26 | + valAddr := vars["valAddr"] |
| 27 | + validatorAddr, err := sdk.GetAccAddressBech32(valAddr) |
| 28 | + |
| 29 | + // get validator |
| 30 | + validator, err := getValidator(validatorAddr, ctx, cdc) |
| 31 | + if err != nil { |
| 32 | + w.WriteHeader(http.StatusInternalServerError) |
| 33 | + w.Write([]byte(err.Error())) |
| 34 | + return |
| 35 | + } |
| 36 | + if validator.Owner == nil { |
| 37 | + w.WriteHeader(http.StatusBadRequest) |
| 38 | + w.Write([]byte("validator not exist")) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + //poolShares, _ := validator.PoolShares.Amount.Float64() |
| 43 | + //delegatorShares, _ := validator.DelegatorShares.Float64() |
| 44 | + //fmt.Printf("validator address is %v, poolShares is %v, delegator shares is %v\n", |
| 45 | + // validator.Owner.String(), poolShares, delegatorShares) |
| 46 | + |
| 47 | + // get pool |
| 48 | + pool, err := getPool(ctx, cdc) |
| 49 | + if err != nil { |
| 50 | + w.WriteHeader(http.StatusInternalServerError) |
| 51 | + w.Write([]byte(err.Error())) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + // validator exRate |
| 56 | + valExRate := validator.DelegatorShareExRate(pool) |
| 57 | + |
| 58 | + // pool exRate |
| 59 | + poolExRate := bondedShareExRate(pool) |
| 60 | + |
| 61 | + |
| 62 | + exRate := poolExRate.Mul(valExRate) |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + //floatValRate, _ := valExRate.Float64() |
| 67 | + //floatPoolExRate, _ := poolExRate.Float64() |
| 68 | + //fmt.Printf("valRate is %v, poolRate is %v\n", floatValRate, floatPoolExRate) |
| 69 | + |
| 70 | + |
| 71 | + floatExRate, _ := exRate.Float64() |
| 72 | + res := ExRateResponse{ |
| 73 | + ExRate: floatExRate, |
| 74 | + } |
| 75 | + |
| 76 | + //fmt.Printf("exRate is %v\n", floatExRate) |
| 77 | + |
| 78 | + resRaw, err := json.Marshal(res) |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + w.WriteHeader(http.StatusInternalServerError) |
| 82 | + w.Write([]byte(err.Error())) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + w.Write(resRaw) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func getPool(ctx context.CoreContext, cdc *wire.Codec) (stake.Pool, error) { |
| 91 | + var ( |
| 92 | + res []byte |
| 93 | + pool stake.Pool |
| 94 | + ) |
| 95 | + res, err := query(ctx, stake.PoolKey) |
| 96 | + if err != nil { |
| 97 | + return pool, err |
| 98 | + } |
| 99 | + |
| 100 | + cdc.MustUnmarshalBinary(res, &pool) |
| 101 | + |
| 102 | + return pool, err |
| 103 | +} |
| 104 | + |
| 105 | +func getValidator(address sdk.Address, ctx context.CoreContext, cdc *wire.Codec) (stake.Validator, error) { |
| 106 | + var ( |
| 107 | + res []byte |
| 108 | + validator stake.Validator |
| 109 | + ) |
| 110 | + res, err := query(ctx, stake.GetValidatorKey(address)) |
| 111 | + if err != nil { |
| 112 | + return validator, err |
| 113 | + } |
| 114 | + |
| 115 | + cdc.MustUnmarshalBinary(res, &validator) |
| 116 | + |
| 117 | + return validator, err |
| 118 | +} |
| 119 | + |
| 120 | +// get the exchange rate of bonded token per issued share |
| 121 | +func bondedShareExRate(p stake.Pool) sdk.Rat { |
| 122 | + if p.BondedShares.IsZero() { |
| 123 | + return sdk.OneRat() |
| 124 | + } |
| 125 | + return sdk.NewRat(p.BondedTokens).Quo(p.BondedShares) |
| 126 | +} |
| 127 | + |
| 128 | +func query(ctx context.CoreContext, key cmn.HexBytes) ([]byte, error) { |
| 129 | + res, err := ctx.Query(key, "stake") |
| 130 | + return res, err |
| 131 | +} |
| 132 | + |
0 commit comments