github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/simulation/genesis.go (about)

     1  package simulation
     2  
     3  // DONTCOVER
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"math/rand"
     9  	"strings"
    10  
    11  	sdk "github.com/cosmos/cosmos-sdk/types"
    12  	"github.com/cosmos/cosmos-sdk/types/module"
    13  	"github.com/cosmos/cosmos-sdk/types/simulation"
    14  
    15  	"github.com/gravity-devs/liquidity/x/liquidity/types"
    16  )
    17  
    18  // Simulation parameter constants
    19  const (
    20  	LiquidityPoolTypes     = "liquidity_pool_types"
    21  	MinInitDepositAmount   = "min_init_deposit_amount"
    22  	InitPoolCoinMintAmount = "init_pool_coin_mint_amount"
    23  	MaxReserveCoinAmount   = "max_reserve_coin_amount"
    24  	PoolCreationFee        = "pool_creation_fee"
    25  	SwapFeeRate            = "swap_fee_rate"
    26  	WithdrawFeeRate        = "withdraw_fee_rate"
    27  	MaxOrderAmountRatio    = "max_order_amount_ratio"
    28  	UnitBatchHeight        = "unit_batch_height"
    29  )
    30  
    31  // GenLiquidityPoolTypes return default PoolType temporarily, It will be randomized in the liquidity v2
    32  func GenLiquidityPoolTypes(r *rand.Rand) (liquidityPoolTypes []types.PoolType) {
    33  	return types.DefaultPoolTypes
    34  }
    35  
    36  // GenMinInitDepositAmount randomized MinInitDepositAmount
    37  func GenMinInitDepositAmount(r *rand.Rand) sdk.Int {
    38  	return sdk.NewInt(int64(simulation.RandIntBetween(r, int(types.DefaultMinInitDepositAmount.Int64()), 1e7)))
    39  }
    40  
    41  // GenInitPoolCoinMintAmount randomized InitPoolCoinMintAmount
    42  func GenInitPoolCoinMintAmount(r *rand.Rand) sdk.Int {
    43  	return sdk.NewInt(int64(simulation.RandIntBetween(r, int(types.DefaultInitPoolCoinMintAmount.Int64()), 1e8)))
    44  }
    45  
    46  // GenMaxReserveCoinAmount randomized MaxReserveCoinAmount
    47  func GenMaxReserveCoinAmount(r *rand.Rand) sdk.Int {
    48  	return sdk.NewInt(int64(simulation.RandIntBetween(r, int(types.DefaultMaxReserveCoinAmount.Int64()), 1e13)))
    49  }
    50  
    51  // GenPoolCreationFee randomized PoolCreationFee
    52  // list of 1 to 4 coins with an amount greater than 1
    53  func GenPoolCreationFee(r *rand.Rand) sdk.Coins {
    54  	var coins sdk.Coins
    55  	var denoms []string
    56  
    57  	count := simulation.RandIntBetween(r, 1, 4)
    58  	for i := 0; i < count; i++ {
    59  		randomDenom := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 4, 6))
    60  		denoms = append(denoms, strings.ToLower(randomDenom))
    61  	}
    62  
    63  	sortedDenoms := types.SortDenoms(denoms)
    64  
    65  	for i := 0; i < count; i++ {
    66  		randomCoin := sdk.NewCoin(sortedDenoms[i], sdk.NewInt(int64(simulation.RandIntBetween(r, 1e6, 1e7))))
    67  		coins = append(coins, randomCoin)
    68  	}
    69  
    70  	return coins
    71  }
    72  
    73  // GenSwapFeeRate randomized SwapFeeRate ranging from 0.00001 to 1
    74  func GenSwapFeeRate(r *rand.Rand) sdk.Dec {
    75  	return sdk.NewDecWithPrec(int64(simulation.RandIntBetween(r, 1, 1e5)), 5)
    76  }
    77  
    78  // GenWithdrawFeeRate randomized WithdrawFeeRate ranging from 0.00001 to 1
    79  func GenWithdrawFeeRate(r *rand.Rand) sdk.Dec {
    80  	return sdk.NewDecWithPrec(int64(simulation.RandIntBetween(r, 1, 1e5)), 5)
    81  }
    82  
    83  // GenMaxOrderAmountRatio randomized MaxOrderAmountRatio ranging from 0.00001 to 1
    84  func GenMaxOrderAmountRatio(r *rand.Rand) sdk.Dec {
    85  	return sdk.NewDecWithPrec(int64(simulation.RandIntBetween(r, 1, 1e5)), 5)
    86  }
    87  
    88  // GenUnitBatchHeight randomized UnitBatchHeight ranging from 1 to 20
    89  func GenUnitBatchHeight(r *rand.Rand) uint32 {
    90  	return uint32(simulation.RandIntBetween(r, int(types.DefaultUnitBatchHeight), 20))
    91  }
    92  
    93  // RandomizedGenState generates a random GenesisState for liquidity
    94  func RandomizedGenState(simState *module.SimulationState) {
    95  	var liquidityPoolTypes []types.PoolType
    96  	simState.AppParams.GetOrGenerate(
    97  		simState.Cdc, LiquidityPoolTypes, &liquidityPoolTypes, simState.Rand,
    98  		func(r *rand.Rand) { liquidityPoolTypes = GenLiquidityPoolTypes(r) },
    99  	)
   100  
   101  	var minInitDepositAmount sdk.Int
   102  	simState.AppParams.GetOrGenerate(
   103  		simState.Cdc, MinInitDepositAmount, &minInitDepositAmount, simState.Rand,
   104  		func(r *rand.Rand) { minInitDepositAmount = GenMinInitDepositAmount(r) },
   105  	)
   106  
   107  	var initPoolCoinMintAmount sdk.Int
   108  	simState.AppParams.GetOrGenerate(
   109  		simState.Cdc, InitPoolCoinMintAmount, &initPoolCoinMintAmount, simState.Rand,
   110  		func(r *rand.Rand) { initPoolCoinMintAmount = GenInitPoolCoinMintAmount(r) },
   111  	)
   112  
   113  	var maxReserveCoinAmount sdk.Int
   114  	simState.AppParams.GetOrGenerate(
   115  		simState.Cdc, MaxReserveCoinAmount, &maxReserveCoinAmount, simState.Rand,
   116  		func(r *rand.Rand) { maxReserveCoinAmount = GenMaxReserveCoinAmount(r) },
   117  	)
   118  
   119  	var poolCreationFee sdk.Coins
   120  	simState.AppParams.GetOrGenerate(
   121  		simState.Cdc, PoolCreationFee, &poolCreationFee, simState.Rand,
   122  		func(r *rand.Rand) { poolCreationFee = GenPoolCreationFee(r) },
   123  	)
   124  
   125  	var swapFeeRate sdk.Dec
   126  	simState.AppParams.GetOrGenerate(
   127  		simState.Cdc, SwapFeeRate, &swapFeeRate, simState.Rand,
   128  		func(r *rand.Rand) { swapFeeRate = GenSwapFeeRate(r) },
   129  	)
   130  
   131  	var withdrawFeeRate sdk.Dec
   132  	simState.AppParams.GetOrGenerate(
   133  		simState.Cdc, WithdrawFeeRate, &withdrawFeeRate, simState.Rand,
   134  		func(r *rand.Rand) { withdrawFeeRate = GenWithdrawFeeRate(r) },
   135  	)
   136  
   137  	var maxOrderAmountRatio sdk.Dec
   138  	simState.AppParams.GetOrGenerate(
   139  		simState.Cdc, MaxOrderAmountRatio, &maxOrderAmountRatio, simState.Rand,
   140  		func(r *rand.Rand) { maxOrderAmountRatio = GenMaxOrderAmountRatio(r) },
   141  	)
   142  
   143  	var unitBatchHeight uint32
   144  	simState.AppParams.GetOrGenerate(
   145  		simState.Cdc, UnitBatchHeight, &unitBatchHeight, simState.Rand,
   146  		func(r *rand.Rand) { unitBatchHeight = GenUnitBatchHeight(r) },
   147  	)
   148  
   149  	liquidityGenesis := types.GenesisState{
   150  		Params: types.Params{
   151  			PoolTypes:              liquidityPoolTypes,
   152  			MinInitDepositAmount:   minInitDepositAmount,
   153  			InitPoolCoinMintAmount: initPoolCoinMintAmount,
   154  			MaxReserveCoinAmount:   maxReserveCoinAmount,
   155  			PoolCreationFee:        poolCreationFee,
   156  			SwapFeeRate:            swapFeeRate,
   157  			WithdrawFeeRate:        withdrawFeeRate,
   158  			MaxOrderAmountRatio:    maxOrderAmountRatio,
   159  			UnitBatchHeight:        unitBatchHeight,
   160  		},
   161  		PoolRecords: []types.PoolRecord{},
   162  	}
   163  
   164  	bz, _ := json.MarshalIndent(&liquidityGenesis, "", " ")
   165  	fmt.Printf("Selected randomly generated liquidity parameters:\n%s\n", bz)
   166  	simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&liquidityGenesis)
   167  }