Skip to content

Commit 54ded10

Browse files
committed
feat: add cip20 message as metadata to push event notification
Signed-off-by: Ales Verbic <[email protected]>
1 parent d8bfda6 commit 54ded10

File tree

1 file changed

+80
-9
lines changed

1 file changed

+80
-9
lines changed

output/push/push.go

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"os"
2222

23+
"github.com/blinklabs-io/gouroboros/cbor"
2324
"github.com/blinklabs-io/snek/event"
2425
"github.com/blinklabs-io/snek/fcm"
2526
"github.com/blinklabs-io/snek/input/chainsync"
@@ -139,16 +140,42 @@ func (p *PushOutput) Start() error {
139140

140141
// Create notification message
141142
title := "Snek"
142-
body := fmt.Sprintf(
143-
"New Transaction!\nBlockNumber: %d, SlotNumber: %d\nInputs: %d, Outputs: %d\nFee: %d\nHash: %s",
144-
tc.BlockNumber,
145-
tc.SlotNumber,
146-
len(te.Inputs),
147-
len(te.Outputs),
148-
te.Fee,
149-
tc.TransactionHash,
150-
)
151143

144+
// Get metadata
145+
var cip20Message string
146+
if te.Metadata != nil {
147+
jsonMessage, err := extractCIP20FromMetadata(te.Metadata)
148+
if err != nil {
149+
fmt.Println("Error:", err)
150+
} else {
151+
cip20Message = jsonMessage
152+
fmt.Println("JSON CIP20 Message:", cip20Message)
153+
}
154+
}
155+
156+
var body string
157+
if cip20Message != "" {
158+
body = fmt.Sprintf(
159+
"New Transaction!\nBlockNumber: %d, SlotNumber: %d\nInputs: %d, Outputs: %d\nFee: %d\nHash: %s\nMetadata: %s",
160+
tc.BlockNumber,
161+
tc.SlotNumber,
162+
len(te.Inputs),
163+
len(te.Outputs),
164+
te.Fee,
165+
tc.TransactionHash,
166+
cip20Message,
167+
)
168+
} else {
169+
body = fmt.Sprintf(
170+
"New Transaction!\nBlockNumber: %d, SlotNumber: %d\nInputs: %d, Outputs: %d\nFee: %d\nHash: %s",
171+
tc.BlockNumber,
172+
tc.SlotNumber,
173+
len(te.Inputs),
174+
len(te.Outputs),
175+
te.Fee,
176+
tc.TransactionHash,
177+
)
178+
}
152179
// Send notification
153180
p.processFcmNotifications(title, body)
154181

@@ -273,3 +300,47 @@ func (p *PushOutput) InputChan() chan<- event.Event {
273300
func (p *PushOutput) OutputChan() <-chan event.Event {
274301
return nil
275302
}
303+
304+
// This should probably go in gouroboros module
305+
// extractCIP20FromMetadata extracts the CIP20 message from the transaction metadata
306+
// and returns it as a JSON string.
307+
func extractCIP20FromMetadata(metadata *cbor.Value) (string, error) {
308+
if metadata == nil {
309+
return "", fmt.Errorf("metadata is nil")
310+
}
311+
312+
metadataMap, ok := metadata.Value().(map[any]any)
313+
if !ok {
314+
return "", fmt.Errorf("metadata value is not of the expected map type")
315+
}
316+
317+
// Extract the nested value for key 674
318+
nestedValue, found := metadataMap[uint64(674)]
319+
if !found {
320+
return "", fmt.Errorf("key 674 not found in metadata")
321+
}
322+
323+
// Assert the nested value is a map
324+
nestedMap, ok := nestedValue.(map[any]any)
325+
if !ok {
326+
return "", fmt.Errorf("nested value for key 674 is not a map")
327+
}
328+
329+
msgValue, found := nestedMap["msg"]
330+
if !found {
331+
return "", fmt.Errorf("key 'msg' not found in nested metadata map")
332+
}
333+
334+
msgStruct := map[string]any{
335+
"674": map[string]any{
336+
"msg": msgValue,
337+
},
338+
}
339+
340+
jsonBytes, err := json.Marshal(msgStruct)
341+
if err != nil {
342+
return "", fmt.Errorf("error marshalling message to JSON: %v", err)
343+
}
344+
345+
return string(jsonBytes), nil
346+
}

0 commit comments

Comments
 (0)