Skip to content

Commit

Permalink
Gamm Spec Out V3 - Modulize File Structure (#541)
Browse files Browse the repository at this point in the history
* Change proto Pool -> BalancerPool

* Proto changes implementation

* Change proto

* Revert Pool Query from BalancerPool

* Change method names accordingly

* Adjust marshal.go to struct change

* Fix test

* Revert Pool Params, remove Exitfee, Swapfee query

* Remove unused Yaml from gamm query import

* Add Pool Params Query

* Revert yaml import

* Revert marshal back to poolParams

* WIP: move balancerPool.proto into pool-models folder

* WIP: moving balancer_pool.go logic from types -> pool-models

* WIP: Change in file structure

* WIP: Changing packages

* Use types from balancer package

* Revert go mod back to original state

* Register Codec

* WIP: Fixing CLI test

* Fix proto Any field to Balancer specific

* Remove BalancerPoolParamsI

* Add switch cases to poolParams Query, clean up code

* Merge branch 'main' into mattverse/gamm-spec-out-v3

* Fix type from proto

* Cleanup code for test codes

* Cleanup code for merge
  • Loading branch information
mattverse authored Dec 21, 2021
1 parent b03cc8c commit d8cc5b3
Show file tree
Hide file tree
Showing 41 changed files with 2,752 additions and 2,381 deletions.
59 changes: 59 additions & 0 deletions go.sum

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package osmosis.gamm.v1beta1;
package osmosis.gamm.poolmodels;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
Expand All @@ -10,29 +10,9 @@ import "google/protobuf/timestamp.proto";
import "cosmos/auth/v1beta1/auth.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/osmosis-labs/osmosis/x/gamm/types";
import "osmosis/gamm/v1beta1/pool.proto";

message PoolAsset {
// Coins we are talking about,
// the denomination must be unique amongst all PoolAssets for this pool.
cosmos.base.v1beta1.Coin token = 1
[ (gogoproto.moretags) = "yaml:\"token\"", (gogoproto.nullable) = false ];
// Weight that is not normalized. This weight must be less than 2^50
string weight = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"weight\"",
(gogoproto.nullable) = false
];

// Weight that is normalized s.t. the sum of all pool assets' weights
// equals 1. If the user provides this value, it is ignored. This should only
// ever be set by the state machine. This is left as a TODO for a future PR.
// string normalizedWeight = 3 [
// (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
// (gogoproto.moretags) = "yaml:\"normalized_weight\"",
// (gogoproto.nullable) = true
// ];
}
option go_package = "github.com/osmosis-labs/osmosis/x/gamm/pool-models/balancer";

