Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BE-594/CreateFormattedLimitOrder | OrderbookUsecase CreateFormattedLimitOrder #531

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 18 additions & 4 deletions domain/mocks/orderbook_usecase_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mocks
import (
"context"

"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/domain/mvc"
orderbookdomain "github.com/osmosis-labs/sqs/domain/orderbook"
"github.com/osmosis-labs/sqs/sqsdomain"
Expand All @@ -12,10 +13,11 @@ var _ mvc.OrderBookUsecase = &OrderbookUsecaseMock{}

// OrderbookUsecaseMock is a mock implementation of the RouterUsecase interface
type OrderbookUsecaseMock struct {
ProcessPoolFunc func(ctx context.Context, pool sqsdomain.PoolI) error
GetAllTicksFunc func(poolID uint64) (map[int64]orderbookdomain.OrderbookTick, bool)
GetActiveOrdersFunc func(ctx context.Context, address string) ([]orderbookdomain.LimitOrder, bool, error)
GetActiveOrdersStreamFunc func(ctx context.Context, address string) <-chan orderbookdomain.OrderbookResult
ProcessPoolFunc func(ctx context.Context, pool sqsdomain.PoolI) error
GetAllTicksFunc func(poolID uint64) (map[int64]orderbookdomain.OrderbookTick, bool)
GetActiveOrdersFunc func(ctx context.Context, address string) ([]orderbookdomain.LimitOrder, bool, error)
GetActiveOrdersStreamFunc func(ctx context.Context, address string) <-chan orderbookdomain.OrderbookResult
CreateFormattedLimitOrderFunc func(orderbook domain.CanonicalOrderBooksResult, order orderbookdomain.Order) (orderbookdomain.LimitOrder, error)
}

func (m *OrderbookUsecaseMock) ProcessPool(ctx context.Context, pool sqsdomain.PoolI) error {
Expand Down Expand Up @@ -45,3 +47,15 @@ func (m *OrderbookUsecaseMock) GetActiveOrdersStream(ctx context.Context, addres
}
panic("unimplemented")
}
func (m *OrderbookUsecaseMock) WithCreateFormattedLimitOrder(order orderbookdomain.LimitOrder, err error) {
m.CreateFormattedLimitOrderFunc = func(domain.CanonicalOrderBooksResult, orderbookdomain.Order) (orderbookdomain.LimitOrder, error) {
return order, err
}
}

func (m *OrderbookUsecaseMock) CreateFormattedLimitOrder(orderbook domain.CanonicalOrderBooksResult, order orderbookdomain.Order) (orderbookdomain.LimitOrder, error) {
if m.CreateFormattedLimitOrderFunc != nil {
return m.CreateFormattedLimitOrderFunc(orderbook, order)
}
panic("unimplemented")
}
4 changes: 4 additions & 0 deletions domain/mvc/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mvc
import (
"context"

"github.com/osmosis-labs/sqs/domain"
orderbookdomain "github.com/osmosis-labs/sqs/domain/orderbook"
"github.com/osmosis-labs/sqs/sqsdomain"
)
Expand All @@ -21,4 +22,7 @@ type OrderBookUsecase interface {
// The caller should range over the channel, but note that channel is never closed since there may be multiple
// sender goroutines.
GetActiveOrdersStream(ctx context.Context, address string) <-chan orderbookdomain.OrderbookResult

// CreateFormattedLimitOrder creates a formatted limit order from the given orderbook and order.
CreateFormattedLimitOrder(orderbook domain.CanonicalOrderBooksResult, order orderbookdomain.Order) (orderbookdomain.LimitOrder, error)
}
11 changes: 0 additions & 11 deletions orderbook/usecase/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ func (o *OrderbookUseCaseImpl) SetFetchActiveOrdersEveryDuration(duration time.D
fetchActiveOrdersDuration = duration
}

// CreateFormattedLimitOrder is an alias of createFormattedLimitOrder for testing purposes
func (o *OrderbookUseCaseImpl) CreateFormattedLimitOrder(
poolID uint64,
order orderbookdomain.Order,
quoteAsset orderbookdomain.Asset,
baseAsset orderbookdomain.Asset,
orderbookAddress string,
) (orderbookdomain.LimitOrder, error) {
return o.createFormattedLimitOrder(poolID, order, quoteAsset, baseAsset, orderbookAddress)
}

// ProcessOrderBookActiveOrders is an alias of processOrderBookActiveOrders for testing purposes
func (o *OrderbookUseCaseImpl) ProcessOrderBookActiveOrders(ctx context.Context, orderBook domain.CanonicalOrderBooksResult, ownerAddress string) ([]orderbookdomain.LimitOrder, bool, error) {
return o.processOrderBookActiveOrders(ctx, orderBook, ownerAddress)
Expand Down
79 changes: 37 additions & 42 deletions orderbook/usecase/orderbook_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ func (o *OrderbookUseCaseImpl) GetActiveOrders(ctx context.Context, address stri
//
// For every order, if an error occurs processing the order, it is skipped rather than failing the entire process.
// This is a best-effort process.
func (o *OrderbookUseCaseImpl) processOrderBookActiveOrders(ctx context.Context, orderBook domain.CanonicalOrderBooksResult, ownerAddress string) ([]orderbookdomain.LimitOrder, bool, error) {
if err := orderBook.Validate(); err != nil {
func (o *OrderbookUseCaseImpl) processOrderBookActiveOrders(ctx context.Context, orderbook domain.CanonicalOrderBooksResult, ownerAddress string) ([]orderbookdomain.LimitOrder, bool, error) {
if err := orderbook.Validate(); err != nil {
return nil, false, err
}

orders, count, err := o.orderBookClient.GetActiveOrders(ctx, orderBook.ContractAddress, ownerAddress)
orders, count, err := o.orderBookClient.GetActiveOrders(ctx, orderbook.ContractAddress, ownerAddress)
if err != nil {
return nil, false, types.FailedToGetActiveOrdersError{
ContractAddress: orderBook.ContractAddress,
ContractAddress: orderbook.ContractAddress,
OwnerAddress: ownerAddress,
Err: err,
}
Expand All @@ -281,22 +281,6 @@ func (o *OrderbookUseCaseImpl) processOrderBookActiveOrders(ctx context.Context,
return nil, false, nil
}

quoteToken, err := o.tokensUsecease.GetMetadataByChainDenom(orderBook.Quote)
if err != nil {
return nil, false, types.FailedToGetMetadataError{
TokenDenom: orderBook.Quote,
Err: err,
}
}

baseToken, err := o.tokensUsecease.GetMetadataByChainDenom(orderBook.Base)
if err != nil {
return nil, false, types.FailedToGetMetadataError{
TokenDenom: orderBook.Base,
Err: err,
}
}

// Create a slice to store the results
results := make([]orderbookdomain.LimitOrder, 0, len(orders))

Expand All @@ -306,18 +290,9 @@ func (o *OrderbookUseCaseImpl) processOrderBookActiveOrders(ctx context.Context,
// For each order, create a formatted limit order
for _, order := range orders {
// create limit order
result, err := o.createFormattedLimitOrder(
orderBook.PoolID,
result, err := o.CreateFormattedLimitOrder(
orderbook,
order,
orderbookdomain.Asset{
Symbol: quoteToken.CoinMinimalDenom,
Decimals: quoteToken.Precision,
},
orderbookdomain.Asset{
Symbol: baseToken.CoinMinimalDenom,
Decimals: baseToken.Precision,
},
orderBook.ContractAddress,
)
if err != nil {
telemetry.CreateLimitOrderErrorCounter.Inc()
Expand All @@ -338,19 +313,39 @@ func (o *OrderbookUseCaseImpl) processOrderBookActiveOrders(ctx context.Context,
// It is defined in a global space to avoid creating a new instance every time.
var zeroDec = osmomath.ZeroDec()

// createFormattedLimitOrder creates a limit order from the orderbook order.
func (o *OrderbookUseCaseImpl) createFormattedLimitOrder(
poolID uint64,
order orderbookdomain.Order,
quoteAsset orderbookdomain.Asset,
baseAsset orderbookdomain.Asset,
orderbookAddress string,
) (orderbookdomain.LimitOrder, error) {
tickForOrder, ok := o.orderbookRepository.GetTickByID(poolID, order.TickId)
// CreateFormattedLimitOrder creates a limit order from the orderbook order.
func (o *OrderbookUseCaseImpl) CreateFormattedLimitOrder(orderbook domain.CanonicalOrderBooksResult, order orderbookdomain.Order) (orderbookdomain.LimitOrder, error) {
quoteToken, err := o.tokensUsecease.GetMetadataByChainDenom(orderbook.Quote)
if err != nil {
Comment on lines +318 to +319
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Typo in Variable Name tokensUsecease

The field tokensUsecease appears to be a misspelling of tokensUsecase. This typo could lead to confusion and potential errors.

Apply this diff to correct the typo:

- quoteToken, err := o.tokensUsecease.GetMetadataByChainDenom(orderbook.Quote)
+ quoteToken, err := o.tokensUsecase.GetMetadataByChainDenom(orderbook.Quote)

- baseToken, err := o.tokensUsecease.GetMetadataByChainDenom(orderbook.Base)
+ baseToken, err := o.tokensUsecase.GetMetadataByChainDenom(orderbook.Base)

Also applies to: 331-332

return orderbookdomain.LimitOrder{}, types.FailedToGetMetadataError{
TokenDenom: orderbook.Quote,
Err: err,
}
}

quoteAsset := orderbookdomain.Asset{
Symbol: quoteToken.CoinMinimalDenom,
Decimals: quoteToken.Precision,
}

baseToken, err := o.tokensUsecease.GetMetadataByChainDenom(orderbook.Base)
if err != nil {
return orderbookdomain.LimitOrder{}, types.FailedToGetMetadataError{
TokenDenom: orderbook.Base,
Err: err,
}
}

baseAsset := orderbookdomain.Asset{
Symbol: baseToken.CoinMinimalDenom,
Decimals: baseToken.Precision,
}

tickForOrder, ok := o.orderbookRepository.GetTickByID(orderbook.PoolID, order.TickId)
if !ok {
telemetry.GetTickByIDNotFoundCounter.Inc()
return orderbookdomain.LimitOrder{}, types.TickForOrderbookNotFoundError{
OrderbookAddress: orderbookAddress,
OrderbookAddress: orderbook.ContractAddress,
TickID: order.TickId,
}
}
Expand Down Expand Up @@ -497,7 +492,7 @@ func (o *OrderbookUseCaseImpl) createFormattedLimitOrder(
PercentClaimed: percentClaimed,
TotalFilled: totalFilled,
PercentFilled: percentFilled,
OrderbookAddress: orderbookAddress,
OrderbookAddress: orderbook.ContractAddress,
Price: normalizedPrice,
Status: status,
Output: output,
Expand Down
Loading
Loading