Skip to content

Commit

Permalink
fix: NoOpMsg types
Browse files Browse the repository at this point in the history
  • Loading branch information
poorphd committed Jul 9, 2024
1 parent fe7b5b8 commit 2887303
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const (
OpWeightMsgRemoveLiquidity = "op_weight_msg_remove_liquidity"
)

var (
TypeMsgSwapOrder = sdk.MsgTypeURL(&types.MsgSwapOrder{})
TypeMsgAddLiquidity = sdk.MsgTypeURL(&types.MsgAddLiquidity{})
TypeMsgRemoveLiquidity = sdk.MsgTypeURL(&types.MsgRemoveLiquidity{})
)

func WeightedOperations(
appParams simtypes.AppParams,
cdc codec.JSONCodec,
Expand Down Expand Up @@ -99,31 +105,31 @@ func SimulateMsgAddLiquidity(k keeper.Keeper, ak types.AccountKeeper, bk types.B
exactStandardAmt := simtypes.RandomAmount(r, spendable.AmountOf(standardDenom))

if !exactStandardAmt.IsPositive() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddLiquidity, "standardAmount should be positive"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "standardAmount should be positive"), nil, nil
}

maxToken = RandomSpendableToken(r, spendable)
if maxToken.Denom == standardDenom {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddLiquidity, "tokenDenom should not be standardDenom"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "tokenDenom should not be standardDenom"), nil, err
}

_, err = k.GetMaximumSwapAmount(ctx, maxToken.Denom)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddLiquidity, err.Error()), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, err.Error()), nil, nil
}

if strings.HasPrefix(maxToken.Denom, types.LptTokenPrefix) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddLiquidity, "tokenDenom should not be liquidity token"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "tokenDenom should not be liquidity token"), nil, err
}

if !maxToken.Amount.IsPositive() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddLiquidity, "maxToken must is positive"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "maxToken must is positive"), nil, err
}

poolId := types.GetPoolId(maxToken.Denom)
pool, has := k.GetPool(ctx, poolId)
if has {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddLiquidity, "pool not found"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "pool not found"), nil, err
}

reservePool, err := k.GetPoolBalances(ctx, pool.EscrowAddress)
Expand All @@ -136,7 +142,7 @@ func SimulateMsgAddLiquidity(k keeper.Keeper, ak types.AccountKeeper, bk types.B
minLiquidity = liquidity.Mul(exactStandardAmt).Quo(standardReserveAmt)

if !maxToken.Amount.Sub(reservePool.AmountOf(maxToken.GetDenom()).Mul(exactStandardAmt).Quo(standardReserveAmt)).IsPositive() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddLiquidity, "insufficient funds"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "insufficient funds"), nil, err
}

params := k.GetParams(ctx)
Expand All @@ -147,7 +153,7 @@ func SimulateMsgAddLiquidity(k keeper.Keeper, ak types.AccountKeeper, bk types.B
spendTotal = spendTotal.Add(exactStandardAmt)
}
if spendable.AmountOf(poolCreationFee.Denom).LT(spendTotal) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddLiquidity, "insufficient funds"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "insufficient funds"), nil, err
}
}

Expand All @@ -165,7 +171,7 @@ func SimulateMsgAddLiquidity(k keeper.Keeper, ak types.AccountKeeper, bk types.B
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coinsTemp)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "unable to generate fees"), nil, nil
}
}

Expand All @@ -184,11 +190,11 @@ func SimulateMsgAddLiquidity(k keeper.Keeper, ak types.AccountKeeper, bk types.B
)

if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "unable to generate mock tx"), nil, err
}

if _, _, err := app.SimDeliver(txGen.TxEncoder(), tx); err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgAddLiquidity, "unable to deliver tx"), nil, err
}

return simtypes.NewOperationMsg(msg, true, ""), nil, nil
Expand All @@ -214,33 +220,33 @@ func SimulateMsgSwapOrder(k keeper.Keeper, ak types.AccountKeeper, bk types.Bank
standardDenom, _ := k.GetStandardDenom(ctx)

if spendable.IsZero() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "spendable is zero"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "spendable is zero"), nil, err
}

// sold coin
inputCoin = RandomSpendableToken(r, spendable)

_, err = k.GetMaximumSwapAmount(ctx, inputCoin.Denom)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, err.Error()), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, err.Error()), nil, nil
}

