Skip to content

Commit 92dbd68

Browse files
committed
feat: transaction context
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent ccc07f3 commit 92dbd68

File tree

6 files changed

+95
-27
lines changed

6 files changed

+95
-27
lines changed

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Events are created with a simple schema.
1818
{
1919
"type": "event type",
2020
"timestamp": "wall clock timestamp of event",
21+
"context": "metadata about the event",
2122
"payload": "the full event specific payload"
2223
}
2324
```
@@ -28,10 +29,14 @@ The chainsync input produces three event types: `block`, `rollback`, and
2829
block:
2930
```json
3031
{
31-
"payload": {
32+
"context": {
3233
"blockNumber": 123,
33-
"blockHash": "abcd123...",
3434
"slotNumber": 1234567,
35+
},
36+
"payload": {
37+
"blockBodySize": 123,
38+
"issuerVkey": "a712f81ab2eac...",
39+
"blockHash": "abcd123...",
3540
"blockCbor": "85828a1a000995c21..."
3641
}
3742
}
@@ -50,12 +55,43 @@ rollback:
5055
transaction:
5156
```json
5257
{
53-
"payload": {
58+
"context": {
5459
"blockNumber": 123,
55-
"blockHash": "abcd123...",
5660
"slotNumber": 1234567,
5761
"transactionHash": "0deadbeef123...",
62+
"transactionIdx": 0,
63+
},
64+
"payload": {
65+
"blockHash": "abcd123...",
5866
"transactionCbor": "a500828258200a1ad..."
67+
"inputs": [
68+
"abcdef123...#0",
69+
"abcdef123...#1",
70+
],
71+
"outputs": [
72+
{
73+
"address": "addr1qwerty123...",
74+
"amount": 12345687,
75+
"assets": [
76+
{
77+
"name": "Foo",
78+
"nameHex": "abcd123...",
79+
"amount": 123,
80+
"fingerprint": "asset1abcd...",
81+
"policyId": "54321..."
82+
}
83+
]
84+
}
85+
],
86+
"metadata": {
87+
674: {
88+
"msg": [
89+
"Test message"
90+
]
91+
}
92+
},
93+
"fee": 1234567,
94+
"ttl": 123
5995
}
6096
}
6197
```

input/chainsync/chainsync.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ func (c *ChainSync) handleRollForward(blockType uint, blockData interface{}, tip
220220
}
221221
blockEvt := event.New("chainsync.block", time.Now(), NewBlockHeaderContext(v), NewBlockEvent(block, c.includeCbor))
222222
c.eventChan <- blockEvt
223-
for _, transaction := range block.Transactions() {
224-
txEvt := event.New("chainsync.transaction", time.Now(), nil, NewTransactionEvent(block, transaction, c.includeCbor))
223+
for t, transaction := range block.Transactions() {
224+
txEvt := event.New("chainsync.transaction", time.Now(), NewTransactionContext(block, transaction, uint32(t)), NewTransactionEvent(block, transaction, c.includeCbor))
225225
c.eventChan <- txEvt
226226
}
227227
c.updateStatus(v.SlotNumber(), v.BlockNumber(), v.Hash(), tip.Point.Slot, hex.EncodeToString(tip.Point.Hash))
@@ -230,10 +230,10 @@ func (c *ChainSync) handleRollForward(blockType uint, blockData interface{}, tip
230230
}
231231

232232
func (c *ChainSync) handleBlockFetchBlock(block ledger.Block) error {
233-
blockEvt := event.New("chainsync.block", time.Now(), nil, NewBlockEvent(block, c.includeCbor))
233+
blockEvt := event.New("chainsync.block", time.Now(), NewBlockContext(block), NewBlockEvent(block, c.includeCbor))
234234
c.eventChan <- blockEvt
235-
for _, transaction := range block.Transactions() {
236-
txEvt := event.New("chainsync.transaction", time.Now(), nil, NewTransactionEvent(block, transaction, c.includeCbor))
235+
for t, transaction := range block.Transactions() {
236+
txEvt := event.New("chainsync.transaction", time.Now(), NewTransactionContext(block, transaction, uint32(t)), NewTransactionEvent(block, transaction, c.includeCbor))
237237
c.eventChan <- txEvt
238238
}
239239
c.updateStatus(block.SlotNumber(), block.BlockNumber(), block.Hash(), c.bulkRangeEnd.Slot, hex.EncodeToString(c.bulkRangeEnd.Hash))

input/chainsync/tx.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,40 @@ import (
1919
"github.com/blinklabs-io/gouroboros/ledger"
2020
)
2121

22+
type TransactionContext struct {
23+
BlockNumber uint64 `json:"blockNumber"`
24+
SlotNumber uint64 `json:"slotNumber"`
25+
TransactionHash string `json:"transactionHash"`
26+
TransactionIdx uint32 `json:"transactionIdx"`
27+
}
28+
2229
type TransactionEvent struct {
23-
BlockNumber uint64 `json:"blockNumber"`
2430
BlockHash string `json:"blockHash"`
25-
SlotNumber uint64 `json:"slotNumber"`
26-
TransactionHash string `json:"transactionHash"`
2731
TransactionCbor byteSliceJsonHex `json:"transactionCbor,omitempty"`
2832
Inputs []ledger.TransactionInput `json:"inputs"`
2933
Outputs []ledger.TransactionOutput `json:"outputs"`
3034
Metadata *cbor.Value `json:"metadata,omitempty"`
35+
Fee uint64 `json:"fee"`
36+
TTL uint64 `json:"ttl,omitempty"`
3137
}
3238

33-
func NewTransactionEvent(block ledger.Block, tx ledger.Transaction, includeCbor bool) TransactionEvent {
34-
evt := TransactionEvent{
39+
func NewTransactionContext(block ledger.Block, tx ledger.Transaction, index uint32) TransactionContext {
40+
ctx := TransactionContext{
3541
BlockNumber: block.BlockNumber(),
36-
BlockHash: block.Hash(),
3742
SlotNumber: block.SlotNumber(),
3843
TransactionHash: tx.Hash(),
44+
TransactionIdx: index,
45+
}
46+
return ctx
47+
}
48+
49+
func NewTransactionEvent(block ledger.Block, tx ledger.Transaction, includeCbor bool) TransactionEvent {
50+
evt := TransactionEvent{
51+
BlockHash: block.Hash(),
3952
Inputs: tx.Inputs(),
4053
Outputs: tx.Outputs(),
54+
Fee: tx.Fee(),
55+
TTL: tx.TTL(),
4156
}
4257
if includeCbor {
4358
evt.TransactionCbor = tx.Cbor()

output/notify/notify.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,22 @@ func (n *NotifyOutput) Start() error {
9797
if payload == nil {
9898
panic(fmt.Errorf("ERROR: %v", payload))
9999
}
100+
context := evt.Context
101+
if context == nil {
102+
panic(fmt.Errorf("ERROR: %v", context))
103+
}
100104

101105
te := payload.(chainsync.TransactionEvent)
106+
tc := context.(chainsync.TransactionContext)
102107
err := beeep.Notify(
103108
n.title,
104-
fmt.Sprintf("New Transaction!\nBlockNumber: %d, SlotNumber: %d\nInputs: %d, Outputs: %d\nHash: %s",
105-
te.BlockNumber,
106-
te.SlotNumber,
109+
fmt.Sprintf("New Transaction!\nBlockNumber: %d, SlotNumber: %d\nInputs: %d, Outputs: %d\nFee: %d\nHash: %s",
110+
tc.BlockNumber,
111+
tc.SlotNumber,
107112
len(te.Inputs),
108113
len(te.Outputs),
109-
te.TransactionHash,
114+
te.Fee,
115+
tc.TransactionHash,
110116
),
111117
"assets/snek-icon.png",
112118
)

output/push/push.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,23 @@ func (p *PushOutput) Start() error {
114114
if payload == nil {
115115
panic(fmt.Errorf("ERROR: %v", payload))
116116
}
117+
context := evt.Context
118+
if context == nil {
119+
panic(fmt.Errorf("ERROR: %v", context))
120+
}
117121

118122
te := payload.(chainsync.TransactionEvent)
123+
tc := context.(chainsync.TransactionContext)
119124

120125
// Create notification message
121126
title := "Snek"
122-
body := fmt.Sprintf("New Transaction!\nBlockNumber: %d, SlotNumber: %d\nInputs: %d, Outputs: %d\nHash: %s",
123-
te.BlockNumber,
124-
te.SlotNumber,
127+
body := fmt.Sprintf("New Transaction!\nBlockNumber: %d, SlotNumber: %d\nInputs: %d, Outputs: %d\nFee: %d\nHash: %s",
128+
tc.BlockNumber,
129+
tc.SlotNumber,
125130
len(te.Inputs),
126131
len(te.Outputs),
127-
te.TransactionHash,
132+
te.Fee,
133+
tc.TransactionHash,
128134
)
129135

130136
// Send notification

output/webhook/webhook.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,15 @@ func formatWebhook(e *event.Event, format string) []byte {
155155
})
156156
case "chainsync.transaction":
157157
te := e.Payload.(chainsync.TransactionEvent)
158+
tc := e.Context.(chainsync.TransactionContext)
158159
dme.Title = "New Cardano Transaction"
159160
dmefs = append(dmefs, &DiscordMessageEmbedField{
160161
Name: "Block Number",
161-
Value: fmt.Sprintf("%d", te.BlockNumber),
162+
Value: fmt.Sprintf("%d", tc.BlockNumber),
162163
})
163164
dmefs = append(dmefs, &DiscordMessageEmbedField{
164165
Name: "Slot Number",
165-
Value: fmt.Sprintf("%d", te.SlotNumber),
166+
Value: fmt.Sprintf("%d", tc.SlotNumber),
166167
})
167168
dmefs = append(dmefs, &DiscordMessageEmbedField{
168169
Name: "Inputs",
@@ -172,12 +173,16 @@ func formatWebhook(e *event.Event, format string) []byte {
172173
Name: "Outputs",
173174
Value: fmt.Sprintf("%d", len(te.Outputs)),
174175
})
176+
dmefs = append(dmefs, &DiscordMessageEmbedField{
177+
Name: "Fee",
178+
Value: fmt.Sprintf("%d", te.Fee),
179+
})
175180
dmefs = append(dmefs, &DiscordMessageEmbedField{
176181
Name: "Transaction Hash",
177-
Value: te.TransactionHash,
182+
Value: tc.TransactionHash,
178183
})
179184
// TODO: fix this URL for different networks
180-
dme.URL = fmt.Sprintf("https://cexplorer.io/tx/%s", te.TransactionHash)
185+
dme.URL = fmt.Sprintf("https://cexplorer.io/tx/%s", tc.TransactionHash)
181186
default:
182187
dwe.Content = fmt.Sprintf("%v", e.Payload)
183188
}

0 commit comments

Comments
 (0)