Skip to content

Commit

Permalink
feat: set default market/pool params in upgrade handler
Browse files Browse the repository at this point in the history
also delete PairIndex when upgrading
  • Loading branch information
kingcre committed Sep 11, 2023
1 parent f1c7203 commit a6057c7
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 4 deletions.
95 changes: 95 additions & 0 deletions app/upgrades/mainnet/v5/param_changes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package v5

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

type ParamChange struct {
MakerFeeRate *sdk.Dec
TakerFeeRate *sdk.Dec
TickSpacing *uint32
}

var ParamChanges = map[uint64]ParamChange{} // pairId => ParamChange

var (
marketParamChanges = []struct {
MakerFeeRate sdk.Dec
TakerFeeRate sdk.Dec
TargetPairIds []uint64
}{
{
MakerFeeRate: sdk.NewDecWithPrec(5, 4),
TakerFeeRate: sdk.NewDecWithPrec(1, 3),
TargetPairIds: []uint64{
18, // USDC.axl/USDC.grv
22, // USDC.grv/IST
23, // USDC.axl/IST
24, // IST/USDC.grv
25, // IST/USDC.axl
28, // CMST/USDC.axl
35, // CMST/USDC.grv
52, // USDC.axl/USDT.grv
53, // USDC.grv/USDT.grv
},
},
}
poolParamChanges = []struct {
TickSpacing uint32
TargetPairIds []uint64
}{
{
TickSpacing: 10,
TargetPairIds: []uint64{
11, // USDC.grv/WETH.grv
12, // WETH.grv/USDC.grv
13, // bCRE/USDC.grv
15, // WETH.axl/USDC.axl
16, // bCRE/USDC.axl
19, // ATOM/USDC.axl
20, // ATOM/USDC.grv
27, // bCRE/CMST
32, // stATOM/IST
33, // bCRE/IST
39, // WBTC.grv/USDC.grv
},
},
{
TickSpacing: 5,
TargetPairIds: []uint64{
18, // USDC.axl/USDC.grv
22, // USDC.grv/IST
23, // USDC.axl/IST
24, // IST/USDC.grv
25, // IST/USDC.axl
28, // CMST/USDC.axl
35, // CMST/USDC.grv
37, // stkATOM/ATOM
42, // stEVMOS/EVMOS
43, // stATOM/ATOM
52, // USDC.axl/USDT.grv
53, // USDC.grv/USDT.grv
},
},
}
)

func init() {
for _, marketParamChange := range marketParamChanges {
marketParamChange := marketParamChange // copy
for _, pairId := range marketParamChange.TargetPairIds {
change := ParamChanges[pairId]
change.MakerFeeRate = &marketParamChange.MakerFeeRate
change.TakerFeeRate = &marketParamChange.TakerFeeRate
ParamChanges[pairId] = change
}
}
for _, poolParamChange := range poolParamChanges {
poolParamChange := poolParamChange
for _, pairId := range poolParamChange.TargetPairIds {
change := ParamChanges[pairId]
change.TickSpacing = &poolParamChange.TickSpacing
ParamChanges[pairId] = change
}
}
}
28 changes: 28 additions & 0 deletions app/upgrades/mainnet/v5/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"strings"
"time"

"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

store "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -371,6 +374,7 @@ func UpgradeHandler(
return nil, err
}
liquidityKeeper.DeletePair(ctx, pairId)
liquidityKeeper.DeletePairIndex(ctx, pair.BaseCoinDenom, pair.QuoteCoinDenom)
liquidityKeeper.DeletePairLookupIndex(ctx, pair)
}
liquidityKeeper.DeleteLastPairId(ctx)
Expand Down Expand Up @@ -554,6 +558,30 @@ func UpgradeHandler(
panic("legacy historical rewards exists")
}

// Set default market/pool parameters for the upgrade.
changedPairIds := maps.Keys(ParamChanges)
slices.Sort(changedPairIds)
for _, pairId := range changedPairIds {
if ParamChanges[pairId].MakerFeeRate != nil || ParamChanges[pairId].TakerFeeRate != nil {
market, found := exchangeKeeper.GetMarket(ctx, pairId) // marketId == pairId
if !found { // maybe in test
continue
}
market.MakerFeeRate = *ParamChanges[pairId].MakerFeeRate
market.TakerFeeRate = *ParamChanges[pairId].TakerFeeRate
exchangeKeeper.SetMarket(ctx, market)
}
if ParamChanges[pairId].TickSpacing != nil {
poolId, ok := newPoolIdByPairId[pairId]
if !ok { // maybe in test
continue
}
pool := ammKeeper.MustGetPool(ctx, poolId)
pool.TickSpacing = *ParamChanges[pairId].TickSpacing
ammKeeper.SetPool(ctx, pool)
}
}

return vm, nil
}
}
Expand Down
81 changes: 77 additions & 4 deletions app/upgrades/mainnet/v5/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package v5_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/suite"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -28,10 +31,7 @@ func (s *UpgradeTestSuite) TestUpgradeV5() {
enoughCoins := utils.ParseCoins(
"1000000000000000ucre,1000000000000000uusd,1000000000000000uatom,1000000000000000stake")
creatorAddr := s.FundedAccount(1, enoughCoins)
acc := s.App.AccountKeeper.GetAccount(s.Ctx, creatorAddr)
_ = acc.SetSequence(1)
_ = acc.SetPubKey(ed25519.GenPrivKey().PubKey())
s.App.AccountKeeper.SetAccount(s.Ctx, acc)

