Skip to content

Commit

Permalink
automatic addition of staking tokens to staking basket when a new mul…
Browse files Browse the repository at this point in the history
…tistaking pool created after a validator join
  • Loading branch information
kiragpg committed Nov 3, 2023
1 parent 7027803 commit d676cb0
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 75 deletions.
12 changes: 8 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions proto/kira/basket/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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
77 changes: 77 additions & 0 deletions x/basket/keeper/hooks.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 4 additions & 1 deletion x/basket/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
}
Expand Down
2 changes: 2 additions & 0 deletions x/basket/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions x/basket/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion x/basket/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

// DefaultGenesis returns the default CustomGo genesis state
// DefaultGenesis returns the default genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{}
}
90 changes: 64 additions & 26 deletions x/basket/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d676cb0

Please sign in to comment.