github.com/Finschia/finschia-sdk@v0.49.1/x/feegrant/basic_fee.go (about)

     1  package feegrant
     2  
     3  import (
     4  	sdk "github.com/Finschia/finschia-sdk/types"
     5  	sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
     6  )
     7  
     8  var _ FeeAllowanceI = (*BasicAllowance)(nil)
     9  
    10  // Accept can use fee payment requested as well as timestamp of the current block
    11  // to determine whether or not to process this. This is checked in
    12  // Keeper.UseGrantedFees and the return values should match how it is handled there.
    13  //
    14  // If it returns an error, the fee payment is rejected, otherwise it is accepted.
    15  // The FeeAllowance implementation is expected to update it's internal state
    16  // and will be saved again after an acceptance.
    17  //
    18  // If remove is true (regardless of the error), the FeeAllowance will be deleted from storage
    19  // (eg. when it is used up). (See call to RevokeAllowance in Keeper.UseGrantedFees)
    20  func (a *BasicAllowance) Accept(ctx sdk.Context, fee sdk.Coins, _ []sdk.Msg) (bool, error) {
    21  	if a.Expiration != nil && a.Expiration.Before(ctx.BlockTime()) {
    22  		return true, sdkerrors.Wrap(ErrFeeLimitExpired, "basic allowance")
    23  	}
    24  
    25  	if a.SpendLimit != nil {
    26  		left, invalid := a.SpendLimit.SafeSub(fee)
    27  		if invalid {
    28  			return false, sdkerrors.Wrap(ErrFeeLimitExceeded, "basic allowance")
    29  		}
    30  
    31  		a.SpendLimit = left
    32  		return left.IsZero(), nil
    33  	}
    34  
    35  	return false, nil
    36  }
    37  
    38  // ValidateBasic implements FeeAllowance and enforces basic sanity checks
    39  func (a BasicAllowance) ValidateBasic() error {
    40  	if a.SpendLimit != nil {
    41  		if !a.SpendLimit.IsValid() {
    42  			return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "send amount is invalid: %s", a.SpendLimit)
    43  		}
    44  		if !a.SpendLimit.IsAllPositive() {
    45  			return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "spend limit must be positive")
    46  		}
    47  	}
    48  
    49  	if a.Expiration != nil && a.Expiration.Unix() < 0 {
    50  		return sdkerrors.Wrap(ErrInvalidDuration, "expiration time cannot be negative")
    51  	}
    52  
    53  	return nil
    54  }