Skip to content

Commit da44875

Browse files
committed
feedback:QuoteHashToIndex
1 parent c1cb4b3 commit da44875

File tree

11 files changed

+106
-139
lines changed

11 files changed

+106
-139
lines changed

OpenApi.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -685,22 +685,24 @@ components:
685685
type: object
686686
SummaryData:
687687
properties:
688-
confirmedQuotesCount:
688+
acceptedQuotesCount:
689689
type: integer
690690
lpEarnings:
691691
$ref: '#/components/schemas/Wei'
692+
paidQuotesAmount:
693+
$ref: '#/components/schemas/Wei'
694+
paidQuotesCount:
695+
type: integer
692696
refundedQuotesCount:
693697
type: integer
694698
totalAcceptedQuotedAmount:
695699
$ref: '#/components/schemas/Wei'
696-
totalAcceptedQuotesCount:
697-
type: integer
698700
totalFeesCollected:
699701
$ref: '#/components/schemas/Wei'
700702
totalPenaltyAmount:
701703
$ref: '#/components/schemas/Wei'
702-
totalQuotedAmount:
703-
$ref: '#/components/schemas/Wei'
704+
totalQuotesCount:
705+
type: integer
704706
type: object
705707
SummaryResult:
706708
properties:

internal/adapters/dataproviders/database/mongo/common.go

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ func (c *Connection) CheckConnection(ctx context.Context) bool {
8080
}
8181

8282
type QuoteResult[Q any, R QuoteHashProvider] struct {
83-
Quotes []Q
84-
RetainedQuotes []R
85-
QuoteHashToIndex map[string]int
86-
Error error
83+
Quotes []Q
84+
RetainedQuotes []R
85+
Error error
8786
}
8887

