Skip to content

Commit

Permalink
Merge pull request #3012 from Sifchain/feature/margin-1-params-rest
Browse files Browse the repository at this point in the history
feat: margin params rest endpoint
  • Loading branch information
timlind authored Jul 12, 2022
2 parents 1279aa2 + aa3fc02 commit ef360cc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
20 changes: 20 additions & 0 deletions x/margin/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

func registerQueryRoutes(cliCtx client.Context, r *mux.Router) {
r.HandleFunc("/margin/mtps-by-address", getMTPsForAddress(cliCtx))
r.HandleFunc("/margin/params", getParams(cliCtx))
}

func getMTPsForAddress(cliCtx client.Context) http.HandlerFunc {
Expand Down Expand Up @@ -49,3 +50,22 @@ func getMTPsForAddress(cliCtx client.Context) http.HandlerFunc {
rest.PostProcessResponse(w, cliCtx, res)
}
}

func getParams(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, height, err := cliCtx.Query(route)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
2 changes: 1 addition & 1 deletion x/margin/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ func (k Keeper) GetSQ(ctx sdk.Context, pool clptypes.Pool) sdk.Dec {
panic(err)
}

sq := value.Quo(blocks).Quo(modifier)
sq := value.Mul(blocks).Quo(modifier)

return sq
}
14 changes: 14 additions & 0 deletions x/margin/keeper/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func NewLegacyQuerier(k types.Keeper, cdc *codec.LegacyAmino) sdk.Querier {
switch path[0] {
case types.QueryMTPsForAddress:
return queryMtpsForAddress(ctx, req, cdc, querier)
case types.QueryParams:
return queryParams(ctx, req, cdc, querier)
default:
return nil, sdkerrors.Wrap(types.ErrUnknownRequest, "unknown request")
}
Expand Down Expand Up @@ -65,3 +67,15 @@ func queryMtpsForAddress(ctx sdk.Context, req abci.RequestQuery, cdc *codec.Lega
}
return bz, nil
}

func queryParams(ctx sdk.Context, req abci.RequestQuery, cdc *codec.LegacyAmino, querier queryServer) ([]byte, error) {
res, err := querier.GetParams(sdk.WrapSDKContext(ctx), &types.ParamsRequest{})
if err != nil {
return nil, err
}
bz, err := codec.MarshalJSONIndent(cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
1 change: 1 addition & 0 deletions x/margin/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package types

const (
QueryMTPsForAddress = "mtps-for-address"
QueryParams = "params"
)

0 comments on commit ef360cc

Please sign in to comment.