/
transactions_func.go
202 lines (164 loc) · 4.62 KB
/
transactions_func.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package handlers
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/flow-hydraulics/flow-wallet-api/errors"
"github.com/flow-hydraulics/flow-wallet-api/transactions"
"github.com/gorilla/mux"
)
func (s *Transactions) ListFunc(rw http.ResponseWriter, r *http.Request) {
var (
transactionSlice []transactions.Transaction
err error
)
limit, err := strconv.Atoi(r.FormValue("limit"))
if err != nil {
limit = 0
}
offset, err := strconv.Atoi(r.FormValue("offset"))
if err != nil {
offset = 0
}
vars := mux.Vars(r)
if address, ok := vars["address"]; ok {
// Handle account specific transactions
// This endpoint is used to handle "raw" transactions for an account
// so we use transactions.General type here
transactionSlice, err = s.service.ListForAccount(transactions.General, address, limit, offset)
} else {
// Handle all transactions
transactionSlice, err = s.service.List(limit, offset)
}
if err != nil {
handleError(rw, r, err)
return
}
res := make([]transactions.JSONResponse, len(transactionSlice))
for i, job := range transactionSlice {
res[i] = job.ToJSONResponse()
}
handleJsonResponse(rw, http.StatusOK, res)
}
func (s *Transactions) CreateFunc(rw http.ResponseWriter, r *http.Request) {
var err error
if r.Body == nil || r.Body == http.NoBody {
err = &errors.RequestError{
StatusCode: http.StatusBadRequest,
Err: fmt.Errorf("empty body"),
}
handleError(rw, r, err)
return
}
vars := mux.Vars(r)
var txReq transactions.JSONRequest
// Try to decode the request body into the struct.
err = json.NewDecoder(r.Body).Decode(&txReq)
if err != nil {
err = &errors.RequestError{
StatusCode: http.StatusBadRequest,
Err: fmt.Errorf("invalid body"),
}
handleError(rw, r, err)
return
}
// Decide whether to serve sync or async, default async
sync := r.FormValue(SyncQueryParameter) != ""
job, transaction, err := s.service.Create(r.Context(), sync, vars["address"], txReq.Code, txReq.Arguments, transactions.General)
if err != nil {
handleError(rw, r, err)
return
}
var res interface{}
if sync {
res = transaction.ToJSONResponse()
} else {
res = job.ToJSONResponse()
}
handleJsonResponse(rw, http.StatusCreated, res)
}
func (s *Transactions) SignFunc(rw http.ResponseWriter, r *http.Request) {
err := checkNonEmptyBody(r)
if err != nil {
handleError(rw, r, err)
return
}
vars := mux.Vars(r)
var txReq transactions.JSONRequest
// Try to decode the request body into the struct.
err = json.NewDecoder(r.Body).Decode(&txReq)
if err != nil {
err = &errors.RequestError{
StatusCode: http.StatusBadRequest,
Err: fmt.Errorf("invalid body: %#v", err),
}
handleError(rw, r, err)
return
}
tx, err := s.service.Sign(r.Context(), vars["address"], txReq.Code, txReq.Arguments)
if err != nil {
handleError(rw, r, err)
return
}
resp, err := tx.ToJSONResponse()
if err != nil {
err = &errors.RequestError{
StatusCode: http.StatusInternalServerError,
Err: fmt.Errorf("cannot decode signed transaction"),
}
handleError(rw, r, err)
return
}
handleJsonResponse(rw, http.StatusCreated, resp)
}
func (s *Transactions) DetailsFunc(rw http.ResponseWriter, r *http.Request) {
var (
transaction *transactions.Transaction
err error
)
vars := mux.Vars(r)
if address, ok := vars["address"]; ok {
// Handle account specific transactions
// This endpoint is used to handle "raw" transactions for an account
// so we use transactions.General type here
transaction, err = s.service.DetailsForAccount(r.Context(), transactions.General, address, vars["transactionId"])
} else {
// Handle all transactions
transaction, err = s.service.Details(r.Context(), vars["transactionId"])
}
if err != nil {
handleError(rw, r, err)
return
}
res := transaction.ToJSONResponse()
handleJsonResponse(rw, http.StatusOK, res)
}
func (s *Transactions) ExecuteScriptFunc(rw http.ResponseWriter, r *http.Request) {
var err error
if r.Body == nil || r.Body == http.NoBody {
err = &errors.RequestError{
StatusCode: http.StatusBadRequest,
Err: fmt.Errorf("empty body"),
}
handleError(rw, r, err)
return
}
var txReq transactions.JSONRequest
// Try to decode the request body into the struct.
err = json.NewDecoder(r.Body).Decode(&txReq)
if err != nil {
err = &errors.RequestError{
StatusCode: http.StatusBadRequest,
Err: fmt.Errorf("invalid body"),
}
handleError(rw, r, err)
return
}
res, err := s.service.ExecuteScript(r.Context(), txReq.Code, txReq.Arguments)
if err != nil {
handleError(rw, r, err)
return
}
handleJsonResponse(rw, http.StatusOK, res)
}