-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsonrpc_event.go
56 lines (48 loc) · 1.19 KB
/
jsonrpc_event.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
package gostratum
import "encoding/json"
type JsonRpcEvent struct {
Id any `json:"id"` // id can be nil, a string, or an int 🙄
Version string `json:"jsonrpc"`
Method StratumMethod `json:"method"`
Params []any `json:"params"`
}
type JsonRpcResponse struct {
Id any `json:"id"`
Result any `json:"result"`
Error []any `json:"error"`
}
func NewEvent(id string, method string, params []any) JsonRpcEvent {
var finalId any
if len(id) == 0 {
finalId = nil
} else {
finalId = id
}
return JsonRpcEvent{
Id: finalId,
Version: "2.0",
Method: StratumMethod(method),
Params: params,
}
}
func NewResponse(event JsonRpcEvent, results any, err []any) JsonRpcResponse {
return JsonRpcResponse{
Id: event.Id,
Result: results,
Error: err,
}
}
func UnmarshalEvent(in string) (JsonRpcEvent, error) {
event := JsonRpcEvent{}
if err := json.Unmarshal([]byte(in), &event); err != nil {
return JsonRpcEvent{}, err
}
return event, nil
}
func UnmarshalResponse(in string) (JsonRpcResponse, error) {
resp := JsonRpcResponse{}
if err := json.Unmarshal([]byte(in), &resp); err != nil {
return JsonRpcResponse{}, err
}
return resp, nil
}