91 lines
2.5 KiB
Go
91 lines
2.5 KiB
Go
package keeper
|
|
|
|
import (
|
|
"context"
|
|
|
|
"cosmossdk.io/errors"
|
|
"cosmossdk.io/x/protocolpool/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
)
|
|
|
|
type MsgServer struct {
|
|
Keeper
|
|
}
|
|
|
|
var _ types.MsgServer = MsgServer{}
|
|
|
|
// NewMsgServerImpl returns an implementation of the protocolpool MsgServer interface
|
|
// for the provided Keeper.
|
|
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
|
|
return &MsgServer{Keeper: keeper}
|
|
}
|
|
|
|
func (k MsgServer) FundCommunityPool(ctx context.Context, msg *types.MsgFundCommunityPool) (*types.MsgFundCommunityPoolResponse, error) {
|
|
depositor, err := k.authKeeper.AddressCodec().StringToBytes(msg.Depositor)
|
|
if err != nil {
|
|
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid depositor address: %s", err)
|
|
}
|
|
|
|
if err := validateAmount(msg.Amount); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// send funds to community pool module account
|
|
if err := k.Keeper.FundCommunityPool(ctx, msg.Amount, depositor); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &types.MsgFundCommunityPoolResponse{}, nil
|
|
}
|
|
|
|
func (k MsgServer) CommunityPoolSpend(ctx context.Context, msg *types.MsgCommunityPoolSpend) (*types.MsgCommunityPoolSpendResponse, error) {
|
|
if err := k.validateAuthority(msg.Authority); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := validateAmount(msg.Amount); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
recipient, err := k.authKeeper.AddressCodec().StringToBytes(msg.Recipient)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// distribute funds from community pool module account
|
|
if err := k.Keeper.DistributeFromFeePool(ctx, msg.Amount, recipient); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
logger := k.Logger(ctx)
|
|
logger.Info("transferred from the community pool to recipient", "amount", msg.Amount.String(), "recipient", msg.Recipient)
|
|
|
|
return &types.MsgCommunityPoolSpendResponse{}, nil
|
|
}
|
|
|
|
func (k *Keeper) validateAuthority(authority string) error {
|
|
if _, err := k.authKeeper.AddressCodec().StringToBytes(authority); err != nil {
|
|
return sdkerrors.ErrInvalidAddress.Wrapf("invalid authority address: %s", err)
|
|
}
|
|
|
|
if k.authority != authority {
|
|
return errors.Wrapf(types.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, authority)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func validateAmount(amount sdk.Coins) error {
|
|
if amount == nil {
|
|
return errors.Wrap(sdkerrors.ErrInvalidCoins, "amount cannot be nil")
|
|
}
|
|
|
|
if err := amount.Validate(); err != nil {
|
|
return errors.Wrap(sdkerrors.ErrInvalidCoins, amount.String())
|
|
}
|
|
|
|
return nil
|
|
}
|