github.com/Finschia/finschia-sdk@v0.49.1/x/fswap/types/genesis_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	sdk "github.com/Finschia/finschia-sdk/types"
     9  	authtypes "github.com/Finschia/finschia-sdk/x/auth/types"
    10  	"github.com/Finschia/finschia-sdk/x/foundation"
    11  	"github.com/Finschia/finschia-sdk/x/fswap/types"
    12  	govtypes "github.com/Finschia/finschia-sdk/x/gov/types"
    13  )
    14  
    15  func TestGenesisStateValidate(t *testing.T) {
    16  	exampleGenesis := func() *types.GenesisState {
    17  		testSwapRate, _ := sdk.NewDecFromStr("1234567890")
    18  		return &types.GenesisState{
    19  			Swaps: []types.Swap{
    20  				{
    21  					FromDenom:           "aaa",
    22  					ToDenom:             "bbb",
    23  					AmountCapForToDenom: sdk.NewInt(1234567890000),
    24  					SwapRate:            testSwapRate,
    25  				},
    26  			},
    27  			SwapStats: types.SwapStats{
    28  				SwapCount: 1,
    29  			},
    30  			Swappeds: []types.Swapped{
    31  				{
    32  					FromCoinAmount: sdk.Coin{
    33  						Denom:  "aaa",
    34  						Amount: sdk.ZeroInt(),
    35  					},
    36  					ToCoinAmount: sdk.Coin{
    37  						Denom:  "bbb",
    38  						Amount: sdk.ZeroInt(),
    39  					},
    40  				},
    41  			},
    42  		}
    43  	}
    44  
    45  	for _, tc := range []struct {
    46  		desc     string
    47  		genState *types.GenesisState
    48  		modify   func(*types.GenesisState)
    49  		valid    bool
    50  	}{
    51  		{
    52  			desc:     "default is valid",
    53  			genState: types.DefaultGenesis(),
    54  			valid:    true,
    55  		},
    56  		{
    57  			desc:     "example is valid",
    58  			genState: exampleGenesis(),
    59  			valid:    true,
    60  		},
    61  		{
    62  			desc:     "SwapCount is nagative in SwapStats is invalid",
    63  			genState: exampleGenesis(),
    64  			modify: func(gs *types.GenesisState) {
    65  				gs.SwapStats.SwapCount = -1
    66  			},
    67  			valid: false,
    68  		},
    69  		{
    70  			desc:     "number of swaps does not match number of Swappeds",
    71  			genState: exampleGenesis(),
    72  			modify: func(gs *types.GenesisState) {
    73  				gs.Swaps = append(gs.Swaps, types.Swap{})
    74  			},
    75  			valid: false,
    76  		},
    77  		{
    78  			desc:     "number of swaps does not match number of Swappeds",
    79  			genState: exampleGenesis(),
    80  			modify: func(gs *types.GenesisState) {
    81  				gs.Swaps = append(gs.Swaps, types.Swap{})
    82  				gs.Swappeds = append(gs.Swappeds, types.Swapped{})
    83  			},
    84  			valid: false,
    85  		},
    86  		{
    87  			desc:     "fromDenom=toDenom in Swap is invalid",
    88  			genState: exampleGenesis(),
    89  			modify: func(gs *types.GenesisState) {
    90  				gs.Swaps[0].ToDenom = "aaa"
    91  			},
    92  			valid: false,
    93  		},
    94  		{
    95  			desc:     "AmountCapForToDenom=0 in Swap is invalid",
    96  			genState: exampleGenesis(),
    97  			modify: func(gs *types.GenesisState) {
    98  				gs.Swaps[0].AmountCapForToDenom = sdk.ZeroInt()
    99  			},
   100  			valid: false,
   101  		},
   102  		{
   103  			desc:     "SwapRate=0 in Swap is invalid",
   104  			genState: exampleGenesis(),
   105  			modify: func(gs *types.GenesisState) {
   106  				gs.Swaps[0].SwapRate = sdk.ZeroDec()
   107  			},
   108  			valid: false,
   109  		},
   110  		{
   111  			desc:     "FromCoinAmount is nagative in Swappeds is invalid",
   112  			genState: exampleGenesis(),
   113  			modify: func(gs *types.GenesisState) {
   114  				gs.Swappeds[0].FromCoinAmount.Amount = sdk.NewInt(-1)
   115  			},
   116  			valid: false,
   117  		},
   118  		{
   119  			desc:     "ToCoinAmount is nagative in Swappeds is invalid",
   120  			genState: exampleGenesis(),
   121  			modify: func(gs *types.GenesisState) {
   122  				gs.Swappeds[0].ToCoinAmount.Amount = sdk.NewInt(-1)
   123  			},
   124  			valid: false,
   125  		},
   126  		{
   127  			desc:     "Swap in Swaps is invalid",
   128  			genState: exampleGenesis(),
   129  			modify: func(gs *types.GenesisState) {
   130  				gs.Swaps[0].FromDenom = ""
   131  			},
   132  			valid: false,
   133  		},
   134  		{
   135  			desc:     "Swapped in Swappeds is invalide ",
   136  			genState: exampleGenesis(),
   137  			modify: func(gs *types.GenesisState) {
   138  				gs.Swappeds[0].FromCoinAmount.Denom = ""
   139  			},
   140  			valid: false,
   141  		},
   142  		{
   143  			desc:     "FromCoin in swap and swapped do not correspond",
   144  			genState: exampleGenesis(),
   145  			modify: func(gs *types.GenesisState) {
   146  				gs.Swappeds[0].FromCoinAmount.Denom = "ccc"
   147  			},
   148  			valid: false,
   149  		},
   150  		{
   151  			desc:     "ToCoin in swap and swapped do not correspond",
   152  			genState: exampleGenesis(),
   153  			modify: func(gs *types.GenesisState) {
   154  				gs.Swappeds[0].ToCoinAmount.Denom = "ccc"
   155  			},
   156  			valid: false,
   157  		},
   158  		{
   159  			desc:     "AmountCapForToDenom has been exceeded is invalid",
   160  			genState: exampleGenesis(),
   161  			modify: func(gs *types.GenesisState) {
   162  				gs.Swappeds[0].ToCoinAmount.Amount = sdk.NewInt(12345678900000000)
   163  			},
   164  			valid: false,
   165  		},
   166  	} {
   167  		t.Run(tc.desc, func(t *testing.T) {
   168  			if tc.modify != nil {
   169  				tc.modify(tc.genState)
   170  			}
   171  			err := tc.genState.Validate()
   172  			if tc.valid {
   173  				require.NoError(t, err)
   174  			} else {
   175  				require.Error(t, err)
   176  			}
   177  		})
   178  	}
   179  }
   180  
   181  func TestDefaultAuthority(t *testing.T) {
   182  	expectedDefaultAuthority := authtypes.NewModuleAddress(foundation.ModuleName)
   183  
   184  	defaultAuthority := types.DefaultAuthority()
   185  
   186  	require.Equal(t, expectedDefaultAuthority, defaultAuthority)
   187  }
   188  
   189  func TestAuthorityCandidates(t *testing.T) {
   190  	defaultAuthorityCandidates := []sdk.AccAddress{
   191  		authtypes.NewModuleAddress(govtypes.ModuleName),
   192  		authtypes.NewModuleAddress(foundation.ModuleName),
   193  	}
   194  
   195  	authorityCandidates := types.AuthorityCandidates()
   196  
   197  	require.Equal(t, defaultAuthorityCandidates, authorityCandidates)
   198  }