Skip to content

Commit

Permalink
feat: implement AllPools query in x/poolmanager (#4659)
Browse files Browse the repository at this point in the history
* feat: implement AllPools query in x/poolmanager

* go mod

* Update x/poolmanager/router_test.go

Co-authored-by: Matt, Park <[email protected]>

* improve godoc

* Update go.mod

* Revert "Update go.mod"

This reverts commit 40986dd.

* real pools

---------

Co-authored-by: Matt, Park <[email protected]>
Co-authored-by: p0mvn <[email protected]>
  • Loading branch information
3 people authored Mar 20, 2023
1 parent 095a668 commit 931ed16
Show file tree
Hide file tree
Showing 25 changed files with 1,134 additions and 98 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Features

* [#4659](https://github.com/osmosis-labs/osmosis/pull/4659) implement AllPools query in x/poolmanager.

### Misc Improvements

* [#4582](https://github.com/osmosis-labs/osmosis/pull/4582) Consistently generate build tags metadata, to return a comma-separated list without stray quotes. This affects the output from `version` CLI subcommand and server info API calls.
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ require (
github.com/mattn/go-sqlite3 v1.14.16
github.com/ory/dockertest/v3 v3.9.1
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230317170635-70db68a41756
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230317170635-70db68a41756
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230317170635-70db68a41756
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230317170635-70db68a41756
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230316203321-2edd49875f80
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230317232332-81bd3c5ef0c2
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230313084619-8d291c1d9813
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230316203321-2edd49875f80
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.5.0
Expand Down
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -933,14 +933,16 @@ github.com/osmosis-labs/cosmos-sdk v0.45.1-0.20230313061712-4f857c6a941a h1:ycdL
github.com/osmosis-labs/cosmos-sdk v0.45.1-0.20230313061712-4f857c6a941a/go.mod h1:rud0OaBIuq3+qOqtwT4SR7Q7iSzRp7w41fjninTjfnQ=
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:YlmchqTmlwdWSmrRmXKR+PcU96ntOd8u10vTaTZdcNY=
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230317170635-70db68a41756 h1:31CYo5M3EG0w9WKIJsvRLSbYOBgGChb4zRQGMv7mXpg=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230317170635-70db68a41756/go.mod h1:PT796t7vS3STUWYikgwzY9e7jhLo0N/hwZf4R6haj0o=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230317170635-70db68a41756 h1:F+c8S5V3F79iomTcJ4IClazDXiCQ1+/UPfLytXcqQGU=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230317170635-70db68a41756/go.mod h1:spcwVLt51T9NAUpFb+W0UKO7e9mwF5K/hc3nTrmGoP4=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230317170635-70db68a41756 h1:y/xTswWzB7WlAlOIjbY6Eli3REbtScyc2jfd3NRvihE=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230317170635-70db68a41756/go.mod h1:lJebPe8cmSTwXXuWmgSNc1S4Bobz9g3x05ZL4igbAAI=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230317170635-70db68a41756 h1:HUc7lu50WjvV0ZwD2eymdtv/glgJUG57KDuRSntfjNc=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230317170635-70db68a41756/go.mod h1:M8MLeBNbbG6qB5/2B5RUPTvqhsNaG0XagqdQfk/lVfY=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230316203321-2edd49875f80 h1:MqSb3Q5Uqb2r3PcyijgFL0lcIe57PqncqyfkRtyyn6E=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230316203321-2edd49875f80/go.mod h1:PT796t7vS3STUWYikgwzY9e7jhLo0N/hwZf4R6haj0o=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230316203321-2edd49875f80 h1:p/+OkuP3fvSK6UvUxeaX6JirBMhs2yveDmT9Vi80Je0=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230316203321-2edd49875f80/go.mod h1:spcwVLt51T9NAUpFb+W0UKO7e9mwF5K/hc3nTrmGoP4=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230317232332-81bd3c5ef0c2 h1:syLNakfGKE9SjReHzcE8B0vWaykxKeno10uOyHi+0aI=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230317232332-81bd3c5ef0c2/go.mod h1:spcwVLt51T9NAUpFb+W0UKO7e9mwF5K/hc3nTrmGoP4=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230313084619-8d291c1d9813 h1:OVT8hqFET8DkAeiSUs5h2nTg1uZFU6kPRP2sDWzVm/o=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230313084619-8d291c1d9813/go.mod h1:lJebPe8cmSTwXXuWmgSNc1S4Bobz9g3x05ZL4igbAAI=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230316203321-2edd49875f80 h1:8gZdQsLkU0XOJ43xlzr1ig9fTVqgdZcoGbLL0ZnUzZs=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230316203321-2edd49875f80/go.mod h1:M8MLeBNbbG6qB5/2B5RUPTvqhsNaG0XagqdQfk/lVfY=
github.com/osmosis-labs/wasmd v0.30.0-osmo-v15 h1:vjbzq9ta/KmQwUM6PcRmrBJuDwILgLbV4POypC1Uhn8=
github.com/osmosis-labs/wasmd v0.30.0-osmo-v15/go.mod h1:5fDYJyMXBq6u2iuHqqOTYiZ5NitIOeIW5k7qEXqbwJE=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
Expand Down
29 changes: 29 additions & 0 deletions osmoutils/slice_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,32 @@ func ContainsDuplicate[T any](arr []T) bool {
}
return false
}

type LessFunc[T any] func(a, b T) bool

// MergeSlices efficiently merges two sorted slices into a single sorted slice.
// The resulting slice contains all elements from slice1 and slice2, sorted according to the less function.
// The input slices must be sorted in ascending order according to the less function.
// The less function takes two elements of type T and returns a boolean value indicating whether the first element is less than the second element.
// The function returns a new slice containing all elements from slice1 and slice2, sorted according to the less function.
// The function does not modify the input slices.
func MergeSlices[T any](slice1, slice2 []T, less LessFunc[T]) []T {
result := make([]T, 0, len(slice1)+len(slice2))
i, j := 0, 0

for i < len(slice1) && j < len(slice2) {
if less(slice1[i], slice2[j]) {
result = append(result, slice1[i])
i++
} else {
result = append(result, slice2[j])
j++
}
}

// Append any remaining elements from slice1 and slice2
result = append(result, slice1[i:]...)
result = append(result, slice2[j:]...)

return result
}
45 changes: 45 additions & 0 deletions osmoutils/slice_helper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package osmoutils_test

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -28,3 +29,47 @@ func TestReverseSlice(t *testing.T) {
})
}
}

