-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor x/distribution according to ADR 031 (#7524)
* Refactor x/distribution according to ADR 31 * lint * removed unused * Apply suggestions from code review Co-authored-by: Marie Gauthier <[email protected]> Co-authored-by: Aaron Craelius <[email protected]> Co-authored-by: Marie Gauthier <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1279da5
commit bf71654
Showing
4 changed files
with
1,009 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/armon/go-metrics" | ||
"github.com/cosmos/cosmos-sdk/telemetry" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/distribution/types" | ||
) | ||
|
||
type msgServer struct { | ||
Keeper | ||
} | ||
|
||
// NewMsgServerImpl returns an implementation of the bank MsgServer interface | ||
// for the provided Keeper. | ||
func NewMsgServerImpl(keeper Keeper) types.MsgServer { | ||
return &msgServer{Keeper: keeper} | ||
} | ||
|
||
var _ types.MsgServer = msgServer{} | ||
|
||
func (k msgServer) SetWithdrawAddress(goCtx context.Context, msg *types.MsgSetWithdrawAddress) (*types.MsgSetWithdrawAddressResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
withdrawAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = k.SetWithdrawAddr(ctx, delegatorAddress, withdrawAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress), | ||
), | ||
) | ||
|
||
return &types.MsgSetWithdrawAddressResponse{}, nil | ||
} | ||
|
||
func (k msgServer) WithdrawDelegatorReward(goCtx context.Context, msg *types.MsgWithdrawDelegatorReward) (*types.MsgWithdrawDelegatorRewardResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
amount, err := k.WithdrawDelegationRewards(ctx, delegatorAddress, valAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func() { | ||
for _, a := range amount { | ||
telemetry.SetGaugeWithLabels( | ||
[]string{"tx", "msg", "withdraw_reward"}, | ||
float32(a.Amount.Int64()), | ||
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)}, | ||
) | ||
} | ||
}() | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
sdk.NewAttribute(sdk.AttributeKeySender, msg.DelegatorAddress), | ||
), | ||
) | ||
return &types.MsgWithdrawDelegatorRewardResponse{}, nil | ||
} | ||
|
||
func (k msgServer) WithdrawValidatorCommission(goCtx context.Context, msg *types.MsgWithdrawValidatorCommission) (*types.MsgWithdrawValidatorCommissionResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
amount, err := k.Keeper.WithdrawValidatorCommission(ctx, valAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func() { | ||
for _, a := range amount { | ||
telemetry.SetGaugeWithLabels( | ||
[]string{"tx", "msg", "withdraw_commission"}, | ||
float32(a.Amount.Int64()), | ||
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)}, | ||
) | ||
} | ||
}() | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
sdk.NewAttribute(sdk.AttributeKeySender, msg.ValidatorAddress), | ||
), | ||
) | ||
|
||
return &types.MsgWithdrawValidatorCommissionResponse{}, nil | ||
} | ||
|
||
func (k msgServer) FundCommunityPool(goCtx context.Context, msg *types.MsgFundCommunityPool) (*types.MsgFundCommunityPoolResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
depositer, err := sdk.AccAddressFromBech32(msg.Depositor) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := k.Keeper.FundCommunityPool(ctx, msg.Amount, depositer); err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor), | ||
), | ||
) | ||
|
||
return &types.MsgFundCommunityPoolResponse{}, nil | ||
} |
Oops, something went wrong.