52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package types
|
|
|
|
import (
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/x/authz"
|
|
)
|
|
|
|
var (
|
|
_ authz.Authorization = &SendAuthorization{}
|
|
)
|
|
|
|
// NewSendAuthorization creates a new SendAuthorization object.
|
|
func NewSendAuthorization(spendLimit sdk.Coins) *SendAuthorization {
|
|
return &SendAuthorization{
|
|
SpendLimit: spendLimit,
|
|
}
|
|
}
|
|
|
|
// MsgTypeURL implements Authorization.MsgTypeURL.
|
|
func (a SendAuthorization) MsgTypeURL() string {
|
|
return sdk.MsgTypeURL(&MsgSend{})
|
|
}
|
|
|
|
// Accept implements Authorization.Accept.
|
|
func (a SendAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
|
|
mSend, ok := msg.(*MsgSend)
|
|
if !ok {
|
|
return authz.AcceptResponse{}, sdkerrors.ErrInvalidType.Wrap("type mismatch")
|
|
}
|
|
limitLeft, isNegative := a.SpendLimit.SafeSub(mSend.Amount)
|
|
if isNegative {
|
|
return authz.AcceptResponse{}, sdkerrors.ErrInsufficientFunds.Wrapf("requested amount is more than spend limit")
|
|
}
|
|
if limitLeft.IsZero() {
|
|
return authz.AcceptResponse{Accept: true, Delete: true}, nil
|
|
}
|
|
|
|
return authz.AcceptResponse{Accept: true, Delete: false, Updated: &SendAuthorization{SpendLimit: limitLeft}}, nil
|
|
}
|
|
|
|
// ValidateBasic implements Authorization.ValidateBasic.
|
|
func (a SendAuthorization) ValidateBasic() error {
|
|
if a.SpendLimit == nil {
|
|
return sdkerrors.ErrInvalidCoins.Wrap("spend limit cannot be nil")
|
|
}
|
|
if !a.SpendLimit.IsAllPositive() {
|
|
return sdkerrors.ErrInvalidCoins.Wrapf("spend limit cannot be negitive")
|
|
}
|
|
return nil
|
|
}
|