github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/staking/client/rest/tx.go (about)

     1  package rest
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/client/utils"
    11  	"github.com/fibonacci-chain/fbc/x/common"
    12  	"github.com/fibonacci-chain/fbc/x/staking/types"
    13  	"github.com/gorilla/mux"
    14  )
    15  
    16  func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
    17  	r.HandleFunc(
    18  		"/staking/delegators/{delegatorAddr}/delegations",
    19  		postDelegationsHandlerFn(cliCtx),
    20  	).Methods("POST")
    21  	r.HandleFunc(
    22  		"/staking/delegators/{delegatorAddr}/unbonding_delegations",
    23  		postUnbondingDelegationsHandlerFn(cliCtx),
    24  	).Methods("POST")
    25  }
    26  
    27  type (
    28  	// DelegateRequest defines the properties of a delegation request's body.
    29  	DelegateRequest struct {
    30  		BaseReq          rest.BaseReq   `json:"base_req" yaml:"base_req"`
    31  		DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // in bech32
    32  		ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"` // in bech32
    33  		Amount           sdk.SysCoin    `json:"amount" yaml:"amount"`
    34  	}
    35  
    36  	// WithdrawRequest defines the properties of a withdraw request's body.
    37  	WithdrawRequest struct {
    38  		BaseReq          rest.BaseReq   `json:"base_req" yaml:"base_req"`
    39  		DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"` // in bech32
    40  		ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"` // in bech32
    41  		Amount           sdk.SysCoin    `json:"amount" yaml:"amount"`
    42  	}
    43  )
    44  
    45  func postDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    46  	return func(w http.ResponseWriter, r *http.Request) {
    47  		var req DelegateRequest
    48  
    49  		if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
    50  			return
    51  		}
    52  
    53  		req.BaseReq = req.BaseReq.Sanitize()
    54  		if !req.BaseReq.ValidateBasic(w) {
    55  			return
    56  		}
    57  
    58  		msg := types.NewMsgDeposit(req.DelegatorAddress, req.Amount)
    59  		if err := msg.ValidateBasic(); err != nil {
    60  			common.HandleErrorMsg(w, cliCtx, common.CodeInvalidParam, err.Error())
    61  			return
    62  		}
    63  
    64  		fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
    65  		if err != nil {
    66  			common.HandleErrorMsg(w, cliCtx, common.CodeCreateAddrFromBech32Failed, err.Error())
    67  			return
    68  		}
    69  
    70  		if !bytes.Equal(fromAddr, req.DelegatorAddress) {
    71  			common.HandleErrorMsg(w, cliCtx, types.CodeAddressNotEqual, "must use own delegator address")
    72  			return
    73  		}
    74  
    75  		utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
    76  	}
    77  }
    78  
    79  func postUnbondingDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    80  	return func(w http.ResponseWriter, r *http.Request) {
    81  		var req WithdrawRequest
    82  
    83  		if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
    84  			return
    85  		}
    86  
    87  		req.BaseReq = req.BaseReq.Sanitize()
    88  		if !req.BaseReq.ValidateBasic(w) {
    89  			return
    90  		}
    91  
    92  		msg := types.NewMsgWithdraw(req.DelegatorAddress, req.Amount)
    93  		if err := msg.ValidateBasic(); err != nil {
    94  			common.HandleErrorMsg(w, cliCtx, common.CodeInvalidParam, err.Error())
    95  			return
    96  		}
    97  
    98  		fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
    99  		if err != nil {
   100  			common.HandleErrorMsg(w, cliCtx, common.CodeCreateAddrFromBech32Failed, err.Error())
   101  			return
   102  		}
   103  
   104  		if !bytes.Equal(fromAddr, req.DelegatorAddress) {
   105  			common.HandleErrorMsg(w, cliCtx, types.CodeAddressNotEqual, "must use own delegator address")
   106  			return
   107  		}
   108  
   109  		utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
   110  	}
   111  }