74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
package rest
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
|
"github.com/cosmos/cosmos-sdk/x/ibc-transfer/types"
|
|
)
|
|
|
|
func registerTxRoutes(clientCtx client.Context, r *mux.Router) {
|
|
r.HandleFunc(fmt.Sprintf("/ibc/ports/{%s}/channels/{%s}/transfer", restPortID, restChannelID), transferHandlerFn(clientCtx)).Methods("POST")
|
|
}
|
|
|
|
// transferHandlerFn implements a transfer handler
|
|
//
|
|
// @Summary Transfer token
|
|
// @Tags IBC
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param port-id path string true "Port ID"
|
|
// @Param channel-id path string true "Channel ID"
|
|
// @Param body body rest.TransferTxReq true "Transfer token request body"
|
|
// @Success 200 {object} PostTransfer "OK"
|
|
// @Failure 400 {object} rest.ErrorResponse "Invalid port id or channel id"
|
|
// @Failure 500 {object} rest.ErrorResponse "Internal Server Error"
|
|
// @Router /ibc/ports/{port-id}/channels/{channel-id}/transfer [post]
|
|
func transferHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
portID := vars[restPortID]
|
|
channelID := vars[restChannelID]
|
|
|
|
var req TransferTxReq
|
|
if !rest.ReadRESTReq(w, r, clientCtx.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
|
|
}
|
|
|
|
// create the message
|
|
msg := types.NewMsgTransfer(
|
|
portID,
|
|
channelID,
|
|
req.Amount,
|
|
fromAddr,
|
|
req.Receiver,
|
|
req.TimeoutHeight,
|
|
req.TimeoutTimestamp,
|
|
)
|
|
|
|
if err := msg.ValidateBasic(); err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
|
|
}
|
|
}
|