if strings.HasPrefix(inputCoin.Denom, types.LptTokenPrefix) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "inputCoin should not be liquidity token"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "inputCoin should not be liquidity token"), nil, err
}

if !inputCoin.Amount.IsPositive() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "inputCoin must is positive"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "inputCoin must is positive"), nil, err
}

poolId := types.GetPoolId(inputCoin.Denom)
pool, has := k.GetPool(ctx, poolId)
if !has {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
}

if _, err := k.GetPoolBalancesByLptDenom(ctx, pool.LptDenom); err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
}

// bought coin
Expand All @@ -250,34 +256,34 @@ func SimulateMsgSwapOrder(k keeper.Keeper, ak types.AccountKeeper, bk types.Bank
return false
})
if coins.IsZero() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "total supply is zero"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "total supply is zero"), nil, err
}
outputCoin = RandomTotalToken(r, coins)
_, err = k.GetMaximumSwapAmount(ctx, inputCoin.Denom)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, err.Error()), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, err.Error()), nil, nil
}

if strings.HasPrefix(outputCoin.Denom, types.LptTokenPrefix) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "outputCoin should not be liquidity token"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "outputCoin should not be liquidity token"), nil, err
}

if !outputCoin.Amount.IsPositive() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "outputCoin must is positive"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "outputCoin must is positive"), nil, err
}

poolId = types.GetPoolId(outputCoin.Denom)
pool, has = k.GetPool(ctx, poolId)
if !has {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
}

if _, err := k.GetPoolBalancesByLptDenom(ctx, pool.LptDenom); err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
}

if outputCoin.Denom == inputCoin.Denom {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "outputCoin denom and inputcoin denom should not be the same"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "outputCoin denom and inputcoin denom should not be the same"), nil, nil
}

isDoubleSwap := (outputCoin.Denom != standardDenom) && (inputCoin.Denom != standardDenom)
Expand All @@ -286,26 +292,26 @@ func SimulateMsgSwapOrder(k keeper.Keeper, ak types.AccountKeeper, bk types.Bank
if isBuyOrder && isDoubleSwap {
inputCoin, outputCoin, err = doubleSwapBill(inputCoin, outputCoin, ctx, k)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, err.Error()), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, err.Error()), nil, nil
}
} else if isBuyOrder && !isDoubleSwap {
inputCoin, outputCoin, err = singleSwapBill(inputCoin, outputCoin, ctx, k)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, err.Error()), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, err.Error()), nil, nil
}
} else if !isBuyOrder && isDoubleSwap {
inputCoin, outputCoin, err = doubleSwapSellOrder(inputCoin, outputCoin, ctx, k)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, err.Error()), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, err.Error()), nil, nil
}
} else if !isBuyOrder && !isDoubleSwap {
inputCoin, outputCoin, err = singleSwapSellOrder(inputCoin, outputCoin, ctx, k)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, err.Error()), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, err.Error()), nil, nil
}
}
if !outputCoin.Amount.IsPositive() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "outputCoin must is positive"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "outputCoin must is positive"), nil, err
}

deadline := randDeadline(r)
Expand All @@ -327,7 +333,7 @@ func SimulateMsgSwapOrder(k keeper.Keeper, ak types.AccountKeeper, bk types.Bank
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coinsTemp)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "unable to generate fees"), nil, nil
}
}

Expand All @@ -345,11 +351,11 @@ func SimulateMsgSwapOrder(k keeper.Keeper, ak types.AccountKeeper, bk types.Bank
)

if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "unable to generate mock tx"), nil, err
}

if _, _, err := app.SimDeliver(txGen.TxEncoder(), tx); err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgSwapOrder, "unable to deliver tx"), nil, err
}

return simtypes.NewOperationMsg(msg, true, ""), nil, nil
Expand All @@ -375,23 +381,23 @@ func SimulateMsgRemoveLiquidity(k keeper.Keeper, ak types.AccountKeeper, bk type

spendable := bk.SpendableCoins(ctx, account.GetAddress())
if spendable.IsZero() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "spendable is zero"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "spendable is zero"), nil, err
}

token := RandomSpendableToken(r, spendable)

if token.Denom == standardDenom {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgRemoveLiquidity, "tokenDenom should not be standardDenom"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "tokenDenom should not be standardDenom"), nil, err
}

pool, has := k.GetPoolByLptDenom(ctx, token.Denom)
if !has {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "inputCoin should exist in the pool"), nil, nil
}

