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

     1  package simulation
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  
     7  	sdk "github.com/Finschia/finschia-sdk/types"
     8  	"github.com/Finschia/finschia-sdk/types/module"
     9  	simtypes "github.com/Finschia/finschia-sdk/types/simulation"
    10  	"github.com/Finschia/finschia-sdk/x/feegrant"
    11  )
    12  
    13  // genFeeGrants returns a slice of randomly generated allowances.
    14  func genFeeGrants(r *rand.Rand, accounts []simtypes.Account) []feegrant.Grant {
    15  	allowances := make([]feegrant.Grant, len(accounts)-1)
    16  	for i := 0; i < len(accounts)-1; i++ {
    17  		granter := accounts[i].Address
    18  		grantee := accounts[i+1].Address
    19  		allowances[i] = generateRandomAllowances(granter, grantee, r)
    20  	}
    21  	return allowances
    22  }
    23  
    24  func generateRandomAllowances(granter, grantee sdk.AccAddress, r *rand.Rand) feegrant.Grant {
    25  	allowances := make([]feegrant.Grant, 3)
    26  	spendLimit := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100)))
    27  	periodSpendLimit := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(10)))
    28  
    29  	basic := feegrant.BasicAllowance{
    30  		SpendLimit: spendLimit,
    31  	}
    32  
    33  	basicAllowance, err := feegrant.NewGrant(granter, grantee, &basic)
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	allowances[0] = basicAllowance
    38  
    39  	periodicAllowance, err := feegrant.NewGrant(granter, grantee, &feegrant.PeriodicAllowance{
    40  		Basic:            basic,
    41  		PeriodSpendLimit: periodSpendLimit,
    42  		Period:           time.Hour,
    43  	})
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  	allowances[1] = periodicAllowance
    48  
    49  	filteredAllowance, err := feegrant.NewGrant(granter, grantee, &feegrant.AllowedMsgAllowance{
    50  		Allowance:       basicAllowance.GetAllowance(),
    51  		AllowedMessages: []string{"/cosmos.gov.v1beta1.MsgSubmitProposal"},
    52  	})
    53  	if err != nil {
    54  		panic(err)
    55  	}
    56  	allowances[2] = filteredAllowance
    57  
    58  	return allowances[r.Intn(len(allowances))]
    59  }
    60  
    61  // RandomizedGenState generates a random GenesisState for feegrant
    62  func RandomizedGenState(simState *module.SimulationState) {
    63  	var feegrants []feegrant.Grant
    64  
    65  	simState.AppParams.GetOrGenerate(
    66  		simState.Cdc, "feegrant", &feegrants, simState.Rand,
    67  		func(r *rand.Rand) { feegrants = genFeeGrants(r, simState.Accounts) },
    68  	)
    69  
    70  	feegrantGenesis := feegrant.NewGenesisState(feegrants)
    71  	bz, err := simState.Cdc.MarshalJSON(feegrantGenesis)
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  
    76  	simState.GenState[feegrant.ModuleName] = bz
    77  }