-
Notifications
You must be signed in to change notification settings - Fork 1
/
changeBuyerBid.go
executable file
·114 lines (94 loc) · 3.82 KB
/
changeBuyerBid.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package rest
import (
"fmt"
"github.com/asaskevich/govalidator"
"github.com/commitHub/commitBlockchain/kafka"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
cTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/commitHub/commitBlockchain/types"
rest2 "github.com/commitHub/commitBlockchain/client/rest"
"github.com/commitHub/commitBlockchain/modules/acl"
negotiationTypes "github.com/commitHub/commitBlockchain/modules/negotiation/internal/types"
)
type changeBuyerBidReq struct {
BaseReq rest.BaseReq `json:"base_req"`
To string `json:"to" valid:"required~Enter the ToAddress,matches(^commit[a-z0-9]{39}$)~ToAddress is Invalid"`
Bid int64 `json:"bid" valid:"required~Enter the Valid Bid,matches(^[1-9]{1}[0-9]*$)~Invalid Bid"`
Time int64 `json:"time" valid:"required~Enter the Valid Time,matches(^[1-9]{1}[0-9]*$)~Invalid Time"`
PegHash string `json:"pegHash" valid:"required~Enter the PegHash,matches(^[0-9]+$)~Invalid PegHash,length(2|40)~PegHash length between 2-40"`
Password string `json:"password" valid:"required~Enter the Password"`
Mode string `json:"mode"`
}
func ChangeBuyerBidRequestHandlerFn(cliCtx context.CLIContext, kafkaBool bool, kafkaState kafka.KafkaState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req changeBuyerBidReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
_, err := govalidator.ValidateStruct(req)
if err != nil {
rest2.WriteErrorResponse(w, cTypes.NewError(negotiationTypes.DefaultCodeSpace, http.StatusBadRequest, err.Error()))
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
fromAddr, name, err := context.GetFromFields(req.BaseReq.From, false)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
to, err := cTypes.AccAddressFromBech32(req.To)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithFromAddress(fromAddr)
cliCtx = cliCtx.WithFromName(name)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", acl.QuerierRoute, "queryACLAccount", fromAddr), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
fmt.Sprintf("couldn't query account. Error: %s", err.Error()))
return
}
if len(res) == 0 {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
fmt.Sprintf("Unauthorized transaction. ACL is not defined for buyer"))
return
}
var account acl.ACLAccount
cliCtx.Codec.MustUnmarshalJSON(res, &account)
if !account.GetACL().ChangeBuyerBid {
rest.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Unauthorized transaction"))
return
}
pegHashHex, err := types.GetAssetPegHashHex(req.PegHash)
negotiationID := negotiationTypes.NegotiationID(append(append(fromAddr.Bytes(), to.Bytes()...), pegHashHex...))
proposedNegotiation := negotiationTypes.BaseNegotiation{
NegotiationID: negotiationID,
BuyerAddress: fromAddr,
SellerAddress: to,
PegHash: pegHashHex,
Bid: req.Bid,
Time: req.Time,
}
msg := negotiationTypes.BuildMsgChangeBuyerBid(&proposedNegotiation)
if kafkaBool == true {
ticketID := kafka.TicketIDGenerator("CHBB")
jsonResponse := kafka.SendToKafka(kafka.NewKafkaMsgFromRest(msg, ticketID, req.BaseReq, cliCtx, req.Mode, req.Password), kafkaState, cliCtx.Codec)
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write(jsonResponse)
} else {
output, err := rest2.SignAndBroadcast(req.BaseReq, cliCtx, req.Mode, req.Password, []cTypes.Msg{msg})
if err != nil {
rest2.WriteErrorResponse(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(output)
}
}
}