forked from fiatjaf/etleneum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contract_handlers.go
184 lines (158 loc) · 4.48 KB
/
contract_handlers.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/fiatjaf/etleneum/data"
"github.com/gorilla/mux"
"github.com/lucsky/cuid"
)
func listContracts(w http.ResponseWriter, r *http.Request) {
contracts, err := data.ListContracts()
if err != nil {
log.Warn().Err(err).Msg("failed to fetch contracts")
jsonError(w, "failed to fetch contracts", 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(Result{Ok: true, Value: contracts})
}
func prepareContract(w http.ResponseWriter, r *http.Request) {
// making a contract only saves it temporarily.
// the contract can be inspected only by its creator.
// once the creator knows everything is right, he can call init.
ct := &data.Contract{}
err := json.NewDecoder(r.Body).Decode(ct)
if err != nil {
log.Warn().Err(err).Msg("failed to parse contract json")
jsonError(w, "failed to parse json", 400)
return
}
if strings.TrimSpace(ct.Name) == "" {
jsonError(w, "contract must have a name", 400)
return
}
ct.Id = "c" + cuid.Slug()
if ok := checkContractCode(ct.Code); !ok {
log.Warn().Err(err).Msg("invalid contract code")
jsonError(w, "invalid contract code", 400)
return
}
invoice, err := makeInvoice(
s.FreeMode,
ct.Id,
ct.Id,
s.ServiceId+" __init__ ["+ct.Id+"]",
nil,
getContractCost(*ct),
0,
)
if err != nil {
log.Warn().Err(err).Msg("failed to make invoice.")
jsonError(w, "failed to make invoice", 500)
return
}
if s.FreeMode {
// wait 10 seconds and notify this payment was received
go func() {
time.Sleep(5 * time.Second)
contractPaymentReceived(ct.Id, getContractCost(*ct))
}()
}
_, err = saveContractOnRedis(*ct)
if err != nil {
log.Warn().Err(err).Interface("ct", ct).Msg("failed to save to redis")
jsonError(w, "failed to save prepared contract", 500)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(Result{Ok: true, Value: map[string]interface{}{
"id": ct.Id,
"invoice": invoice,
}})
}
func getContract(w http.ResponseWriter, r *http.Request) {
ctid := mux.Vars(r)["ctid"]
ct, err := data.GetContract(ctid)
if err != nil {
// it's a database error
log.Warn().Err(err).Str("ctid", ctid).Msg("database error fetching contract")
jsonError(w, "database error", 500)
return
}
if ct == nil {
// couldn't find on database, maybe it's a temporary contract?
ct, err = contractFromRedis(ctid)
if err != nil {
log.Warn().Err(err).Str("ctid", ctid).
Msg("failed to fetch fetch prepared contract from redis")
jsonError(w, "failed to fetch prepared contract", 404)
return
}
}
if ct.Methods == nil {
ct.Methods = make([]data.Method, 0)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(Result{Ok: true, Value: ct})
}
func getContractState(w http.ResponseWriter, r *http.Request) {
ctid := mux.Vars(r)["ctid"]
ct, _ := data.GetContract(ctid)
if ct == nil {
jsonError(w, "contract not found", 404)
return
}
w.Header().Set("Content-Type", "application/json")
var jqfilter string
if r.Method == "GET" {
jqfilter, _ = mux.Vars(r)["jq"]
} else if r.Method == "POST" {
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
jqfilter = string(b)
}
state := ct.State
if strings.TrimSpace(jqfilter) != "" {
if result, err := runJQ(r.Context(), state, jqfilter); err != nil {
log.Warn().Err(err).Str("ctid", ctid).
Str("f", jqfilter).Str("state", string(state)).
Msg("error applying jq filter")
jsonError(w, "error applying jq filter", 400)
return
} else {
jresult, _ := json.Marshal(result)
state = json.RawMessage(jresult)
}
}
json.NewEncoder(w).Encode(Result{Ok: true, Value: state})
}
func getContractFunds(w http.ResponseWriter, r *http.Request) {
ctid := mux.Vars(r)["ctid"]
ct, _ := data.GetContract(ctid)
if ct == nil {
jsonError(w, "contract not found", 404)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(Result{Ok: true, Value: ct.Funds})
}
func deleteContract(w http.ResponseWriter, r *http.Request) {
ctid := mux.Vars(r)["ctid"]
var err error
// can only delete on free mode
if s.FreeMode {
err = data.DeleteContract(ctid)
} else {
err = errors.New("only works on free mode")
}
if err != nil {
log.Info().Err(err).Str("id", ctid).Msg("can't delete contract")
jsonError(w, "can't delete contract", 404)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(Result{Ok: true})
}