This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
tx.go
148 lines (127 loc) · 4.08 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
package rest
import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
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/cosmos/modules/incubator/nft/internal/types"
"github.com/gorilla/mux"
)
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router,
cdc *codec.Codec, queryRoute string) {
// Transfer an NFT to an address
r.HandleFunc(
"/nfts/transfer",
transferNFTHandler(cdc, cliCtx),
).Methods("POST")
// Update an NFT metadata
r.HandleFunc(
"/nfts/collection/{denom}/nft/{id}/metadata",
editNFTMetadataHandler(cdc, cliCtx),
).Methods("PUT")
// Mint an NFT
r.HandleFunc(
"/nfts/mint",
mintNFTHandler(cdc, cliCtx),
).Methods("POST")
// Burn an NFT
r.HandleFunc(
"/nfts/collection/{denom}/nft/{id}/burn",
burnNFTHandler(cdc, cliCtx),
).Methods("PUT")
}
type transferNFTReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Denom string `json:"denom"`
ID string `json:"id"`
Recipient string `json:"recipient"`
}
func transferNFTHandler(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req transferNFTReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
recipient, err := sdk.AccAddressFromBech32(req.Recipient)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
// create the message
msg := types.NewMsgTransferNFT(cliCtx.GetFromAddress(), recipient, req.Denom, req.ID)
utils.WriteGenerateStdTxResponse(w, cliCtx, baseReq, []sdk.Msg{msg})
}
}
type editNFTMetadataReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Denom string `json:"denom"`
ID string `json:"id"`
TokenURI string `json:"tokenURI"`
}
func editNFTMetadataHandler(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req editNFTMetadataReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
// create the message
msg := types.NewMsgEditNFTMetadata(cliCtx.GetFromAddress(), req.ID, req.Denom, req.TokenURI)
utils.WriteGenerateStdTxResponse(w, cliCtx, baseReq, []sdk.Msg{msg})
}
}
type mintNFTReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Recipient sdk.AccAddress `json:"recipient"`
Denom string `json:"denom"`
ID string `json:"id"`
TokenURI string `json:"tokenURI"`
}
func mintNFTHandler(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req mintNFTReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
// create the message
msg := types.NewMsgMintNFT(cliCtx.GetFromAddress(), req.Recipient, req.ID, req.Denom, req.TokenURI)
utils.WriteGenerateStdTxResponse(w, cliCtx, baseReq, []sdk.Msg{msg})
}
}
type burnNFTReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Denom string `json:"denom"`
ID string `json:"id"`
}
func burnNFTHandler(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req burnNFTReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse request")
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
// create the message
msg := types.NewMsgBurnNFT(cliCtx.GetFromAddress(), req.ID, req.Denom)
utils.WriteGenerateStdTxResponse(w, cliCtx, baseReq, []sdk.Msg{msg})
}
}