diff --git a/types/utils.go b/types/utils.go index 4a8d9f23..5dfaadbf 100644 --- a/types/utils.go +++ b/types/utils.go @@ -3,6 +3,8 @@ package types import ( "encoding/json" "fmt" + "math/big" + "math/rand" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -89,3 +91,13 @@ func DecApproxEqual(a, b sdk.Dec) bool { } return a.Sub(b).Quo(a).LTE(sdk.NewDecWithPrec(1, 3)) } + +// RandomInt returns a random integer in the half-open interval [min, max). +func RandomInt(r *rand.Rand, min, max sdk.Int) sdk.Int { + return min.Add(sdk.NewIntFromBigInt(new(big.Int).Rand(r, max.Sub(min).BigInt()))) +} + +// RandomDec returns a random decimal in the half-open interval [min, max). +func RandomDec(r *rand.Rand, min, max sdk.Dec) sdk.Dec { + return min.Add(sdk.NewDecFromBigIntWithPrec(new(big.Int).Rand(r, max.Sub(min).BigInt()), sdk.Precision)) +} diff --git a/x/liquidity/amm/match_bench_test.go b/x/liquidity/amm/match_bench_test.go index 6b333727..228b70c6 100644 --- a/x/liquidity/amm/match_bench_test.go +++ b/x/liquidity/amm/match_bench_test.go @@ -2,7 +2,6 @@ package amm_test import ( "fmt" - "math/big" "math/rand" "testing" @@ -12,14 +11,6 @@ import ( "github.com/cosmosquad-labs/squad/x/liquidity/amm" ) -func randInt(r *rand.Rand, min, max sdk.Int) sdk.Int { - return min.Add(sdk.NewIntFromBigInt(new(big.Int).Rand(r, max.Sub(min).BigInt()))) -} - -func randDec(r *rand.Rand, min, max sdk.Dec) sdk.Dec { - return min.Add(sdk.NewDecFromBigIntWithPrec(new(big.Int).Rand(r, max.Sub(min).BigInt()), sdk.Precision)) -} - func BenchmarkFindMatchPrice(b *testing.B) { minPrice, maxPrice := squad.ParseDec("0.0000001"), squad.ParseDec("10000000") minAmt, maxAmt := sdk.NewInt(100), sdk.NewInt(10000000) @@ -30,12 +21,12 @@ func BenchmarkFindMatchPrice(b *testing.B) { r := rand.New(rand.NewSource(seed)) ob := amm.NewOrderBook() for i := 0; i < 10000; i++ { - ob.Add(newOrder(amm.Buy, randDec(r, minPrice, maxPrice), randInt(r, minAmt, maxAmt))) - ob.Add(newOrder(amm.Sell, randDec(r, minPrice, maxPrice), randInt(r, minAmt, maxAmt))) + ob.Add(newOrder(amm.Buy, squad.RandomDec(r, minPrice, maxPrice), squad.RandomInt(r, minAmt, maxAmt))) + ob.Add(newOrder(amm.Sell, squad.RandomDec(r, minPrice, maxPrice), squad.RandomInt(r, minAmt, maxAmt))) } var poolOrderSources []amm.OrderSource for i := 0; i < 1000; i++ { - rx, ry := randInt(r, minReserveAmt, maxReserveAmt), randInt(r, minReserveAmt, maxReserveAmt) + rx, ry := squad.RandomInt(r, minReserveAmt, maxReserveAmt), squad.RandomInt(r, minReserveAmt, maxReserveAmt) pool := amm.NewBasicPool(rx, ry, sdk.ZeroInt()) poolOrderSources = append(poolOrderSources, amm.NewMockPoolOrderSource(pool, "denom1", "denom2")) } diff --git a/x/liquidity/simulation/operations.go b/x/liquidity/simulation/operations.go index 5ede2a72..5d0620c1 100644 --- a/x/liquidity/simulation/operations.go +++ b/x/liquidity/simulation/operations.go @@ -13,6 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/simulation" squadappparams "github.com/cosmosquad-labs/squad/app/params" + squad "github.com/cosmosquad-labs/squad/types" "github.com/cosmosquad-labs/squad/x/liquidity/amm" "github.com/cosmosquad-labs/squad/x/liquidity/keeper" "github.com/cosmosquad-labs/squad/x/liquidity/types" @@ -194,11 +195,11 @@ func SimulateMsgCreatePool(ak types.AccountKeeper, bk types.BankKeeper, k keeper depositCoins := sdk.NewCoins( sdk.NewCoin( pair.BaseCoinDenom, - randomInt(r, minDepositAmt, spendable.Sub(params.PoolCreationFee).AmountOf(pair.BaseCoinDenom)), + squad.RandomInt(r, minDepositAmt, spendable.Sub(params.PoolCreationFee).AmountOf(pair.BaseCoinDenom)), ), sdk.NewCoin( pair.QuoteCoinDenom, - randomInt(r, minDepositAmt, spendable.Sub(params.PoolCreationFee).AmountOf(pair.QuoteCoinDenom)), + squad.RandomInt(r, minDepositAmt, spendable.Sub(params.PoolCreationFee).AmountOf(pair.QuoteCoinDenom)), ), ) @@ -249,10 +250,10 @@ func SimulateMsgDeposit(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Ke depositCoins = sdk.NewCoins( sdk.NewCoin( pair.BaseCoinDenom, - randomInt(r, sdk.OneInt(), spendable.AmountOf(pair.BaseCoinDenom))), + squad.RandomInt(r, sdk.OneInt(), spendable.AmountOf(pair.BaseCoinDenom))), sdk.NewCoin( pair.QuoteCoinDenom, - randomInt(r, sdk.OneInt(), spendable.AmountOf(pair.QuoteCoinDenom))), + squad.RandomInt(r, sdk.OneInt(), spendable.AmountOf(pair.QuoteCoinDenom))), ) if depositCoins.IsAllLTE(spendable) { skip = false @@ -316,7 +317,7 @@ func SimulateMsgWithdraw(ak types.AccountKeeper, bk types.BankKeeper, k keeper.K } pool, _ := k.GetPool(ctx, poolId) - poolCoin := sdk.NewCoin(pool.PoolCoinDenom, randomInt(r, sdk.OneInt(), spendable.AmountOf(pool.PoolCoinDenom))) + poolCoin := sdk.NewCoin(pool.PoolCoinDenom, squad.RandomInt(r, sdk.OneInt(), spendable.AmountOf(pool.PoolCoinDenom))) msg := types.NewMsgWithdraw(simAccount.Address, poolId, poolCoin) txCtx := simulation.OperationInput{ @@ -376,9 +377,9 @@ func SimulateMsgLimitOrder(ak types.AccountKeeper, bk types.BankKeeper, k keeper ammPool := amm.NewBasicPool(rx, ry, sdk.ZeroInt()) minPrice, maxPrice = minMaxPrice(k, ctx, ammPool.Price()) } - price := amm.PriceToDownTick(randomDec(r, minPrice, maxPrice), int(params.TickPrecision)) + price := amm.PriceToDownTick(squad.RandomDec(r, minPrice, maxPrice), int(params.TickPrecision)) - amt := randomInt(r, types.MinCoinAmount, sdk.NewInt(1000000)) + amt := squad.RandomInt(r, types.MinCoinAmount, sdk.NewInt(1000000)) var offerCoin sdk.Coin var demandCoinDenom string @@ -452,7 +453,7 @@ func SimulateMsgMarketOrder(ak types.AccountKeeper, bk types.BankKeeper, k keepe _, maxPrice := minMaxPrice(k, ctx, *pair.LastPrice) - amt := randomInt(r, types.MinCoinAmount, sdk.NewInt(1000000)) + amt := squad.RandomInt(r, types.MinCoinAmount, sdk.NewInt(1000000)) var offerCoin sdk.Coin var demandCoinDenom string @@ -614,16 +615,6 @@ func SimulateMsgCancelAllOrders(ak types.AccountKeeper, bk types.BankKeeper, k k } } -// randomInt returns an integer within a range [min, max]. -func randomInt(r *rand.Rand, min, max sdk.Int) sdk.Int { - return sdk.MaxInt(min, min.Add(simtypes.RandomAmount(r, max.Sub(min)))) -} - -// randomDec returns a decimal within a range [min, max]. -func randomDec(r *rand.Rand, min, max sdk.Dec) sdk.Dec { - return sdk.MaxDec(min, min.Add(simtypes.RandomDecAmount(r, max.Sub(min)))) -} - var once sync.Once func fundAccountsOnce(r *rand.Rand, ctx sdk.Context, bk types.BankKeeper, accs []simtypes.Account) {