github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/accounts.go (about)

     1  package rest
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/access"
     5  	"github.com/koko1123/flow-go-1/engine/access/rest/models"
     6  	"github.com/koko1123/flow-go-1/engine/access/rest/request"
     7  )
     8  
     9  // GetAccount handler retrieves account by address and returns the response
    10  func GetAccount(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) {
    11  	req, err := r.GetAccountRequest()
    12  	if err != nil {
    13  		return nil, NewBadRequestError(err)
    14  	}
    15  
    16  	// in case we receive special height values 'final' and 'sealed', fetch that height and overwrite request with it
    17  	if req.Height == request.FinalHeight || req.Height == request.SealedHeight {
    18  		header, _, err := backend.GetLatestBlockHeader(r.Context(), req.Height == request.SealedHeight)
    19  		if err != nil {
    20  			return nil, err
    21  		}
    22  		req.Height = header.Height
    23  	}
    24  
    25  	account, err := backend.GetAccountAtBlockHeight(r.Context(), req.Address, req.Height)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	var response models.Account
    31  	err = response.Build(account, link, r.ExpandFields)
    32  	return response, err
    33  }