Skip to content

Commit

Permalink
feat: add RandomInt and RandomDec helper
Browse files Browse the repository at this point in the history
Merge pull request #179 from cosmosquad-labs/squad-util
  • Loading branch information
hallazzang committed Feb 14, 2022
2 parents 12a052b + ee5eb8c commit d787640
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
12 changes: 12 additions & 0 deletions types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
"encoding/json"
"fmt"
"math/big"
"math/rand"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -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))
}
15 changes: 3 additions & 12 deletions x/liquidity/amm/match_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package amm_test

import (
"fmt"
"math/big"
"math/rand"
"testing"

Expand All @@ -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)
Expand All @@ -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"))
}
Expand Down
27 changes: 9 additions & 18 deletions x/liquidity/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)),
),
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d787640

Please sign in to comment.