/
redeemFiat.go
executable file
·103 lines (86 loc) · 3.29 KB
/
redeemFiat.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
package rest
import (
"fmt"
"github.com/asaskevich/govalidator"
"github.com/commitHub/commitBlockchain/kafka"
bankTypes "github.com/commitHub/commitBlockchain/modules/bank/internal/types"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
cTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
rest2 "github.com/commitHub/commitBlockchain/client/rest"
"github.com/commitHub/commitBlockchain/modules/acl"
"github.com/commitHub/commitBlockchain/modules/bank/client"
)
type RedeemFiatReq 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"`
RedeemAmount int64 `json:"redeemAmount" valid:"required~Enter the Valid Amount,matches(^[1-9]{1}[0-9]*$)~Invalid Amount"`
Password string `json:"password" valid:"required~Enter the Password"`
Mode string `json:"mode"`
}
func RedeemFiatHandlerFunction(cliCtx context.CLIContext, kafkaBool bool, kafkaState kafka.KafkaState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req RedeemFiatReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
_, err := govalidator.ValidateStruct(req)
if err != nil {
rest2.WriteErrorResponse(w, cTypes.NewError(bankTypes.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
}
cliCtx = cliCtx.WithFromAddress(fromAddr)
cliCtx = cliCtx.WithFromName(name)
to, err := cTypes.AccAddressFromBech32(req.To)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
res, _, err := cliCtx.QueryStore(acl.GetACLAccountKey(fromAddr), "acl")
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"))
return
}
var account acl.ACLAccount
err = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(res, &account)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
fmt.Sprintf("couldn't parse query result. Result: %s. Error: %s", res, err.Error()))
return
}
if !account.GetACL().RedeemFiat {
rest.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Unauthorized transaction"))
return
}
msg := client.BuildRedeemFiatMsg(fromAddr, to, req.RedeemAmount)
if kafkaBool == true {
ticketID := kafka.TicketIDGenerator("RDFI")
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)
}
}
}