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

feat(CL): gamm refactor, switch to swaprouter for pool creation & swaps (merge to main 4) #3504

Merged
merged 2 commits into from
Nov 24, 2022
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
18 changes: 10 additions & 8 deletions app/apptesting/gamm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/osmosis-labs/osmosis/v13/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v13/x/gamm/pool-models/stableswap"
gammtypes "github.com/osmosis-labs/osmosis/v13/x/gamm/types"
swaprouterkeeper "github.com/osmosis-labs/osmosis/v13/x/swaprouter"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
)

var DefaultAcctFunds sdk.Coins = sdk.NewCoins(
Expand Down Expand Up @@ -131,7 +133,7 @@ func (s *KeeperTestHelper) PrepareBasicStableswapPool() uint64 {
}

msg := stableswap.NewMsgCreateStableswapPool(s.TestAccs[0], params, DefaultStableswapLiquidity, []uint64{}, "")
poolId, err := s.App.GAMMKeeper.CreatePool(s.Ctx, msg)
poolId, err := s.App.SwapRouterKeeper.CreatePool(s.Ctx, msg)
s.NoError(err)
return poolId
}
Expand All @@ -146,7 +148,7 @@ func (s *KeeperTestHelper) PrepareImbalancedStableswapPool() uint64 {
}

