Skip to content

Commit

Permalink
CreatePosition randomize TokenMinAmount0, TokenMinAmount1 (#5374)
Browse files Browse the repository at this point in the history
* calculate min amount by random percent of desired tokens

* fix spell
  • Loading branch information
hieuvubk authored Jun 2, 2023
1 parent 692462a commit e80594e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions x/concentrated-liquidity/simulation/sim_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

appParams "github.com/osmosis-labs/osmosis/v15/app/params"
osmosimtypes "github.com/osmosis-labs/osmosis/v15/simulation/simtypes"
sdkrand "github.com/osmosis-labs/osmosis/v15/simulation/simtypes/random"
clkeeper "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity"
clmodeltypes "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/model"
cltypes "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/types"
Expand Down Expand Up @@ -66,6 +67,11 @@ func RandMsgCreatePosition(k clkeeper.Keeper, sim *osmosimtypes.SimCtx, ctx sdk.
return nil, err
}

token0Desired := tokens.AmountOf(clPool.GetToken0())
token1Desired := tokens.AmountOf(clPool.GetToken1())

tokenMinAmount0, tokenMinAmount1 := RandomMinAmount(sim, token0Desired, token1Desired)

accountBalancePoolDenom0 := sim.BankKeeper().GetBalance(ctx, positionCreator, poolDenoms[0])
accountBalancePoolDenom1 := sim.BankKeeper().GetBalance(ctx, positionCreator, poolDenoms[1])
if accountBalancePoolDenom0.Amount.LT(tokens[0].Amount) || accountBalancePoolDenom1.Amount.LT(tokens[1].Amount) {
Expand All @@ -78,8 +84,8 @@ func RandMsgCreatePosition(k clkeeper.Keeper, sim *osmosimtypes.SimCtx, ctx sdk.
LowerTick: lowerTick,
UpperTick: upperTick,
TokensProvided: tokens,
TokenMinAmount0: sdk.NewInt(0),
TokenMinAmount1: sdk.NewInt(0),
TokenMinAmount0: tokenMinAmount0,
TokenMinAmount1: tokenMinAmount1,
}, nil
}

Expand Down Expand Up @@ -289,6 +295,14 @@ func getRandomTickPositions(sim *osmosimtypes.SimCtx, minTick, maxTick int64, ti
return lowerTick, upperTick, nil
}

func RandomMinAmount(sim *osmosimtypes.SimCtx, token0Desired, token1Desired sdk.Int) (sdk.Int, sdk.Int) {
rand := sim.GetRand()
percent := sdk.NewDec(int64(sdkrand.RandIntBetween(rand, 0, 100) / 100))
minAmount0 := sdk.NewDecFromInt(token0Desired).Mul(percent).TruncateInt()
minAmount1 := sdk.NewDecFromInt(token1Desired).Mul(percent).TruncateInt()
return minAmount0, minAmount1
}

// RandomTickDivisibility calculates a random number between minTick - maxTick (inclusive) that is divisible by tickSpacing
func RandomTickDivisibility(sim *osmosimtypes.SimCtx, minTick int64, maxTick int64, tickSpacing uint64) (int64, error) {
rand := sim.GetRand()
Expand Down

0 comments on commit e80594e

Please sign in to comment.