Skip to content

Commit

Permalink
move upgrade v501 => v500
Browse files Browse the repository at this point in the history
  • Loading branch information
jcompagni10 committed Oct 15, 2024
1 parent 959c1c6 commit 3a47232
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 304 deletions.
2 changes: 0 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
servicemetrics "github.com/skip-mev/slinky/service/metrics"

v500 "github.com/neutron-org/neutron/v5/app/upgrades/v5.0.0"
v501 "github.com/neutron-org/neutron/v5/app/upgrades/v5.0.1"
"github.com/neutron-org/neutron/v5/x/globalfee"
globalfeetypes "github.com/neutron-org/neutron/v5/x/globalfee/types"

Expand Down Expand Up @@ -229,7 +228,6 @@ const (
var (
Upgrades = []upgrades.Upgrade{
v500.Upgrade,
v501.Upgrade,
}

// DefaultNodeHome default home directories for the application daemon
Expand Down
48 changes: 48 additions & 0 deletions app/upgrades/v5.0.0/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"cosmossdk.io/math"
upgradetypes "cosmossdk.io/x/upgrade/types"
adminmoduletypes "github.com/cosmos/admin-module/v2/x/adminmodule/types"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -44,6 +45,11 @@ func CreateUpgradeHandler(
}
}

err = upgradePools(ctx, *keepers.DexKeeper)
if err != nil {
return nil, err
}

err = setMarketMapParams(ctx, keepers.MarketmapKeeper)
if err != nil {
return nil, err
Expand Down Expand Up @@ -77,6 +83,48 @@ func upgradeDexPause(ctx sdk.Context, k dexkeeper.Keeper) error {
return nil
}

func upgradePools(ctx sdk.Context, k dexkeeper.Keeper) error {
// Due to an issue with autoswap logic any pools with multiple shareholders must be withdrawn to ensure correct accounting
ctx.Logger().Info("Migrating Pools...")

allSharesholders := k.GetAllPoolShareholders(ctx)

for poolID, shareholders := range allSharesholders {
if len(shareholders) > 1 {
pool, found := k.GetPoolByID(ctx, poolID)
if !found {
return fmt.Errorf("cannot find pool with ID %d", poolID)
}
for _, shareholder := range shareholders {
addr := sdk.MustAccAddressFromBech32(shareholder.Address)
pairID := pool.LowerTick0.Key.TradePairId.MustPairID()
tick := pool.CenterTickIndexToken1()
fee := pool.Fee()
nShares := shareholder.Shares

reserve0Removed, reserve1Removed, sharesBurned, err := k.WithdrawCore(ctx, pairID, addr, addr, []math.Int{nShares}, []int64{tick}, []uint64{fee})
if err != nil {
return fmt.Errorf("user %s failed to withdraw from pool %d", addr, poolID)
}

ctx.Logger().Info(
"Withdrew user from pool",
"User", addr.String(),
"Pool", poolID,
"SharesBurned", sharesBurned.String(),
"Reserve0Withdrawn", reserve0Removed.String(),
"Reserve1Withdrawn", reserve1Removed.String(),
)

}
}
}

ctx.Logger().Info("Finished migrating Pools...")

return nil
}

func upgradeIbcRateLimitSetContract(ctx sdk.Context, k ibcratelimitkeeper.Keeper) error {
// Set the dex to paused
ctx.Logger().Info("Setting ibc rate limiting contract...")
Expand Down
108 changes: 108 additions & 0 deletions app/upgrades/v5.0.0/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"cosmossdk.io/math"
upgradetypes "cosmossdk.io/x/upgrade/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -85,6 +87,112 @@ func (suite *UpgradeTestSuite) TestUpgradeDexPause() {
suite.ErrorIs(err, dextypes.ErrDexPaused)
}

func (suite *UpgradeTestSuite) TestPoolMigrationSingleShareHolder() {
var (
app = suite.GetNeutronZoneApp(suite.ChainA)
ctx = suite.ChainA.GetContext().WithChainID("neutron-1")
alice = []byte("alice")
pairID = &dextypes.PairID{Token0: "TokenA", Token1: "TokenB"}
depositAmount = math.NewInt(10_000)
)

// create a pool with 1 shareholder
FundAccount(app.BankKeeper, ctx, alice, sdk.NewCoins(sdk.NewCoin("TokenA", depositAmount)))
shares, err := suite.makeDeposit(ctx, app.DexKeeper, alice, pairID, depositAmount, math.ZeroInt(), 0, 1)
suite.NoError(err)

// run upgrade
upgrade := upgradetypes.Plan{
Name: v500.UpgradeName,
Info: "some text here",
Height: 100,
}
suite.NoError(app.UpgradeKeeper.ApplyUpgrade(ctx, upgrade))

// assert pool and shareholder balance are unchanged
poolID, err := dextypes.ParsePoolIDFromDenom(shares[0].Denom)
suite.NoError(err)

pool, _ := app.DexKeeper.GetPoolByID(ctx, poolID)

suite.True(pool.LowerTick0.ReservesMakerDenom.Equal(depositAmount), "Pool value changed")
aliceBalance := app.BankKeeper.GetAllBalances(ctx, alice)
suite.True(aliceBalance.Equal(shares))
}

func (suite *UpgradeTestSuite) TestPoolMigrationMultiShareHolder() {
var (
app = suite.GetNeutronZoneApp(suite.ChainA)
ctx = suite.ChainA.GetContext().WithChainID("neutron-1")
alice = []byte("alice")
bob = []byte("bob")
pairID = &dextypes.PairID{Token0: "TokenA", Token1: "TokenB"}
depositAmount = math.NewInt(10_000)
initialBalance = sdk.NewCoins(sdk.NewCoin("TokenA", depositAmount))
)
FundAccount(app.BankKeeper, ctx, alice, initialBalance)
FundAccount(app.BankKeeper, ctx, bob, initialBalance)

// create a pool with 2 shareholders
shares, err := suite.makeDeposit(ctx, app.DexKeeper, alice, pairID, depositAmount, math.ZeroInt(), 0, 1)
suite.NoError(err)
aliceBalance := app.BankKeeper.GetAllBalances(ctx, alice)
suite.True(aliceBalance.Equal(shares))

shares, err = suite.makeDeposit(ctx, app.DexKeeper, bob, pairID, depositAmount, math.ZeroInt(), 0, 1)
suite.NoError(err)
bobBalance := app.BankKeeper.GetAllBalances(ctx, bob)
suite.True(bobBalance.Equal(shares))

// run upgrade
upgrade := upgradetypes.Plan{
Name: v500.UpgradeName,
Info: "some text here",
Height: 100,
}
suite.NoError(app.UpgradeKeeper.ApplyUpgrade(ctx, upgrade))

// assert that all users have withdrawn from the pool
poolID, err := dextypes.ParsePoolIDFromDenom(shares[0].Denom)
suite.NoError(err)

pool, _ := app.DexKeeper.GetPoolByID(ctx, poolID)
suite.True(pool.LowerTick0.ReservesMakerDenom.Equal(math.ZeroInt()), "Pool not withdrawn")

// AND funds are returned to the users
aliceBalance = app.BankKeeper.GetAllBalances(ctx, alice)
suite.True(aliceBalance.Equal(initialBalance))

bobBalance = app.BankKeeper.GetAllBalances(ctx, bob)
suite.True(bobBalance.Equal(initialBalance))
}

func FundAccount(bankKeeper bankkeeper.Keeper, ctx sdk.Context, addr sdk.AccAddress, amounts sdk.Coins) {
if err := bankKeeper.MintCoins(ctx, dextypes.ModuleName, amounts); err != nil {
panic(err)
}

if err := bankKeeper.SendCoinsFromModuleToAccount(ctx, dextypes.ModuleName, addr, amounts); err != nil {
panic(err)
}
}

func (suite *UpgradeTestSuite) makeDeposit(
ctx sdk.Context,
k dexkeeper.Keeper,
addr sdk.AccAddress,
pairID *dextypes.PairID,
amount0, amount1 math.Int,
tick int64,
fee uint64,
) (sharesIssued sdk.Coins, err error) {
deposit0, deposit1, sharesIssued, _, err := k.DepositCore(ctx, pairID, addr, addr, []math.Int{amount0}, []math.Int{amount1}, []int64{tick}, []uint64{fee}, []*dextypes.DepositOptions{{}})
suite.True(deposit0[0].Equal(amount0))
suite.True(deposit1[0].Equal(amount1))

return sharesIssued, err
}

func (suite *UpgradeTestSuite) TestUpgradeSetRateLimitContractMainnet() {
var (
app = suite.GetNeutronZoneApp(suite.ChainA)
Expand Down
20 changes: 0 additions & 20 deletions app/upgrades/v5.0.1/constants.go

This file was deleted.

108 changes: 0 additions & 108 deletions app/upgrades/v5.0.1/upgrades.go

This file was deleted.

Loading

0 comments on commit 3a47232

Please sign in to comment.