Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion OpenApi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ components:
- quote
- quoteHash
type: object
GetReportsPeginPegoutRequest:
properties:
endDate:
type: string
startDate:
type: string
type: object
HealthResponse:
properties:
services:
Expand Down Expand Up @@ -520,7 +527,6 @@ components:
ProviderDetail:
properties:
fee:
deprecated: true
type: integer
feePercentage:
type: number
Expand Down Expand Up @@ -716,6 +722,13 @@ components:
required:
- collateral
type: object
pkg.GetReportsPeginPegoutRequest:
properties:
endDate:
type: string
startDate:
type: string
type: object
info:
title: Liquidity Provider Server
version: 1.2.1
Expand Down Expand Up @@ -1034,6 +1047,32 @@ paths:
"204":
description: ""
summary: Withdraw PegIn Collateral
/reports/pegin:
get:
description: ' Get the last pegins on the API. Included in the management API.'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/GetReportsPeginPegoutRequest'
required: true
responses:
"200":
description: ""
summary: Get Pegin Reports
/reports/pegout:
get:
description: ' Get the last pegouts on the API. Included in the management API.'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/GetReportsPeginPegoutRequest'
required: true
responses:
"200":
description: ""
summary: Get Pegout Reports
/userQuotes:
get:
description: ' Returns user quotes for address.'
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcutil v1.1.5
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
github.com/ethereum/go-ethereum v1.14.5
github.com/go-playground/validator/v10 v10.17.0
github.com/gorilla/csrf v1.7.2
Expand Down Expand Up @@ -50,7 +51,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down
111 changes: 111 additions & 0 deletions go.mongodb.org/mongo-driver/mongo/cursor_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 125 additions & 0 deletions internal/adapters/dataproviders/database/mongo/pegin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

const (
Expand Down Expand Up @@ -107,6 +108,37 @@ func (repo *peginMongoRepository) GetQuote(ctx context.Context, hash string) (*q
return &result.PeginQuote, nil
}

func (repo *peginMongoRepository) GetQuotes(ctx context.Context, hashes []string) ([]quote.PeginQuote, error) {
dbCtx, cancel := context.WithTimeout(ctx, repo.conn.timeout)
defer cancel()

for _, hash := range hashes {
if err := quote.ValidateQuoteHash(hash); err != nil {
return nil, err
}
}

collection := repo.conn.Collection(PeginQuoteCollection)
filter := bson.M{"hash": bson.M{"$in": hashes}}

quotesReturn := make([]quote.PeginQuote, 0)

cursor, err := collection.Find(dbCtx, filter)
if err != nil {
return nil, err
}
for cursor.Next(ctx) {
var result StoredPeginQuote
err = cursor.Decode(&result)
if err != nil {
return nil, err
}
quotesReturn = append(quotesReturn, result.PeginQuote)
}
logDbInteraction(Read, quotesReturn)
return quotesReturn, nil
}

func (repo *peginMongoRepository) GetRetainedQuote(ctx context.Context, hash string) (*quote.RetainedPeginQuote, error) {
var result quote.RetainedPeginQuote
dbCtx, cancel := context.WithTimeout(ctx, repo.conn.timeout)
Expand Down Expand Up @@ -208,3 +240,96 @@ func (repo *peginMongoRepository) DeleteQuotes(ctx context.Context, quotes []str
}
return uint(peginResult.DeletedCount + retainedResult.DeletedCount + creationDataResult.DeletedCount), nil
}

func (repo *peginMongoRepository) GetQuotesByState(ctx context.Context, filter quote.GetPeginQuotesByStateFilter) ([]quote.PeginQuote, error) {
dbCtx, cancel := context.WithTimeout(ctx, repo.conn.timeout)
defer cancel()

quoteHashes, err := repo.getRetainedQuoteHashes(dbCtx, filter.States)
if err != nil {
return nil, err
}

if len(quoteHashes) == 0 {
return []quote.PeginQuote{}, nil
}

peginQuotes, err := repo.getPeginQuotesByHashesAndDate(dbCtx, quoteHashes, filter.StartDate, filter.EndDate)
if err != nil {
return nil, err
}

logDbInteraction(Read, peginQuotes)
return peginQuotes, nil
}

func (repo *peginMongoRepository) getRetainedQuoteHashes(ctx context.Context, states []quote.PeginState) ([]string, error) {
retainedCollection := repo.conn.Collection(RetainedPeginQuoteCollection)
retainedFilter := bson.D{
primitive.E{
Key: "state",
Value: bson.D{primitive.E{Key: "$in", Value: states}},
},
}

projection := bson.D{
primitive.E{Key: "quote_hash", Value: 1},
primitive.E{Key: "_id", Value: 0},
}

retainedCursor, err := retainedCollection.Find(ctx, retainedFilter, options.Find().SetProjection(projection))
if err != nil {
return nil, err
}
defer retainedCursor.Close(ctx)

var retainedResults []struct {
QuoteHash string `bson:"quote_hash"`
}

if err = retainedCursor.All(ctx, &retainedResults); err != nil {
return nil, err
}

quoteHashes := make([]string, len(retainedResults))
for i, result := range retainedResults {
quoteHashes[i] = result.QuoteHash
}

return quoteHashes, nil
}

func (repo *peginMongoRepository) getPeginQuotesByHashesAndDate(
ctx context.Context,
quoteHashes []string,
startDate, endDate uint32) ([]quote.PeginQuote, error) {

peginCollection := repo.conn.Collection(PeginQuoteCollection)
peginFilter := bson.D{
primitive.E{
Key: "hash",
Value: bson.D{primitive.E{Key: "$in", Value: quoteHashes}},
},
primitive.E{
Key: "agreement_timestamp",
Value: bson.D{
primitive.E{Key: "$gte", Value: startDate},
primitive.E{Key: "$lte", Value: endDate},
},
},
}

sortOptions := options.Find().SetSort(bson.D{primitive.E{Key: "agreement_timestamp", Value: -1}})
peginCursor, err := peginCollection.Find(ctx, peginFilter, sortOptions)
if err != nil {
return nil, err
}
defer peginCursor.Close(ctx)

var peginQuotes []quote.PeginQuote
if err = peginCursor.All(ctx, &peginQuotes); err != nil {
return nil, err
}

return peginQuotes, nil
}
Loading
Loading