func TestMergeSlices(t *testing.T) {
lessInt := func(a, b int) bool {
return a < b
}
testCases := []struct {
name string
slice1 []int
slice2 []int
less func(a, b int) bool
want []int
}{
{
name: "basic merge",
slice1: []int{1, 3, 5},
slice2: []int{2, 4, 6},
less: lessInt,
want: []int{1, 2, 3, 4, 5, 6},
},
{
name: "Empty slice1",
slice1: []int{},
slice2: []int{2, 4, 6},
less: lessInt,
want: []int{2, 4, 6},
},
{
name: "Empty slice2",
slice1: []int{1, 3, 5},
slice2: []int{},
less: lessInt,
want: []int{1, 3, 5},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := osmoutils.MergeSlices(tc.slice1, tc.slice2, lessInt)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("got: %v, want: %v", got, tc.want)
}
})
}
}
14 changes: 14 additions & 0 deletions proto/osmosis/poolmanager/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ service Query {
option (google.api.http).get =
"/osmosis/poolmanager/v1beta1/pools/{pool_id}";
}

// AllPools returns all pools on the Osmosis chain sorted by IDs.
rpc AllPools(AllPoolsRequest) returns (AllPoolsResponse) {
option (google.api.http).get = "/osmosis/poolmanager/v1beta1/all-pools";
}
}

//=============================== Params
Expand Down Expand Up @@ -103,3 +108,12 @@ message PoolRequest {
message PoolResponse {
google.protobuf.Any pool = 1 [ (cosmos_proto.accepts_interface) = "PoolI" ];
}

//=============================== AllPools
message AllPoolsRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
}
message AllPoolsResponse {
repeated google.protobuf.Any pools = 1
[ (cosmos_proto.accepts_interface) = "PoolI" ];
}
7 changes: 6 additions & 1 deletion proto/osmosis/poolmanager/v1beta1/query.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ queries:
cmd: "NumPools"
Pool:
proto_wrapper:
query_func: "k.Pool"
query_func: "k.RoutePool"
cli:
cmd: "Pool"
AllPools:
proto_wrapper:
query_func: "k.AllPools"
cli:
cmd: "AllPools"
171 changes: 171 additions & 0 deletions tests/mocks/pool_module.go

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

2 changes: 1 addition & 1 deletion x/concentrated-liquidity/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState genesis.GenesisState) {

// ExportGenesis returns the concentrated-liquidity module's exported genesis state.
func (k Keeper) ExportGenesis(ctx sdk.Context) *genesis.GenesisState {
pools, err := k.GetAllPools(ctx)
pools, err := k.GetPools(ctx)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (s *KeeperTestSuite) TestInitGenesis() {
clParamsAfterInitialization := clKeeper.GetParams(ctx)
s.Require().Equal(tc.genesis.Params.String(), clParamsAfterInitialization.String())

clPoolsAfterInitialization, err := clKeeper.GetAllPools(ctx)
clPoolsAfterInitialization, err := clKeeper.GetPools(ctx)
s.Require().NoError(err)

// Check pools
Expand Down
4 changes: 2 additions & 2 deletions x/concentrated-liquidity/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func (k Keeper) getPoolById(ctx sdk.Context, poolId uint64) (types.ConcentratedP
return &pool, nil
}

func (k Keeper) GetAllPools(ctx sdk.Context) ([]types.ConcentratedPoolExtension, error) {
func (k Keeper) GetPools(ctx sdk.Context) ([]poolmanagertypes.PoolI, error) {
return osmoutils.GatherValuesFromStorePrefix(
ctx.KVStore(k.storeKey), types.PoolPrefix, func(value []byte) (types.ConcentratedPoolExtension, error) {
ctx.KVStore(k.storeKey), types.PoolPrefix, func(value []byte) (poolmanagertypes.PoolI, error) {
pool := model.Pool{}
err := k.cdc.Unmarshal(value, &pool)
if err != nil {
Expand Down
Loading

0 comments on commit 931ed16

Please sign in to comment.