// Parameters for changing the weights in a balancer pool smoothly from
// a start weight and end weight over a period of time.
Expand Down Expand Up @@ -66,15 +46,15 @@ message SmoothWeightChangeParams {
// The amount PoolAsset.token.amount field is ignored if present,
// future type refactorings should just have a type with the denom & weight
// here.
repeated PoolAsset initialPoolWeights = 3 [
repeated osmosis.gamm.v1beta1.PoolAsset initialPoolWeights = 3 [
(gogoproto.moretags) = "yaml:\"initial_pool_weights\"",
(gogoproto.nullable) = false
];
// The target pool weights. The pool weights will change linearly with respect
// to time between start_time, and start_time + duration. The amount
// PoolAsset.token.amount field is ignored if present, future type
// refactorings should just have a type with the denom & weight here.
repeated PoolAsset targetPoolWeights = 4 [
repeated osmosis.gamm.v1beta1.PoolAsset targetPoolWeights = 4 [
(gogoproto.moretags) = "yaml:\"target_pool_weights\"",
(gogoproto.nullable) = false
];
Expand Down Expand Up @@ -139,7 +119,7 @@ message BalancerPool {
];
// These are assumed to be sorted by denomiation.
// They contain the pool asset and the information about the weight
repeated PoolAsset poolAssets = 6 [
repeated osmosis.gamm.v1beta1.PoolAsset poolAssets = 6 [
(gogoproto.moretags) = "yaml:\"pool_assets\"",
(gogoproto.nullable) = false
];
Expand Down
29 changes: 29 additions & 0 deletions proto/osmosis/gamm/pool-models/balancer/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
syntax = "proto3";
package osmosis.gamm.poolmodels;

import "gogoproto/gogo.proto";
import "osmosis/gamm/pool-models/balancer/balancerPool.proto";
import "osmosis/gamm/v1beta1/pool.proto";

option go_package = "github.com/osmosis-labs/osmosis/x/gamm/pool-models/balancer";

service Msg {
rpc CreateBalancerPool(MsgCreateBalancerPool)
returns (MsgCreateBalancerPoolResponse);
}

// ===================== MsgCreatePool
message MsgCreateBalancerPool {
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];

BalancerPoolParams poolParams = 2
[ (gogoproto.moretags) = "yaml:\"pool_params\"" ];

repeated osmosis.gamm.v1beta1.PoolAsset poolAssets = 3
[ (gogoproto.nullable) = false ];

string future_pool_governor = 4
[ (gogoproto.moretags) = "yaml:\"future_pool_governor\"" ];
}

message MsgCreateBalancerPoolResponse {}
30 changes: 30 additions & 0 deletions proto/osmosis/gamm/v1beta1/pool.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
syntax = "proto3";
package osmosis.gamm.v1beta1;

import "gogoproto/gogo.proto";

import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/osmosis-labs/osmosis/x/gamm/types";

message PoolAsset {
// Coins we are talking about,
// the denomination must be unique amongst all PoolAssets for this pool.
cosmos.base.v1beta1.Coin token = 1
[ (gogoproto.moretags) = "yaml:\"token\"", (gogoproto.nullable) = false ];
// Weight that is not normalized. This weight must be less than 2^50
string weight = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"weight\"",
(gogoproto.nullable) = false
];

// Weight that is normalized s.t. the sum of all pool assets' weights
// equals 1. If the user provides this value, it is ignored. This should only
// ever be set by the state machine. This is left as a TODO for a future PR.
// string normalizedWeight = 3 [
// (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
// (gogoproto.moretags) = "yaml:\"normalized_weight\"",
// (gogoproto.nullable) = true
// ];
}
7 changes: 3 additions & 4 deletions proto/osmosis/gamm/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ syntax = "proto3";
package osmosis.gamm.v1beta1;

import "gogoproto/gogo.proto";
import "osmosis/gamm/v1beta1/balancerPool.proto";
import "osmosis/gamm/v1beta1/tx.proto";
import "osmosis/gamm/v1beta1/pool.proto";

import "cosmos/base/v1beta1/coin.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "google/api/annotations.proto";
Expand Down Expand Up @@ -90,9 +91,7 @@ message QueryNumPoolsResponse {
message QueryPoolParamsRequest {
uint64 poolId = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
}
message QueryPoolParamsResponse {
oneof params { BalancerPoolParams balancerPoolParams = 1; }
}
message QueryPoolParamsResponse { google.protobuf.Any params = 1; }

//=============================== TotalShares
message QueryTotalSharesRequest {
Expand Down
20 changes: 0 additions & 20 deletions proto/osmosis/gamm/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ syntax = "proto3";
package osmosis.gamm.v1beta1;

import "gogoproto/gogo.proto";
import "osmosis/gamm/v1beta1/balancerPool.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/osmosis-labs/osmosis/x/gamm/types";

service Msg {
rpc CreateBalancerPool(MsgCreateBalancerPool)
returns (MsgCreateBalancerPoolResponse);
rpc JoinPool(MsgJoinPool) returns (MsgJoinPoolResponse);
rpc ExitPool(MsgExitPool) returns (MsgExitPoolResponse);
rpc SwapExactAmountIn(MsgSwapExactAmountIn)
Expand All @@ -26,23 +23,6 @@ service Msg {
returns (MsgExitSwapShareAmountInResponse);
}

// ===================== MsgCreatePool
message MsgCreateBalancerPool {
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];

BalancerPoolParams poolParams = 2 [
(gogoproto.moretags) = "yaml:\"pool_params\"",
(gogoproto.nullable) = false
];

repeated PoolAsset poolAssets = 3 [ (gogoproto.nullable) = false ];

string future_pool_governor = 4
[ (gogoproto.moretags) = "yaml:\"future_pool_governor\"" ];
}

message MsgCreateBalancerPoolResponse {}

// ===================== MsgJoinPool
message MsgJoinPool {
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
Expand Down
17 changes: 10 additions & 7 deletions x/gamm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/osmosis-labs/osmosis/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/x/gamm/types"
)

Expand Down Expand Up @@ -375,12 +376,14 @@ func NewBuildCreateBalancerPoolMsg(clientCtx client.Context, txf tx.Factory, fs
})
}

msg := &types.MsgCreateBalancerPool{
Sender: clientCtx.GetFromAddress().String(),
PoolParams: types.BalancerPoolParams{
SwapFee: swapFee,
ExitFee: exitFee,
},
poolParams := &balancer.BalancerPoolParams{
SwapFee: swapFee,
ExitFee: exitFee,
}

msg := &balancer.MsgCreateBalancerPool{
Sender: clientCtx.GetFromAddress().String(),
PoolParams: poolParams,
PoolAssets: poolAssets,
FuturePoolGovernor: pool.FutureGovernor,
}
Expand Down Expand Up @@ -410,7 +413,7 @@ func NewBuildCreateBalancerPoolMsg(clientCtx client.Context, txf tx.Factory, fs
})
}

smoothWeightParams := types.SmoothWeightChangeParams{
smoothWeightParams := balancer.SmoothWeightChangeParams{
Duration: duration,
InitialPoolWeights: poolAssets,
TargetPoolWeights: targetPoolAssets,
Expand Down
14 changes: 6 additions & 8 deletions x/gamm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
osmoapp "github.com/osmosis-labs/osmosis/app"
appparams "github.com/osmosis-labs/osmosis/app/params"
"github.com/osmosis-labs/osmosis/x/gamm"
"github.com/osmosis-labs/osmosis/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/x/gamm/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -20,7 +21,7 @@ func TestGammInitGenesis(t *testing.T) {
app := osmoapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

poolI, err := types.NewBalancerPool(1, types.BalancerPoolParams{
balancerPool, err := balancer.NewBalancerPool(1, balancer.BalancerPoolParams{
SwapFee: sdk.NewDecWithPrec(1, 2),
ExitFee: sdk.NewDecWithPrec(1, 2),
}, []types.PoolAsset{
Expand All @@ -35,10 +36,7 @@ func TestGammInitGenesis(t *testing.T) {
}, "", ctx.BlockTime())
require.NoError(t, err)

balancerPool, ok := poolI.(*types.BalancerPool)
require.True(t, ok)

any, err := codectypes.NewAnyWithValue(poolI)
any, err := codectypes.NewAnyWithValue(&balancerPool)
require.NoError(t, err)

gamm.InitGenesis(ctx, app.GAMMKeeper, types.GenesisState{
Expand Down Expand Up @@ -80,7 +78,7 @@ func TestGammExportGenesis(t *testing.T) {
))
require.NoError(t, err)

_, err = app.GAMMKeeper.CreateBalancerPool(ctx, acc1, types.BalancerPoolParams{
_, err = app.GAMMKeeper.CreateBalancerPool(ctx, acc1, balancer.BalancerPoolParams{
SwapFee: sdk.NewDecWithPrec(1, 2),
ExitFee: sdk.NewDecWithPrec(1, 2),
}, []types.PoolAsset{{
Expand All @@ -92,7 +90,7 @@ func TestGammExportGenesis(t *testing.T) {
}}, "")
require.NoError(t, err)

_, err = app.GAMMKeeper.CreateBalancerPool(ctx, acc1, types.BalancerPoolParams{
_, err = app.GAMMKeeper.CreateBalancerPool(ctx, acc1, balancer.BalancerPoolParams{
SwapFee: sdk.NewDecWithPrec(1, 2),
ExitFee: sdk.NewDecWithPrec(1, 2),
}, []types.PoolAsset{{
Expand Down Expand Up @@ -124,7 +122,7 @@ func TestMarshalUnmarshalGenesis(t *testing.T) {
))
require.NoError(t, err)

_, err = app.GAMMKeeper.CreateBalancerPool(ctx, acc1, types.BalancerPoolParams{
_, err = app.GAMMKeeper.CreateBalancerPool(ctx, acc1, balancer.BalancerPoolParams{
SwapFee: sdk.NewDecWithPrec(1, 2),
ExitFee: sdk.NewDecWithPrec(1, 2),
}, []types.PoolAsset{{
Expand Down
6 changes: 4 additions & 2 deletions x/gamm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/osmosis-labs/osmosis/x/gamm/keeper"
"github.com/osmosis-labs/osmosis/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/x/gamm/types"
)

// NewHandler returns a handler for "gamm" type messages.
func NewHandler(k keeper.Keeper) sdk.Handler {
msgServer := keeper.NewMsgServerImpl(k)
msgBalancerServer := keeper.NewBalancerMsgServerImpl(k)

return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
Expand All @@ -23,8 +25,8 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
res, err := msgServer.ExitPool(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)

case *types.MsgCreateBalancerPool:
res, err := msgServer.CreateBalancerPool(sdk.WrapSDKContext(ctx), msg)
case *balancer.MsgCreateBalancerPool:
res, err := msgBalancerServer.CreateBalancerPool(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)

case *types.MsgSwapExactAmountIn:
Expand Down
9 changes: 5 additions & 4 deletions x/gamm/keeper/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
balanacertypes "github.com/osmosis-labs/osmosis/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/x/gamm/types"
)

Expand Down Expand Up @@ -70,7 +71,7 @@ func (suite *KeeperTestSuite) TestJoinPoolGas() {
suite.Require().NoError(err)

firstJoinGas := suite.measureJoinPoolGas(defaultAddr, poolId, minShareOutAmount, defaultCoins)
suite.Assert().Equal(65580, int(firstJoinGas))
suite.Assert().Equal(65679, int(firstJoinGas))

for i := 1; i < startAveragingAt; i++ {
err := suite.app.GAMMKeeper.JoinPool(suite.ctx, defaultAddr, poolId, minShareOutAmount, sdk.Coins{})
Expand All @@ -79,8 +80,8 @@ func (suite *KeeperTestSuite) TestJoinPoolGas() {

avgGas, maxGas := suite.measureAvgAndMaxJoinPoolGas(totalNumJoins, defaultAddr, poolIDFn, minShareOutAmountFn, maxCoinsFn)
fmt.Printf("test deets: total %d of pools joined, begin average at %d\n", totalNumJoins, startAveragingAt)
suite.Assert().Equal(67095, int(avgGas), "average gas / join pool")
suite.Assert().Equal(67194, int(maxGas), "max gas / join pool")
suite.Assert().Equal(67194, int(avgGas), "average gas / join pool")
suite.Assert().Equal(67293, int(maxGas), "max gas / join pool")
}

func (suite *KeeperTestSuite) TestRepeatedJoinPoolDistinctDenom() {
Expand All @@ -102,7 +103,7 @@ func (suite *KeeperTestSuite) TestRepeatedJoinPoolDistinctDenom() {
err = simapp.FundAccount(suite.app.BankKeeper, suite.ctx, defaultAddr, coins)
suite.Require().NoError(err)

defaultBalancerPoolParams := types.BalancerPoolParams{
defaultBalancerPoolParams := balanacertypes.BalancerPoolParams{
SwapFee: sdk.NewDec(0),
ExitFee: sdk.NewDec(0),
}
Expand Down
Loading

0 comments on commit d8cc5b3

Please sign in to comment.