8988
type QuoteQuery struct {
@@ -105,37 +104,24 @@ func ListQuotesByDateRange[Q any, R QuoteHashProvider](
105104
if err != nil {
106105
return QuoteResult[Q, R]{Error: err}
107106
}
108-
quoteHashToIndex := make(map[string]int, len(quoteHashes))
109-
for i, hash := range quoteHashes {
110-
if hash != "" {
111-
quoteHashToIndex[hash] = i
112-
}
113-
}
114107
retainedQuotes, additionalHashes, err := fetchRetainedQuotes[R](dbCtx, query.Conn, query.StartDate, query.EndDate, query.RetainedCollection, quoteHashes)
115108
if err != nil {
116109
return QuoteResult[Q, R]{Error: err}
117110
}
118111
if len(additionalHashes) > 0 {
119-
additionalQuotes, additionalHashIndices, err := fetchAdditionalQuotes(dbCtx, query.Conn, query.QuoteCollection, additionalHashes, mapper)
112+
additionalQuotes, err := fetchAdditionalQuotes(dbCtx, query.Conn, query.QuoteCollection, additionalHashes, mapper)
120113
if err != nil {
121114
log.Errorf("Error processing additional quotes: %v", err)
122115
} else {
123-
baseIndex := len(quotes)
124-
for i, hash := range additionalHashIndices {
125-
if hash != "" {
126-
quoteHashToIndex[hash] = baseIndex + i
127-
}
128-
}
129116
quotes = append(quotes, additionalQuotes...)
130117
}
131118
}
132119
logDbInteraction(Read, fmt.Sprintf("Found %d quotes and %d retained quotes in date range",
133120
len(quotes), len(retainedQuotes)))
134121
return QuoteResult[Q, R]{
135-
Quotes: quotes,
136-
RetainedQuotes: retainedQuotes,
137-
QuoteHashToIndex: quoteHashToIndex,
138-
Error: nil,
122+
Quotes: quotes,
123+
RetainedQuotes: retainedQuotes,
124+
Error: nil,
139125
}
140126
}
141127

@@ -247,7 +233,7 @@ func fetchAdditionalQuotes[Q any](
247233
collectionName string,
248234
hashes []string,
249235
mapper func(bson.D) Q,
250-
) ([]Q, []string, error) {
236+
) ([]Q, error) {
251237
quoteFilter := bson.D{
252238
{Key: "hash", Value: bson.D{
253239
{Key: "$in", Value: hashes},
@@ -256,22 +242,15 @@ func fetchAdditionalQuotes[Q any](
256242
var storedQuotes []bson.D
257243
quoteCursor, err := conn.Collection(collectionName).Find(ctx, quoteFilter)
258244
if err != nil {
259-
return nil, nil, err
245+
return nil, err
260246
}
261247
if err = quoteCursor.All(ctx, &storedQuotes); err != nil {
262-
return nil, nil, err
248+
return nil, err
263249
}
264250
quotes := make([]Q, 0, len(storedQuotes))
265-
resultHashes := make([]string, 0, len(storedQuotes))
266251
for _, stored := range storedQuotes {
267252
quoteObj := mapper(stored)
268253
quotes = append(quotes, quoteObj)
269-
hashValue, ok := getStringValueFromBSON(stored, "hash")
270-
if ok {
271-
resultHashes = append(resultHashes, hashValue)
272-
} else {
273-
resultHashes = append(resultHashes, "")
274-
}
275254
}
276-
return quotes, resultHashes, nil
255+
return quotes, nil
277256
}

internal/adapters/dataproviders/database/mongo/pegin.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ func (repo *peginMongoRepository) ListQuotesByDateRange(ctx context.Context, sta
240240
return quote.PeginQuoteResult{}, result.Error
241241
}
242242
return quote.PeginQuoteResult{
243-
Quotes: result.Quotes,
244-
RetainedQuotes: result.RetainedQuotes,
245-
QuoteHashToIndex: result.QuoteHashToIndex,
243+
Quotes: result.Quotes,
244+
RetainedQuotes: result.RetainedQuotes,
246245
}, nil
247246
}

internal/adapters/dataproviders/database/mongo/pegout.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ func (repo *pegoutMongoRepository) ListQuotesByDateRange(ctx context.Context, st
351351
return quote.PegoutQuoteResult{}, result.Error
352352
}
353353
return quote.PegoutQuoteResult{
354-
Quotes: result.Quotes,
355-
RetainedQuotes: result.RetainedQuotes,
356-
QuoteHashToIndex: result.QuoteHashToIndex,
354+
Quotes: result.Quotes,
355+
RetainedQuotes: result.RetainedQuotes,
357356
}, nil
358357
}

internal/adapters/entrypoints/rest/assets/static/management.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,10 @@ const displaySummaryData = (container, data) => {
577577
const table = document.createElement('table');
578578
table.classList.add('table', 'table-striped');
579579
const rows = [
580-
{ label: 'Total Accepted Quotes', value: data.totalAcceptedQuotesCount },
581-
{ label: 'Confirmed Quotes', value: data.confirmedQuotesCount },
582-
{ label: 'Total Quoted Amount', value: data.totalQuotedAmount },
580+
{ label: 'Total Quotes', value: data.totalQuotesCount },
581+
{ label: 'Accepted Quotes', value: data.acceptedQuotesCount },
582+
{ label: 'Paid Quotes', value: data.paidQuotesCount },
583+
{ label: 'Paid Quotes Amount', value: data.paidQuotesAmount },
583584
{ label: 'Total Accepted Amount', value: data.totalAcceptedQuotedAmount },
584585
{ label: 'Total Fees Collected', value: data.totalFeesCollected },
585586
{ label: 'Refunded Quotes', value: data.refundedQuotesCount },

internal/adapters/entrypoints/rest/handlers/get_report_summaries_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,24 @@ func TestGetReportSummariesHandler(t *testing.T) { //nolint:funlen
6363
expectedStatus: http.StatusOK,
6464
mockResponse: liquidity_provider.SummaryResult{
6565
PeginSummary: liquidity_provider.SummaryData{
66-
TotalAcceptedQuotesCount: 10,
67-
ConfirmedQuotesCount: 8,
68-
TotalQuotedAmount: entities.NewWei(1000),
69-
TotalAcceptedQuotedAmount: entities.NewWei(800),
70-
TotalFeesCollected: entities.NewWei(50),
71-
RefundedQuotesCount: 2,
72-
TotalPenaltyAmount: entities.NewWei(20),
73-
LpEarnings: entities.NewWei(30),
66+
TotalQuotesCount: 10,
67+
AcceptedQuotesCount: 8,
68+
PaidQuotesCount: 6,
69+
PaidQuotesAmount: entities.NewWei(1000),
70+
TotalFeesCollected: entities.NewWei(50),
71+
RefundedQuotesCount: 2,
72+
TotalPenaltyAmount: entities.NewWei(20),
73+
LpEarnings: entities.NewWei(30),
7474
},
7575
PegoutSummary: liquidity_provider.SummaryData{
76-
TotalAcceptedQuotesCount: 5,
77-
ConfirmedQuotesCount: 4,
78-
TotalQuotedAmount: entities.NewWei(500),
79-
TotalAcceptedQuotedAmount: entities.NewWei(400),
80-
TotalFeesCollected: entities.NewWei(40),
81-
RefundedQuotesCount: 1,
82-
TotalPenaltyAmount: entities.NewWei(0),
83-
LpEarnings: entities.NewWei(20),
76+
TotalQuotesCount: 5,
77+
AcceptedQuotesCount: 4,
78+
PaidQuotesCount: 3,
79+
PaidQuotesAmount: entities.NewWei(500),
80+
TotalFeesCollected: entities.NewWei(40),
81+
RefundedQuotesCount: 1,
82+
TotalPenaltyAmount: entities.NewWei(0),
83+
LpEarnings: entities.NewWei(40),
8484
},
8585
},
8686
mockErr: nil,

internal/entities/quote/common.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ type FeeProvider interface {
4747
type QuoteResult[Q any, R RetainedQuote] interface {
4848
GetQuotes() []Q
4949
GetRetainedQuotes() []R
50-
GetQuoteHashToIndex() map[string]int
5150
}
5251

5352
// ValidateQuoteHash checks if a given string is a valid 32-byte quote hash

internal/entities/quote/pegin_quote.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ type PeginQuoteRepository interface {
4040
}
4141

4242
type PeginQuoteResult struct {
43-
Quotes []PeginQuote
44-
RetainedQuotes []RetainedPeginQuote
45-
QuoteHashToIndex map[string]int
43+
Quotes []PeginQuote
44+
RetainedQuotes []RetainedPeginQuote
4645
}
4746

4847
type CreatedPeginQuote struct {

internal/entities/quote/pegout_quote.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ type PegoutQuoteRepository interface {
4747
}
4848

4949
type PegoutQuoteResult struct {
50-
Quotes []PegoutQuote
51-
RetainedQuotes []RetainedPegoutQuote
52-
QuoteHashToIndex map[string]int
50+
Quotes []PegoutQuote
51+
RetainedQuotes []RetainedPegoutQuote
5352
}
5453

5554
type CreatedPegoutQuote struct {

internal/usecases/liquidity_provider/summaries.go

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ type SummaryResult struct {
2020
}
2121

2222
type SummaryData struct {
23-
TotalAcceptedQuotesCount int64 `json:"totalAcceptedQuotesCount"`
24-
ConfirmedQuotesCount int64 `json:"confirmedQuotesCount"`
25-
TotalQuotedAmount *entities.Wei `json:"totalQuotedAmount"`
23+
TotalQuotesCount int64 `json:"totalQuotesCount"`
24+
AcceptedQuotesCount int64 `json:"acceptedQuotesCount"`
25+
PaidQuotesCount int64 `json:"paidQuotesCount"`
26+
PaidQuotesAmount *entities.Wei `json:"paidQuotesAmount"`
2627
TotalAcceptedQuotedAmount *entities.Wei `json:"totalAcceptedQuotedAmount"`
2728
TotalFeesCollected *entities.Wei `json:"totalFeesCollected"`
2829
RefundedQuotesCount int64 `json:"refundedQuotesCount"`
@@ -38,9 +39,8 @@ type feeAdapter struct {
3839
}
3940

4041
type quoteResultAdapter[Q any, R quote.RetainedQuote] struct {
41-
quotes []Q
42-
retainedQuotes []R
43-
quoteHashToIndex map[string]int
42+
quotes []Q
43+
retainedQuotes []R
4444
}
4545

4646
type SummariesUseCase struct {
@@ -50,9 +50,10 @@ type SummariesUseCase struct {
5050

5151
func NewSummaryData() SummaryData {
5252
return SummaryData{
53-
TotalAcceptedQuotesCount: 0,
54-
ConfirmedQuotesCount: 0,
55-
TotalQuotedAmount: entities.NewWei(0),
53+
TotalQuotesCount: 0,
54+
AcceptedQuotesCount: 0,
55+
PaidQuotesCount: 0,
56+
PaidQuotesAmount: entities.NewWei(0),
5657
TotalAcceptedQuotedAmount: entities.NewWei(0),
5758
TotalFeesCollected: entities.NewWei(0),
5859
RefundedQuotesCount: 0,
@@ -116,39 +117,46 @@ func processQuoteData[Q any, R quote.RetainedQuote, F quote.FeeProvider](
116117
ctx context.Context,
117118
quotes []Q,
118119
retainedQuotes []R,
119-
quoteHashToIndex map[string]int,
120120
getQuote func(context.Context, string) (*Q, error),
121121
isPaid func(R) bool,
122122
isRefunded func(R) bool,
123123
feeProvider func(*Q) F,
124124
) SummaryData {
125125
data := NewSummaryData()
126126
totalAmount := calculateTotalAmount(quotes)
127+
data.TotalQuotesCount = int64(len(quotes))
127128
if len(retainedQuotes) == 0 {
128-
data.TotalAcceptedQuotesCount = int64(len(quotes))
129-
data.TotalQuotedAmount = totalAmount
129+
data.PaidQuotesAmount = totalAmount
130+
data.PaidQuotesCount = 0
130131
return data
131132
}
132-
data.TotalAcceptedQuotesCount = int64(len(retainedQuotes))
133-
quotesByHash := createQuoteHashMap(quotes, quoteHashToIndex)
133+
data.AcceptedQuotesCount = int64(len(retainedQuotes))
134+
quotesByHash := make(map[string]*Q)
135+
for i := range quotes {
136+
if retainedQuote, ok := any(&quotes[i]).(quote.RetainedQuote); ok {
137+
hash := retainedQuote.GetQuoteHash()
138+
quoteCopy := quotes[i]
139+
quotesByHash[hash] = &quoteCopy
140+
}
141+
}
134142
fetchMissingQuotes(ctx, quotesByHash, retainedQuotes, totalAmount, getQuote)
135-
data.TotalQuotedAmount = totalAmount
143+
data.PaidQuotesAmount = totalAmount
144+
data.PaidQuotesCount = 0
136145
acceptedTotalAmount := entities.NewWei(0)
137146
totalFees := entities.NewWei(0)
138147
callFees := entities.NewWei(0)
139148
totalPenalty := entities.NewWei(0)
140149
for _, retained := range retainedQuotes {
141-
quoteHash := retained.GetQuoteHash()
142-
quoteObj, exists := quotesByHash[quoteHash]
150+
quoteObj, exists := quotesByHash[retained.GetQuoteHash()]
143151
if !exists {
144152
continue
145153
}
146154
fees := feeProvider(quoteObj)
147-
data.ConfirmedQuotesCount++
148155
if q, ok := any(quoteObj).(quote.Quote); ok {
149156
acceptedTotalAmount.Add(acceptedTotalAmount, q.Total())
150157
}
151158
if isPaid(retained) {
159+
data.PaidQuotesCount++
152160
callFee := fees.GetCallFee()
153161
callFees.Add(callFees, callFee)
154162
totalFees.Add(totalFees, callFee)
@@ -183,17 +191,6 @@ func calculateTotalAmount[T any](quotes []T) *entities.Wei {
183191
return totalAmount
184192
}
185193

186-
func createQuoteHashMap[T any](quotes []T, quoteHashToIndex map[string]int) map[string]*T {
187-
quotesByHash := make(map[string]*T, len(quoteHashToIndex))
188-
for hash, index := range quoteHashToIndex {
189-
if index >= 0 && index < len(quotes) {
190-
quoteCopy := quotes[index]
191-
quotesByHash[hash] = &quoteCopy
192-
}
193-
}
194-
return quotesByHash
195-
}
196-
197194
func fetchMissingQuotes[Q any, R quote.RetainedQuote](
198195
ctx context.Context,
199196
quotesByHash map[string]*Q,
@@ -224,17 +221,15 @@ func fetchMissingQuotes[Q any, R quote.RetainedQuote](
224221

225222
func adaptPeginResult(result quote.PeginQuoteResult) quote.QuoteResult[quote.PeginQuote, quote.RetainedPeginQuote] {
226223
return quoteResultAdapter[quote.PeginQuote, quote.RetainedPeginQuote]{
227-
quotes: result.Quotes,
228-
retainedQuotes: result.RetainedQuotes,
229-
quoteHashToIndex: result.QuoteHashToIndex,
224+
quotes: result.Quotes,
225+
retainedQuotes: result.RetainedQuotes,
230226
}
231227
}
232228

233229
func adaptPegoutResult(result quote.PegoutQuoteResult) quote.QuoteResult[quote.PegoutQuote, quote.RetainedPegoutQuote] {
234230
return quoteResultAdapter[quote.PegoutQuote, quote.RetainedPegoutQuote]{
235-
quotes: result.Quotes,
236-
retainedQuotes: result.RetainedQuotes,
237-
quoteHashToIndex: result.QuoteHashToIndex,
231+
quotes: result.Quotes,
232+
retainedQuotes: result.RetainedQuotes,
238233
}
239234
}
240235

@@ -256,7 +251,6 @@ func aggregateData[Q any, RQ quote.RetainedQuote](
256251
ctx,
257252
result.GetQuotes(),
258253
result.GetRetainedQuotes(),
259-
result.GetQuoteHashToIndex(),
260254
getQuote,
261255
isPaid,
262256
isRefunded,
@@ -328,7 +322,3 @@ func (a quoteResultAdapter[Q, R]) GetQuotes() []Q {
328322
func (a quoteResultAdapter[Q, R]) GetRetainedQuotes() []R {
329323
return a.retainedQuotes
330324
}
331-
332-
func (a quoteResultAdapter[Q, R]) GetQuoteHashToIndex() map[string]int {
333-
return a.quoteHashToIndex
334-
}

0 commit comments

Comments
 (0)