Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/clean-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverHappened committed Oct 16, 2024
2 parents 71fa1b5 + 813221c commit 5f9b01e
Show file tree
Hide file tree
Showing 44 changed files with 1,304 additions and 387 deletions.
4 changes: 3 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ const (
)

var (
Upgrades = []upgrades.Upgrade{v500.Upgrade}
Upgrades = []upgrades.Upgrade{
v500.Upgrade,
}

// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/v5.0.0/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

const (
// UpgradeName defines the on-chain upgrade name.
UpgradeName = "v5.0.0"
UpgradeName = "v5.0.0-rc0"

MarketMapAuthorityMultisig = "neutron1anjpluecd0tdc0n8xzc3l5hua4h93wyq0x7v56"
// MainnetRateLimitContract defines the RL contract addr which we set as a contract address in ibc-rate-limit middleware
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
Loading

0 comments on commit 5f9b01e

Please sign in to comment.