-
Notifications
You must be signed in to change notification settings - Fork 17
/
service.go
70 lines (60 loc) · 1.9 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package operations
import (
"context"
"fmt"
"github.com/wormhole-foundation/wormhole-explorer/api/internal/pagination"
"github.com/wormhole-foundation/wormhole-explorer/common/types"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
"go.uber.org/zap"
)
type Service struct {
repo *Repository
logger *zap.Logger
}
// NewService create a new Service.
func NewService(repo *Repository, logger *zap.Logger) *Service {
return &Service{repo: repo, logger: logger.With(zap.String("module", "OperationService"))}
}
// FindById returns the operations for the given chainID/emitter/seq.
func (s *Service) FindById(ctx context.Context, chainID vaa.ChainID,
emitter *types.Address, seq string) (*OperationDto, error) {
id := fmt.Sprintf("%d/%s/%s", chainID, emitter.Hex(), seq)
operation, err := s.repo.FindById(ctx, id)
if err != nil {
return nil, err
}
return operation, nil
}
type OperationFilter struct {
TxHash *types.TxHash
Address string
SourceChainIDs []vaa.ChainID
TargetChainIDs []vaa.ChainID
AppIDs []string
ExclusiveAppId bool
Pagination pagination.Pagination
}
// FindAll returns all operations filtered by q.
func (s *Service) FindAll(ctx context.Context, filter OperationFilter) ([]*OperationDto, error) {
var txHash string
if filter.TxHash != nil {
txHash = filter.TxHash.String()
}
operationQuery := OperationQuery{
TxHash: txHash,
Address: filter.Address,
Pagination: filter.Pagination,
SourceChainIDs: filter.SourceChainIDs,
TargetChainIDs: filter.TargetChainIDs,
AppIDs: filter.AppIDs,
ExclusiveAppId: filter.ExclusiveAppId,
}
if len(operationQuery.AppIDs) != 0 || len(operationQuery.SourceChainIDs) > 0 || len(operationQuery.TargetChainIDs) > 0 {
return s.repo.FindByChainAndAppId(ctx, operationQuery)
}
operations, err := s.repo.FindAll(ctx, operationQuery)
if err != nil {
return nil, err
}
return operations, nil
}