msg := stableswap.NewMsgCreateStableswapPool(s.TestAccs[0], params, ImbalancedStableswapLiquidity, []uint64{1, 1, 1}, "")
poolId, err := s.App.GAMMKeeper.CreatePool(s.Ctx, msg)
poolId, err := s.App.SwapRouterKeeper.CreatePool(s.Ctx, msg)
s.NoError(err)
return poolId
}
Expand All @@ -157,7 +159,7 @@ func (s *KeeperTestHelper) PrepareBalancerPoolWithPoolParams(poolParams balancer
s.FundAcc(s.TestAccs[0], DefaultAcctFunds)

msg := balancer.NewMsgCreateBalancerPool(s.TestAccs[0], poolParams, DefaultPoolAssets, "")
poolId, err := s.App.GAMMKeeper.CreatePool(s.Ctx, msg)
poolId, err := s.App.SwapRouterKeeper.CreatePool(s.Ctx, msg)
s.NoError(err)
return poolId
}
Expand All @@ -175,7 +177,7 @@ func (s *KeeperTestHelper) PrepareBalancerPoolWithPoolAsset(assets []balancer.Po
SwapFee: sdk.ZeroDec(),
ExitFee: sdk.ZeroDec(),
}, assets, "")
poolId, err := s.App.GAMMKeeper.CreatePool(s.Ctx, msg)
poolId, err := s.App.SwapRouterKeeper.CreatePool(s.Ctx, msg)
s.NoError(err)
return poolId
}
Expand Down Expand Up @@ -205,15 +207,15 @@ func (s *KeeperTestHelper) RunBasicSwap(poolId uint64) {
swapIn := sdk.NewCoins(sdk.NewCoin(denoms[0], sdk.NewInt(1000)))
s.FundAcc(s.TestAccs[0], swapIn)

msg := gammtypes.MsgSwapExactAmountIn{
msg := swaproutertypes.MsgSwapExactAmountIn{
Sender: s.TestAccs[0].String(),
Routes: []gammtypes.SwapAmountInRoute{{PoolId: poolId, TokenOutDenom: denoms[1]}},
Routes: []swaproutertypes.SwapAmountInRoute{{PoolId: poolId, TokenOutDenom: denoms[1]}},
TokenIn: swapIn[0],
TokenOutMinAmount: sdk.ZeroInt(),
}

gammMsgServer := gammkeeper.NewMsgServerImpl(s.App.GAMMKeeper)
_, err = gammMsgServer.SwapExactAmountIn(sdk.WrapSDKContext(s.Ctx), &msg)
swaprouterMsgServer := swaprouterkeeper.NewMsgServerImpl(s.App.SwapRouterKeeper)
_, err = swaprouterMsgServer.SwapExactAmountIn(sdk.WrapSDKContext(s.Ctx), &msg)
s.Require().NoError(err)
}

Expand Down
13 changes: 9 additions & 4 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (s *KeeperTestHelper) SetupGammPoolsWithBondDenomMultiplier(multipliers []s
// TODO: use sdk crypto instead of tendermint to generate address
acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes())

params := s.App.GAMMKeeper.GetParams(s.Ctx)
params := s.App.SwapRouterKeeper.GetParams(s.Ctx)

pools := []gammtypes.PoolI{}
for index, multiplier := range multipliers {
Expand Down Expand Up @@ -291,7 +291,7 @@ func (s *KeeperTestHelper) SetupGammPoolsWithBondDenomMultiplier(multipliers []s
}
msg := balancer.NewMsgCreateBalancerPool(acc1, poolParams, poolAssets, defaultFutureGovernor)

poolId, err := s.App.GAMMKeeper.CreatePool(s.Ctx, msg)
poolId, err := s.App.SwapRouterKeeper.CreatePool(s.Ctx, msg)
s.Require().NoError(err)

pool, err := s.App.GAMMKeeper.GetPoolAndPoke(s.Ctx, poolId)
Expand All @@ -313,13 +313,18 @@ func (s *KeeperTestHelper) SwapAndSetSpotPrice(poolId uint64, fromAsset sdk.Coin
coins := sdk.Coins{sdk.NewInt64Coin(fromAsset.Denom, 100000000000000)}
s.FundAcc(acc1, coins)

_, err := s.App.GAMMKeeper.SwapExactAmountOutLegacy(
pool, err := s.App.GAMMKeeper.GetPool(s.Ctx, poolId)
s.Require().NoError(err)
swapFee := pool.GetSwapFee(s.Ctx)

_, err = s.App.GAMMKeeper.SwapExactAmountOut(
s.Ctx,
acc1,
poolId,
pool,
fromAsset.Denom,
fromAsset.Amount,
sdk.NewCoin(toAsset.Denom, toAsset.Amount.Quo(sdk.NewInt(4))),
swapFee,
)
s.Require().NoError(err)

Expand Down
17 changes: 12 additions & 5 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ func (appKeepers *AppKeepers) InitNormalKeepers(

gammKeeper := gammkeeper.NewKeeper(
appCodec, appKeepers.keys[gammtypes.StoreKey],
appKeepers.GetSubspace(gammtypes.ModuleName),
appKeepers.AccountKeeper, appKeepers.BankKeeper, appKeepers.DistrKeeper)
appKeepers.GAMMKeeper = &gammKeeper

Expand All @@ -259,7 +258,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.keys[swaproutertypes.StoreKey],
appKeepers.GetSubspace(swaproutertypes.ModuleName),
appKeepers.GAMMKeeper,
nil, // TODO: set CL keeper once it is merged.
nil, // TODO: propagate CL keeper once it is merged.
appKeepers.BankKeeper,
appKeepers.AccountKeeper,
appKeepers.DistrKeeper,
Expand All @@ -278,7 +277,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.keys[txfeestypes.StoreKey],
appKeepers.GAMMKeeper,
appKeepers.SwapRouterKeeper,
appKeepers.GAMMKeeper,
txfeestypes.FeeCollectorName,
txfeestypes.NonNativeFeeCollectorName,
Expand Down Expand Up @@ -318,10 +317,10 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.BankKeeper,
appKeepers.IncentivesKeeper,
appKeepers.DistrKeeper,
appKeepers.GAMMKeeper,
appKeepers.SwapRouterKeeper,
)
appKeepers.PoolIncentivesKeeper = &poolIncentivesKeeper
appKeepers.GAMMKeeper.SetPoolIncentivesKeeper(appKeepers.PoolIncentivesKeeper)
appKeepers.SwapRouterKeeper.SetPoolIncentivesKeeper(appKeepers.PoolIncentivesKeeper)

tokenFactoryKeeper := tokenfactorykeeper.NewKeeper(
appKeepers.keys[tokenfactorytypes.StoreKey],
Expand Down Expand Up @@ -552,6 +551,14 @@ func (appKeepers *AppKeepers) SetupHooks() {
),
)

appKeepers.SwapRouterKeeper.SetPoolCreationListeners(
swaproutertypes.NewPoolCreationListeners(
// insert gamm hooks receivers here
appKeepers.PoolIncentivesKeeper.Hooks(),
appKeepers.TwapKeeper.PoolCreationListeners(),
),
)

appKeepers.LockupKeeper.SetHooks(
lockuptypes.NewMultiLockupHooks(
// insert lockup hooks receivers here
Expand Down
4 changes: 4 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ import (
poolincentivestypes "github.com/osmosis-labs/osmosis/v13/x/pool-incentives/types"
superfluid "github.com/osmosis-labs/osmosis/v13/x/superfluid"
superfluidtypes "github.com/osmosis-labs/osmosis/v13/x/superfluid/types"
swaproutermodule "github.com/osmosis-labs/osmosis/v13/x/swaprouter/module"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
"github.com/osmosis-labs/osmosis/v13/x/tokenfactory"
tokenfactorytypes "github.com/osmosis-labs/osmosis/v13/x/tokenfactory/types"
"github.com/osmosis-labs/osmosis/v13/x/twap/twapmodule"
Expand Down Expand Up @@ -148,6 +150,7 @@ func appModules(
app.EpochsKeeper,
),
tokenfactory.NewAppModule(*app.TokenFactoryKeeper, app.AccountKeeper, app.BankKeeper),
swaproutermodule.NewAppModule(*app.SwapRouterKeeper, app.GAMMKeeper),
valsetprefmodule.NewAppModule(appCodec, *app.ValidatorSetPreferenceKeeper),
ibc_hooks.NewAppModule(app.AccountKeeper),
}
Expand Down Expand Up @@ -209,6 +212,7 @@ func OrderInitGenesis(allModuleNames []string) []string {
icatypes.ModuleName,
gammtypes.ModuleName,
twaptypes.ModuleName,
swaproutertypes.ModuleName,
txfeestypes.ModuleName,
genutiltypes.ModuleName,
evidencetypes.ModuleName,
Expand Down
8 changes: 5 additions & 3 deletions app/upgrades/v12/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func CreateUpgradeHandler(
// Change: Removed authz messages
sdk.MsgTypeURL(&gammtypes.MsgJoinPool{}),
sdk.MsgTypeURL(&gammtypes.MsgExitPool{}),
sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountIn{}),
sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountOut{}),
// N.B.: messsages were moved to another module.
// Leaving for historic reasons.
// sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountIn{}),
// sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountOut{}),
sdk.MsgTypeURL(&gammtypes.MsgJoinSwapExternAmountIn{}),
sdk.MsgTypeURL(&gammtypes.MsgJoinSwapShareAmountOut{}),
sdk.MsgTypeURL(&gammtypes.MsgExitSwapExternAmountOut{}),
Expand All @@ -75,7 +77,7 @@ func CreateUpgradeHandler(
keepers.ICAHostKeeper.SetParams(ctx, hostParams)

// Initialize TWAP state
latestPoolId := keepers.GAMMKeeper.GetNextPoolId(ctx) - 1
latestPoolId := keepers.SwapRouterKeeper.GetNextPoolId(ctx) - 1
err := keepers.TwapKeeper.MigrateExistingPools(ctx, latestPoolId)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions app/upgrades/v13/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
store "github.com/cosmos/cosmos-sdk/store/types"

"github.com/osmosis-labs/osmosis/v13/app/upgrades"
valsetpreftypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/valset-pref/types"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
valsetpreftypes "github.com/osmosis-labs/osmosis/v13/x/valset-pref/types"
)

// UpgradeName defines the on-chain upgrade name for the Osmosis v13 upgrade.
Expand Down
12 changes: 12 additions & 0 deletions app/upgrades/v13/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package v13

import (
sdk "github.com/cosmos/cosmos-sdk/types"

gammkeeper "github.com/osmosis-labs/osmosis/v13/x/gamm/keeper"
swaprouterkeeper "github.com/osmosis-labs/osmosis/v13/x/swaprouter"
)

func MigrateNextPoolId(ctx sdk.Context, gammKeeper *gammkeeper.Keeper, swaprouterKeeper *swaprouterkeeper.Keeper) {
migrateNextPoolId(ctx, gammKeeper, swaprouterKeeper)
}
45 changes: 45 additions & 0 deletions app/upgrades/v13/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package v13_test

import (
"fmt"
"reflect"
"testing"

gamm "github.com/osmosis-labs/osmosis/v13/x/gamm/keeper"

ibcratelimittypes "github.com/osmosis-labs/osmosis/v13/x/ibc-rate-limit/types"

"github.com/cosmos/cosmos-sdk/store/prefix"
Expand All @@ -12,6 +15,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"

"github.com/osmosis-labs/osmosis/v13/app/apptesting"
v13 "github.com/osmosis-labs/osmosis/v13/app/upgrades/v13"
ibc_hooks "github.com/osmosis-labs/osmosis/v13/x/ibc-hooks"
)

Expand Down Expand Up @@ -101,3 +105,44 @@ func (suite *UpgradeTestSuite) TestUpgrade() {
})
}
}

func (suite *UpgradeTestSuite) TestMigrateNextPoolIdAndCreatePool() {
suite.SetupTest() // reset

const (
expectedNextPoolId uint64 = 3
)

var (
gammKeeperType = reflect.TypeOf(&gamm.Keeper{})
)

ctx := suite.Ctx
gammKeeper := suite.App.GAMMKeeper
swaprouterKeeper := suite.App.SwapRouterKeeper

// Set next pool id to given constant, because creating pools doesn't
// increment id on current version
gammKeeper.SetNextPoolId(ctx, expectedNextPoolId)
nextPoolId := gammKeeper.GetNextPoolId(ctx)
suite.Require().Equal(expectedNextPoolId, nextPoolId)

// system under test.
v13.MigrateNextPoolId(ctx, gammKeeper, swaprouterKeeper)

// validate swaprouter's next pool id.
actualNextPoolId := swaprouterKeeper.GetNextPoolId(ctx)
suite.Require().Equal(expectedNextPoolId, actualNextPoolId)

// create a pool after migration.
actualCreatedPoolId := suite.PrepareBalancerPool()
suite.Require().Equal(expectedNextPoolId, actualCreatedPoolId)

// validate that module route mapping has been created for each pool id.
for poolId := uint64(1); poolId < expectedNextPoolId; poolId++ {
swapModule, err := swaprouterKeeper.GetSwapModule(ctx, poolId)
suite.Require().NoError(err)

suite.Require().Equal(gammKeeperType, reflect.TypeOf(swapModule))
}
}
26 changes: 26 additions & 0 deletions app/upgrades/v13/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import (

"github.com/osmosis-labs/osmosis/v13/app/keepers"
"github.com/osmosis-labs/osmosis/v13/app/upgrades"
gammkeeper "github.com/osmosis-labs/osmosis/v13/x/gamm/keeper"
ibcratelimittypes "github.com/osmosis-labs/osmosis/v13/x/ibc-rate-limit/types"
lockuptypes "github.com/osmosis-labs/osmosis/v13/x/lockup/types"
"github.com/osmosis-labs/osmosis/v13/x/swaprouter"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
)

//go:embed rate_limiter.wasm
Expand Down Expand Up @@ -72,9 +75,32 @@ func CreateUpgradeHandler(
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
keepers.LockupKeeper.SetParams(ctx, lockuptypes.DefaultParams())

keepers.SwapRouterKeeper.SetParams(ctx, swaproutertypes.DefaultParams())

// N.B: pool id in gamm is to be deprecated in the future
// Instead,it is moved to swaprouter.
migrateNextPoolId(ctx, keepers.GAMMKeeper, keepers.SwapRouterKeeper)

// N.B.: this is done to avoid initializing genesis for swaprouter module.
// Otherwise, it would overwrite migrations with InitGenesis().
// See RunMigrations() for details.
fromVM[swaproutertypes.ModuleName] = 0

if err := setupRateLimiting(ctx, keepers); err != nil {
return nil, err
}
return mm.RunMigrations(ctx, configurator, fromVM)
}
}

func migrateNextPoolId(ctx sdk.Context, gammKeeper *gammkeeper.Keeper, swaprouterKeeper *swaprouter.Keeper) {
// N.B: pool id in gamm is to be deprecated in the future
// Instead,it is moved to swaprouter.
nextPoolId := gammKeeper.GetNextPoolId(ctx)
swaprouterKeeper.SetNextPoolId(ctx, nextPoolId)

for poolId := uint64(1); poolId < nextPoolId; poolId++ {
swaprouterKeeper.SetModuleRoute(ctx, poolId, swaproutertypes.Balancer)
}
}
6 changes: 4 additions & 2 deletions app/upgrades/v9/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ func CreateUpgradeHandler(
sdk.MsgTypeURL(&authz.MsgRevoke{}),
sdk.MsgTypeURL(&gammtypes.MsgJoinPool{}),
sdk.MsgTypeURL(&gammtypes.MsgExitPool{}),
sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountIn{}),
sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountOut{}),
// N.B.: messsages were moved to another module.
// Leaving for historic reasons.
// sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountIn{}),
// sdk.MsgTypeURL(&gammtypes.MsgSwapExactAmountOut{}),
sdk.MsgTypeURL(&gammtypes.MsgJoinSwapExternAmountIn{}),
sdk.MsgTypeURL(&gammtypes.MsgJoinSwapShareAmountOut{}),
sdk.MsgTypeURL(&gammtypes.MsgExitSwapExternAmountOut{}),
Expand Down
8 changes: 4 additions & 4 deletions cmd/osmosisd/cmd/balances_from_state_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newDerivedAccount(address string) DerivedAccount {
}

// underlyingCoins returns liquidity pool's underlying coin balances.
func underlyingCoins(originCoins sdk.Coins, pools map[string]gammtypes.PoolI) sdk.Coins {
func underlyingCoins(originCoins sdk.Coins, pools map[string]gammtypes.TraditionalAmmInterface) sdk.Coins {
balances := sdk.Coins{}
convertAgain := false
for _, coin := range originCoins {
Expand Down Expand Up @@ -85,7 +85,7 @@ func underlyingCoins(originCoins sdk.Coins, pools map[string]gammtypes.PoolI) sd
// TODO: Make a separate type for this.
func underlyingCoinsForSelectPools(
originCoins sdk.Coins,
pools map[string]gammtypes.PoolI,
pools map[string]gammtypes.TraditionalAmmInterface,
selectPoolIDs []uint64,
) map[uint64]sdk.Coins {
balancesByPool := make(map[uint64]sdk.Coins)
Expand Down Expand Up @@ -263,9 +263,9 @@ Example:
}

// collect gamm pools
pools := make(map[string]gammtypes.PoolI)
pools := make(map[string]gammtypes.TraditionalAmmInterface)
for _, any := range gammGenesis.Pools {
var pool gammtypes.PoolI
var pool gammtypes.TraditionalAmmInterface
err := clientCtx.InterfaceRegistry.UnpackAny(any, &pool)
if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions osmoutils/store_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ func (s *TestSuite) TestMustGet() {
}
}

// TestGetIfFound tests that GetIfFound returns a boolean indicating
// TestGetIfFound tests that GetIfFound returns a boolean indicating
// whether value exists for the given key and error
func (s *TestSuite) TestGetIfFound() {
tests := map[string]struct {
Expand Down Expand Up @@ -893,7 +893,7 @@ func (s *TestSuite) TestGetIfFound() {
s.Require().Equal(found, tc.expectFound)
if tc.expectErr {
s.Require().Error(err)
}
}
// make sure found by key & Unmarshal successfully
if !tc.expectErr && tc.expectFound {
s.Require().Equal(expectedValue.String(), tc.actualResultProto.String())
Expand Down
Loading