-
Notifications
You must be signed in to change notification settings - Fork 15
/
service.go
123 lines (110 loc) · 4.08 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package bid_rpc_query
import (
"github.com/Oneledger/protocol/data/balance"
"github.com/Oneledger/protocol/data/ons"
"github.com/Oneledger/protocol/external_apps/bid/bid_data"
"github.com/Oneledger/protocol/external_apps/bid/bid_rpc"
"github.com/Oneledger/protocol/log"
)
type Service struct {
balances *balance.Store
currencies *balance.CurrencySet
ons *ons.DomainStore
logger *log.Logger
bidMaster *bid_data.BidMasterStore
}
func Name() string {
return "bid_query"
}
func NewService(balances *balance.Store, currencies *balance.CurrencySet,
domains *ons.DomainStore, logger *log.Logger, bidMaster *bid_data.BidMasterStore) *Service {
return &Service{
currencies: currencies,
balances: balances,
ons: domains,
logger: logger,
bidMaster: bidMaster,
}
}
func (svc *Service) ShowBidConv(req bid_rpc.ListBidConvRequest, reply *bid_rpc.ListBidConvsReply) error {
bidConv, _, err := svc.bidMaster.BidConv.QueryAllStores(req.BidConvId)
if err != nil {
return bid_rpc.ErrGettingBidConvInQuery.Wrap(err)
}
inactiveOffers := svc.bidMaster.BidOffer.GetInActiveOffers(bidConv.BidConvId, bid_data.TypeInvalid)
activeOffer, err := svc.bidMaster.BidOffer.GetActiveOffer(bidConv.BidConvId, bid_data.TypeInvalid)
if err != nil {
return bid_rpc.ErrGettingActiveOfferInQuery.Wrap(err)
}
activeOfferField := bid_data.BidOffer{}
if activeOffer != nil {
activeOfferField = *activeOffer
}
bcs := bid_rpc.BidConvStat{
BidConv: *bidConv,
ActiveOffer: activeOfferField,
InactiveOffers: inactiveOffers,
}
*reply = bid_rpc.ListBidConvsReply{
BidConvStats: []bid_rpc.BidConvStat{bcs},
Height: svc.bidMaster.BidConv.GetState().Version(),
}
return nil
}
// list single proposal by id or list proposals
func (svc *Service) ListBidConvs(req bid_rpc.ListBidConvsRequest, reply *bid_rpc.ListBidConvsReply) error {
// Validate parameters
if len(req.Owner) != 0 {
err := req.Owner.Err()
if err != nil {
return bid_rpc.ErrInvalidOwnerAddressInQuery.Wrap(err)
}
}
if len(req.Bidder) != 0 {
err := req.Bidder.Err()
if err != nil {
return bid_rpc.ErrInvalidBidderAddressInQuery.Wrap(err)
}
}
// Query in single store if specified
var bidConvs []bid_data.BidConv
if req.State != bid_data.BidStateInvalid {
bidConvs = svc.bidMaster.BidConv.FilterBidConvs(req.State, req.Owner, req.AssetName, req.AssetType, req.Bidder)
} else { // Query in all stores otherwise
active := svc.bidMaster.BidConv.FilterBidConvs(bid_data.BidStateActive, req.Owner, req.AssetName, req.AssetType, req.Bidder)
succeed := svc.bidMaster.BidConv.FilterBidConvs(bid_data.BidStateSucceed, req.Owner, req.AssetName, req.AssetType, req.Bidder)
rejected := svc.bidMaster.BidConv.FilterBidConvs(bid_data.BidStateRejected, req.Owner, req.AssetName, req.AssetType, req.Bidder)
expired := svc.bidMaster.BidConv.FilterBidConvs(bid_data.BidStateExpired, req.Owner, req.AssetName, req.AssetType, req.Bidder)
cancelled := svc.bidMaster.BidConv.FilterBidConvs(bid_data.BidStateCancelled, req.Owner, req.AssetName, req.AssetType, req.Bidder)
bidConvs = append(bidConvs, active...)
bidConvs = append(bidConvs, succeed...)
bidConvs = append(bidConvs, rejected...)
bidConvs = append(bidConvs, expired...)
bidConvs = append(bidConvs, cancelled...)
}
// Organize reply packet:
// Bid conversations and their offers
bidConvStats := make([]bid_rpc.BidConvStat, len(bidConvs))
for i, bidConv := range bidConvs {
inactiveOffers := svc.bidMaster.BidOffer.GetInActiveOffers(bidConv.BidConvId, bid_data.TypeInvalid)
activeOffer, err := svc.bidMaster.BidOffer.GetActiveOffer(bidConv.BidConvId, bid_data.TypeInvalid)
if err != nil {
return bid_rpc.ErrGettingActiveOfferInQuery.Wrap(err)
}
activeOfferField := bid_data.BidOffer{}
if activeOffer != nil {
activeOfferField = *activeOffer
}
bcs := bid_rpc.BidConvStat{
BidConv: bidConv,
ActiveOffer: activeOfferField,
InactiveOffers: inactiveOffers,
}
bidConvStats[i] = bcs
}
*reply = bid_rpc.ListBidConvsReply{
BidConvStats: bidConvStats,
Height: svc.bidMaster.BidConv.GetState().Version(),
}
return nil
}