package services import ( "context" "github.com/coinbase/rosetta-sdk-go/types" wikiTypes "github.com/woop-chain/woop/core/types" "github.com/woop-chain/woop/wiki" ) // EventAPI implements the server.EventsAPIServicer interface. type EventAPI struct { wiki *wiki.Woop } func NewEventAPI(wiki *wiki.Woop) *EventAPI { return &EventAPI{wiki: wiki} } // EventsBlocks implements the /events/blocks endpoint func (e *EventAPI) EventsBlocks(ctx context.Context, request *types.EventsBlocksRequest) (resp *types.EventsBlocksResponse, err *types.Error) { cacheItem, cacheHelper, cacheErr := rosettaCacheHelper("EventsBlocks", request) if cacheErr == nil { if cacheItem != nil { return cacheItem.resp.(*types.EventsBlocksResponse), nil } else { defer cacheHelper(resp, err) } } if err := assertValidNetworkIdentifier(request.NetworkIdentifier, e.wiki.ShardID); err != nil { return nil, err } var offset, limit int64 if request.Limit == nil { limit = 10 } else { limit = *request.Limit if limit > 1000 { limit = 1000 } } if request.Offset == nil { offset = 0 } else { offset = *request.Offset } resp = &types.EventsBlocksResponse{ MaxSequence: e.wiki.BlockChain.CurrentHeader().Number().Int64(), } for i := offset; i < offset+limit; i++ { block := e.wiki.BlockChain.GetBlockByNumber(uint64(i)) if block == nil { break } resp.Events = append(resp.Events, buildFromBlock(block)) } return resp, nil } func buildFromBlock(block *wikiTypes.Block) *types.BlockEvent { return &types.BlockEvent{ Sequence: block.Number().Int64(), BlockIdentifier: &types.BlockIdentifier{ Index: block.Number().Int64(), Hash: block.Hash().Hex(), }, Type: types.ADDED, } }