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

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package keeper
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    11  	swaptypes "github.com/fibonacci-chain/fbc/x/ammswap/types"
    12  	"github.com/fibonacci-chain/fbc/x/farm/types"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  type tokenPair struct {
    17  	token0 string
    18  	token1 string
    19  }
    20  
    21  func TestGetLockInfo(t *testing.T) {
    22  	ctx, keeper := GetKeeper(t)
    23  	keeper.swapKeeper.SetParams(ctx, swaptypes.DefaultParams())
    24  	quoteSymbol := types.DefaultParams().QuoteSymbol
    25  	token1Sym, token2Sym, _ := initSwapExchange(ctx, keeper, quoteSymbol)
    26  
    27  	tests := []struct {
    28  		lockedSymbol  string
    29  		lockedValue   sdk.Dec
    30  		isLPT         bool
    31  		expectedValue sdk.Dec
    32  	}{
    33  		{
    34  			lockedSymbol:  quoteSymbol,
    35  			lockedValue:   sdk.NewDec(100),
    36  			isLPT:         false,
    37  			expectedValue: sdk.NewDec(100),
    38  		},
    39  		{
    40  			lockedSymbol:  swaptypes.PoolTokenPrefix + swaptypes.GetSwapTokenPairName(token1Sym, quoteSymbol),
    41  			lockedValue:   sdk.NewDec(100),
    42  			isLPT:         true,
    43  			expectedValue: sdk.NewDec(200),
    44  		},
    45  		{
    46  			lockedSymbol:  swaptypes.PoolTokenPrefix + swaptypes.GetSwapTokenPairName(token1Sym, token2Sym),
    47  			lockedValue:   sdk.NewDec(100),
    48  			isLPT:         true,
    49  			expectedValue: sdk.NewDec(200),
    50  		},
    51  		{
    52  			lockedSymbol:  token2Sym,
    53  			lockedValue:   sdk.NewDec(100),
    54  			isLPT:         false,
    55  			expectedValue: sdk.NewDec(100),
    56  		},
    57  		{
    58  			lockedSymbol:  token2Sym,
    59  			lockedValue:   sdk.ZeroDec(),
    60  			isLPT:         false,
    61  			expectedValue: sdk.ZeroDec(),
    62  		},
    63  	}
    64  
    65  	for _, test := range tests {
    66  		pool := types.FarmPool{
    67  			MinLockAmount:    sdk.NewDecCoinFromDec(test.lockedSymbol, sdk.ZeroDec()),
    68  			TotalValueLocked: sdk.NewDecCoinFromDec(test.lockedSymbol, test.lockedValue),
    69  		}
    70  		if test.isLPT {
    71  			retValue := keeper.calculateLockedLPTValue(ctx, pool, quoteSymbol, swaptypes.DefaultParams())
    72  			require.Equal(t, test.expectedValue, retValue)
    73  		}
    74  		retValue := keeper.GetPoolLockedValue(ctx, pool)
    75  		require.Equal(t, test.expectedValue, retValue)
    76  	}
    77  }
    78  
    79  func initSwapExchange(
    80  	ctx sdk.Context, keeper MockFarmKeeper, quoteSymbol string,
    81  ) (string, string, []tokenPair) {
    82  	token1Symbol := "xxb"
    83  	token2Symbol := "okb"
    84  
    85  	tokenPairs := []tokenPair{
    86  		{token1Symbol, quoteSymbol},
    87  		{token2Symbol, quoteSymbol},
    88  		{token1Symbol, token2Symbol},
    89  	}
    90  
    91  	for _, tokenPair := range tokenPairs {
    92  		tokenPairName := swaptypes.GetSwapTokenPairName(tokenPair.token0, tokenPair.token1)
    93  		exchange := swaptypes.NewSwapPair(tokenPair.token0, tokenPair.token1)
    94  		exchange.QuotePooledCoin.Amount = sdk.NewDec(10000)
    95  		exchange.BasePooledCoin.Amount = sdk.NewDec(10000)
    96  		keeper.swapKeeper.SetSwapTokenPair(ctx, tokenPairName, exchange)
    97  		err := keeper.supplyKeeper.MintCoins(
    98  			ctx, swaptypes.ModuleName,
    99  			sdk.NewDecCoinsFromDec(swaptypes.PoolTokenPrefix+tokenPairName, sdk.NewDec(10000)),
   100  		)
   101  		if err != nil {
   102  			panic("should not happen")
   103  		}
   104  	}
   105  	return token1Symbol, token2Symbol, tokenPairs
   106  }
   107  
   108  func TestIterateAllLockInfos(t *testing.T) {
   109  	ctx, keeper := GetKeeper(t)
   110  	lockInfoList := []types.LockInfo{}
   111  	lockInfoNum := 10
   112  	for i := 0; i < lockInfoNum; i++ {
   113  		keeper.Keeper.SetLockInfo(ctx, types.LockInfo{Owner: Addrs[i], PoolName: fmt.Sprintf("pool%d", i)})
   114  	}
   115  	handler := func(lockInfo types.LockInfo) (stop bool) {
   116  		lockInfoList = append(lockInfoList, lockInfo)
   117  		return false
   118  	}
   119  	keeper.IterateAllLockInfos(ctx, handler)
   120  	require.Equal(t, lockInfoNum, len(lockInfoList))
   121  }
   122  
   123  func TestReadWriteFarmPool(t *testing.T) {
   124  	ctx, keeper := GetKeeper(t)
   125  	poolName := "pool"
   126  	_, found := keeper.Keeper.GetFarmPool(ctx, poolName)
   127  	require.False(t, found)
   128  	pool := types.FarmPool{
   129  		Name: poolName,
   130  	}
   131  	keeper.Keeper.SetFarmPool(ctx, pool)
   132  	_, found = keeper.Keeper.GetFarmPool(ctx, poolName)
   133  	require.True(t, found)
   134  	keeper.Keeper.DeleteFarmPool(ctx, poolName)
   135  	_, found = keeper.Keeper.GetFarmPool(ctx, poolName)
   136  	require.False(t, found)
   137  }