-
Notifications
You must be signed in to change notification settings - Fork 13
/
api.go
341 lines (266 loc) · 8.32 KB
/
api.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package boltz
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
)
type Boltz struct {
symbol string
URL string `long:"boltz.url" description:"URL endpoint of the Boltz API"`
}
// Types for Boltz API
type GetVersionResponse struct {
Version string `json:"version"`
}
type symbolMinerFees struct {
Normal int `json:"normal"`
Reverse struct {
Lockup int `json:"lockup"`
Claim int `json:"claim"`
} `json:"reverse"`
}
type GetPairsResponse struct {
Warnings []string `json:"warnings"`
Pairs map[string]struct {
Rate float32 `json:"rate"`
Limits struct {
Maximal int `json:"maximal"`
Minimal int `json:"minimal"`
} `json:"limits"`
Fees struct {
Percentage float32 `json:"percentage"`
MinerFees struct {
BaseAsset symbolMinerFees `json:"baseAsset"`
QuoteAsset symbolMinerFees `json:"quoteAsset"`
} `json:"minerFees"`
} `json:"fees"`
} `json:"pairs"`
}
type GetNodesResponse struct {
Nodes map[string]struct {
NodeKey string `json:"nodeKey"`
URIs []string `json:"uris"`
} `json:"nodes"`
}
type SwapStatusRequest struct {
Id string `json:"id"`
}
type SwapStatusResponse struct {
Status string `json:"status"`
Channel struct {
FundingTransactionId string `json:"fundingTransactionId"`
FundingTransactionVout int `json:"fundingTransactionVout"`
} `json:"channel"`
Transaction struct {
Id string `json:"id"`
Hex string `json:"hex"`
} `json:"transaction"`
Error string `json:"error"`
}
type GetSwapTransactionRequest struct {
Id string `json:"id"`
}
type GetSwapTransactionResponse struct {
TransactionHex string `json:"transactionHex"`
TimeoutBlockHeight uint32 `json:"timeoutBlockHeight"`
TimeoutEta uint64 `json:"timeoutEta"`
Error string `json:"error"`
}
type BroadcastTransactionRequest struct {
Currency string `json:"currency"`
TransactionHex string `json:"transactionHex"`
}
type BroadcastTransactionResponse struct {
TransactionId string `json:"transactionId"`
Error string `json:"error"`
}
type CreateSwapRequest struct {
Type string `json:"type"`
PairId string `json:"pairId"`
OrderSide string `json:"orderSide"`
RefundPublicKey string `json:"refundPublicKey"`
Invoice string `json:"invoice"`
PreimageHash string `json:"preimageHash"`
}
type CreateSwapResponse struct {
Id string `json:"id"`
Bip21 string `json:"bip21"`
Address string `json:"address"`
RedeemScript string `json:"redeemScript"`
AcceptZeroConf bool `json:"acceptZeroConf"`
ExpectedAmount int `json:"expectedAmount"`
TimeoutBlockHeight int `json:"timeoutBlockHeight"`
Error string `json:"error"`
}
type SwapRatesRequest struct {
Id string `json:"id"`
}
type SwapRatesResponse struct {
OnchainAmount int `json:"onchainAmount"`
SubmarineSwap struct {
InvoiceAmount int `json:"invoiceAmount"`
} `json:"submarineSwap"`
Error string `json:"error"`
}
type SetInvoiceRequest struct {
Id string `json:"id"`
Invoice string `json:"invoice"`
}
type SetInvoiceResponse struct {
Error string `json:"error"`
}
type CreateReverseSwapRequest struct {
Type string `json:"type"`
PairId string `json:"pairId"`
OrderSide string `json:"orderSide"`
InvoiceAmount int `json:"invoiceAmount"`
PreimageHash string `json:"preimageHash"`
ClaimPublicKey string `json:"claimPublicKey"`
}
type Channel struct {
Auto bool `json:"auto"`
Private bool `json:"private"`
InboundLiquidity uint32 `json:"inboundLiquidity"`
}
type CreateChannelCreationRequest struct {
Type string `json:"type"`
PairId string `json:"pairId"`
OrderSide string `json:"orderSide"`
RefundPublicKey string `json:"refundPublicKey"`
Invoice string `json:"invoice"`
PreimageHash string `json:"preimageHash"`
Channel Channel `json:"channel"`
}
type CreateReverseSwapResponse struct {
Id string `json:"id"`
Invoice string `json:"invoice"`
OnchainAmount int `json:"onchainAmount"`
RedeemScript string `json:"redeemScript"`
LockupAddress string `json:"lockupAddress"`
TimeoutBlockHeight int `json:"TimeoutBlockHeight"`
Error string `json:"error"`
}
func (boltz *Boltz) Init(symbol string) {
boltz.symbol = symbol
}
func (boltz *Boltz) GetVersion() (*GetVersionResponse, error) {
var response GetVersionResponse
err := boltz.sendGetRequest("/version", &response)
return &response, err
}
func (boltz *Boltz) GetPairs() (*GetPairsResponse, error) {
var response GetPairsResponse
err := boltz.sendGetRequest("/getpairs", &response)
return &response, err
}
func (boltz *Boltz) GetFeeEstimation() (*map[string]int, error) {
var response map[string]int
err := boltz.sendGetRequest("/getfeeestimation", &response)
return &response, err
}
func (boltz *Boltz) GetNodes() (*GetNodesResponse, error) {
var response GetNodesResponse
err := boltz.sendGetRequest("/getnodes", &response)
return &response, err
}
func (boltz *Boltz) SwapStatus(id string) (*SwapStatusResponse, error) {
var response SwapStatusResponse
err := boltz.sendPostRequest("/swapstatus", SwapStatusRequest{
Id: id,
}, &response)
if response.Error != "" {
return nil, errors.New(response.Error)
}
return &response, err
}
func (boltz *Boltz) StreamSwapStatus(id string, events chan *SwapStatusResponse, stopListening chan bool) error {
return streamSwapStatus(boltz.URL+"/streamswapstatus?id="+id, events, stopListening)
}
func (boltz *Boltz) GetSwapTransaction(id string) (*GetSwapTransactionResponse, error) {
var response GetSwapTransactionResponse
err := boltz.sendPostRequest("/getswaptransaction", GetSwapTransactionRequest{
Id: id,
}, &response)
if response.Error != "" {
return nil, errors.New(response.Error)
}
return &response, err
}
func (boltz *Boltz) BroadcastTransaction(transactionHex string) (*BroadcastTransactionResponse, error) {
var response BroadcastTransactionResponse
err := boltz.sendPostRequest("/broadcasttransaction", BroadcastTransactionRequest{
Currency: boltz.symbol,
TransactionHex: transactionHex,
}, &response)
if response.Error != "" {
return nil, errors.New(response.Error)
}
return &response, err
}
func (boltz *Boltz) CreateSwap(request CreateSwapRequest) (*CreateSwapResponse, error) {
var response CreateSwapResponse
err := boltz.sendPostRequest("/createswap", request, &response)
if response.Error != "" {
return nil, errors.New(response.Error)
}
return &response, err
}
func (boltz *Boltz) SwapRates(request SwapRatesRequest) (*SwapRatesResponse, error) {
var response SwapRatesResponse
err := boltz.sendPostRequest("/swaprates", request, &response)
if response.Error != "" {
return nil, errors.New(response.Error)
}
return &response, err
}
func (boltz *Boltz) SetInvoice(request SetInvoiceRequest) (*SetInvoiceResponse, error) {
var response SetInvoiceResponse
err := boltz.sendPostRequest("/setinvoice", request, &response)
if response.Error != "" {
return nil, errors.New(response.Error)
}
return &response, err
}
func (boltz *Boltz) CreateChannelCreation(request CreateChannelCreationRequest) (response *CreateSwapResponse, err error) {
err = boltz.sendPostRequest("/createswap", request, &response)
if response.Error != "" {
return nil, errors.New(response.Error)
}
return response, err
}
func (boltz *Boltz) CreateReverseSwap(request CreateReverseSwapRequest) (*CreateReverseSwapResponse, error) {
var response CreateReverseSwapResponse
err := boltz.sendPostRequest("/createswap", request, &response)
if response.Error != "" {
return nil, errors.New(response.Error)
}
return &response, err
}
func (boltz *Boltz) sendGetRequest(endpoint string, response interface{}) error {
res, err := http.Get(boltz.URL + endpoint)
if err != nil {
return err
}
return unmarshalJson(res.Body, &response)
}
func (boltz *Boltz) sendPostRequest(endpoint string, requestBody interface{}, response interface{}) error {
rawBody, err := json.Marshal(requestBody)
if err != nil {
return err
}
res, err := http.Post(boltz.URL+endpoint, "application/json", bytes.NewBuffer(rawBody))
if err != nil {
return err
}
return unmarshalJson(res.Body, &response)
}
func unmarshalJson(body io.ReadCloser, response interface{}) error {
rawBody, err := ioutil.ReadAll(body)
if err != nil {
return err
}
return json.Unmarshal(rawBody, &response)
}