github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/neorpc/events.go (about) 1 package neorpc 2 3 import ( 4 "encoding/json" 5 "errors" 6 ) 7 8 // EventID represents an event type happening on the chain. 9 type EventID byte 10 11 const ( 12 // InvalidEventID is an invalid event id that is the default value of 13 // EventID. It's only used as an initial value similar to nil. 14 InvalidEventID EventID = iota 15 // BlockEventID is a `block_added` event. 16 BlockEventID 17 // TransactionEventID corresponds to the `transaction_added` event. 18 TransactionEventID 19 // NotificationEventID represents `notification_from_execution` events. 20 NotificationEventID 21 // ExecutionEventID is used for `transaction_executed` events. 22 ExecutionEventID 23 // NotaryRequestEventID is used for the `notary_request_event` event. 24 NotaryRequestEventID 25 // HeaderOfAddedBlockEventID is used for the `header_of_added_block` event. 26 HeaderOfAddedBlockEventID 27 // MissedEventID notifies user of missed events. 28 MissedEventID EventID = 255 29 ) 30 31 // String is a good old Stringer implementation. 32 func (e EventID) String() string { 33 switch e { 34 case BlockEventID: 35 return "block_added" 36 case TransactionEventID: 37 return "transaction_added" 38 case NotificationEventID: 39 return "notification_from_execution" 40 case ExecutionEventID: 41 return "transaction_executed" 42 case NotaryRequestEventID: 43 return "notary_request_event" 44 case HeaderOfAddedBlockEventID: 45 return "header_of_added_block" 46 case MissedEventID: 47 return "event_missed" 48 default: 49 return "unknown" 50 } 51 } 52 53 // GetEventIDFromString converts an input string into an EventID if it's possible. 54 func GetEventIDFromString(s string) (EventID, error) { 55 switch s { 56 case "block_added": 57 return BlockEventID, nil 58 case "transaction_added": 59 return TransactionEventID, nil 60 case "notification_from_execution": 61 return NotificationEventID, nil 62 case "transaction_executed": 63 return ExecutionEventID, nil 64 case "notary_request_event": 65 return NotaryRequestEventID, nil 66 case "header_of_added_block": 67 return HeaderOfAddedBlockEventID, nil 68 case "event_missed": 69 return MissedEventID, nil 70 default: 71 return 255, errors.New("invalid stream name") 72 } 73 } 74 75 // MarshalJSON implements the json.Marshaler interface. 76 func (e EventID) MarshalJSON() ([]byte, error) { 77 return json.Marshal(e.String()) 78 } 79 80 // UnmarshalJSON implements the json.Unmarshaler interface. 81 func (e *EventID) UnmarshalJSON(b []byte) error { 82 var s string 83 84 err := json.Unmarshal(b, &s) 85 if err != nil { 86 return err 87 } 88 id, err := GetEventIDFromString(s) 89 if err != nil { 90 return err 91 } 92 *e = id 93 return nil 94 }