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

     1  package routes
     2  
     3  import (
     4  	entitiesproto "github.com/onflow/flow/protobuf/go/flow/entities"
     5  
     6  	"github.com/onflow/flow-go/access"
     7  	"github.com/onflow/flow-go/engine/access/rest/models"
     8  	"github.com/onflow/flow-go/engine/access/rest/request"
     9  )
    10  
    11  // GetTransactionByID gets a transaction by requested ID.
    12  func GetTransactionByID(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) {
    13  	req, err := r.GetTransactionRequest()
    14  	if err != nil {
    15  		return nil, models.NewBadRequestError(err)
    16  	}
    17  
    18  	tx, err := backend.GetTransaction(r.Context(), req.ID)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	var txr *access.TransactionResult
    24  	// only lookup result if transaction result is to be expanded
    25  	if req.ExpandsResult {
    26  		txr, err = backend.GetTransactionResult(
    27  			r.Context(),
    28  			req.ID,
    29  			req.BlockID,
    30  			req.CollectionID,
    31  			entitiesproto.EventEncodingVersion_JSON_CDC_V0,
    32  		)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  	}
    37  
    38  	var response models.Transaction
    39  	response.Build(tx, txr, link)
    40  	return response, nil
    41  }
    42  
    43  // GetTransactionResultByID retrieves transaction result by the transaction ID.
    44  func GetTransactionResultByID(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) {
    45  	req, err := r.GetTransactionResultRequest()
    46  	if err != nil {
    47  		return nil, models.NewBadRequestError(err)
    48  	}
    49  
    50  	txr, err := backend.GetTransactionResult(
    51  		r.Context(),
    52  		req.ID,
    53  		req.BlockID,
    54  		req.CollectionID,
    55  		entitiesproto.EventEncodingVersion_JSON_CDC_V0,
    56  	)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	var response models.TransactionResult
    62  	response.Build(txr, req.ID, link)
    63  	return response, nil
    64  }
    65  
    66  // CreateTransaction creates a new transaction from provided payload.
    67  func CreateTransaction(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) {
    68  	req, err := r.CreateTransactionRequest()
    69  	if err != nil {
    70  		return nil, models.NewBadRequestError(err)
    71  	}
    72  
    73  	err = backend.SendTransaction(r.Context(), &req.Transaction)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	var response models.Transaction
    79  	response.Build(&req.Transaction, nil, link)
    80  	return response, nil
    81  }