2022-11-29 13:43:18 +00:00
|
|
|
package walletevent
|
|
|
|
|
|
|
|
import (
|
2024-02-09 01:55:33 +00:00
|
|
|
"encoding/json"
|
2022-11-29 13:43:18 +00:00
|
|
|
"math/big"
|
2023-10-09 12:43:53 +00:00
|
|
|
"strings"
|
2022-11-29 13:43:18 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EventType type for event types.
|
|
|
|
type EventType string
|
|
|
|
|
2023-10-09 12:43:53 +00:00
|
|
|
// EventType prefix to be used for internal events.
|
|
|
|
// These events are not forwarded to the client, they are only used
|
|
|
|
// within status-go.
|
|
|
|
const InternalEventTypePrefix = "INT-"
|
|
|
|
|
|
|
|
func (t EventType) IsInternal() bool {
|
|
|
|
return strings.HasPrefix(string(t), InternalEventTypePrefix)
|
|
|
|
}
|
|
|
|
|
2022-11-29 13:43:18 +00:00
|
|
|
// Event is a type for transfer events.
|
|
|
|
type Event struct {
|
|
|
|
Type EventType `json:"type"`
|
|
|
|
BlockNumber *big.Int `json:"blockNumber"`
|
|
|
|
Accounts []common.Address `json:"accounts"`
|
|
|
|
Message string `json:"message"`
|
2023-03-20 13:02:09 +00:00
|
|
|
At int64 `json:"at"`
|
|
|
|
ChainID uint64 `json:"chainId"`
|
2023-07-24 22:54:53 +00:00
|
|
|
RequestID *int `json:"requestId,omitempty"`
|
2024-01-08 05:21:50 +00:00
|
|
|
// For Internal events only, not serialized
|
|
|
|
EventParams interface{}
|
2022-11-29 13:43:18 +00:00
|
|
|
}
|
2024-02-09 01:55:33 +00:00
|
|
|
|
|
|
|
func GetPayload[T any](e Event) (*T, error) {
|
|
|
|
var payload T
|
|
|
|
err := json.Unmarshal([]byte(e.Message), &payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &payload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExtractPayload[T any](e Event, payload *T) error {
|
|
|
|
return json.Unmarshal([]byte(e.Message), payload)
|
|
|
|
}
|