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

     1  package feegrant_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    10  
    11  	"github.com/Finschia/finschia-sdk/simapp"
    12  	sdk "github.com/Finschia/finschia-sdk/types"
    13  	"github.com/Finschia/finschia-sdk/x/feegrant"
    14  )
    15  
    16  func TestBasicFeeValidAllow(t *testing.T) {
    17  	app := simapp.Setup(false)
    18  
    19  	ctx := app.BaseApp.NewContext(false, tmproto.Header{})
    20  	badTime := ctx.BlockTime().AddDate(0, 0, -1)
    21  	allowace := &feegrant.BasicAllowance{
    22  		Expiration: &badTime,
    23  	}
    24  	require.Error(t, allowace.ValidateBasic())
    25  
    26  	ctx = app.BaseApp.NewContext(false, tmproto.Header{
    27  		Time: time.Now(),
    28  	})
    29  	eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 10))
    30  	atom := sdk.NewCoins(sdk.NewInt64Coin("atom", 555))
    31  	smallAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 43))
    32  	bigAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 1000))
    33  	leftAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 512))
    34  	now := ctx.BlockTime()
    35  	oneHour := now.Add(1 * time.Hour)
    36  
    37  	cases := map[string]struct {
    38  		allowance *feegrant.BasicAllowance
    39  		// all other checks are ignored if valid=false
    40  		fee       sdk.Coins
    41  		blockTime time.Time
    42  		valid     bool
    43  		accept    bool
    44  		remove    bool
    45  		remains   sdk.Coins
    46  	}{
    47  		"empty": {
    48  			allowance: &feegrant.BasicAllowance{},
    49  			accept:    true,
    50  		},
    51  		"small fee without expire": {
    52  			allowance: &feegrant.BasicAllowance{
    53  				SpendLimit: atom,
    54  			},
    55  			fee:     smallAtom,
    56  			accept:  true,
    57  			remove:  false,
    58  			remains: leftAtom,
    59  		},
    60  		"all fee without expire": {
    61  			allowance: &feegrant.BasicAllowance{
    62  				SpendLimit: smallAtom,
    63  			},
    64  			fee:    smallAtom,
    65  			accept: true,
    66  			remove: true,
    67  		},
    68  		"wrong fee": {
    69  			allowance: &feegrant.BasicAllowance{
    70  				SpendLimit: smallAtom,
    71  			},
    72  			fee:    eth,
    73  			accept: false,
    74  		},
    75  		"non-expired": {
    76  			allowance: &feegrant.BasicAllowance{
    77  				SpendLimit: atom,
    78  				Expiration: &oneHour,
    79  			},
    80  			valid:     true,
    81  			fee:       smallAtom,
    82  			blockTime: now,
    83  			accept:    true,
    84  			remove:    false,
    85  			remains:   leftAtom,
    86  		},
    87  		"expired": {
    88  			allowance: &feegrant.BasicAllowance{
    89  				SpendLimit: atom,
    90  				Expiration: &now,
    91  			},
    92  			valid:     true,
    93  			fee:       smallAtom,
    94  			blockTime: oneHour,
    95  			accept:    false,
    96  			remove:    true,
    97  		},
    98  		"fee more than allowed": {
    99  			allowance: &feegrant.BasicAllowance{
   100  				SpendLimit: atom,
   101  				Expiration: &oneHour,
   102  			},
   103  			valid:     true,
   104  			fee:       bigAtom,
   105  			blockTime: now,
   106  			accept:    false,
   107  		},
   108  		"with out spend limit": {
   109  			allowance: &feegrant.BasicAllowance{
   110  				Expiration: &oneHour,
   111  			},
   112  			valid:     true,
   113  			fee:       bigAtom,
   114  			blockTime: now,
   115  			accept:    true,
   116  		},
   117  		"expired no spend limit": {
   118  			allowance: &feegrant.BasicAllowance{
   119  				Expiration: &now,
   120  			},
   121  			valid:     true,
   122  			fee:       bigAtom,
   123  			blockTime: oneHour,
   124  			accept:    false,
   125  		},
   126  	}
   127  	require.Error(t, allowace.ValidateBasic())
   128  
   129  	for name, stc := range cases {
   130  		tc := stc // to make scopelint happy
   131  		t.Run(name, func(t *testing.T) {
   132  			err := tc.allowance.ValidateBasic()
   133  			require.NoError(t, err)
   134  
   135  			ctx := app.BaseApp.NewContext(false, tmproto.Header{}).WithBlockTime(tc.blockTime)
   136  
   137  			// now try to deduct
   138  			removed, err := tc.allowance.Accept(ctx, tc.fee, []sdk.Msg{})
   139  			if !tc.accept {
   140  				require.Error(t, err)
   141  				return
   142  			}
   143  			require.NoError(t, err)
   144  
   145  			require.Equal(t, tc.remove, removed)
   146  			if !removed {
   147  				assert.Equal(t, tc.allowance.SpendLimit, tc.remains)
   148  			}
   149  		})
   150  	}
   151  }