github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/feesplit/keeper/fee_splits_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	"github.com/fibonacci-chain/fbc/app/crypto/ethsecp256k1"
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	"github.com/fibonacci-chain/fbc/x/feesplit/types"
    10  )
    11  
    12  func (suite *KeeperTestSuite) TestGetFees() {
    13  	var expRes []types.FeeSplit
    14  
    15  	testCases := []struct {
    16  		name     string
    17  		malleate func()
    18  	}{
    19  		{
    20  			"no fee splits registered",
    21  			func() { expRes = []types.FeeSplit{} },
    22  		},
    23  		{
    24  			"one fee split registered with withdraw address",
    25  			func() {
    26  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
    27  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
    28  				expRes = []types.FeeSplit{feeSplit}
    29  			},
    30  		},
    31  		{
    32  			"one fee split registered with no withdraw address",
    33  			func() {
    34  				feeSplit := types.NewFeeSplit(contract, deployer, nil)
    35  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
    36  				expRes = []types.FeeSplit{feeSplit}
    37  			},
    38  		},
    39  		{
    40  			"multiple fee splits registered",
    41  			func() {
    42  				deployer2 := sdk.AccAddress(ethsecp256k1.GenerateAddress().Bytes())
    43  				contract2 := ethsecp256k1.GenerateAddress()
    44  				contract3 := ethsecp256k1.GenerateAddress()
    45  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
    46  				feeSplit2 := types.NewFeeSplit(contract2, deployer, nil)
    47  				feeSplit3 := types.NewFeeSplit(contract3, deployer2, nil)
    48  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
    49  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit2)
    50  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit3)
    51  				expRes = []types.FeeSplit{feeSplit, feeSplit2, feeSplit3}
    52  			},
    53  		},
    54  	}
    55  	for _, tc := range testCases {
    56  		suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
    57  			suite.SetupTest() // reset
    58  			tc.malleate()
    59  
    60  			res := suite.app.FeeSplitKeeper.GetFeeSplits(suite.ctx)
    61  			suite.Require().ElementsMatch(expRes, res, tc.name)
    62  		})
    63  	}
    64  }
    65  
    66  func (suite *KeeperTestSuite) TestIterateFees() {
    67  	var expRes []types.FeeSplit
    68  
    69  	testCases := []struct {
    70  		name     string
    71  		malleate func()
    72  	}{
    73  		{
    74  			"no fee splits registered",
    75  			func() { expRes = []types.FeeSplit{} },
    76  		},
    77  		{
    78  			"one fee split registered with withdraw address",
    79  			func() {
    80  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
    81  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
    82  				expRes = []types.FeeSplit{
    83  					types.NewFeeSplit(contract, deployer, withdraw),
    84  				}
    85  			},
    86  		},
    87  		{
    88  			"one fee split registered with no withdraw address",
    89  			func() {
    90  				feeSplit := types.NewFeeSplit(contract, deployer, nil)
    91  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
    92  				expRes = []types.FeeSplit{
    93  					types.NewFeeSplit(contract, deployer, nil),
    94  				}
    95  			},
    96  		},
    97  		{
    98  			"multiple fee splits registered",
    99  			func() {
   100  				deployer2 := sdk.AccAddress(ethsecp256k1.GenerateAddress().Bytes())
   101  				contract2 := ethsecp256k1.GenerateAddress()
   102  				contract3 := ethsecp256k1.GenerateAddress()
   103  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
   104  				feeSplit2 := types.NewFeeSplit(contract2, deployer, nil)
   105  				feeSplit3 := types.NewFeeSplit(contract3, deployer2, nil)
   106  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
   107  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit2)
   108  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit3)
   109  				expRes = []types.FeeSplit{feeSplit, feeSplit2, feeSplit3}
   110  			},
   111  		},
   112  	}
   113  	for _, tc := range testCases {
   114  		suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
   115  			suite.SetupTest() // reset
   116  			tc.malleate()
   117  
   118  			suite.app.FeeSplitKeeper.IterateFeeSplits(suite.ctx, func(feeSplit types.FeeSplit) (stop bool) {
   119  				suite.Require().Contains(expRes, feeSplit, tc.name)
   120  				return false
   121  			})
   122  		})
   123  	}
   124  }
   125  
   126  func (suite *KeeperTestSuite) TestGetFeeSplit() {
   127  	testCases := []struct {
   128  		name        string
   129  		contract    common.Address
   130  		deployer    sdk.AccAddress
   131  		withdraw    sdk.AccAddress
   132  		found       bool
   133  		expWithdraw bool
   134  	}{
   135  		{
   136  			"fee with no withdraw address",
   137  			contract,
   138  			deployer,
   139  			nil,
   140  			true,
   141  			false,
   142  		},
   143  		{
   144  			"fee with withdraw address same as deployer",
   145  			contract,
   146  			deployer,
   147  			deployer,
   148  			true,
   149  			false,
   150  		},
   151  		{
   152  			"fee with withdraw address same as contract",
   153  			contract,
   154  			deployer,
   155  			sdk.AccAddress(contract.Bytes()),
   156  			true,
   157  			true,
   158  		},
   159  		{
   160  			"fee with withdraw address different than deployer",
   161  			contract,
   162  			deployer,
   163  			withdraw,
   164  			true,
   165  			true,
   166  		},
   167  		{
   168  			"no fee",
   169  			common.Address{},
   170  			nil,
   171  			nil,
   172  			false,
   173  			false,
   174  		},
   175  	}
   176  	for _, tc := range testCases {
   177  		suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
   178  			suite.SetupTest() // reset
   179  
   180  			if tc.found {
   181  				feeSplit := types.NewFeeSplit(tc.contract, tc.deployer, tc.withdraw)
   182  				if tc.deployer.Equals(tc.withdraw) {
   183  					feeSplit.WithdrawerAddress = nil
   184  				}
   185  
   186  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
   187  				suite.app.FeeSplitKeeper.SetDeployerMap(suite.ctx, tc.deployer, tc.contract)
   188  			}
   189  
   190  			if tc.expWithdraw {
   191  				suite.app.FeeSplitKeeper.SetWithdrawerMap(suite.ctx, tc.withdraw, tc.contract)
   192  			}
   193  
   194  			feeSplit, found := suite.app.FeeSplitKeeper.GetFeeSplit(suite.ctx, tc.contract)
   195  			foundD := suite.app.FeeSplitKeeper.IsDeployerMapSet(suite.ctx, tc.deployer, tc.contract)
   196  			foundW := suite.app.FeeSplitKeeper.IsWithdrawerMapSet(suite.ctx, tc.withdraw, tc.contract)
   197  
   198  			if tc.found {
   199  				suite.Require().True(found, tc.name)
   200  				suite.Require().Equal(tc.deployer, feeSplit.DeployerAddress, tc.name)
   201  				suite.Require().Equal(tc.contract, feeSplit.ContractAddress, tc.name)
   202  
   203  				suite.Require().True(foundD, tc.name)
   204  
   205  				if tc.expWithdraw {
   206  					suite.Require().Equal(tc.withdraw, feeSplit.WithdrawerAddress, tc.name)
   207  					suite.Require().True(foundW, tc.name)
   208  				} else {
   209  					suite.Require().Equal(tc.deployer, feeSplit.WithdrawerAddress, tc.name)
   210  					suite.Require().False(foundW, tc.name)
   211  				}
   212  			} else {
   213  				suite.Require().False(found, tc.name)
   214  			}
   215  		})
   216  	}
   217  }
   218  
   219  func (suite *KeeperTestSuite) TestDeleteFeeSplit() {
   220  	feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
   221  	suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
   222  
   223  	initialFee, found := suite.app.FeeSplitKeeper.GetFeeSplit(suite.ctx, contract)
   224  	suite.Require().True(found)
   225  
   226  	testCases := []struct {
   227  		name     string
   228  		malleate func()
   229  		ok       bool
   230  	}{
   231  		{"existing fee split", func() {}, true},
   232  		{
   233  			"deleted fee split",
   234  			func() {
   235  				suite.app.FeeSplitKeeper.DeleteFeeSplit(suite.ctx, feeSplit)
   236  			},
   237  			false,
   238  		},
   239  	}
   240  	for _, tc := range testCases {
   241  		tc.malleate()
   242  		feeSplit, found := suite.app.FeeSplitKeeper.GetFeeSplit(suite.ctx, contract)
   243  		if tc.ok {
   244  			suite.Require().True(found, tc.name)
   245  			suite.Require().Equal(initialFee, feeSplit, tc.name)
   246  		} else {
   247  			suite.Require().False(found, tc.name)
   248  			suite.Require().Equal(types.FeeSplit{}, feeSplit, tc.name)
   249  		}
   250  	}
   251  }
   252  
   253  func (suite *KeeperTestSuite) TestDeleteDeployerMap() {
   254  	suite.app.FeeSplitKeeper.SetDeployerMap(suite.ctx, deployer, contract)
   255  	found := suite.app.FeeSplitKeeper.IsDeployerMapSet(suite.ctx, deployer, contract)
   256  	suite.Require().True(found)
   257  
   258  	testCases := []struct {
   259  		name     string
   260  		malleate func()
   261  		ok       bool
   262  	}{
   263  		{"existing deployer", func() {}, true},
   264  		{
   265  			"deleted deployer",
   266  			func() {
   267  				suite.app.FeeSplitKeeper.DeleteDeployerMap(suite.ctx, deployer, contract)
   268  			},
   269  			false,
   270  		},
   271  	}
   272  	for _, tc := range testCases {
   273  		tc.malleate()
   274  		found := suite.app.FeeSplitKeeper.IsDeployerMapSet(suite.ctx, deployer, contract)
   275  		if tc.ok {
   276  			suite.Require().True(found, tc.name)
   277  		} else {
   278  			suite.Require().False(found, tc.name)
   279  		}
   280  	}
   281  }
   282  
   283  func (suite *KeeperTestSuite) TestDeleteWithdrawMap() {
   284  	suite.app.FeeSplitKeeper.SetWithdrawerMap(suite.ctx, withdraw, contract)
   285  	found := suite.app.FeeSplitKeeper.IsWithdrawerMapSet(suite.ctx, withdraw, contract)
   286  	suite.Require().True(found)
   287  
   288  	testCases := []struct {
   289  		name     string
   290  		malleate func()
   291  		ok       bool
   292  	}{
   293  		{"existing withdraw", func() {}, true},
   294  		{
   295  			"deleted withdraw",
   296  			func() {
   297  				suite.app.FeeSplitKeeper.DeleteWithdrawerMap(suite.ctx, withdraw, contract)
   298  			},
   299  			false,
   300  		},
   301  	}
   302  	for _, tc := range testCases {
   303  		tc.malleate()
   304  		found := suite.app.FeeSplitKeeper.IsWithdrawerMapSet(suite.ctx, withdraw, contract)
   305  		if tc.ok {
   306  			suite.Require().True(found, tc.name)
   307  		} else {
   308  			suite.Require().False(found, tc.name)
   309  		}
   310  	}
   311  }
   312  
   313  func (suite *KeeperTestSuite) TestIsFeeSplitRegistered() {
   314  	feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
   315  	suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
   316  	_, found := suite.app.FeeSplitKeeper.GetFeeSplit(suite.ctx, contract)
   317  	suite.Require().True(found)
   318  
   319  	testCases := []struct {
   320  		name     string
   321  		contract common.Address
   322  		ok       bool
   323  	}{
   324  		{"registered fee split", contract, true},
   325  		{"fee split not registered", common.Address{}, false},
   326  		{"fee split not registered", ethsecp256k1.GenerateAddress(), false},
   327  	}
   328  	for _, tc := range testCases {
   329  		found := suite.app.FeeSplitKeeper.IsFeeSplitRegistered(suite.ctx, tc.contract)
   330  		if tc.ok {
   331  			suite.Require().True(found, tc.name)
   332  		} else {
   333  			suite.Require().False(found, tc.name)
   334  		}
   335  	}
   336  }