Skip to content

Commit

Permalink
fix: handle liquid staked coins in sim; update sim ops
Browse files Browse the repository at this point in the history
  • Loading branch information
MSalopek committed Jul 17, 2023
1 parent 0081ec4 commit e81dfc9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 14 additions & 3 deletions types/simulation/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simulation
import (
"fmt"
"math/rand"
"strings"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand Down Expand Up @@ -67,14 +68,24 @@ func FindAccount(accs []Account, address sdk.Address) (Account, bool) {
// amount from the account's available balance. If the user doesn't have enough
// funds for paying fees, it returns empty coins.
func RandomFees(r *rand.Rand, ctx sdk.Context, spendableCoins sdk.Coins) (sdk.Coins, error) {
if spendableCoins.Empty() {
spendable := sdk.NewCoins()
// remove liquid staking denoms from spendable coins since fees cannot be paid in those denoms
valoperPrefix := fmt.Sprintf("%s%s%s", sdk.Bech32MainPrefix, sdk.PrefixValidator, sdk.PrefixOperator)
for _, coin := range spendableCoins {
if strings.Contains(coin.Denom, valoperPrefix) {
continue
}
spendable = append(spendable, coin)
}

if spendable.Empty() {
return nil, nil
}

perm := r.Perm(len(spendableCoins))
perm := r.Perm(len(spendable))
var randCoin sdk.Coin
for _, index := range perm {
randCoin = spendableCoins[index]
randCoin = spendable[index]
if !randCoin.Amount.IsZero() {
break
}
Expand Down
3 changes: 3 additions & 0 deletions x/staking/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ func SimulateMsgTokenizeShares(ak types.AccountKeeper, bk types.BankKeeper, k ke
// check that tokenization would not exceed global cap
params := k.GetParams(ctx)
totalStaked := k.TotalBondedTokens(ctx).ToDec()
if totalStaked.IsZero() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgTokenizeShares, "cannot happend - no validators bonded if stake is 0.0"), nil, nil // skip
}
totalLiquidStaked := k.GetTotalLiquidStakedTokens(ctx).Add(tokenizeShareAmt).ToDec()
liquidStakedPercent := totalLiquidStaked.Quo(totalStaked)
if liquidStakedPercent.GT(params.GlobalLiquidStakingCap) {
Expand Down

0 comments on commit e81dfc9

Please sign in to comment.