mesg-foundation/core

View on GitHub
x/ownership/client/rest/tx.go

Summary

Maintainability
A
0 mins
Test Coverage
package rest

import (
    "net/http"

    "github.com/cosmos/cosmos-sdk/client/context"
    sdk "github.com/cosmos/cosmos-sdk/types"
    "github.com/cosmos/cosmos-sdk/types/rest"
    "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
    "github.com/gorilla/mux"
    "github.com/mesg-foundation/engine/hash"
    "github.com/mesg-foundation/engine/x/ownership/internal/types"
)

type withdrawReq struct {
    BaseReq rest.BaseReq `json:"base_req"`
    Amount  string       `json:"amount"`
    Hash    hash.Hash    `json:"hash"`
}

func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
    r.HandleFunc(
        "/ownership/withdraw-coins",
        txWithdrawHandlerFn(cliCtx),
    ).Methods(http.MethodPost)
}

func txWithdrawHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        var req withdrawReq

        if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
            rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
            return
        }

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

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

        msg := types.MsgWithdraw{
            Owner:        owner,
            ResourceHash: req.Hash,
            Amount:       req.Amount,
        }
        if err := msg.ValidateBasic(); err != nil {
            rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
            return
        }

        utils.WriteGenerateStdTxResponse(w, cliCtx, baseReq, []sdk.Msg{msg})
    }
}