-
Notifications
You must be signed in to change notification settings - Fork 2
/
tx.go
169 lines (139 loc) · 4.56 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package rest
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/Electronic-Signatures-Industries/ancon-protocol/x/token/types"
)
func registerTxRoutes(cliCtx client.Context, r *mux.Router) {
// issue a token
r.HandleFunc(fmt.Sprintf("/%s/tokens", types.ModuleName), issueTokenHandlerFn(cliCtx)).Methods("POST")
// edit a token
r.HandleFunc(fmt.Sprintf("/%s/tokens/{%s}", types.ModuleName, RestParamSymbol), editTokenHandlerFn(cliCtx)).Methods("PUT")
// transfer owner
r.HandleFunc(fmt.Sprintf("/%s/tokens/{%s}/transfer", types.ModuleName, RestParamSymbol), transferOwnerHandlerFn(cliCtx)).Methods("POST")
// mint token
r.HandleFunc(fmt.Sprintf("/%s/tokens/{%s}/mint", types.ModuleName, RestParamSymbol), mintTokenHandlerFn(cliCtx)).Methods("POST")
// burn token
r.HandleFunc(fmt.Sprintf("/%s/tokens/{%s}/burn", types.ModuleName, RestParamSymbol), burnTokenHandlerFn(cliCtx)).Methods("POST")
}
func issueTokenHandlerFn(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req issueTokenReq
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
// create the MsgIssueToken message
msg := &types.MsgIssueToken{
Symbol: req.Symbol,
Name: req.Name,
Scale: req.Scale,
MinUnit: req.MinUnit,
InitialSupply: req.InitialSupply,
MaxSupply: req.MaxSupply,
Mintable: req.Mintable,
Owner: req.Owner,
}
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}
func editTokenHandlerFn(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
symbol := vars[RestParamSymbol]
var req editTokenReq
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
mintable, err := types.ParseBool(req.Mintable)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
// create the MsgEditToken message
msg := types.NewMsgEditToken(req.Name, symbol, req.MaxSupply, mintable, req.Owner)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}
func transferOwnerHandlerFn(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
symbol := vars[RestParamSymbol]
var req transferTokenOwnerReq
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
// create the MsgTransferTokenOwner message
msg := types.NewMsgTransferTokenOwner(req.SrcOwner, req.DstOwner, symbol)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}
func mintTokenHandlerFn(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
symbol := vars[RestParamSymbol]
var req mintTokenReq
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
// create the MsgMintToken message
msg := types.NewMsgMintToken(symbol, req.Owner, req.To, req.Amount)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}
func burnTokenHandlerFn(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
symbol := vars[RestParamSymbol]
var req burnTokenReq
if !rest.ReadRESTReq(w, r, cliCtx.LegacyAmino, &req) {
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
// create the MsgMintToken message
msg := types.NewMsgBurnToken(symbol, req.Sender, req.Amount)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}