Skip to content

Commit 7b9e84c

Browse files
authored
BE-689 | feat: orderbook orders cache plugin (#622)
1 parent 0d1cd0b commit 7b9e84c

File tree

20 files changed

+573
-106
lines changed

20 files changed

+573
-106
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ Ref: https://keepachangelog.com/en/1.0.0/
3535

3636
# Changelog
3737

38+
## Unreleased
39+
40+
- #622 - feat: orderbook orders cache plugin
41+
3842
## v28.3.1
3943

4044
- #637 - BE-714 | Optimize route finding algorithm
4145

4246
## v28.3.0
4347

4448
- Remove dependency on keyring, wire in CosmosSigner and grab private key from SQS_PRIVATE_KEY environment variable.
45-
- #632 - E-697 | Increase max routes for pricing worker
49+
- #632 - BE-697 | Increase max routes for pricing worker
4650
- #633 - BE-707 | Skip processing quote requests when system is not healthy
4751

4852
## v28.2.0

app/sidecar_query_server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/osmosis-labs/sqs/ingest/usecase/plugins/basefee"
3535
orderbookclaimbot "github.com/osmosis-labs/sqs/ingest/usecase/plugins/orderbook/claimbot"
3636
orderbookfillbot "github.com/osmosis-labs/sqs/ingest/usecase/plugins/orderbook/fillbot"
37+
orderbookorderscache "github.com/osmosis-labs/sqs/ingest/usecase/plugins/orderbook/orderscache"
3738
orderbookrepository "github.com/osmosis-labs/sqs/orderbook/repository"
3839
orderbookusecase "github.com/osmosis-labs/sqs/orderbook/usecase"
3940
"github.com/osmosis-labs/sqs/quotesimulator"
@@ -309,6 +310,10 @@ func NewSideCarQueryServer(ctx context.Context, appCodec codec.Codec, config dom
309310
if plugin.IsEnabled() {
310311
var currentPlugin domain.EndBlockProcessPlugin
311312

313+
if plugin.GetName() == orderbookplugindomain.OrderbookOrdersCachePlugin {
314+
currentPlugin = orderbookorderscache.New(orderBookRepository, poolsUseCase, logger, passthroughGRPCClient)
315+
}
316+
312317
if plugin.GetName() == orderbookplugindomain.OrderbookFillbotPlugin {
313318
signer, err := initializeCosmosSigner()
314319
if err != nil {

config.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
"name": "orderbook-claimbot-plugin",
1717
"enabled": false
1818
},
19-
{
19+
{
2020
"name": "custom-submodule-plugin",
2121
"enabled": false
22+
},
23+
{
24+
"name": "orderbook-orders-cache-plugin",
25+
"enabled": true
2226
}
2327
]
2428
}

domain/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ var DefaultConfig = Config{
160160
ServerAddress: ":50051",
161161
ServerConnectionTimeoutSeconds: 10,
162162
Plugins: []Plugin{
163+
&OrderBookPluginConfig{
164+
Enabled: true,
165+
Name: orderbookplugindomain.OrderbookOrdersCachePlugin,
166+
},
163167
&OrderBookPluginConfig{
164168
Enabled: false,
165169
Name: orderbookplugindomain.OrderbookFillbotPlugin,
@@ -385,6 +389,8 @@ func validateDynamicMinLiquidityCapDesc(values []DynamicMinLiquidityCapFilterEnt
385389
// PluginFactory creates a Plugin instance based on the provided name.
386390
func PluginFactory(name string) Plugin {
387391
switch name {
392+
case orderbookplugindomain.OrderbookOrdersCachePlugin:
393+
return &OrderBookPluginConfig{}
388394
case orderbookplugindomain.OrderbookFillbotPlugin:
389395
return &OrderBookPluginConfig{}
390396
case orderbookplugindomain.OrderbookClaimbotPlugin:

domain/mocks/orderbook_repository_mock.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type OrderbookRepositoryMock struct {
1212
GetAllTicksFunc func(poolID uint64) (map[int64]orderbookdomain.OrderbookTick, bool)
1313
GetTicksFunc func(poolID uint64, tickIDs []int64) (map[int64]orderbookdomain.OrderbookTick, error)
1414
GetTickByIDFunc func(poolID uint64, tickID int64) (orderbookdomain.OrderbookTick, bool)
15+
StoreOrdersFunc func(poolID uint64, orders []orderbookdomain.Order) error
16+
GetOrdersFunc func(poolID uint64, ownerAddress string) ([]orderbookdomain.Order, bool)
1517
}
1618

1719
// StoreTicks implements OrderBookRepository.
@@ -52,3 +54,31 @@ func (m *OrderbookRepositoryMock) GetTickByID(poolID uint64, tickID int64) (orde
5254
}
5355
panic("GetTickByID not implemented")
5456
}
57+
58+
func (m *OrderbookRepositoryMock) WithStoreOrders(err error) {
59+
m.StoreOrdersFunc = func(poolID uint64, orders []orderbookdomain.Order) error {
60+
return err
61+
}
62+
}
63+
64+
// StoreOrders implements OrderBookRepository.
65+
func (m *OrderbookRepositoryMock) StoreOrders(poolID uint64, orders []orderbookdomain.Order) error {
66+
if m.StoreOrdersFunc != nil {
67+
return m.StoreOrdersFunc(poolID, orders)
68+
}
69+
panic("StoreOrders not implemented")
70+
}
71+
72+
func (m *OrderbookRepositoryMock) WithGetOrdersFunc(orders []orderbookdomain.Order, ok bool) {
73+
m.GetOrdersFunc = func(poolID uint64, ownerAddress string) ([]orderbookdomain.Order, bool) {
74+
return orders, ok
75+
}
76+
}
77+
78+
// GetOrders implements OrderBookRepository.
79+
func (m *OrderbookRepositoryMock) GetOrders(poolID uint64, ownerAddress string) ([]orderbookdomain.Order, bool) {
80+
if m.GetOrdersFunc != nil {
81+
return m.GetOrdersFunc(poolID, ownerAddress)
82+
}
83+
panic("GetOrders not implemented")
84+
}

domain/mocks/passthrough_grpc_client_mock.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import (
99
"google.golang.org/grpc"
1010
)
1111

12+
var _ passthroughdomain.PassthroughGRPCClient = &PassthroughGRPCClientMock{}
13+
1214
type PassthroughGRPCClientMock struct {
1315
MockAllBalancesCb func(ctx context.Context, address string) (sdk.Coins, error)
1416
MockAccountLockedCoinsCb func(ctx context.Context, address string) (sdk.Coins, error)
1517
MockAccountUnlockingCoinsCb func(ctx context.Context, address string) (sdk.Coins, error)
1618
MockDelegatorDelegationsCb func(ctx context.Context, address string) (sdk.Coins, error)
1719
MockDelegatorUnbondingDelegationsCb func(ctx context.Context, address string) (sdk.Coins, error)
1820
MockUserPositionsBalancesCb func(ctx context.Context, address string) (sdk.Coins, sdk.Coins, error)
21+
MockGetOrderbookOrdersRawCb func(ctx context.Context, poolID uint64) ([][]byte, error)
1922
MockDelegationRewardsCb func(ctx context.Context, address string) (sdk.Coins, error)
2023
}
2124

@@ -78,6 +81,14 @@ func (p *PassthroughGRPCClientMock) AccountUnlockingCoins(ctx context.Context, a
7881
return nil, errors.New("MockAccountLockedCoinsCb is not implemented")
7982
}
8083

84+
// GetOrderbookOrdersRaw implements passthroughdomain.PassthroughGRPCClient.
85+
func (p *PassthroughGRPCClientMock) GetOrderbookOrdersRaw(ctx context.Context, poolID uint64) ([][]byte, error) {
86+
if p.MockGetOrderbookOrdersRawCb != nil {
87+
return p.MockGetOrderbookOrdersRawCb(ctx, poolID)
88+
}
89+
return nil, errors.New("MockGetOrderbookOrdersRawCb is not implemented")
90+
}
91+
8192
// DelegationRewards implements passthroughdomain.PassthroughGRPCClient.
8293
func (p *PassthroughGRPCClientMock) DelegationRewards(ctx context.Context, address string) (sdk.Coins, error) {
8394
if p.MockDelegationRewardsCb != nil {
@@ -86,5 +97,3 @@ func (p *PassthroughGRPCClientMock) DelegationRewards(ctx context.Context, addre
8697

8798
return nil, errors.New("MockDelegationRewardsCb is not implemented")
8899
}
89-
90-
var _ passthroughdomain.PassthroughGRPCClient = &PassthroughGRPCClientMock{}

domain/orderbook/orderbook_repository.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ type OrderBookRepository interface {
1515
// GetTickByID returns a specific orderbook tick for a given orderbook pool id.
1616
// Returns false if the tick is not found.
1717
GetTickByID(poolID uint64, tickID int64) (OrderbookTick, bool)
18+
19+
// StoreOrders stores the orders for a given orderbook pool id.
20+
StoreOrders(poolID uint64, orders []Order) error
21+
22+
// GetOrders returns the orders for a given orderbook pool id.
23+
GetOrders(poolID uint64, ownerAddress string) ([]Order, bool)
1824
}

domain/orderbook/plugin/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package orderbookplugindomain
22

33
// Orderbook plugin names
44
const (
5-
OrderbookFillbotPlugin = "orderbook-fillbot-plugin"
6-
OrderbookClaimbotPlugin = "orderbook-claimbot-plugin"
7-
CustomSubmodulePlugin = "custom-submodule-plugin"
5+
OrderbookFillbotPlugin = "orderbook-fillbot-plugin"
6+
OrderbookClaimbotPlugin = "orderbook-claimbot-plugin"
7+
OrderbookOrdersCachePlugin = "orderbook-orders-cache-plugin"
8+
CustomSubmodulePlugin = "custom-submodule-plugin"
89
)

domain/passthrough/passthrough_grpc_client.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
1111
math "github.com/osmosis-labs/osmosis/osmomath"
1212
concentratedLiquidity "github.com/osmosis-labs/osmosis/v28/x/concentrated-liquidity/client/queryproto"
13+
cosmwasmpool "github.com/osmosis-labs/osmosis/v28/x/cosmwasmpool/client/queryproto"
1314
lockup "github.com/osmosis-labs/osmosis/v28/x/lockup/types"
1415
polarisgrpc "github.com/osmosis-labs/sqs/delivery/grpc"
1516
"google.golang.org/grpc"
@@ -39,6 +40,9 @@ type PassthroughGRPCClient interface {
3940
// DelegationTotalRewards returns the total unclaimed staking rewards accrued of the user with the given address.
4041
DelegationRewards(ctx context.Context, address string) (sdk.Coins, error)
4142

43+
// GetOrderbookOrdersRaw returns the raw orderbook orders for the given pool ID.
44+
GetOrderbookOrdersRaw(ctx context.Context, poolID uint64) ([][]byte, error)
45+
4246
GetChainGRPCClient() grpc.ClientConnInterface
4347
}
4448

@@ -55,8 +59,8 @@ type passthroughGRPCClient struct {
5559
lockupQueryClient lockup.QueryClient
5660
concentratedLiquidityQueryClient concentratedLiquidity.QueryClient
5761
distributionClient distribution.QueryClient
58-
59-
chainGRPCClient grpc.ClientConnInterface
62+
cosmwasmpoolClient cosmwasmpool.QueryClient
63+
chainGRPCClient grpc.ClientConnInterface
6064
}
6165

6266
const (
@@ -79,6 +83,7 @@ func NewPassthroughGRPCClient(grpcURI string) (PassthroughGRPCClient, error) {
7983
lockupQueryClient: lockup.NewQueryClient(grpcClient),
8084
concentratedLiquidityQueryClient: concentratedLiquidity.NewQueryClient(grpcClient),
8185
distributionClient: distribution.NewQueryClient(grpcClient),
86+
cosmwasmpoolClient: cosmwasmpool.NewQueryClient(grpcClient),
8287

8388
chainGRPCClient: grpcClient,
8489
}, nil
@@ -89,7 +94,6 @@ func (p *passthroughGRPCClient) AccountLockedCoins(ctx context.Context, address
8994
if err != nil {
9095
return nil, err
9196
}
92-
9397
return response.Coins, nil
9498
}
9599

@@ -194,6 +198,14 @@ func (p *passthroughGRPCClient) DelegationRewards(ctx context.Context, address s
194198
return rewardCoins, nil
195199
}
196200

201+
func (p *passthroughGRPCClient) GetOrderbookOrdersRaw(ctx context.Context, poolID uint64) ([][]byte, error) {
202+
response, err := p.cosmwasmpoolClient.PoolRawFilteredState(ctx, &cosmwasmpool.PoolRawFilteredStateRequest{PoolId: poolID, KeyFilter: "order", ValueFilter: "order_id"})
203+
if err != nil {
204+
return nil, err
205+
}
206+
return response.Values, nil
207+
}
208+
197209
// GetChainGRPCClient implements PassthroughGRPCClient.
198210
func (p *passthroughGRPCClient) GetChainGRPCClient() grpc.ClientConnInterface {
199211
return p.chainGRPCClient

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/osmosis-labs/sqs
22

3-
go 1.22.7
3+
go 1.22.11
44

55
require (
66
cosmossdk.io/math v1.5.0
@@ -25,7 +25,7 @@ require (
2525
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.34.0
2626
go.opentelemetry.io/otel/sdk v1.34.0
2727
go.uber.org/zap v1.26.0
28-
google.golang.org/grpc v1.69.4
28+
google.golang.org/grpc v1.70.0
2929
)
3030

3131
require (
@@ -142,7 +142,7 @@ require (
142142
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
143143
github.com/gogo/googleapis v1.4.1 // indirect
144144
github.com/gogo/protobuf v1.3.3 // indirect
145-
github.com/golang/glog v1.2.2 // indirect
145+
github.com/golang/glog v1.2.3 // indirect
146146
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
147147
github.com/golang/mock v1.6.0 // indirect
148148
github.com/golang/protobuf v1.5.4
@@ -203,7 +203,7 @@ require (
203203
github.com/pkg/errors v0.9.1 // indirect
204204
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
205205
github.com/prometheus/client_model v0.6.1 // indirect
206-
github.com/prometheus/common v0.60.1 // indirect
206+
github.com/prometheus/common v0.62.0 // indirect
207207
github.com/prometheus/procfs v0.15.1 // indirect
208208
github.com/rakyll/statik v0.1.7 // indirect
209209
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
@@ -236,20 +236,20 @@ require (
236236
go.opentelemetry.io/otel/trace v1.34.0
237237
go.uber.org/multierr v1.11.0 // indirect
238238
golang.org/x/crypto v0.32.0 // indirect
239-
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
239+
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
240240
golang.org/x/net v0.34.0 // indirect
241241
golang.org/x/oauth2 v0.24.0 // indirect
242242
golang.org/x/sync v0.10.0 // indirect
243243
golang.org/x/sys v0.29.0 // indirect
244244
golang.org/x/term v0.28.0 // indirect
245245
golang.org/x/text v0.21.0 // indirect
246246
golang.org/x/time v0.6.0 // indirect
247-
golang.org/x/tools v0.22.0 // indirect
247+
golang.org/x/tools v0.23.0 // indirect
248248
google.golang.org/api v0.197.0 // indirect
249249
google.golang.org/genproto v0.0.0-20240903143218-8af14fe29dc1 // indirect
250250
google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f
251251
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
252-
google.golang.org/protobuf v1.36.3
252+
google.golang.org/protobuf v1.36.4
253253
gopkg.in/ini.v1 v1.67.0 // indirect
254254
gopkg.in/yaml.v2 v2.4.0 // indirect
255255
gopkg.in/yaml.v3 v3.0.1 // indirect
@@ -284,7 +284,7 @@ replace (
284284

285285
// This replacement can be removed once Osmosis v28.0.3 or higher is released
286286
// It is required to pull in test cases for BE-676 from backport branch: https://github.com/osmosis-labs/osmosis/pull/8962
287-
github.com/osmosis-labs/osmosis/v28 v28.0.2 => github.com/osmosis-labs/osmosis/v28 v28.0.3-0.20250128064600-532afea930f9
287+
github.com/osmosis-labs/osmosis/v28 v28.0.2 => github.com/osmosis-labs/osmosis/v28 v28.0.5-0.20250218091759-db70d39ef671
288288

289289
github.com/osmosis-labs/osmosis/x/epochs => github.com/osmosis-labs/osmosis/x/epochs v0.0.5-0.20240825083448-87db4447a1ff
290290
github.com/osmosis-labs/osmosis/x/ibc-hooks => github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20240825083448-87db4447a1ff

0 commit comments

Comments
 (0)