-
Notifications
You must be signed in to change notification settings - Fork 3
/
event.go
94 lines (83 loc) · 2.81 KB
/
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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
)
type EventType string
type TxDirection string
type ContractEvents string
type SpecificActionType string
const (
Block EventType = "block"
Transaction EventType = "transaction"
// a special event type that is used to track in and out tx of a address
Wallet EventType = "wallet"
StakeDeposited SpecificActionType = "stake_deposited"
StakeRebalanced SpecificActionType = "stake_rebalanced"
WithdrawalInitiated SpecificActionType = "withdrawal_initiated"
WithdrawalFullfilled SpecificActionType = "withdrawal_fullfilled"
// wallet event actions
Received SpecificActionType = "received"
Sent SpecificActionType = "sent"
)
type Event struct {
Chain ChainType `json:"chain"`
Network NetworkType `json:"network"`
Provider ProviderType `json:"provider"`
Type EventType `json:"type"`
Height uint64 `json:"height"`
Block string `json:"block"`
Transaction string `json:"transaction"`
ReceivedAt uint64 `json:"received_at"`
Sender string `json:"sender"`
Recipient string `json:"recipient"`
SenderBalance string `json:"sender_balance"`
RecipientBalance string `json:"recipient_balance"`
Price string `json:"price"`
Amount string `json:"amount"`
GasFee string `json:"gas_fee"`
}
type Action struct {
Chain ChainType `json:"chain"`
Network NetworkType `json:"network"`
Type EventType `json:"type"`
Action SpecificActionType `json:"action"`
Address string `json:"address"`
Amount string `json:"amount"`
Balance string `json:"balance"`
Gas string `json:"gas"`
Hash string `json:"hash"`
Price string `json:"price"`
ReceivedAt uint64 `json:"received_at"`
RewardsAllTime string `json:"rewards_all_time"`
StakingFees string `json:"staking_fees"`
}
type BlockEventsResult struct {
Events []Event `json:"events"`
Action []Action `json:"action"`
EventsPartitionKey Partition `json:"events_partition_key"`
ActionPartitionKey Partition `json:"action_partition_key"`
}
func (e EventType) String() string {
switch e {
case Block:
return "block"
case Transaction:
return "transaction"
default:
return ""
}
}
func NDJSON[T Event | Action](events []T) (*bytes.Buffer, error) {
var buf bytes.Buffer
for _, ev := range events {
txj, err := json.Marshal(ev)
if err != nil {
return nil, fmt.Errorf("failed to encode wallet to JSON: %v", err)
}
buf.Write(txj)
buf.WriteString("\n")
}
return &buf, nil
}