Skip to content

Commit

Permalink
re-organize event system and add audit check
Browse files Browse the repository at this point in the history
  • Loading branch information
ramin committed Mar 19, 2024
1 parent d5a8c97 commit 54cccd4
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 145 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ if err != nil {
// example fo handling events
for message := range channel {
switch message.Type {
case eclair.ChannelOpened:
case eclair.ChannelOpenedEvent:
// handle channel opened event
case eclair.ChannelClosed:
case eclair.ChannelClosedEvent:
// handle channel closed event
case eclair.PaymentReceived
case eclair.PaymentReceivedEvent
// handle channel closed event
//...etc for all/any message type
}
Expand Down
29 changes: 29 additions & 0 deletions audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package eclair

import (
"encoding/json"
)

const (
auditPath = "/audit"
)

type AuditResponse struct {
Sent []PaymentSent `json:"sent"`
Received []PaymentReceived `json:"received"`
Relayed []PaymentRelayed `json:"relayed"`
}

func (c *Client) Audit() (*AuditResponse, error) {
var response AuditResponse
data, err := c.Post(auditPath, nil, nil)
if err != nil {
return nil, err
}

err = json.Unmarshal(data, &response)
if err != nil {
return nil, err
}
return &response, nil
}
30 changes: 30 additions & 0 deletions channels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package eclair

type ChannelCreated struct {
Type string `json:"type"`
RemoteNodeId string `json:"remoteNodeId"`
IsInitiator bool `json:"isInitiator"`
TemporaryChannelId string `json:"temporaryChannelId"`
InitialFeeRatePerKw int `json:"initialFeeratePerKw"`
FundingTxFeeRatePerKw int `json:"fundingTxFeeratePerKw"`
}

type ChannelOpened struct {
Type string `json:"type"`
RemoteNodeId string `json:"remoteNodeId"`
ChannelId string `json:"channelId"`
}

type ChannelStateChanged struct {
Type string `json:"type"`
ChannelId string `json:"channelId"`
RemoteNodeId string `json:"remoteNodeId"`
PreviousState string `json:"previousState"`
CurrentState string `json:"currentState"`
}

type ChannelClosed struct {
Type string `json:"type"`
ChannelId string `json:"channelId"`
ClosingType string `json:"closingType"`
}
177 changes: 40 additions & 137 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ import (
type eventType int

const (
PaymentReceived eventType = iota
PaymentRelayed
PaymentSent
PaymentSettlingOnchain
PaymentFailed
ChannelCreated
ChannelOpened
ChannelStateChanged
ChannelClosed
OnionMessageReceived
PaymentReceivedEvent eventType = iota
PaymentRelayedEvent
PaymentSentEvent
PaymentSettlingOnchainEvent
PaymentFailedEvent
ChannelCreatedEvent
ChannelOpenedEvent
ChannelStateChangedEvent
ChannelClosedEvent
OnionMessageReceivedEvent
)

func ToString(kind eventType) string {
switch kind {
case PaymentReceived:
case PaymentReceivedEvent:
return "payment-received"
case PaymentRelayed:
case PaymentRelayedEvent:
return "payment-relayed"
case PaymentSent:
case PaymentSentEvent:
return "payment-sent"
case PaymentSettlingOnchain:
case PaymentSettlingOnchainEvent:
return "payment-settling-onchain"
case PaymentFailed:
case PaymentFailedEvent:
return "payment-failed"
case ChannelCreated:
case ChannelCreatedEvent:
return "channel-created"
case ChannelOpened:
case ChannelOpenedEvent:
return "channel-opened"
case ChannelStateChanged:
case ChannelStateChangedEvent:
return "channel-state-changed"
case ChannelClosed:
case ChannelClosedEvent:
return "channel-closed"
case OnionMessageReceived:
case OnionMessageReceivedEvent:
return "onion-message-received"
default:
return "unknown"
Expand All @@ -55,25 +55,25 @@ type Message struct {
func ToEventType(eventType string) eventType {
switch eventType {
case "payment-received":
return PaymentReceived
return PaymentReceivedEvent
case "payment-relayed":
return PaymentRelayed
return PaymentRelayedEvent
case "payment-sent":
return PaymentSent
return PaymentSentEvent
case "payment-settling-onchain":
return PaymentSettlingOnchain
return PaymentSettlingOnchainEvent
case "payment-failed":
return PaymentFailed
return PaymentFailedEvent
case "channel-created":
return ChannelCreated
return ChannelCreatedEvent
case "channel-opened":
return ChannelOpened
return ChannelOpenedEvent
case "channel-state-changed":
return ChannelStateChanged
return ChannelStateChangedEvent
case "channel-closed":
return ChannelClosed
return ChannelClosedEvent
case "onion-message-received":
return OnionMessageReceived
return OnionMessageReceivedEvent
default:
return -1
}
Expand All @@ -94,7 +94,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {

switch baseEvent.Type {
case "payment-received":
var event PaymentReceivedEvent
var event PaymentReceived
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -103,7 +103,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {
Data: event,
}, nil
case "payment-relayed":
var event PaymentRelayedEvent
var event PaymentRelayed
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -113,7 +113,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {
}, nil

case "payment-sent":
var event PaymentSentEvent
var event PaymentSent
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -123,7 +123,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {
}, nil

case "payment-settling-onchain":
var event PaymentSettlingOnchainEvent
var event PaymentSettlingOnchain
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -133,7 +133,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {
}, nil

case "payment-failed":
var event PaymentFailedEvent
var event PaymentFailed
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -143,7 +143,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {
}, nil

case "channel-created":
var event ChannelCreatedEvent
var event ChannelCreated
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -153,7 +153,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {
}, nil

case "channel-opened":
var event ChannelOpenedEvent
var event ChannelOpened
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -163,7 +163,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {
}, nil

case "channel-state-changed":
var event ChannelStateChangedEvent
var event ChannelStateChanged
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -173,7 +173,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {
}, nil

case "channel-closed":
var event ChannelClosedEvent
var event ChannelClosed
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -183,7 +183,7 @@ func UnmarshalEvent(data []byte) (*Message, error) {
}, nil

case "onion-message-received":
var event OnionMessageReceivedEvent
var event OnionMessageReceived
if err := json.Unmarshal(data, &event); err != nil {
return nil, err
}
Expand All @@ -196,100 +196,3 @@ func UnmarshalEvent(data []byte) (*Message, error) {
return nil, fmt.Errorf("unknown event type: %s", baseEvent.Type)
}
}

type PaymentRelayedEvent struct {
Type string `json:"type"`
AmountIn int `json:"amountIn"`
AmountOut int `json:"amountOut"`
PaymentHash string `json:"paymentHash"`
FromChannelId string `json:"fromChannelId"`
ToChannelId string `json:"toChannelId"`
Timestamp Timestamp `json:"timestamp"`
}

type Timestamp struct {
Iso string `json:"iso"`
Unix int64 `json:"unix"`
}

type PaymentReceivedEvent struct {
Type string `json:"type"`
PaymentHash string `json:"paymentHash"`
Parts []PaymentPart `json:"parts"`
}

type PaymentFailedEvent struct {
Type string `json:"type"`
ID string `json:"id"`
PaymentHash string `json:"paymentHash"`
Failures []Failure `json:"failures"`
Timestamp Timestamp `json:"timestamp"`
}

type Failure struct {
FailureType string `json:"failureType"`
FailureMessage string `json:"failureMessage"`
FailedRoute []string `json:"failedRoute"`
}

type PaymentSentEvent struct {
Type string `json:"type"`
ID string `json:"id"`
PaymentHash string `json:"paymentHash"`
PaymentPreimage string `json:"paymentPreimage"`
RecipientAmount int `json:"recipientAmount"`
RecipientNodeId string `json:"recipientNodeId"`
Parts []PaymentPart `json:"parts"`
}

type PaymentPart struct {
ID string `json:"id"`
Amount int `json:"amount"`
FeesPaid int `json:"feesPaid"`
FromChannelId string `json:"fromChannelId"`
ToChannelId string `json:"toChannelId"`
Timestamp Timestamp `json:"timestamp"`
}

type PaymentSettlingOnchainEvent struct {
Type string `json:"type"`
ID string `json:"id"`
Amount int `json:"amount"`
PaymentHash string `json:"paymentHash"`
Timestamp Timestamp `json:"timestamp"`
}

type ChannelCreatedEvent struct {
Type string `json:"type"`
RemoteNodeId string `json:"remoteNodeId"`
IsInitiator bool `json:"isInitiator"`
TemporaryChannelId string `json:"temporaryChannelId"`
InitialFeeRatePerKw int `json:"initialFeeratePerKw"`
FundingTxFeeRatePerKw int `json:"fundingTxFeeratePerKw"`
}

type ChannelOpenedEvent struct {
Type string `json:"type"`
RemoteNodeId string `json:"remoteNodeId"`
ChannelId string `json:"channelId"`
}

type ChannelStateChangedEvent struct {
Type string `json:"type"`
ChannelId string `json:"channelId"`
RemoteNodeId string `json:"remoteNodeId"`
PreviousState string `json:"previousState"`
CurrentState string `json:"currentState"`
}

type ChannelClosedEvent struct {
Type string `json:"type"`
ChannelId string `json:"channelId"`
ClosingType string `json:"closingType"`
}

type OnionMessageReceivedEvent struct {
Type string `json:"type"`
PathId string `json:"pathId"`
UnknownTlVS map[string]string `json:"unknownTlvs"`
}
4 changes: 2 additions & 2 deletions events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func TestUnmarshalEvent(t *testing.T) {
name: "PaymentRelayedEvent",
data: []byte(`{"type":"payment-relayed","data":{"amount":200,"payment_hash":"def456"}}`),
expected: &Message{
Type: PaymentRelayed,
Data: PaymentRelayedEvent{
Type: PaymentRelayedEvent,
Data: PaymentRelayed{
AmountIn: 200,
AmountOut: 100,
PaymentHash: "def456",
Expand Down
6 changes: 3 additions & 3 deletions examples/subscribe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func main() {
// handle messages here

switch message.Type {
case eclair.ChannelOpened:
case eclair.ChannelOpenedEvent:
// handle channel opened event
case eclair.ChannelClosed:
case eclair.ChannelClosedEvent:
// handle channel closed event
case eclair.PaymentReceived:
case eclair.PaymentReceivedEvent:
// handle channel closed event
}
}
Expand Down
7 changes: 7 additions & 0 deletions failure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package eclair

type Failure struct {
FailureType string `json:"failureType"`
FailureMessage string `json:"failureMessage"`
FailedRoute []string `json:"failedRoute"`
}
Loading

0 comments on commit 54cccd4

Please sign in to comment.