-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
53 lines (43 loc) · 1.4 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package rest
import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/furya-official/mage/x/auction/types"
)
func registerTxRoutes(cliCtx client.Context, r *mux.Router) {
r.HandleFunc(fmt.Sprintf("/%s/auctions/{%s}/bids", types.ModuleName, restAuctionID), postPlaceBidHandlerFn(cliCtx)).Methods("POST")
}
func postPlaceBidHandlerFn(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
var requestBody PlaceBidReq
if !rest.ReadRESTReq(w, req, cliCtx.LegacyAmino, &requestBody) {
return
}
baseReq := requestBody.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
bidderAddr, err := sdk.AccAddressFromBech32(requestBody.BaseReq.From)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
// Get auction ID from url
auctionID, ok := rest.ParseUint64OrReturnBadRequest(w, mux.Vars(req)[restAuctionID])
if !ok {
return
}
// Create and return a StdTx
msg := types.NewMsgPlaceBid(auctionID, bidderAddr.String(), requestBody.Amount)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
tx.WriteGeneratedTxResponse(cliCtx, w, baseReq, &msg)
}
}