Skip to content

Commit

Permalink
Added the REST and CLI txs
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed Oct 25, 2019
1 parent d2569c7 commit 6929767
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
33 changes: 33 additions & 0 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
GetCmdWithdrawRewards(cdc),
GetCmdSetWithdrawAddr(cdc),
GetCmdWithdrawAllRewards(cdc, storeKey),
GetCmdFundCommunityPool(cdc),
)...)

return distTxCmd
Expand Down Expand Up @@ -254,3 +255,35 @@ Where proposal.json contains:

return cmd
}

// command to fund the community pool
func GetCmdFundCommunityPool(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "fund-community-pool [amount]",
Short: "funds the community pool with the specified amount",
Long: strings.TrimSpace(
fmt.Sprintf(`Funds the community pool with the specified amount
Example:
$ %s tx fund-community-pool 100uatom --from mykey
`,
version.ClientName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)

depositorAddr := cliCtx.GetFromAddress()
amount, err := sdk.ParseCoins(args[0])
if err != nil {
return err
}

msg := types.NewMsgDepositIntoCommunityPool(amount, depositorAddr)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
35 changes: 35 additions & 0 deletions x/distribution/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute strin
withdrawValidatorRewardsHandlerFn(cliCtx),
).Methods("POST")

// Fund the community pool
r.HandleFunc(
"/distribution/community_pool",
withdrawValidatorRewardsHandlerFn(cliCtx),
).Methods("POST")

}

type (
Expand All @@ -50,6 +56,11 @@ type (
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
WithdrawAddress sdk.AccAddress `json:"withdraw_address" yaml:"withdraw_address"`
}

fundCommunityPoolReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Amount sdk.Coins `json:"amount" yaml:"amount"`
}
)

// Withdraw delegator rewards
Expand Down Expand Up @@ -177,6 +188,30 @@ func withdrawValidatorRewardsHandlerFn(cliCtx context.CLIContext) http.HandlerFu
}
}

// Fund the community pool
func fundCommunityPoolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req fundCommunityPoolReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}

req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}

fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

msg := types.NewMsgDepositIntoCommunityPool(req.Amount, fromAddr)
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}

// Auxiliary

func checkDelegatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.AccAddress, bool) {
Expand Down

0 comments on commit 6929767

Please sign in to comment.