github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/routes/subscribe_events.go (about)

     1  package routes
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/onflow/flow-go/engine/access/rest/models"
     7  	"github.com/onflow/flow-go/engine/access/rest/request"
     8  	"github.com/onflow/flow-go/engine/access/state_stream"
     9  	"github.com/onflow/flow-go/engine/access/subscription"
    10  )
    11  
    12  // SubscribeEvents create websocket connection and write to it requested events.
    13  func SubscribeEvents(
    14  	ctx context.Context,
    15  	request *request.Request,
    16  	wsController *WebsocketController,
    17  ) (subscription.Subscription, error) {
    18  	req, err := request.SubscribeEventsRequest()
    19  	if err != nil {
    20  		return nil, models.NewBadRequestError(err)
    21  	}
    22  	// Retrieve the filter parameters from the request, if provided
    23  	filter, err := state_stream.NewEventFilter(
    24  		wsController.eventFilterConfig,
    25  		request.Chain,
    26  		req.EventTypes,
    27  		req.Addresses,
    28  		req.Contracts,
    29  	)
    30  	if err != nil {
    31  		return nil, models.NewBadRequestError(err)
    32  	}
    33  
    34  	// Check if heartbeat interval was passed via request
    35  	if req.HeartbeatInterval > 0 {
    36  		wsController.heartbeatInterval = req.HeartbeatInterval
    37  	}
    38  
    39  	return wsController.api.SubscribeEvents(ctx, req.StartBlockID, req.StartHeight, filter), nil
    40  }