pair, err := s.App.LiquidityKeeper.CreatePair(s.Ctx, liquiditytypes.NewMsgCreatePair(
creatorAddr, "ucre", "uusd"))
s.Require().NoError(err)
Expand All @@ -53,6 +53,13 @@ func (s *UpgradeTestSuite) TestUpgradeV5() {
s.FundAccount(lpfarmPlan.GetFarmingPoolAddress(), enoughCoins)
s.NextBlock()

// We do this so that the account is considered as a normal account,
// not a module account.
acc := s.App.AccountKeeper.GetAccount(s.Ctx, creatorAddr)
_ = acc.SetSequence(1)
_ = acc.SetPubKey(ed25519.GenPrivKey().PubKey())
s.App.AccountKeeper.SetAccount(s.Ctx, acc)

// Set the upgrade plan.
upgradeHeight := s.Ctx.BlockHeight() + 1
upgradePlan := upgradetypes.Plan{Name: v5.UpgradeName, Height: upgradeHeight}
Expand All @@ -77,3 +84,69 @@ func (s *UpgradeTestSuite) TestUpgradeV5() {
s.Require().Equal("ucre", market.BaseDenom)
s.Require().Equal("uusd", market.QuoteDenom)
}

func (s *UpgradeTestSuite) TestUpgradeV5Params() {
creatorAddr := s.FundedAccount(1, utils.ParseCoins("1000_000000ucre,1000_000000stake"))

var denoms []string
for i := 0; i < 60; i++ {
denom := fmt.Sprintf("denom%d", i+1)
denoms = append(denoms, denom)
s.FundAccount(creatorAddr, sdk.NewCoins(sdk.NewInt64Coin(denom, 1000_000000)))
}
// Dummy pair to make pairId != poolId
_, err := s.App.LiquidityKeeper.CreatePair(s.Ctx, liquiditytypes.NewMsgCreatePair(
creatorAddr, denoms[59], denoms[0]))
s.Require().NoError(err)

for i := 0; i < 59; i++ {
pair, err := s.App.LiquidityKeeper.CreatePair(s.Ctx, liquiditytypes.NewMsgCreatePair(
creatorAddr, denoms[i], denoms[i+1]))
s.Require().NoError(err)
_, err = s.App.LiquidityKeeper.CreatePool(s.Ctx, liquiditytypes.NewMsgCreatePool(
creatorAddr, pair.Id,
sdk.NewCoins(sdk.NewInt64Coin(denoms[i], 10_000000), sdk.NewInt64Coin(denoms[i+1], 10_000000))))
s.Require().NoError(err)
}

// We do this so that the account is considered as a normal account,
// not a module account.
acc := s.App.AccountKeeper.GetAccount(s.Ctx, creatorAddr)
_ = acc.SetSequence(1)
_ = acc.SetPubKey(ed25519.GenPrivKey().PubKey())
s.App.AccountKeeper.SetAccount(s.Ctx, acc)

// Set the upgrade plan.
upgradeHeight := s.Ctx.BlockHeight() + 1
upgradePlan := upgradetypes.Plan{Name: v5.UpgradeName, Height: upgradeHeight}
s.Require().NoError(s.App.UpgradeKeeper.ScheduleUpgrade(s.Ctx, upgradePlan))
_, havePlan := s.App.UpgradeKeeper.GetUpgradePlan(s.Ctx)
s.Require().True(havePlan)

// Let the upgrade happen.
s.NextBlock()

changedPairIds := maps.Keys(v5.ParamChanges)
slices.Sort(changedPairIds)
for _, pairId := range changedPairIds {
change := v5.ParamChanges[pairId]
market := s.App.ExchangeKeeper.MustGetMarket(s.Ctx, pairId)
if change.MakerFeeRate != nil || change.TakerFeeRate != nil {
s.AssertEqual(*change.MakerFeeRate, market.MakerFeeRate)
} else {
s.AssertEqual(sdk.NewDecWithPrec(1, 3), market.MakerFeeRate) // default
}
if change.TakerFeeRate != nil {
s.AssertEqual(*change.TakerFeeRate, market.TakerFeeRate)
} else {
s.AssertEqual(sdk.NewDecWithPrec(2, 3), market.TakerFeeRate) // default
}
pool, found := s.App.AMMKeeper.GetPoolByMarket(s.Ctx, pairId)
s.Require().True(found)
if change.TickSpacing != nil {
s.Require().EqualValues(*change.TickSpacing, pool.TickSpacing)
} else {
s.Require().EqualValues(uint32(50), pool.TickSpacing) // default
}
}
}
5 changes: 5 additions & 0 deletions x/liquidity/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func (k Keeper) SetPairIndex(ctx sdk.Context, baseCoinDenom, quoteCoinDenom stri
store.Set(types.GetPairIndexKey(baseCoinDenom, quoteCoinDenom), bz)
}

func (k Keeper) DeletePairIndex(ctx sdk.Context, baseCoinDenom, quoteCoinDenom string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetPairIndexKey(baseCoinDenom, quoteCoinDenom))
}

// SetPairLookupIndex stores a pair lookup index for given denoms.
func (k Keeper) SetPairLookupIndex(ctx sdk.Context, denomA string, denomB string, pairId uint64) {
store := ctx.KVStore(k.storeKey)
Expand Down

0 comments on commit a6057c7

Please sign in to comment.