Skip to content

Commit

Permalink
halborn finding fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Jun 18, 2024
1 parent 3eec461 commit 8de3a89
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 200 deletions.
12 changes: 6 additions & 6 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41978,10 +41978,10 @@ paths:
format: uint64
from_block:
type: string
format: uint64
format: int64
to_block:
type: string
format: uint64
format: int64
amount_per_block:
type: string
apr:
Expand Down Expand Up @@ -87291,10 +87291,10 @@ definitions:
format: uint64
from_block:
type: string
format: uint64
format: int64
to_block:
type: string
format: uint64
format: int64
amount_per_block:
type: string
apr:
Expand Down Expand Up @@ -87449,10 +87449,10 @@ definitions:
format: uint64
from_block:
type: string
format: uint64
format: int64
to_block:
type: string
format: uint64
format: int64
amount_per_block:
type: string
apr:
Expand Down
10 changes: 6 additions & 4 deletions proto/elys/masterchef/external_incentive.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ message ExternalIncentive {
uint64 id = 1;
string reward_denom = 2;
uint64 pool_id = 3;
uint64 from_block = 4;
uint64 to_block = 5;
string amount_per_block = 6
[ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
int64 from_block = 4;
int64 to_block = 5;
string amount_per_block = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string apr = 7
[ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}
4 changes: 2 additions & 2 deletions proto/elys/masterchef/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ message MsgAddExternalIncentive {
string sender = 1;
string reward_denom = 2;
uint64 pool_id = 3;
uint64 from_block = 4;
uint64 to_block = 5;
int64 from_block = 4;
int64 to_block = 5;
string amount_per_block = 6
[ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
}
Expand Down
50 changes: 39 additions & 11 deletions x/estaking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,46 @@ func (k msgServer) WithdrawReward(goCtx context.Context, msg *types.MsgWithdrawR
return nil, err
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtWithdrawReward,
sdk.NewAttribute(types.AttributeDelegatorAddress, msg.DelegatorAddress),
sdk.NewAttribute(types.AttributeValidatorAddress, msg.ValidatorAddress),
sdk.NewAttribute(types.AttributeAmount, amount.String()),
),
})
return &types.MsgWithdrawRewardResponse{Amount: amount}, nil
}

func (k msgServer) WithdrawElysStakingRewards(goCtx context.Context, msg *types.MsgWithdrawElysStakingRewards) (*types.MsgWithdrawElysStakingRewardsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

delAddr := sdk.MustAccAddressFromBech32(msg.DelegatorAddress)
delegations := k.Keeper.Keeper.GetDelegatorDelegations(ctx, delAddr, 1024)
rewards := sdk.Coins{}
for _, del := range delegations {
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
return nil, err
}
amount, err := k.distrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)

var amount sdk.Coins
var err error = nil
var rewards = sdk.Coins{}
k.Keeper.Keeper.IterateDelegations(ctx, delAddr, func(index int64, del stakingtypes.DelegationI) (stop bool) {
valAddr := del.GetValidatorAddr()
amount, err = k.distrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
if err != nil {
return nil, err
return true
}
rewards = rewards.Add(amount...)
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtWithdrawReward,
sdk.NewAttribute(types.AttributeDelegatorAddress, msg.DelegatorAddress),
sdk.NewAttribute(types.AttributeValidatorAddress, valAddr.String()),
sdk.NewAttribute(types.AttributeAmount, amount.String()),
),
})
return false
})

if err != nil {
return nil, err
}
return &types.MsgWithdrawElysStakingRewardsResponse{Amount: rewards}, nil
}

Expand All @@ -83,6 +102,15 @@ func (k Keeper) WithdrawAllRewards(goCtx context.Context, msg *types.MsgWithdraw
return true
}
rewards = rewards.Add(amount...)

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtWithdrawReward,
sdk.NewAttribute(types.AttributeDelegatorAddress, msg.DelegatorAddress),
sdk.NewAttribute(types.AttributeValidatorAddress, valAddr.String()),
sdk.NewAttribute(types.AttributeAmount, amount.String()),
),
})
return false
})

Expand Down
9 changes: 9 additions & 0 deletions x/estaking/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package types

const (
TypeEvtWithdrawReward = "withdraw_reward"

AttributeDelegatorAddress = "delegator_address"
AttributeValidatorAddress = "validator_address"
AttributeAmount = "amount"
)
13 changes: 1 addition & 12 deletions x/estaking/types/query.pb.gw.go

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

14 changes: 14 additions & 0 deletions x/leveragelp/spec/03_keeper.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,28 @@ The `Close` function allows a user to close a leveraged position, either partial

The `BeginBlocker` function performs regular health checks on all positions and liquidates unhealthy positions if necessary.

### Positions indexing

Two indexings are available to

- sort positions by risk level per pool
- sort positions by stopLossPrice per pool

## BeginBlocker

The `BeginBlocker` function is called at the beginning of each block to perform necessary updates and maintenance for the `leveragelp` module. It processes health checks and liquidates unhealthy positions if needed.

- To avoid time complexity in liquidation flow, it's iterating till it meets the first healthy position.
- To avoid time complexity in stop loss close position, it's iterating till it meets the first acceptable stop loss unclose position.

### LiquidatePositionIfUnhealthy

The `LiquidatePositionIfUnhealthy` function checks the health of a position and liquidates it if it is unhealthy.

### ClosePositionIfUnderStopLossPrice

The `ClosePositionIfUnderStopLossPrice` function checks if the position lp token price is lower than stopLossPrice, it close the position.

### UpdatePoolHealth

The `UpdatePoolHealth` function updates the health of a pool based on the current state of its positions.
Expand Down
Loading

0 comments on commit 8de3a89

Please sign in to comment.