/// 链事件
class ChainEvent {
  final String type;
  final String subscriptionId;
  final Map<String, dynamic> data;

  ChainEvent({
    required this.type,
    required this.subscriptionId,
    required this.data,
  });

  factory ChainEvent.fromJson(Map<String, dynamic> json) {
    return ChainEvent(
      type: json['type'] as String? ?? 'unknown',
      subscriptionId: json['subscription'] as String? ?? '',
      data: json['result'] as Map<String, dynamic>? ?? json,
    );
  }

  Map<String, dynamic> toJson() => {
        'type': type,
        'subscription': subscriptionId,
        'data': data,
      };

  /// 从 WebSocket eth_subscription 消息解析
  factory ChainEvent.fromSubscription(Map<String, dynamic> params) {
    return ChainEvent(
      type: 'subscription',
      subscriptionId: params['subscription'] as String? ?? '',
      data: params['result'] as Map<String, dynamic>? ?? {},
    );
  }
}
