From d676cb01bfe6842c8c41b472bee728dc44b5a3ff Mon Sep 17 00:00:00 2001 From: jgo121 Date: Fri, 3 Nov 2023 20:41:48 +0800 Subject: [PATCH] automatic addition of staking tokens to staking basket when a new multistaking pool created after a validator join --- app/app.go | 12 ++-- proto/kira/basket/genesis.proto | 8 ++- x/basket/keeper/hooks.go | 77 ++++++++++++++++++++ x/basket/keeper/keeper.go | 5 +- x/basket/module.go | 2 + x/basket/types/expected_keepers.go | 6 ++ x/basket/types/genesis.go | 2 +- x/basket/types/genesis.pb.go | 90 +++++++++++++++++------- x/multistaking/keeper/delegation.go | 12 ++-- x/multistaking/keeper/keeper.go | 12 ++++ x/multistaking/keeper/msg_server.go | 10 ++- x/multistaking/keeper/pool.go | 22 ------ x/multistaking/keeper/slash.go | 2 + x/multistaking/types/expected_keepers.go | 5 ++ x/multistaking/types/hooks.go | 18 +++++ x/multistaking/types/pool.go | 27 +++++++ x/tokens/handler_test.go | 2 +- x/tokens/keeper/grpc_query.go | 2 +- x/tokens/keeper/rate.go | 6 +- x/tokens/keeper/rate_test.go | 6 +- x/tokens/module.go | 2 +- 21 files changed, 253 insertions(+), 75 deletions(-) create mode 100644 x/basket/keeper/hooks.go create mode 100644 x/multistaking/types/hooks.go create mode 100644 x/multistaking/types/pool.go diff --git a/app/app.go b/app/app.go index 8877ca25d..369af5ff0 100644 --- a/app/app.go +++ b/app/app.go @@ -271,12 +271,12 @@ func NewInitApp( app.TokensKeeper = tokenskeeper.NewKeeper(keys[tokenstypes.ModuleName], appCodec) app.CustomGovKeeper = customgovkeeper.NewKeeper(keys[govtypes.ModuleName], appCodec, app.BankKeeper) customStakingKeeper := customstakingkeeper.NewKeeper(keys[stakingtypes.ModuleName], cdc, app.CustomGovKeeper) - app.MultiStakingKeeper = multistakingkeeper.NewKeeper(keys[multistakingtypes.ModuleName], appCodec, app.BankKeeper, app.TokensKeeper, app.CustomGovKeeper, customStakingKeeper) + multiStakingKeeper := multistakingkeeper.NewKeeper(keys[multistakingtypes.ModuleName], appCodec, app.BankKeeper, app.TokensKeeper, app.CustomGovKeeper, customStakingKeeper) app.CustomSlashingKeeper = customslashingkeeper.NewKeeper( appCodec, keys[slashingtypes.StoreKey], &customStakingKeeper, - app.MultiStakingKeeper, + multiStakingKeeper, app.CustomGovKeeper, app.GetSubspace(slashingtypes.ModuleName), ) @@ -290,17 +290,21 @@ func NewInitApp( keys[baskettypes.ModuleName], appCodec, app.AccountKeeper, app.BankKeeper, app.CustomGovKeeper, - app.MultiStakingKeeper, + app.TokensKeeper, + multiStakingKeeper, ) app.CollectivesKeeper = collectiveskeeper.NewKeeper( keys[collectivestypes.StoreKey], appCodec, app.BankKeeper, app.CustomGovKeeper, - app.MultiStakingKeeper, + multiStakingKeeper, app.TokensKeeper, app.SpendingKeeper, ) + app.MultiStakingKeeper = *multiStakingKeeper.SetHooks( + multistakingtypes.NewMultiStakingHooks(app.BasketKeeper.Hooks()), + ) app.Layer2Keeper = layer2keeper.NewKeeper( keys[collectivestypes.StoreKey], appCodec, diff --git a/proto/kira/basket/genesis.proto b/proto/kira/basket/genesis.proto index 0782a9560..764fa9287 100644 --- a/proto/kira/basket/genesis.proto +++ b/proto/kira/basket/genesis.proto @@ -10,12 +10,14 @@ import "kira/basket/basket.proto"; message GenesisState { // baskets registered on the module repeated Basket baskets = 1 [ (gogoproto.nullable) = false ]; + // last basket id + uint64 last_basket_id = 2; // mints by time - repeated AmountAtTime historical_mints = 2 [ (gogoproto.nullable) = false ]; + repeated AmountAtTime historical_mints = 3 [ (gogoproto.nullable) = false ]; // burns by time - repeated AmountAtTime historical_burns = 3 [ (gogoproto.nullable) = false ]; + repeated AmountAtTime historical_burns = 4 [ (gogoproto.nullable) = false ]; // swaps by time - repeated AmountAtTime historical_swaps = 4 [ (gogoproto.nullable) = false ]; + repeated AmountAtTime historical_swaps = 5 [ (gogoproto.nullable) = false ]; } // https://www.notion.so/kira-network/KIP-78-Token-Basketing-Module-7e6da1f2667c4c13b2274d546031d5db \ No newline at end of file diff --git a/x/basket/keeper/hooks.go b/x/basket/keeper/hooks.go new file mode 100644 index 000000000..fe04d2c26 --- /dev/null +++ b/x/basket/keeper/hooks.go @@ -0,0 +1,77 @@ +package keeper + +import ( + "fmt" + + "github.com/KiraCore/sekai/x/basket/types" + multistakingtypes "github.com/KiraCore/sekai/x/multistaking/types" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func (k Keeper) AfterUpsertStakingPool(ctx sdk.Context, valAddr sdk.ValAddress, pool multistakingtypes.StakingPool) { + rates := k.tk.GetAllTokenRates(ctx) + for _, rate := range rates { + if rate.StakeToken { + basket, err := k.GetBasketByDenom(ctx, fmt.Sprintf("sdb/%s", rate.Denom)) + if err != nil { + basket = types.Basket{ + Id: 1, + Suffix: fmt.Sprint("staking/%s", rate.Denom), + Description: fmt.Sprintf("Basket of staking derivatives for %s token", rate.Denom), + Amount: sdk.ZeroInt(), + SwapFee: sdk.ZeroDec(), + SlipppageFeeMin: sdk.ZeroDec(), + TokensCap: sdk.ZeroDec(), + LimitsPeriod: 86400, + MintsMin: sdk.OneInt(), + MintsMax: sdk.NewInt(1000_000_000_000), // 1M + MintsDisabled: false, + BurnsMin: sdk.OneInt(), + BurnsMax: sdk.NewInt(1000_000_000_000), // 1M + BurnsDisabled: false, + SwapsMin: sdk.OneInt(), + SwapsMax: sdk.NewInt(1000_000_000_000), // 1M + SwapsDisabled: false, + Tokens: []types.BasketToken{}, + Surplus: []sdk.Coin{}, + } + k.SetBasket(ctx, basket) + } + + shareDenom := multistakingtypes.GetShareDenom(pool.Id, rate.Denom) + tokenMap := make(map[string]bool) + for _, token := range basket.Tokens { + tokenMap[token.Denom] = true + } + if !tokenMap[shareDenom] { + basket.Tokens = append(basket.Tokens, types.BasketToken{ + Denom: shareDenom, + Weight: sdk.OneDec(), + Amount: sdk.ZeroInt(), + Deposits: true, + Withdraws: true, + Swaps: true, + }) + } + } + } +} + +//_________________________________________________________________________________________ + +// Hooks wrapper struct for multistaking keeper +type Hooks struct { + k Keeper +} + +var _ types.MultistakingHooks = Hooks{} + +// Return the wrapper struct +func (k Keeper) Hooks() Hooks { + return Hooks{k} +} + +// Implements sdk.ValidatorHooks +func (h Hooks) AfterUpsertStakingPool(ctx sdk.Context, valAddr sdk.ValAddress, pool multistakingtypes.StakingPool) { + h.k.AfterUpsertStakingPool(ctx, valAddr, pool) +} diff --git a/x/basket/keeper/keeper.go b/x/basket/keeper/keeper.go index edff2ac26..d63e275c9 100644 --- a/x/basket/keeper/keeper.go +++ b/x/basket/keeper/keeper.go @@ -4,6 +4,7 @@ import ( "github.com/KiraCore/sekai/x/basket/types" govkeeper "github.com/KiraCore/sekai/x/gov/keeper" govtypes "github.com/KiraCore/sekai/x/gov/types" + tokenskeeper "github.com/KiraCore/sekai/x/tokens/keeper" "github.com/cosmos/cosmos-sdk/codec" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -16,17 +17,19 @@ type Keeper struct { ak types.AccountKeeper bk types.BankKeeper gk govkeeper.Keeper + tk tokenskeeper.Keeper mk types.MultiStakingKeeper } // NewKeeper returns instance of a keeper -func NewKeeper(storeKey storetypes.StoreKey, cdc codec.BinaryCodec, ak types.AccountKeeper, bk types.BankKeeper, gk govkeeper.Keeper, mk types.MultiStakingKeeper) Keeper { +func NewKeeper(storeKey storetypes.StoreKey, cdc codec.BinaryCodec, ak types.AccountKeeper, bk types.BankKeeper, gk govkeeper.Keeper, tk tokenskeeper.Keeper, mk types.MultiStakingKeeper) Keeper { return Keeper{ cdc: cdc, storeKey: storeKey, ak: ak, bk: bk, gk: gk, + tk: tk, mk: mk, } } diff --git a/x/basket/module.go b/x/basket/module.go index d9f0ad4bf..e94d4adb2 100644 --- a/x/basket/module.go +++ b/x/basket/module.go @@ -96,6 +96,7 @@ func (am AppModule) InitGenesis( var genesisState baskettypes.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) + am.basketKeeper.SetLastBasketId(ctx, genesisState.LastBasketId) for _, basket := range genesisState.Baskets { am.basketKeeper.SetBasket(ctx, basket) } @@ -118,6 +119,7 @@ func (am AppModule) InitGenesis( func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { genesisState := baskettypes.GenesisState{ Baskets: am.basketKeeper.GetAllBaskets(ctx), + LastBasketId: am.basketKeeper.GetLastBasketId(ctx), HistoricalMints: am.basketKeeper.GetAllMintAmounts(ctx), HistoricalBurns: am.basketKeeper.GetAllBurnAmounts(ctx), HistoricalSwaps: am.basketKeeper.GetAllSwapAmounts(ctx), diff --git a/x/basket/types/expected_keepers.go b/x/basket/types/expected_keepers.go index 3e5d89c09..ad4698bc4 100644 --- a/x/basket/types/expected_keepers.go +++ b/x/basket/types/expected_keepers.go @@ -2,6 +2,7 @@ package types import ( govtypes "github.com/KiraCore/sekai/x/gov/types" + multistakingtypes "github.com/KiraCore/sekai/x/multistaking/types" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -28,3 +29,8 @@ type MultiStakingKeeper interface { ClaimRewards(ctx sdk.Context, delegator sdk.AccAddress) sdk.Coins ClaimRewardsFromModule(ctx sdk.Context, module string) sdk.Coins } + +// MultistakingHooks event hooks for multistaking +type MultistakingHooks interface { + AfterUpsertStakingPool(ctx sdk.Context, valAddr sdk.ValAddress, pool multistakingtypes.StakingPool) // Must be called when a upsert staking pool +} diff --git a/x/basket/types/genesis.go b/x/basket/types/genesis.go index fad64157c..146c3d9cc 100644 --- a/x/basket/types/genesis.go +++ b/x/basket/types/genesis.go @@ -1,6 +1,6 @@ package types -// DefaultGenesis returns the default CustomGo genesis state +// DefaultGenesis returns the default genesis state func DefaultGenesis() *GenesisState { return &GenesisState{} } diff --git a/x/basket/types/genesis.pb.go b/x/basket/types/genesis.pb.go index 2042bd093..967a3cf37 100644 --- a/x/basket/types/genesis.pb.go +++ b/x/basket/types/genesis.pb.go @@ -27,12 +27,14 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type GenesisState struct { // baskets registered on the module Baskets []Basket `protobuf:"bytes,1,rep,name=baskets,proto3" json:"baskets"` + // last basket id + LastBasketId uint64 `protobuf:"varint,2,opt,name=last_basket_id,json=lastBasketId,proto3" json:"last_basket_id,omitempty"` // mints by time - HistoricalMints []AmountAtTime `protobuf:"bytes,2,rep,name=historical_mints,json=historicalMints,proto3" json:"historical_mints"` + HistoricalMints []AmountAtTime `protobuf:"bytes,3,rep,name=historical_mints,json=historicalMints,proto3" json:"historical_mints"` // burns by time - HistoricalBurns []AmountAtTime `protobuf:"bytes,3,rep,name=historical_burns,json=historicalBurns,proto3" json:"historical_burns"` + HistoricalBurns []AmountAtTime `protobuf:"bytes,4,rep,name=historical_burns,json=historicalBurns,proto3" json:"historical_burns"` // swaps by time - HistoricalSwaps []AmountAtTime `protobuf:"bytes,4,rep,name=historical_swaps,json=historicalSwaps,proto3" json:"historical_swaps"` + HistoricalSwaps []AmountAtTime `protobuf:"bytes,5,rep,name=historical_swaps,json=historicalSwaps,proto3" json:"historical_swaps"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -75,6 +77,13 @@ func (m *GenesisState) GetBaskets() []Basket { return nil } +func (m *GenesisState) GetLastBasketId() uint64 { + if m != nil { + return m.LastBasketId + } + return 0 +} + func (m *GenesisState) GetHistoricalMints() []AmountAtTime { if m != nil { return m.HistoricalMints @@ -103,24 +112,26 @@ func init() { func init() { proto.RegisterFile("kira/basket/genesis.proto", fileDescriptor_13d33dee654eeee6) } var fileDescriptor_13d33dee654eeee6 = []byte{ - // 268 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xcc, 0xce, 0x2c, 0x4a, - 0xd4, 0x4f, 0x4a, 0x2c, 0xce, 0x4e, 0x2d, 0xd1, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, - 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x06, 0x49, 0xe9, 0x41, 0xa4, 0xa4, 0x44, 0xd2, 0xf3, - 0xd3, 0xf3, 0xc1, 0xe2, 0xfa, 0x20, 0x16, 0x44, 0x89, 0x94, 0x04, 0xb2, 0x6e, 0x08, 0x05, 0x91, - 0x51, 0x5a, 0xce, 0xc4, 0xc5, 0xe3, 0x0e, 0x31, 0x2e, 0xb8, 0x24, 0xb1, 0x24, 0x55, 0xc8, 0x98, - 0x8b, 0x1d, 0xa2, 0xa0, 0x58, 0x82, 0x51, 0x81, 0x59, 0x83, 0xdb, 0x48, 0x58, 0x0f, 0xc9, 0x7c, - 0x3d, 0x27, 0x30, 0xe5, 0xc4, 0x72, 0xe2, 0x9e, 0x3c, 0x43, 0x10, 0x4c, 0xa5, 0x90, 0x17, 0x97, - 0x40, 0x46, 0x66, 0x71, 0x49, 0x7e, 0x51, 0x66, 0x72, 0x62, 0x4e, 0x7c, 0x6e, 0x66, 0x5e, 0x49, - 0xb1, 0x04, 0x13, 0x58, 0xb7, 0x24, 0x8a, 0x6e, 0xc7, 0xdc, 0xfc, 0xd2, 0xbc, 0x12, 0xc7, 0x92, - 0x90, 0xcc, 0xdc, 0x54, 0xa8, 0x19, 0xfc, 0x08, 0x8d, 0xbe, 0x20, 0x7d, 0x68, 0x66, 0x25, 0x95, - 0x16, 0xe5, 0x15, 0x4b, 0x30, 0x93, 0x6c, 0x96, 0x13, 0x48, 0x1f, 0x9a, 0x59, 0xc5, 0xe5, 0x89, - 0x05, 0xc5, 0x12, 0x2c, 0x24, 0x9b, 0x15, 0x0c, 0xd2, 0xe7, 0xe4, 0x74, 0xe2, 0x91, 0x1c, 0xe3, - 0x85, 0x47, 0x72, 0x8c, 0x0f, 0x1e, 0xc9, 0x31, 0x4e, 0x78, 0x2c, 0xc7, 0x70, 0xe1, 0xb1, 0x1c, - 0xc3, 0x8d, 0xc7, 0x72, 0x0c, 0x51, 0x1a, 0xe9, 0x99, 0x25, 0x19, 0xa5, 0x49, 0x7a, 0xc9, 0xf9, - 0xb9, 0xfa, 0xde, 0x99, 0x45, 0x89, 0xce, 0xf9, 0x45, 0xa9, 0xfa, 0xc5, 0xa9, 0xd9, 0x89, 0x99, - 0xfa, 0x15, 0xb0, 0x40, 0x2f, 0xa9, 0x2c, 0x48, 0x2d, 0x4e, 0x62, 0x03, 0x07, 0xba, 0x31, 0x20, - 0x00, 0x00, 0xff, 0xff, 0x4b, 0xdc, 0x27, 0xb6, 0xce, 0x01, 0x00, 0x00, + // 296 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x91, 0xb1, 0x4e, 0xc3, 0x30, + 0x10, 0x86, 0x93, 0xb6, 0x80, 0x94, 0x56, 0x80, 0x02, 0x83, 0xdb, 0xc1, 0x54, 0x88, 0xa1, 0x53, + 0x2c, 0xd1, 0x27, 0x68, 0x18, 0x10, 0x20, 0x96, 0x96, 0x89, 0xa5, 0x72, 0x5a, 0x2b, 0xb5, 0xd2, + 0xc4, 0x91, 0xef, 0x22, 0xe0, 0x2d, 0x78, 0xac, 0x2e, 0x48, 0x1d, 0x99, 0x10, 0x4a, 0x5e, 0x04, + 0x39, 0xa6, 0xa2, 0x74, 0xeb, 0x74, 0xf6, 0xdd, 0x7d, 0xdf, 0x3f, 0x9c, 0xd7, 0x4d, 0xa4, 0xe6, + 0x2c, 0xe2, 0x90, 0x08, 0x64, 0xb1, 0xc8, 0x04, 0x48, 0x08, 0x72, 0xad, 0x50, 0xf9, 0x6d, 0x33, + 0x0a, 0xec, 0xa8, 0x77, 0x1e, 0xab, 0x58, 0xd5, 0x7d, 0x66, 0x5e, 0x76, 0xa5, 0x47, 0xb6, 0x69, + 0x5b, 0xec, 0xe4, 0xf2, 0xa3, 0xe1, 0x75, 0x6e, 0xad, 0x6e, 0x82, 0x1c, 0x85, 0x3f, 0xf4, 0x8e, + 0xec, 0x02, 0x10, 0xb7, 0xdf, 0x1c, 0xb4, 0xaf, 0xcf, 0x82, 0x2d, 0x7f, 0x10, 0xd6, 0x25, 0x6c, + 0xad, 0xbe, 0x2e, 0x9c, 0xf1, 0x66, 0xd3, 0xbf, 0xf2, 0x8e, 0x97, 0x1c, 0x70, 0x6a, 0xff, 0x53, + 0x39, 0x27, 0x8d, 0xbe, 0x3b, 0x68, 0x8d, 0x3b, 0xa6, 0x6b, 0x91, 0xbb, 0xb9, 0x7f, 0xef, 0x9d, + 0x2e, 0x24, 0xa0, 0xd2, 0x72, 0xc6, 0x97, 0xd3, 0x54, 0x66, 0x08, 0xa4, 0x59, 0x67, 0x74, 0xff, + 0x65, 0x8c, 0x52, 0x55, 0x64, 0x38, 0xc2, 0x27, 0x99, 0x8a, 0xdf, 0xa4, 0x93, 0x3f, 0xf0, 0xd1, + 0x70, 0x3b, 0xae, 0xa8, 0xd0, 0x19, 0x90, 0xd6, 0xde, 0xae, 0xd0, 0x70, 0x3b, 0x2e, 0x78, 0xe1, + 0x39, 0x90, 0x83, 0xbd, 0x5d, 0x13, 0xc3, 0x85, 0xe1, 0xaa, 0xa4, 0xee, 0xba, 0xa4, 0xee, 0x77, + 0x49, 0xdd, 0xf7, 0x8a, 0x3a, 0xeb, 0x8a, 0x3a, 0x9f, 0x15, 0x75, 0x9e, 0x07, 0xb1, 0xc4, 0x45, + 0x11, 0x05, 0x33, 0x95, 0xb2, 0x07, 0xa9, 0xf9, 0x8d, 0xd2, 0x82, 0x81, 0x48, 0xb8, 0x64, 0xaf, + 0x9b, 0xd3, 0xe0, 0x5b, 0x2e, 0x20, 0x3a, 0xac, 0x4f, 0x33, 0xfc, 0x09, 0x00, 0x00, 0xff, 0xff, + 0xb1, 0xf8, 0x1f, 0xa9, 0xf4, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -154,7 +165,7 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0x22 + dAtA[i] = 0x2a } } if len(m.HistoricalBurns) > 0 { @@ -168,7 +179,7 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0x1a + dAtA[i] = 0x22 } } if len(m.HistoricalMints) > 0 { @@ -182,9 +193,14 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0x12 + dAtA[i] = 0x1a } } + if m.LastBasketId != 0 { + i = encodeVarintGenesis(dAtA, i, uint64(m.LastBasketId)) + i-- + dAtA[i] = 0x10 + } if len(m.Baskets) > 0 { for iNdEx := len(m.Baskets) - 1; iNdEx >= 0; iNdEx-- { { @@ -225,6 +241,9 @@ func (m *GenesisState) Size() (n int) { n += 1 + l + sovGenesis(uint64(l)) } } + if m.LastBasketId != 0 { + n += 1 + sovGenesis(uint64(m.LastBasketId)) + } if len(m.HistoricalMints) > 0 { for _, e := range m.HistoricalMints { l = e.Size() @@ -316,6 +335,25 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field LastBasketId", wireType) + } + m.LastBasketId = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.LastBasketId |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 3: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field HistoricalMints", wireType) } @@ -349,7 +387,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 3: + case 4: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field HistoricalBurns", wireType) } @@ -383,7 +421,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 4: + case 5: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field HistoricalSwaps", wireType) } diff --git a/x/multistaking/keeper/delegation.go b/x/multistaking/keeper/delegation.go index 185ea7fde..b71d08494 100644 --- a/x/multistaking/keeper/delegation.go +++ b/x/multistaking/keeper/delegation.go @@ -165,7 +165,7 @@ func (k Keeper) IncreasePoolRewards(ctx sdk.Context, pool types.StakingPool, rew delegators := k.GetPoolDelegators(ctx, pool.Id) for _, shareToken := range pool.TotalShareTokens { - nativeDenom := getNativeDenom(pool.Id, shareToken.Denom) + nativeDenom := types.GetNativeDenom(pool.Id, shareToken.Denom) rate := k.tokenKeeper.GetTokenRate(ctx, nativeDenom) if rate == nil { continue @@ -324,7 +324,7 @@ func (k Keeper) Delegate(ctx sdk.Context, msg *types.MsgDelegate) error { } pool.TotalStakingTokens = sdk.Coins(pool.TotalStakingTokens).Add(msg.Amounts...) - poolCoins := getPoolCoins(pool, msg.Amounts) + poolCoins := types.GetPoolCoins(pool, msg.Amounts) pool.TotalShareTokens = sdk.Coins(pool.TotalShareTokens).Add(poolCoins...) k.SetStakingPool(ctx, pool) @@ -352,7 +352,7 @@ func (k Keeper) Undelegate(ctx sdk.Context, msg *types.MsgUndelegate) error { return err } - poolCoins := getPoolCoins(pool, msg.Amounts) + poolCoins := types.GetPoolCoins(pool, msg.Amounts) err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, delegator, types.ModuleName, poolCoins) if err != nil { @@ -398,7 +398,7 @@ func (k Keeper) GetPoolDelegationValue(ctx sdk.Context, pool types.StakingPool, if rate == nil { continue } - shareToken := getShareDenom(pool.Id, stakingToken.Denom) + shareToken := types.GetShareDenom(pool.Id, stakingToken.Denom) balance := balances.AmountOf(shareToken) delegationValue = delegationValue.Add(sdk.NewDecFromInt(balance).Mul(rate.FeeRate).RoundInt()) } @@ -443,7 +443,7 @@ func (k Keeper) RegisterDelegator(ctx sdk.Context, delegator sdk.AccAddress) { for _, stakingToken := range pool.TotalStakingTokens { rate := k.tokenKeeper.GetTokenRate(ctx, stakingToken.Denom) - shareToken := getShareDenom(pool.Id, stakingToken.Denom) + shareToken := types.GetShareDenom(pool.Id, stakingToken.Denom) balance := balances.AmountOf(shareToken) if balance.GTE(rate.StakeMin) { k.SetPoolDelegator(ctx, pool.Id, delegator) @@ -461,7 +461,7 @@ func (k Keeper) UnregisterNotEnoughStakeDelegator(ctx sdk.Context, pool types.St toBeRemoved := true balances := k.bankKeeper.GetAllBalances(ctx, delegator) for _, shareToken := range pool.TotalShareTokens { - nativeDenom := getNativeDenom(pool.Id, shareToken.Denom) + nativeDenom := types.GetNativeDenom(pool.Id, shareToken.Denom) rate := k.tokenKeeper.GetTokenRate(ctx, nativeDenom) if rate == nil { continue diff --git a/x/multistaking/keeper/keeper.go b/x/multistaking/keeper/keeper.go index bd07a37d0..6de0de082 100644 --- a/x/multistaking/keeper/keeper.go +++ b/x/multistaking/keeper/keeper.go @@ -16,6 +16,7 @@ type Keeper struct { govKeeper govkeeper.Keeper sk types.StakingKeeper distrKeeper types.DistributorKeeper + hooks types.MultistakingHooks } // NewKeeper returns new keeper. @@ -30,6 +31,17 @@ func NewKeeper(storeKey storetypes.StoreKey, cdc codec.BinaryCodec, bankKeeper t } } +// Set the validator hooks +func (k *Keeper) SetHooks(sh types.MultistakingHooks) *Keeper { + if k.hooks != nil { + panic("cannot set validator hooks twice") + } + + k.hooks = sh + + return k +} + func (k *Keeper) SetDistrKeeper(distrKeeper types.DistributorKeeper) { k.distrKeeper = distrKeeper } diff --git a/x/multistaking/keeper/msg_server.go b/x/multistaking/keeper/msg_server.go index 7d6349aa5..0fb884362 100644 --- a/x/multistaking/keeper/msg_server.go +++ b/x/multistaking/keeper/msg_server.go @@ -57,8 +57,7 @@ func (k msgServer) UpsertStakingPool(goCtx context.Context, msg *types.MsgUpsert // increase id when creating a new pool lastPoolId := k.keeper.GetLastPoolId(ctx) + 1 k.keeper.SetLastPoolId(ctx, lastPoolId) - - k.keeper.SetStakingPool(ctx, types.StakingPool{ + pool = types.StakingPool{ Id: lastPoolId, Enabled: msg.Enabled, Validator: msg.Validator, @@ -66,7 +65,12 @@ func (k msgServer) UpsertStakingPool(goCtx context.Context, msg *types.MsgUpsert TotalStakingTokens: []sdk.Coin{}, TotalShareTokens: []sdk.Coin{}, TotalRewards: []sdk.Coin{}, - }) + } + k.keeper.SetStakingPool(ctx, pool) + } + + if k.keeper.hooks != nil { + k.keeper.hooks.AfterUpsertStakingPool(ctx, valAddr, pool) } return &types.MsgUpsertStakingPoolResponse{}, nil diff --git a/x/multistaking/keeper/pool.go b/x/multistaking/keeper/pool.go index 34b389c50..19a2ef23c 100644 --- a/x/multistaking/keeper/pool.go +++ b/x/multistaking/keeper/pool.go @@ -1,9 +1,6 @@ package keeper import ( - "fmt" - "strings" - "github.com/KiraCore/sekai/x/multistaking/types" "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" @@ -62,22 +59,3 @@ func (k Keeper) RemoveStakingPool(ctx sdk.Context, pool types.StakingPool) { key := append([]byte(types.KeyPrefixStakingPool), []byte(pool.Validator)...) store.Delete(key) } - -func getPoolPrefix(poolID uint64) string { - return fmt.Sprintf("v%d/", poolID) -} -func getPoolCoins(pool types.StakingPool, coins sdk.Coins) sdk.Coins { - prefix := getPoolPrefix(pool.Id) - poolCoins := sdk.Coins{} - for _, coin := range coins { - poolCoins = poolCoins.Add(sdk.NewCoin(prefix+coin.Denom, sdk.NewDecFromInt(coin.Amount).Mul(sdk.OneDec().Sub(pool.Slashed)).RoundInt())) - } - return poolCoins -} -func getShareDenom(poolID uint64, denom string) string { - prefix := getPoolPrefix(poolID) - return prefix + denom -} -func getNativeDenom(poolID uint64, denom string) string { - return strings.TrimPrefix(denom, getPoolPrefix(poolID)) -} diff --git a/x/multistaking/keeper/slash.go b/x/multistaking/keeper/slash.go index 3988996ec..37d850d2d 100644 --- a/x/multistaking/keeper/slash.go +++ b/x/multistaking/keeper/slash.go @@ -38,4 +38,6 @@ func (k Keeper) SlashStakingPool(ctx sdk.Context, validator string, slash sdk.De feesTreasury = feesTreasury.Add(treasurySendAmount...) k.distrKeeper.SetFeesTreasury(ctx, feesTreasury) k.SetStakingPool(ctx, pool) + + // TODO: pause the basket when the pool's slashed } diff --git a/x/multistaking/types/expected_keepers.go b/x/multistaking/types/expected_keepers.go index d666a898d..46205827e 100644 --- a/x/multistaking/types/expected_keepers.go +++ b/x/multistaking/types/expected_keepers.go @@ -6,6 +6,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +// MultistakingHooks event hooks for multistaking +type MultistakingHooks interface { + AfterUpsertStakingPool(ctx sdk.Context, valAddr sdk.ValAddress, pool StakingPool) // Must be called when a upsert staking pool +} + // StakingKeeper expected staking keeper type StakingKeeper interface { DefaultDenom(sdk.Context) string diff --git a/x/multistaking/types/hooks.go b/x/multistaking/types/hooks.go new file mode 100644 index 000000000..c8aa4cf3b --- /dev/null +++ b/x/multistaking/types/hooks.go @@ -0,0 +1,18 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// combine multiple staking hooks, all hook functions are run in array sequence +type Hooks []MultistakingHooks + +func NewMultiStakingHooks(hooks ...MultistakingHooks) Hooks { + return hooks +} + +func (h Hooks) AfterUpsertStakingPool(ctx sdk.Context, valAddr sdk.ValAddress, pool StakingPool) { + for i := range h { + h[i].AfterUpsertStakingPool(ctx, valAddr, pool) + } +} diff --git a/x/multistaking/types/pool.go b/x/multistaking/types/pool.go new file mode 100644 index 000000000..be28acfe2 --- /dev/null +++ b/x/multistaking/types/pool.go @@ -0,0 +1,27 @@ +package types + +import ( + "fmt" + "strings" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func GetPoolPrefix(poolID uint64) string { + return fmt.Sprintf("v%d/", poolID) +} +func GetPoolCoins(pool StakingPool, coins sdk.Coins) sdk.Coins { + prefix := GetPoolPrefix(pool.Id) + poolCoins := sdk.Coins{} + for _, coin := range coins { + poolCoins = poolCoins.Add(sdk.NewCoin(prefix+coin.Denom, sdk.NewDecFromInt(coin.Amount).Mul(sdk.OneDec().Sub(pool.Slashed)).RoundInt())) + } + return poolCoins +} +func GetShareDenom(poolID uint64, denom string) string { + prefix := GetPoolPrefix(poolID) + return prefix + denom +} +func GetNativeDenom(poolID uint64, denom string) string { + return strings.TrimPrefix(denom, GetPoolPrefix(poolID)) +} diff --git a/x/tokens/handler_test.go b/x/tokens/handler_test.go index b47f336ed..b1e294c5a 100644 --- a/x/tokens/handler_test.go +++ b/x/tokens/handler_test.go @@ -212,7 +212,7 @@ func TestNewHandler_MsgUpsertTokenRate(t *testing.T) { // test various query commands rate := app.TokensKeeper.GetTokenRate(ctx, theMsg.Denom) require.True(t, rate != nil) - ratesAll := app.TokensKeeper.ListTokenRate(ctx) + ratesAll := app.TokensKeeper.GetAllTokenRates(ctx) require.True(t, len(ratesAll) > 0) ratesByDenom := app.TokensKeeper.GetTokenRatesByDenom(ctx, []string{theMsg.Denom}) require.True(t, ratesByDenom[theMsg.Denom] != nil) diff --git a/x/tokens/keeper/grpc_query.go b/x/tokens/keeper/grpc_query.go index b7cdfcb0c..09340086d 100644 --- a/x/tokens/keeper/grpc_query.go +++ b/x/tokens/keeper/grpc_query.go @@ -50,7 +50,7 @@ func (q Querier) GetTokenRatesByDenom(ctx context.Context, request *types.TokenR } func (q Querier) GetAllTokenRates(ctx context.Context, request *types.AllTokenRatesRequest) (*types.AllTokenRatesResponse, error) { - rates := q.keeper.ListTokenRate(sdk.UnwrapSDKContext(ctx)) + rates := q.keeper.GetAllTokenRates(sdk.UnwrapSDKContext(ctx)) return &types.AllTokenRatesResponse{Data: rates}, nil } diff --git a/x/tokens/keeper/rate.go b/x/tokens/keeper/rate.go index 29f883565..a29fd83b9 100644 --- a/x/tokens/keeper/rate.go +++ b/x/tokens/keeper/rate.go @@ -25,8 +25,8 @@ func (k Keeper) GetTokenRate(ctx sdk.Context, denom string) *types.TokenRate { return rate } -// ListTokenRate returns all list of token rate -func (k Keeper) ListTokenRate(ctx sdk.Context) []*types.TokenRate { +// GetAllTokenRates returns all list of token rate +func (k Keeper) GetAllTokenRates(ctx sdk.Context) []*types.TokenRate { var tokenRates []*types.TokenRate // get iterator for token rates @@ -73,7 +73,7 @@ func (k Keeper) UpsertTokenRate(ctx sdk.Context, rate types.TokenRate) error { store.Set(tokenRateStoreID, k.cdc.MustMarshal(&rate)) totalRewardsCap := sdk.ZeroDec() - rates := k.ListTokenRate(ctx) + rates := k.GetAllTokenRates(ctx) for _, rate := range rates { totalRewardsCap = totalRewardsCap.Add(rate.StakeCap) } diff --git a/x/tokens/keeper/rate_test.go b/x/tokens/keeper/rate_test.go index de13f9bd5..e6bf5ba9f 100644 --- a/x/tokens/keeper/rate_test.go +++ b/x/tokens/keeper/rate_test.go @@ -12,7 +12,7 @@ func (suite *KeeperTestSuite) TestTokenRates() { // check initial token rate before registration rate := suite.app.TokensKeeper.GetTokenRate(ctx, "stake") suite.Require().Nil(rate) - rates := suite.app.TokensKeeper.ListTokenRate(ctx) + rates := suite.app.TokensKeeper.GetAllTokenRates(ctx) suite.Require().Len(rates, 4) rateMap := suite.app.TokensKeeper.GetTokenRatesByDenom(ctx, []string{"stake"}) suite.Require().Equal(len(rateMap), 0) @@ -27,7 +27,7 @@ func (suite *KeeperTestSuite) TestTokenRates() { suite.app.TokensKeeper.UpsertTokenRate(ctx, newRate) rate = suite.app.TokensKeeper.GetTokenRate(ctx, "stake") suite.Require().NotNil(rate) - rates = suite.app.TokensKeeper.ListTokenRate(ctx) + rates = suite.app.TokensKeeper.GetAllTokenRates(ctx) suite.Require().Len(rates, 5) rateMap = suite.app.TokensKeeper.GetTokenRatesByDenom(ctx, []string{"stake"}) suite.Require().Equal(len(rateMap), 1) @@ -37,7 +37,7 @@ func (suite *KeeperTestSuite) TestTokenRates() { suite.app.TokensKeeper.DeleteTokenRate(ctx, "stake") rate = suite.app.TokensKeeper.GetTokenRate(ctx, "stake") suite.Require().Nil(rate) - rates = suite.app.TokensKeeper.ListTokenRate(ctx) + rates = suite.app.TokensKeeper.GetAllTokenRates(ctx) suite.Require().Len(rates, 4) rateMap = suite.app.TokensKeeper.GetTokenRatesByDenom(ctx, []string{"stake"}) suite.Require().Equal(len(rateMap), 0) diff --git a/x/tokens/module.go b/x/tokens/module.go index 069d877a2..0359822e6 100644 --- a/x/tokens/module.go +++ b/x/tokens/module.go @@ -111,7 +111,7 @@ func (am AppModule) InitGenesis( func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { var genesisState tokenstypes.GenesisState genesisState.Aliases = am.tokensKeeper.ListTokenAlias(ctx) - genesisState.Rates = am.tokensKeeper.ListTokenRate(ctx) + genesisState.Rates = am.tokensKeeper.GetAllTokenRates(ctx) genesisState.TokenBlackWhites = am.tokensKeeper.GetTokenBlackWhites(ctx) return cdc.MustMarshalJSON(&genesisState) }