Skip to content

Commit

Permalink
Setup e2e test for Stride pool migration (backport #4393) (#4394)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman <[email protected]>
  • Loading branch information
mergify[bot] and p0mvn authored Feb 22, 2023
1 parent 937d601 commit 19e3b25
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 35 deletions.
192 changes: 157 additions & 35 deletions tests/e2e/initialization/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ const (
EpochDayDuration = time.Second * 60
EpochWeekDuration = time.Second * 120
TWAPPruningKeepPeriod = EpochDayDuration / 4

// Denoms for testing Stride migration in v15.
// Can be removed after v15 upgrade.
StOsmoDenom = "stOsmo"
JunoDenom = "juno"
StJunoDenom = "stJuno"
StarsDenom = "stars"
StStarsDenom = "stStars"
DefaultStrideDenomBalance = OsmoBalanceA

// Stride pool ids to migrate
// Can be removed after v15 upgrade.
StOSMO_OSMOPoolId = 833
StJUNO_JUNOPoolId = 817
StSTARS_STARSPoolId = 810
)

var (
Expand All @@ -88,6 +103,10 @@ var (
StakeAmountIntB = sdk.NewInt(StakeAmountB)
StakeAmountCoinB = sdk.NewCoin(OsmoDenom, StakeAmountIntB)

// Pool balances for testing Stride migration in v15.
// Can be removed after v15 upgrade.
StridePoolBalances = fmt.Sprintf("%d%s,%d%s,%d%s,%d%s,%d%s", DefaultStrideDenomBalance, StOsmoDenom, DefaultStrideDenomBalance, JunoDenom, DefaultStrideDenomBalance, StJunoDenom, DefaultStrideDenomBalance, StarsDenom, DefaultStrideDenomBalance, StStarsDenom)

InitBalanceStrA = fmt.Sprintf("%d%s,%d%s,%d%s,%d%s,%d%s", OsmoBalanceA, OsmoDenom, StakeBalanceA, StakeDenom, IonBalanceA, IonDenom, UstBalanceA, UstIBCDenom, LuncBalanceA, LuncIBCDenom)
InitBalanceStrB = fmt.Sprintf("%d%s,%d%s,%d%s", OsmoBalanceB, OsmoDenom, StakeBalanceB, StakeDenom, IonBalanceB, IonDenom)
OsmoToken = sdk.NewInt64Coin(OsmoDenom, IbcSendAmount) // 3,300uosmo
Expand Down Expand Up @@ -192,11 +211,11 @@ func initGenesis(chain *internalChain, votingPeriod, expeditedVotingPeriod time.
configDir := chain.nodes[0].configDir()
for _, val := range chain.nodes {
if chain.chainMeta.Id == ChainAID {
if err := addAccount(configDir, "", InitBalanceStrA, val.keyInfo.GetAddress(), forkHeight); err != nil {
if err := addAccount(configDir, "", InitBalanceStrA+","+StridePoolBalances, val.keyInfo.GetAddress(), forkHeight); err != nil {
return err
}
} else if chain.chainMeta.Id == ChainBID {
if err := addAccount(configDir, "", InitBalanceStrB, val.keyInfo.GetAddress(), forkHeight); err != nil {
if err := addAccount(configDir, "", InitBalanceStrB+","+StridePoolBalances, val.keyInfo.GetAddress(), forkHeight); err != nil {
return err
}
}
Expand Down Expand Up @@ -225,11 +244,6 @@ func initGenesis(chain *internalChain, votingPeriod, expeditedVotingPeriod time.
return err
}

err = updateModuleGenesis(appGenState, banktypes.ModuleName, &banktypes.GenesisState{}, updateBankGenesis)
if err != nil {
return err
}

err = updateModuleGenesis(appGenState, staketypes.ModuleName, &staketypes.GenesisState{}, updateStakeGenesis)
if err != nil {
return err
Expand Down Expand Up @@ -260,12 +274,17 @@ func initGenesis(chain *internalChain, votingPeriod, expeditedVotingPeriod time.
return err
}

err = updateModuleGenesis(appGenState, banktypes.ModuleName, &banktypes.GenesisState{}, updateBankGenesis(appGenState))
if err != nil {
return err
}

err = updateModuleGenesis(appGenState, epochtypes.ModuleName, &epochtypes.GenesisState{}, updateEpochGenesis)
if err != nil {
return err
}

err = updateModuleGenesis(appGenState, twaptypes.ModuleName, &twaptypes.GenesisState{}, updateTWAPGenesis)
err = updateModuleGenesis(appGenState, twaptypes.ModuleName, &twaptypes.GenesisState{}, updateTWAPGenesis(appGenState))
if err != nil {
return err
}
Expand Down Expand Up @@ -306,10 +325,36 @@ func initGenesis(chain *internalChain, votingPeriod, expeditedVotingPeriod time.
return nil
}

func updateBankGenesis(bankGenState *banktypes.GenesisState) {
denomsToRegister := []string{StakeDenom, IonDenom, OsmoDenom, AtomDenom, LuncIBCDenom, UstIBCDenom}
for _, denom := range denomsToRegister {
setDenomMetadata(bankGenState, denom)
func updateBankGenesis(appGenState map[string]json.RawMessage) func(s *banktypes.GenesisState) {
return func(bankGenState *banktypes.GenesisState) {
strideMigrationDenoms := []string{StOsmoDenom, JunoDenom, StJunoDenom, StarsDenom, StStarsDenom}
denomsToRegister := append([]string{StakeDenom, IonDenom, OsmoDenom, AtomDenom, LuncIBCDenom, UstIBCDenom}, strideMigrationDenoms...)
for _, denom := range denomsToRegister {
setDenomMetadata(bankGenState, denom)
}

// Update pool balances with initial liquidity.
gammGenState := &gammtypes.GenesisState{}
util.Cdc.MustUnmarshalJSON(appGenState[gammtypes.ModuleName], gammGenState)

for _, poolAny := range gammGenState.Pools {
poolBytes := poolAny.GetValue()

var balancerPool balancer.Pool
util.Cdc.MustUnmarshal(poolBytes, &balancerPool)

coins := sdk.NewCoins()
for _, asset := range balancerPool.PoolAssets {
coins = coins.Add(asset.Token)
}

coins = coins.Add(balancerPool.TotalShares)

bankGenState.Balances = append(bankGenState.Balances, banktypes.Balance{
Address: balancerPool.Address,
Coins: coins,
})
}
}
}

Expand Down Expand Up @@ -362,27 +407,31 @@ func updateTxfeesGenesis(txfeesGenState *txfeestypes.GenesisState) {
func updateGammGenesis(gammGenState *gammtypes.GenesisState) {
gammGenState.Params.PoolCreationFee = tenOsmo
// setup fee pool, between "e2e_default_fee_token" and "uosmo"
feePoolParams := balancer.NewPoolParams(sdk.MustNewDecFromStr("0.01"), sdk.ZeroDec(), nil)
feePoolAssets := []balancer.PoolAsset{
{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin("uosmo", sdk.NewInt(100000000000)),
},
{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin(E2EFeeToken, sdk.NewInt(100000000000)),
},
}
pool1, err := balancer.NewBalancerPool(1, feePoolParams, feePoolAssets, "", time.Unix(0, 0))
if err != nil {
panic(err)
}
anyPool, err := types1.NewAnyWithValue(&pool1)
if err != nil {
panic(err)
uosmoFeeTokenPool := setupPool(1, "uosmo", E2EFeeToken)

gammGenState.Pools = []*types1.Any{uosmoFeeTokenPool}

for poolId := uint64(2); poolId <= StOSMO_OSMOPoolId; poolId++ {
var pool *types1.Any
switch poolId {
case StOSMO_OSMOPoolId:
pool = setupPool(StOSMO_OSMOPoolId, StOsmoDenom, OsmoDenom)
case StJUNO_JUNOPoolId:
pool = setupPool(StJUNO_JUNOPoolId, StJunoDenom, JunoDenom)
case StSTARS_STARSPoolId:
pool = setupPool(StSTARS_STARSPoolId, StStarsDenom, StarsDenom)
default:
// repeated dummy pool. We must do this to be able to
// test the migration all the way up to the largest pool id
// of StOSMO_OSMOPoolId.
pool = setupPool(poolId, OsmoDenom, AtomDenom)
}
gammGenState.Pools = append(gammGenState.Pools, pool)
}
gammGenState.Pools = []*types1.Any{anyPool}
gammGenState.NextPoolNumber = 2

// Note that we set the next pool number as 1 greater than the latest created pool.
// This is to ensure that migrations are performed correctly.
gammGenState.NextPoolNumber = StOSMO_OSMOPoolId + 1
}

func updateEpochGenesis(epochGenState *epochtypes.GenesisState) {
Expand All @@ -394,9 +443,57 @@ func updateEpochGenesis(epochGenState *epochtypes.GenesisState) {
}
}

func updateTWAPGenesis(twapGenState *twaptypes.GenesisState) {
// Lower keep period from defaults to allos us to test pruning.
twapGenState.Params.RecordHistoryKeepPeriod = time.Second * 15
func updateTWAPGenesis(appGenState map[string]json.RawMessage) func(twapGenState *twaptypes.GenesisState) {
return func(twapGenState *twaptypes.GenesisState) {
gammGenState := &gammtypes.GenesisState{}
util.Cdc.MustUnmarshalJSON(appGenState[gammtypes.ModuleName], gammGenState)

// Lower keep period from defaults to allos us to test pruning.
twapGenState.Params.RecordHistoryKeepPeriod = time.Second * 15

for _, poolAny := range gammGenState.Pools {
poolBytes := poolAny.GetValue()

var balancerPool balancer.Pool
util.Cdc.MustUnmarshal(poolBytes, &balancerPool)

denoms := []string{}
for _, poolAsset := range balancerPool.PoolAssets {
denoms = append(denoms, poolAsset.Token.Denom)
}

denomPairs := twaptypes.GetAllUniqueDenomPairs(denoms)

for _, denomPair := range denomPairs {
// sp0 = denom0 quote, denom1 base.
sp0, err := balancerPool.SpotPrice(sdk.Context{}, denomPair.Denom0, denomPair.Denom1)
if err != nil {
panic(err)
}

// sp1 = denom0 base, denom1 quote.
sp1, err := balancerPool.SpotPrice(sdk.Context{}, denomPair.Denom1, denomPair.Denom0)
if err != nil {
panic(err)
}

twapRecord := twaptypes.TwapRecord{
PoolId: balancerPool.Id,
Asset0Denom: denomPair.Denom0,
Asset1Denom: denomPair.Denom0,
Height: 1,
Time: time.Date(2023, 02, 1, 0, 0, 0, 0, time.UTC), // some time in the past.
P0LastSpotPrice: sp0,
P1LastSpotPrice: sp1,
P0ArithmeticTwapAccumulator: sdk.ZeroDec(),
P1ArithmeticTwapAccumulator: sdk.ZeroDec(),
GeometricTwapAccumulator: sdk.ZeroDec(),
LastErrorTime: time.Time{}, // no previous error
}
twapGenState.Twaps = append(twapGenState.Twaps, twapRecord)
}
}
}
}

func updateCrisisGenesis(crisisGenState *crisistypes.GenesisState) {
Expand Down Expand Up @@ -460,3 +557,28 @@ func setDenomMetadata(genState *banktypes.GenesisState, denom string) {
},
})
}

// sets up a pool with 1% fee, equal weights, and given denoms with supply of 100000000000,
// and a given pool id.
func setupPool(poolId uint64, denomA, denomB string) *types1.Any {
feePoolParams := balancer.NewPoolParams(sdk.MustNewDecFromStr("0.01"), sdk.ZeroDec(), nil)
feePoolAssets := []balancer.PoolAsset{
{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin(denomA, sdk.NewInt(100000000000)),
},
{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin(denomB, sdk.NewInt(100000000000)),
},
}
pool1, err := balancer.NewBalancerPool(poolId, feePoolParams, feePoolAssets, "", time.Unix(0, 0))
if err != nil {
panic(err)
}
anyPool, err := types1.NewAnyWithValue(&pool1)
if err != nil {
panic(err)
}
return anyPool
}
1 change: 1 addition & 0 deletions x/gamm/keeper/multihop.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (k Keeper) MultihopSwapExactAmountIn(

tokenOutAmount, err = k.swapExactAmountIn(ctx, sender, pool, tokenIn, route.TokenOutDenom, _outMinAmount, swapFee)
if err != nil {
ctx.Logger().Error(err.Error())
return sdk.Int{}, err
}

Expand Down

0 comments on commit 19e3b25

Please sign in to comment.