-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevents.go
198 lines (179 loc) · 4.24 KB
/
events.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
package eclair
import (
"encoding/json"
"fmt"
)
type eventType int
const (
PaymentReceivedEvent eventType = iota
PaymentRelayedEvent
PaymentSentEvent
PaymentSettlingOnchainEvent
PaymentFailedEvent
ChannelCreatedEvent
ChannelOpenedEvent
ChannelStateChangedEvent
ChannelClosedEvent
OnionMessageReceivedEvent
)
func ToString(kind eventType) string {
switch kind {
case PaymentReceivedEvent:
return "payment-received"
case PaymentRelayedEvent:
return "payment-relayed"
case PaymentSentEvent:
return "payment-sent"
case PaymentSettlingOnchainEvent:
return "payment-settling-onchain"
case PaymentFailedEvent:
return "payment-failed"
case ChannelCreatedEvent:
return "channel-created"
case ChannelOpenedEvent:
return "channel-opened"
case ChannelStateChangedEvent:
return "channel-state-changed"
case ChannelClosedEvent:
return "channel-closed"
case OnionMessageReceivedEvent:
return "onion-message-received"
default:
return "unknown"
}
}
type Message struct {
Type eventType `json:"type"`
Data interface{}
}
func ToEventType(eventType string) eventType {
switch eventType {
case "payment-received":
return PaymentReceivedEvent
case "payment-relayed":
return PaymentRelayedEvent
case "payment-sent":
return PaymentSentEvent
case "payment-settling-onchain":
return PaymentSettlingOnchainEvent
case "payment-failed":
return PaymentFailedEvent
case "channel-created":
return ChannelCreatedEvent
case "channel-opened":
return ChannelOpenedEvent
case "channel-state-changed":
return ChannelStateChangedEvent
case "channel-closed":
return ChannelClosedEvent
case "onion-message-received":
return OnionMessageReceivedEvent
default:
return -1
}
}
// BaseEvent is used to unmarshal the initial type information
type BaseEvent struct {
Type string `json:"type"`
}
// UnmarshalEvent takes a byte slice and returns the correct event struct
func UnmarshalEvent(data []byte) (*Message, error) {
var baseEvent BaseEvent
if err := json.Unmarshal(data, &baseEvent); err != nil {
return nil, err
}
switch baseEvent.Type {
case "payment-received":
var event PaymentReceived
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
case "payment-relayed":
var event PaymentRelayed
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
case "payment-sent":
var event PaymentSent
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
case "payment-settling-onchain":
var event PaymentSettlingOnchain
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
case "payment-failed":
var event PaymentFailed
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
case "channel-created":
var event ChannelCreated
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
case "channel-opened":
var event ChannelOpened
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
case "channel-state-changed":
var event ChannelStateChanged
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
case "channel-closed":
var event ChannelClosed
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
case "onion-message-received":
var event OnionMessageReceived
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
return &Message{
Type: ToEventType(baseEvent.Type),
Data: event,
}, nil
default:
return nil, fmt.Errorf("unknown event type: %s", baseEvent.Type)
}
}