reservePool, err := k.GetPoolBalancesByLptDenom(ctx, pool.LptDenom)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSwapOrder, "inputCoin should exist in the pool"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "inputCoin should exist in the pool"), nil, nil
}

standardReserveAmt := reservePool.AmountOf(standardDenom)
Expand All @@ -401,20 +407,20 @@ func SimulateMsgRemoveLiquidity(k keeper.Keeper, ak types.AccountKeeper, bk type
liquidityReserve := bk.GetSupply(ctx, token.Denom).Amount

if !withdrawLiquidity.IsValid() || !withdrawLiquidity.IsPositive() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgRemoveLiquidity, "invalid withdrawLiquidity"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "invalid withdrawLiquidity"), nil, nil
}
if liquidityReserve.LT(withdrawLiquidity.Amount) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgRemoveLiquidity, "insufficient funds"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "insufficient funds"), nil, nil
}

minToken = withdrawLiquidity.Amount.Mul(tokenReserveAmt).Quo(liquidityReserve)
if tokenReserveAmt.LT(minToken) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgRemoveLiquidity, "insufficient funds"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "insufficient funds"), nil, nil
}

minStandardAmt = withdrawLiquidity.Amount.Mul(standardReserveAmt).Quo(liquidityReserve)
if standardReserveAmt.LT(minStandardAmt) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgRemoveLiquidity, "insufficient funds"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "insufficient funds"), nil, nil
}

deadline := randDeadline(r)
Expand All @@ -431,7 +437,7 @@ func SimulateMsgRemoveLiquidity(k keeper.Keeper, ak types.AccountKeeper, bk type
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coinsTemp)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate fees"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "unable to generate fees"), nil, nil
}
}

Expand All @@ -450,11 +456,11 @@ func SimulateMsgRemoveLiquidity(k keeper.Keeper, ak types.AccountKeeper, bk type
)

if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "unable to generate mock tx"), nil, err
}

if _, _, err := app.SimDeliver(txGen.TxEncoder(), tx); err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, TypeMsgRemoveLiquidity, "unable to deliver tx"), nil, nil
}

return simtypes.NewOperationMsg(msg, true, ""), nil, nil
Expand Down
25 changes: 0 additions & 25 deletions x/coinswap/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ const (
LptTokenPrefix = "lpt"
// LptTokenFormat defines the name of liquidity token
LptTokenFormat = "lpt-%d"

// TypeMsgAddLiquidity defines the type of MsgAddLiquidity
TypeMsgAddLiquidity = "add_liquidity"
// TypeMsgRemoveLiquidity defines the type of MsgRemoveLiquidity
TypeMsgRemoveLiquidity = "remove_liquidity"
// TypeMsgSwapOrder defines the type of MsgSwapOrder
TypeMsgSwapOrder = "swap_order"
)

/* --------------------------------------------------------------------------- */
Expand Down Expand Up @@ -52,12 +45,6 @@ func NewMsgSwapOrder(
}
}

// Route implements Msg.
func (msg MsgSwapOrder) Route() string { return RouterKey }

// Type implements Msg.
func (msg MsgSwapOrder) Type() string { return TypeMsgSwapOrder }

// ValidateBasic implements Msg.
func (msg MsgSwapOrder) ValidateBasic() error {
if err := ValidateInput(msg.Input); err != nil {
Expand Down Expand Up @@ -110,12 +97,6 @@ func NewMsgAddLiquidity(
}
}

// Route implements Msg.
func (msg MsgAddLiquidity) Route() string { return RouterKey }

// Type implements Msg.
func (msg MsgAddLiquidity) Type() string { return TypeMsgAddLiquidity }

// ValidateBasic implements Msg.
func (msg MsgAddLiquidity) ValidateBasic() error {
if err := ValidateMaxToken(msg.MaxToken); err != nil {
Expand Down Expand Up @@ -175,12 +156,6 @@ func NewMsgRemoveLiquidity(
}
}

// Route implements Msg.
func (msg MsgRemoveLiquidity) Route() string { return RouterKey }

// Type implements Msg.
func (msg MsgRemoveLiquidity) Type() string { return TypeMsgRemoveLiquidity }

// ValidateBasic implements Msg.
func (msg MsgRemoveLiquidity) ValidateBasic() error {
if err := ValidateMinToken(msg.MinToken); err != nil {
Expand Down
Loading

0 comments on commit 2887303

Please sign in to comment.