Skip to content

Commit

Permalink
stablestake hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
amityadav0 committed Jul 10, 2024
1 parent 4582422 commit 1049158
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 deletions.
37 changes: 37 additions & 0 deletions x/leveragelp/keeper/hooks_stablestake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package keeper

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stablestaketypes "github.com/elys-network/elys/x/stablestake/types"
)

func (k Keeper) AfterUpdateInterestStacked(ctx sdk.Context, address string, old sdk.Int, new sdk.Int) error {
k.SetSortedLiquidation(ctx, address, old, new)
return nil
}

// Hooks wrapper struct for incentive keeper
type StableStakeHooks struct {
k Keeper
}

var _ stablestaketypes.StableStakeHooks = StableStakeHooks{}

// Return the wrapper struct
func (k Keeper) StableStakeHooks() StableStakeHooks {
return StableStakeHooks{k}
}

func (h StableStakeHooks) AfterBond(ctx sdk.Context, sender string, shareAmount math.Int) error {
return nil
}

func (h StableStakeHooks) AfterUnbond(ctx sdk.Context, sender string, shareAmount math.Int) error {
return nil
}

func (h StableStakeHooks) AfterUpdateInterestStacked(ctx sdk.Context, address string, old sdk.Int, new sdk.Int) error {
h.k.AfterUpdateInterestStacked(ctx, address, old, new)
return nil
}
36 changes: 35 additions & 1 deletion x/leveragelp/keeper/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (k Keeper) SetPosition(ctx sdk.Context, position *types.Position, oldDebt s
key := types.GetPositionKey(position.Address, position.Id)
store.Set(key, k.cdc.MustMarshal(position))

// for stablestake hook
store.Set([]byte(position.GetPositionAddress()), key)

// Add position sort keys
addrId := types.AddressId{
Id: position.Id,
Expand All @@ -75,7 +78,7 @@ func (k Keeper) SetPosition(ctx sdk.Context, position *types.Position, oldDebt s
}
}

func (k Keeper) DestroyPosition(ctx sdk.Context, positionAddress string, id uint64) error {
func (k Keeper) DestroyPosition(ctx sdk.Context, positionAddress string, id uint64, oldDebt sdk.Int) error {
key := types.GetPositionKey(positionAddress, id)
store := ctx.KVStore(k.storeKey)
if !store.Has(key) {
Expand All @@ -95,6 +98,7 @@ func (k Keeper) DestroyPosition(ctx sdk.Context, positionAddress string, id uint
if len(stopLossKey) > 0 {
store.Delete(stopLossKey)
}
store.Delete([]byte(old.GetPositionAddress()))
}

// decrement open position count
Expand All @@ -109,6 +113,36 @@ func (k Keeper) DestroyPosition(ctx sdk.Context, positionAddress string, id uint
return nil
}

// Set sorted liquidation
func (k Keeper) SetSortedLiquidation(ctx sdk.Context, address string, old sdk.Int, new sdk.Int) {
store := ctx.KVStore(k.storeKey)
if store.Has([]byte(address)) {
key := store.Get([]byte(address))
if !store.Has(key) {
return
}
res := store.Get(key)
var position types.Position
k.cdc.MustUnmarshal(res, &position)
// Make sure liability changes are handled properly here, this should always be updated whenever liability is changed
liquidationKey := types.GetLiquidationSortKey(position.AmmPoolId, position.LeveragedLpAmount, old, position.Id)
if len(liquidationKey) > 0 {
store.Delete(liquidationKey)
}

// Add position sort keys
addrId := types.AddressId{
Id: position.Id,
Address: position.Address,
}
bz := k.cdc.MustMarshal(&addrId)
liquidationKey = types.GetLiquidationSortKey(position.AmmPoolId, position.LeveragedLpAmount, new, position.Id)
if len(liquidationKey) > 0 {
store.Set(liquidationKey, bz)
}
}
}

// Set Open Position count
func (k Keeper) SetOpenPositionCount(ctx sdk.Context, count uint64) {
store := ctx.KVStore(k.storeKey)
Expand Down
2 changes: 1 addition & 1 deletion x/leveragelp/keeper/position_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (k Keeper) ForceCloseLong(ctx sdk.Context, position types.Position, pool ty
if err != nil {
return sdk.ZeroInt(), err
}
err = k.DestroyPosition(ctx, position.Address, position.Id)
err = k.DestroyPosition(ctx, position.Address, position.Id, oldDebt.Borrowed.Add(debt.InterestStacked).Sub(debt.InterestPaid))
if err != nil {
return sdk.ZeroInt(), err
}
Expand Down
4 changes: 4 additions & 0 deletions x/masterchef/keeper/hooks_stablestake.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ func (h StableStakeHooks) AfterUnbond(ctx sdk.Context, sender string, shareAmoun
h.k.AfterWithdraw(ctx, stablestaketypes.PoolId, sender, shareAmount)
return nil
}

func (h StableStakeHooks) AfterUpdateInterestStacked(ctx sdk.Context, address string, old sdk.Int, new sdk.Int) error {
return nil
}
2 changes: 1 addition & 1 deletion x/stablestake/keeper/begin_blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (k Keeper) BeginBlocker(ctx sdk.Context) {
epochPosition := k.GetEpochPosition(ctx, epochLength)

if epochPosition == 0 { // if epoch has passed
// divide them in blocks, update values
// TODO: divide them in blocks, update values
params := k.GetParams(ctx)
rate := k.InterestRateComputation(ctx)
params.InterestRate = rate
Expand Down
4 changes: 4 additions & 0 deletions x/tier/keeper/hooks_stable_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ func (h StableStakeHooks) AfterUnbond(ctx sdk.Context, sender string, shareAmoun
h.k.AfterUnbond(ctx, sender, shareAmount)
return nil
}

func (h StableStakeHooks) AfterUpdateInterestStacked(ctx sdk.Context, address string, old sdk.Int, new sdk.Int) error {
return nil
}

0 comments on commit 1049158

Please sign in to comment.